PDDocument watermarkDoc = PDDocument.load("watermark.pdf"); //Assuming your empty document with watermark image in it. PDDocument realDoc = PDDocument.load("document-to-be-watermarked.pdf"); //Let's say this is your document that you want to watermark. For example sake, I am opening a new one, you would already have a reference to PDDocument if you are creating one Overlay overlay = new Overlay(); overlay.overlay(realDoc,watermarkDoc); watermarkDoc.save("document-now-watermarked.pdf");
contentStream.close(); doc.save(new File(RESULT_FOLDER, "break-long-string.pdf"));
contentStream.close(); doc.save("break-long-string.pdf");
/** * Save the document to a file. * * @param fileName The file to save as. * * @throws IOException if the output could not be written */ public void save(String fileName) throws IOException { save(new File(fileName)); }
/** * Save the document to a file. * * @param file The file to save as. * * @throws IOException if the output could not be written */ public void save(File file) throws IOException { save(new BufferedOutputStream(new FileOutputStream(file))); }
PDDocument document = new PDDocument(); InputStream in = new FileInputStream(someImage); BufferedImage bimg = ImageIO.read(in); float width = bimg.getWidth(); float height = bimg.getHeight(); PDPage page = new PDPage(new PDRectangle(width, height)); document.addPage(page); PDXObjectImage img = new PDJpeg(document, new FileInputStream(someImage)); PDPageContentStream contentStream = new PDPageContentStream(document, page); contentStream.drawImage(img, 0, 0); contentStream.close(); in.close(); document.save("test.pdf"); document.close();
public static void main(String[] args) throws IOException { if (args.length != 1) { System.err.println("usage: " + CreateBlankPDF.class.getName() + " <outputfile.pdf>"); System.exit(1); } String filename = args[0]; try (PDDocument doc = new PDDocument()) { // a valid PDF document requires at least one page PDPage blankPage = new PDPage(); doc.addPage(blankPage); doc.save(filename); } } }
public static void main(String[] args) throws IOException, COSVisitorException { PDDocument doc = new PDDocument(); PDPage page = new PDPage(); doc.addPage( page ); PDPageContentStream contentStream = new PDPageContentStream(doc, page); String[][] content = { {"Name"," Time "}, {"HTC","01:25"}, {"Samsung Tab2","05:30"} } ; drawTable(page, contentStream, 700, 100, content); contentStream.close(); doc.save("h:\\test.pdf" ); }
//Using PDFBox library available from http://pdfbox.apache.org/ //Writes pdf document of specific pages as a new pdf file //Reads in pdf document PDDocument pdDoc = PDDocument.load(file); //Creates a new pdf document PDDocument document = null; //Adds specific page "i" where "i" is the page number and then saves the new pdf document try { document = new PDDocument(); document.addPage((PDPage) pdDoc.getDocumentCatalog().getAllPages().get(i)); document.save("file path"+"new document title"+".pdf"); document.close(); }catch(Exception e){}
private void importFDF( String[] args ) throws IOException { if( args.length != 3 ) { usage(); } else { ImportFDF importer = new ImportFDF(); try (PDDocument pdf = PDDocument.load( new File(args[0]) ); FDFDocument fdf = FDFDocument.load( args[1] )) { importer.importFDF( pdf, fdf ); pdf.save( args[2] ); } } }
private void importXFDF( String[] args ) throws IOException { if( args.length != 3 ) { usage(); } else { ImportFDF importer = new ImportFDF(); try (PDDocument pdf = PDDocument.load( new File(args[0]) ); FDFDocument fdf = FDFDocument.loadXFDF( args[1] )) { importer.importFDF( pdf, fdf ); pdf.save( args[2] ); } } }
public static void main(String[] args) throws IOException { // Load the PDF document created by SimpleForm.java try (PDDocument document = PDDocument.load(new File("target/SimpleForm.pdf"))) { // Note that the JavaScript will depend on the reader application. // The classes and methods available to Adobe Reader and Adobe Acrobat // are documented in the Acrobat SDK. String javaScript = "var now = util.printd('yyyy-mm-dd', new Date());" + "var oField = this.getField('SampleField');" + "oField.value = now;"; // Create an action as JavaScript action PDActionJavaScript jsAction = new PDActionJavaScript(); jsAction.setAction(javaScript); // Set the action to be executed when the document is opened document.getDocumentCatalog().setOpenAction(jsAction); document.save("target/UpdateFieldOnDocumentOpen.pdf"); } } }
/** * This will print the documents data. * * @param args The command line arguments. * * @throws IOException If there is an error parsing the document. */ public static void main( String[] args ) throws IOException { if( args.length != 2 ) { usage(); } else { try (PDDocument document = PDDocument.load(new File(args[0]))) { PDActionJavaScript javascript = new PDActionJavaScript( "app.alert( {cMsg: 'PDFBox rocks!', nIcon: 3, nType: 0, cTitle: 'PDFBox Javascript example' } );"); document.getDocumentCatalog().setOpenAction( javascript ); if( document.isEncrypted() ) { throw new IOException( "Encrypted documents are not supported for this example" ); } document.save( args[1] ); } } }
private void setField(String[] args) throws IOException { PDDocument pdf = null; try { if (args.length != 3) { usage(); } else { SetField example = new SetField(); pdf = PDDocument.load(new File(args[0])); example.setField(pdf, args[1], args[2]); pdf.save(args[0]); } } finally { if (pdf != null) { pdf.close(); } } }
doc.save(pdfPath); System.out.println(pdfPath + " created!");
doc.save(filename);
import org.apache.pdfbox.exceptions.COSVisitorException; import org.apache.pdfbox.exceptions.CryptographyException; import org.apache.pdfbox.exceptions.InvalidPasswordException; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.PDDocumentCatalog; import org.apache.pdfbox.pdmodel.PDPage; import org.apache.pdfbox.pdmodel.PDResources; import java.io.IOException; public class Main { public static void main(String[] argv) throws COSVisitorException, InvalidPasswordException, CryptographyException, IOException { PDDocument document = PDDocument.load("input.pdf"); if (document.isEncrypted()) { document.decrypt(""); } PDDocumentCatalog catalog = document.getDocumentCatalog(); for (Object pageObj : catalog.getAllPages()) { PDPage page = (PDPage) pageObj; PDResources resources = page.findResources(); resources.getImages().clear(); } document.save("strippedOfImages.pdf"); } }
public static void main(String[] args) throws IOException { String formTemplate = "src/main/resources/org/apache/pdfbox/examples/interactive/form/FillFormField.pdf"; try (PDDocument pdfDocument = PDDocument.load(new File(formTemplate))) { // get the document catalog PDAcroForm acroForm = pdfDocument.getDocumentCatalog().getAcroForm(); // as there might not be an AcroForm entry a null check is necessary if (acroForm != null) { // Retrieve an individual field and set its value. PDTextField field = (PDTextField) acroForm.getField( "sampleField" ); field.setValue("Text Entry"); // If a field is nested within the form tree a fully qualified name // might be provided to access the field. field = (PDTextField) acroForm.getField( "fieldsContainer.nestedSampleField" ); field.setValue("Text Entry"); } // Save and close the filled out form. pdfDocument.save("target/FillFormField.pdf"); } }
public static void main(String[] args) throws IOException { // Load the PDF document created by SimpleForm.java try (PDDocument document = PDDocument.load(new File("target/SimpleForm.pdf"))) { PDAcroForm acroForm = document.getDocumentCatalog().getAcroForm(); // Get the field and the widget associated to it. // Note: there might be multiple widgets PDField field = acroForm.getField("SampleField"); PDAnnotationWidget widget = field.getWidgets().get(0); // Create the definition for a green border PDAppearanceCharacteristicsDictionary fieldAppearance = new PDAppearanceCharacteristicsDictionary(new COSDictionary()); PDColor green = new PDColor(new float[] { 0, 1, 0 }, PDDeviceRGB.INSTANCE); fieldAppearance.setBorderColour(green); // Set the information to be used by the widget which is responsible // for the visual style of the form field. widget.setAppearanceCharacteristics(fieldAppearance); document.save("target/AddBorderToField.pdf"); } } }
contents.fill(); doc.save(filename);