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(); }
public void addAttachments( String src, String dest, String[] attachments) throws IOException, DocumentException { PdfReader reader = new PdfReader(src); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest)); for (int i = 0; i < attachments.length; i++) { addAttachment(stamper.getWriter(), new File(attachments[i])); } stamper.close(); } protected void addAttachment(PdfWriter writer, File src) throws IOException { PdfFileSpecification fs = PdfFileSpecification.fileEmbedded(writer, src.getAbsolutePath(), src.getName(), null); writer.addFileAttachment(src.getName().substring(0, src.getName().indexOf('.')), fs); }
public static final String[] ATTACHMENTS = { "hello", "world", "what", "is", "up" }; public void manipulatePdf(String src, String dest) throws IOException, DocumentException { PdfReader reader = new PdfReader(src); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest)); for (String s : ATTACHMENTS) { PdfFileSpecification fs = PdfFileSpecification.fileEmbedded( stamper.getWriter(), null, String.format("%s.txt", s), String.format("Some test: %s", s).getBytes()); stamper.addFileAttachment(String.format("Content: %s", s), fs); } 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()); stamper.addFileAttachment("some test file", fs); 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()); stamper.addFileAttachment("some test file", fs); stamper.close(); }
/** * Enables compression if compress is true * * @param compress */ public void setCompressionOnStamper(boolean compress) { if (compress) { stamper.setFullCompression(); stamper.getWriter().setCompressionLevel(PdfStream.BEST_COMPRESSION); } }
public void recompressXRef(String src, String dest) throws IOException, DocumentException { PdfReader reader = new PdfReader(src); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest)); stamper.getWriter().setFullCompression(); stamper.close(); reader.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);
PdfStamper myStamper = new PdfStamper( myReader, new FileOutputStream("C:\\outTest.pdf") ); // throws IOE, DocumentException PdfAction jsAction = PdfAction.javaScript ("app.alert('Think again next time!');", myStamper.getWriter()); int pageNum = myReader.getNumberOfPages(); for(int i=1 ; i<pageNum ; i++ ) "try{this.hostContainer.postMessage(names);}" + "catch(e){app.alert(e.message); }"+ "}", myStamper.getWriter());
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
public void compressPdf(String src, String dest) throws IOException, DocumentException { PdfReader reader = new PdfReader(src); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest), PdfWriter.VERSION_1_5); stamper.getWriter().setCompressionLevel(9); int total = reader.getNumberOfPages() + 1; for (int i = 1; i < total; i++) { reader.setPageContent(i, reader.getPageContent(i)); } stamper.setFullCompression(); stamper.close(); reader.close(); }
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(); }
if (PdfName.FONTDESCRIPTOR.equals(font.get(PdfName.TYPE)) && fontname.equals(font.get(PdfName.FONTNAME))) { PdfIndirectObject objref = stamper.getWriter().addToBody(stream); font.put(PdfName.FONTFILE2, objref.getIndirectReference());
public void manipulatePdf(String src, String dest) throws IOException, DocumentException { PdfReader reader = new PdfReader(src); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest)); AffineTransform transform = AffineTransform.getRotateInstance(Math.PI / 6); stamper.getWriter().setPageEvent(new AddAnnotation(stamper, transform)); PdfContentByte canvas = stamper.getOverContent(1); Font bold = new Font(FontFamily.HELVETICA, 12, Font.BOLD); Chunk chunk = new Chunk("The Best iText Questions on StackOverflow", bold); chunk.setGenericTag("http://pages.itextpdf.com/ebook-stackoverflow-questions.html"); Phrase p = new Phrase("Download "); p.add(chunk); p.add(" and discover more than 200 questions and answers."); canvas.saveState(); canvas.concatCTM(transform); ColumnText ct = new ColumnText(canvas); ct.setSimpleColumn(300, 0, 800, 400); ct.addText(p); ct.go(); canvas.restoreState(); stamper.close(); reader.close(); }
public void manipulatePdf(String src, String dest) throws IOException, DocumentException { PdfReader reader = new PdfReader(src); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest)); AffineTransform transform = AffineTransform.getRotateInstance(Math.PI / 2); stamper.getWriter().setPageEvent(new AddAnnotation(stamper, transform)); PdfContentByte canvas = stamper.getOverContent(1); Font bold = new Font(FontFamily.HELVETICA, 12, Font.BOLD); Chunk chunk = new Chunk("The Best iText Questions on StackOverflow", bold); chunk.setGenericTag("http://pages.itextpdf.com/ebook-stackoverflow-questions.html"); Phrase p = new Phrase("Download "); p.add(chunk); p.add(" and discover more than 200 questions and answers."); canvas.saveState(); canvas.concatCTM(transform); ColumnText ct = new ColumnText(canvas); ct.setSimpleColumn(36, -559, 806, -36); ct.addText(p); ct.go(); canvas.restoreState(); stamper.close(); reader.close(); }
final Integer location = locationByTitle.get(pageTitle); final PdfAction newAction = PdfAction.gotoLocalPage(location, new PdfDestination(0), stamper.getWriter());