PdfReader reader = new PdfReader(src); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest)); // create a field for which you define the coordinates using a Rectangle stamper.addAnnotation(field, 1); stamper.close();
public void addAnnotation(String src, String dest) throws IOException, DocumentException { PdfReader reader = new PdfReader(src); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest), '\0', true); PdfAnnotation comment = PdfAnnotation.createText(stamper.getWriter(), new Rectangle(200, 800, 250, 820), "Finally Signed!", "Bruno Specimen has finally signed the document", true, "Comment"); stamper.addAnnotation(comment, 1); stamper.close(); }
public void manipulatePdf(String src, String dest) throws IOException, DocumentException { PdfReader reader = new PdfReader(src); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest)); PdfFileSpecification fs = PdfFileSpecification.fileEmbedded( stamper.getWriter(), null, "test.txt", "Some test".getBytes()); Rectangle rect = new Rectangle(36, 770, 72, 806); PdfAnnotation attachment = dfAnnotation.createFileAttachment( stamper.getWriter(), rect, "My attachment", fs); stamper.addAnnotation(attachment, 1); stamper.close(); }
PdfReader reader = new PdfReader("hello.pdf"); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("hello_link.pdf")); PdfAnnotation link = PdfAnnotation.createLink(stamper.getWriter(), new Rectangle(36, 790, 559, 806), PdfAnnotation.HIGHLIGHT_INVERT, new PdfAction("hello.pdf", 1)); stamper.addAnnotation(link, 1); stamper.close();
import java.io.*; import junit.framework.TestCase; import com.itextpdf.text.Rectangle; import com.itextpdf.text.pdf.*; public class TestAnnotations extends TestCase { // a phonetic approximation final static String LAWRENCE_OF_ARABIA = "\u0644\u0648\u0631\u0627\u0646\u0633\u0627\u0644\u0639\u0631\u0628"; public void testUnicodeAnnotation() throws Exception { final PdfReader origPdfReader = new PdfReader("test.pdf"); final OutputStream outputStream = new FileOutputStream("test-annot.pdf"); final PdfStamper pdfStamper = new PdfStamper(origPdfReader, outputStream, '\0', true); Rectangle rect = new Rectangle(100, 100); PdfAnnotation annotation = PdfAnnotation.createText(pdfStamper.getWriter(), rect, LAWRENCE_OF_ARABIA, LAWRENCE_OF_ARABIA, true, "Help"); pdfStamper.addAnnotation(annotation, 1); pdfStamper.close(); outputStream.close(); } }
String in = "Original.pdf"; String out = "Original.pdf"; PdfReader reader = new PdfReader(in); ByteArrayOutputStream baos = new ByteArrayOutputStream(); PdfStamper stamper = new PdfStamper(reader, baos ); int totalPages = reader.getNumberOfPages(); stamper.insertPage(totalPages + 1, PageSize.A4); stamper.addAnnotation(PdfAnnotation.createText(stamper.getWriter(), new Rectangle(30f, 750f, 80f, 800f), "inserted page", "This page is the title page.", true, null), reader.getNumberOfPages()); stamper.close(); FileOutputStream fileOutputStream = new FileOutputStream(out); IOUtils.write(baos.toByteArray(), fileOutputStream);
PdfReader myReader = new PdfReader( myFilePath ); // throws IOException PdfStamper myStamper = new PdfStamper( myReader, new FileOutputStream(outPath) ); // throws IOE, DocumentException // add a document script myStamper.addJavaScript( myScriptString ); // add a page-open script, 1 is the first page, not zero0 PdfAction jsAction = PdfAction.javaScript( someScriptString ); myStamper.setPageAction( PdfWriter.PAGE_OPEN, jsAction, myStamper.getWriter(), pageNumber ); // throws PdfException (for bad first param) PdfFormField button = PdfFormField.createButton(myWriter, PdfFormField.FF_PUSHBUTTON); button.setWidget( myRectangle, PdfAnnotation.HIGHLIGHT_INVERT ); // the important part, adding jsAction jsAction = PdfAction.javaScript( buttonScriptString ); button.setAdditionalActions( PdfAnnotation.AA_DOWN, jsAction ); // mouse down myStamper.addAnnotation( pageNum, button ); myStamper.close(); // write everything out, throws DocumentException, IOE
/** * Adds an empty signature. * @param name the name of the signature * @param page the page number * @param llx lower left x coordinate of the signature's position * @param lly lower left y coordinate of the signature's position * @param urx upper right x coordinate of the signature's position * @param ury upper right y coordinate of the signature's position * @return a signature form field * @since 2.1.4 */ public PdfFormField addSignature(String name, int page, float llx, float lly, float urx, float ury) { PdfAcroForm acroForm = stamper.getAcroForm(); PdfFormField signature = PdfFormField.createSignature(stamper); acroForm.setSignatureParams(signature, name, llx, lly, urx, ury); acroForm.drawSignatureAppearences(signature, llx, lly, urx, ury); addAnnotation(signature, page); return signature; }
/** * Adds an empty signature. * @param name the name of the signature * @param page the page number * @param llx lower left x coordinate of the signature's position * @param lly lower left y coordinate of the signature's position * @param urx upper right x coordinate of the signature's position * @param ury upper right y coordinate of the signature's position * @return a signature form field * @since 2.1.4 */ public PdfFormField addSignature(final String name, final int page, final float llx, final float lly, final float urx, final float ury) { final PdfAcroForm acroForm = this.stamper.getAcroForm(); final PdfFormField signature = PdfFormField.createSignature(this.stamper); acroForm.setSignatureParams(signature, name, llx, lly, urx, ury); acroForm.drawSignatureAppearences(signature, llx, lly, urx, ury); addAnnotation(signature, page); return signature; }
/** * Adds an empty signature. * @param name the name of the signature * @param page the page number * @param llx lower left x coordinate of the signature's position * @param lly lower left y coordinate of the signature's position * @param urx upper right x coordinate of the signature's position * @param ury upper right y coordinate of the signature's position * @return a signature form field * @since 2.1.4 */ public PdfFormField addSignature(String name, int page, float llx, float lly, float urx, float ury) { PdfAcroForm acroForm = stamper.getAcroForm(); PdfFormField signature = PdfFormField.createSignature(stamper); acroForm.setSignatureParams(signature, name, llx, lly, urx, ury); acroForm.drawSignatureAppearences(signature, llx, lly, urx, ury); addAnnotation(signature, page); return signature; }
public void manipulatePdf(String src, String dest) throws IOException, DocumentException { PdfReader reader = new PdfReader(src); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest)); // Here we define the location: Rectangle linkLocation = new Rectangle(320, 695, 560, 741); // here we add the actual content at this location: ColumnText ct = new ColumnText(stamper.getOverContent(1)); ct.setSimpleColumn(linkLocation); ct.addElement(new Paragraph("This is a link. Click it and you'll be forwarded to another page in this document.")); ct.go(); // now we create the link that will jump to a specific destination: PdfDestination destination = new PdfDestination(PdfDestination.FIT); PdfAnnotation link = PdfAnnotation.createLink(stamper.getWriter(), linkLocation, PdfAnnotation.HIGHLIGHT_INVERT, 3, destination); // If you don't want a border, here's where you remove it: link.setBorder(new PdfBorderArray(0, 0, 0)); // We add the link (that is the clickable area, not the text!) stamper.addAnnotation(link, 1); stamper.close(); reader.close(); }