use of com.lowagie.text.pdf.PdfAction in project OpenPDF by LibrePDF.
the class Actions method main.
/**
* Creates a document with some goto actions.
*
* @param args
* no arguments needed
*/
public static void main(String[] args) {
System.out.println("Actions");
// step 1: creation of a document-object
Document document = new Document();
Document remote = new Document();
try {
// step 2:
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("Actions.pdf"));
PdfWriter.getInstance(remote, new FileOutputStream("remote.pdf"));
// step 3:
document.open();
remote.open();
// step 4: we add some content
PdfAction action = PdfAction.gotoLocalPage(2, new PdfDestination(PdfDestination.XYZ, -1, 10000, 0), writer);
writer.setOpenAction(action);
document.add(new Paragraph("Page 1"));
document.newPage();
document.add(new Paragraph("Page 2"));
document.add(new Chunk("goto page 1").setAction(PdfAction.gotoLocalPage(1, new PdfDestination(PdfDestination.FITH, 500), writer)));
document.add(Chunk.NEWLINE);
document.add(new Chunk("goto another document").setAction(PdfAction.gotoRemotePage("remote.pdf", "test", false, true)));
remote.add(new Paragraph("Some remote document"));
remote.newPage();
Paragraph p = new Paragraph("This paragraph contains a ");
p.add(new Chunk("local destination").setLocalDestination("test"));
remote.add(p);
} catch (Exception de) {
de.printStackTrace();
}
// step 5: we close the document
document.close();
remote.close();
}
use of com.lowagie.text.pdf.PdfAction in project OpenPDF by LibrePDF.
the class ChainedActions method main.
/**
* Creates a document with chained Actions.
*
* @param args
* no arguments needed
*/
public static void main(String[] args) {
System.out.println("Chained actions");
// step 1: creation of a document-object
Document document = new Document();
try {
// step 2:
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("ChainedActions.pdf"));
// step 3: we add Javascript as Metadata and we open the document
document.open();
// step 4: we add some content
PdfAction action = PdfAction.javaScript("app.alert('Welcome at my site');\r", writer);
action.next(new PdfAction("https://github.com/LibrePDF/OpenPDF"));
Paragraph p = new Paragraph(new Chunk("Click to go to Bruno's site").setAction(action));
document.add(p);
} catch (Exception de) {
de.printStackTrace();
}
// step 5: we close the document
document.close();
}
use of com.lowagie.text.pdf.PdfAction in project OpenPDF by LibrePDF.
the class TableEvents2 method tableLayout.
/**
* @see com.lowagie.text.pdf.PdfPTableEvent#tableLayout(com.lowagie.text.pdf.PdfPTable, float[][], float[], int, int, com.lowagie.text.pdf.PdfContentByte[])
*/
public void tableLayout(PdfPTable table, float[][] width, float[] heights, int headerRows, int rowStart, PdfContentByte[] canvases) {
// widths of the different cells of the first row
float[] widths = width[0];
PdfContentByte cb = canvases[PdfPTable.TEXTCANVAS];
cb.saveState();
// border for the complete table
cb.setLineWidth(2);
cb.setRGBColorStroke(255, 0, 0);
cb.rectangle(widths[0], heights[heights.length - 1], widths[widths.length - 1] - widths[0], heights[0] - heights[heights.length - 1]);
cb.stroke();
// border for the header rows
if (headerRows > 0) {
float headerHeight = heights[0];
for (int k = 0; k < headerRows; ++k) headerHeight += heights[k];
cb.setRGBColorStroke(0, 0, 255);
cb.rectangle(widths[0], heights[headerRows], widths[widths.length - 1] - widths[0], heights[0] - heights[headerRows]);
cb.stroke();
}
cb.restoreState();
cb = canvases[PdfPTable.BASECANVAS];
cb.saveState();
// border for the cells
cb.setLineWidth(.5f);
// loop over the rows
for (int line = 0; line < heights.length - 1; ++line) {
widths = width[line];
// loop over the columns
for (int col = 0; col < widths.length - 1; ++col) {
if (line == 0 && col == 0)
cb.setAction(new PdfAction("https://github.com/LibrePDF/OpenPDF"), widths[col], heights[line + 1], widths[col + 1], heights[line]);
cb.setRGBColorStrokeF((float) Math.random(), (float) Math.random(), (float) Math.random());
// horizontal borderline
cb.moveTo(widths[col], heights[line]);
cb.lineTo(widths[col + 1], heights[line]);
cb.stroke();
// vertical borderline
cb.setRGBColorStrokeF((float) Math.random(), (float) Math.random(), (float) Math.random());
cb.moveTo(widths[col], heights[line]);
cb.lineTo(widths[col], heights[line + 1]);
cb.stroke();
}
}
cb.restoreState();
}
use of com.lowagie.text.pdf.PdfAction in project OpenPDF by LibrePDF.
the class PdfAnnotationsImp method convertAnnotation.
public static PdfAnnotation convertAnnotation(PdfWriter writer, Annotation annot, Rectangle defaultRect) throws IOException {
switch(annot.annotationType()) {
case Annotation.URL_NET:
return new PdfAnnotation(writer, annot.llx(), annot.lly(), annot.urx(), annot.ury(), new PdfAction((URL) annot.getAttributes().get(Annotation.URL)));
case Annotation.URL_AS_STRING:
return new PdfAnnotation(writer, annot.llx(), annot.lly(), annot.urx(), annot.ury(), new PdfAction((String) annot.getAttributes().get(Annotation.FILE)));
case Annotation.FILE_DEST:
return new PdfAnnotation(writer, annot.llx(), annot.lly(), annot.urx(), annot.ury(), new PdfAction((String) annot.getAttributes().get(Annotation.FILE), (String) annot.getAttributes().get(Annotation.DESTINATION)));
case Annotation.SCREEN:
boolean[] sparams = (boolean[]) annot.getAttributes().get(Annotation.PARAMETERS);
String fname = (String) annot.getAttributes().get(Annotation.FILE);
String mimetype = (String) annot.getAttributes().get(Annotation.MIMETYPE);
PdfFileSpecification fs;
if (sparams[0]) {
fs = PdfFileSpecification.fileEmbedded(writer, fname, fname, null);
} else {
fs = PdfFileSpecification.fileExtern(writer, fname);
}
return PdfAnnotation.createScreen(writer, new Rectangle(annot.llx(), annot.lly(), annot.urx(), annot.ury()), fname, fs, mimetype, sparams[1]);
case Annotation.FILE_PAGE:
return new PdfAnnotation(writer, annot.llx(), annot.lly(), annot.urx(), annot.ury(), new PdfAction((String) annot.getAttributes().get(Annotation.FILE), (Integer) annot.getAttributes().get(Annotation.PAGE)));
case Annotation.NAMED_DEST:
return new PdfAnnotation(writer, annot.llx(), annot.lly(), annot.urx(), annot.ury(), new PdfAction((Integer) annot.getAttributes().get(Annotation.NAMED)));
case Annotation.LAUNCH:
return new PdfAnnotation(writer, annot.llx(), annot.lly(), annot.urx(), annot.ury(), new PdfAction((String) annot.getAttributes().get(Annotation.APPLICATION), (String) annot.getAttributes().get(Annotation.PARAMETERS), (String) annot.getAttributes().get(Annotation.OPERATION), (String) annot.getAttributes().get(Annotation.DEFAULTDIR)));
default:
return new PdfAnnotation(writer, defaultRect.getLeft(), defaultRect.getBottom(), defaultRect.getRight(), defaultRect.getTop(), new PdfString(annot.title(), PdfObject.TEXT_UNICODE), new PdfString(annot.content(), PdfObject.TEXT_UNICODE));
}
}
use of com.lowagie.text.pdf.PdfAction in project OpenPDF by LibrePDF.
the class TableEvents1 method tableLayout.
/**
* @see com.lowagie.text.pdf.PdfPTableEvent#tableLayout(com.lowagie.text.pdf.PdfPTable, float[][], float[], int, int, com.lowagie.text.pdf.PdfContentByte[])
*/
public void tableLayout(PdfPTable table, float[][] width, float[] heights, int headerRows, int rowStart, PdfContentByte[] canvases) {
// widths of the different cells of the first row
float[] widths = width[0];
PdfContentByte cb = canvases[PdfPTable.TEXTCANVAS];
cb.saveState();
// border for the complete table
cb.setLineWidth(2);
cb.setRGBColorStroke(255, 0, 0);
cb.rectangle(widths[0], heights[heights.length - 1], widths[widths.length - 1] - widths[0], heights[0] - heights[heights.length - 1]);
cb.stroke();
// border for the header rows
if (headerRows > 0) {
float headerHeight = heights[0];
for (int k = 0; k < headerRows; ++k) headerHeight += heights[k];
cb.setRGBColorStroke(0, 0, 255);
cb.rectangle(widths[0], heights[headerRows], widths[widths.length - 1] - widths[0], heights[0] - heights[headerRows]);
cb.stroke();
}
cb.restoreState();
cb = canvases[PdfPTable.BASECANVAS];
cb.saveState();
// border for the cells
cb.setLineWidth(.5f);
// loop over the rows
for (int line = 0; line < heights.length - 1; ++line) {
// loop over the columns
for (int col = 0; col < widths.length - 1; ++col) {
if (line == 0 && col == 0)
cb.setAction(new PdfAction("https://github.com/LibrePDF/OpenPDF"), widths[col], heights[line + 1], widths[col + 1], heights[line]);
cb.setRGBColorStrokeF((float) Math.random(), (float) Math.random(), (float) Math.random());
// horizontal borderline
cb.moveTo(widths[col], heights[line]);
cb.lineTo(widths[col + 1], heights[line]);
cb.stroke();
// vertical borderline
cb.setRGBColorStrokeF((float) Math.random(), (float) Math.random(), (float) Math.random());
cb.moveTo(widths[col], heights[line]);
cb.lineTo(widths[col], heights[line + 1]);
cb.stroke();
}
}
cb.restoreState();
}
Aggregations