From 6025b6016517c6d898d8957d1d7e03ba71431912 Mon Sep 17 00:00:00 2001 From: tknall Date: Fri, 1 Dec 2006 12:20:24 +0000 Subject: Initial import of release 2.2. git-svn-id: https://joinup.ec.europa.eu/svn/pdf-as/trunk@4 7b5415b0-85f9-ee4d-85bd-d5d0c3b42d1c --- .../org/pdfbox/examples/pdmodel/AddJavascript.java | 96 +++++++++++ .../examples/pdmodel/AddMessageToEachPage.java | 148 ++++++++++++++++ .../examples/pdmodel/AddMetadataFromDocInfo.java | 180 ++++++++++++++++++++ .../pdfbox/examples/pdmodel/CreateBlankPDF.java | 123 ++++++++++++++ .../pdfbox/examples/pdmodel/CreateBookmarks.java | 119 +++++++++++++ .../org/pdfbox/examples/pdmodel/EmbeddedFiles.java | 170 +++++++++++++++++++ .../org/pdfbox/examples/pdmodel/HelloWorld.java | 137 +++++++++++++++ .../org/pdfbox/examples/pdmodel/HelloWorldTTF.java | 134 +++++++++++++++ .../examples/pdmodel/HelloWorldType1AfmPfb.java | 134 +++++++++++++++ .../org/pdfbox/examples/pdmodel/ImageToPDF.java | 146 ++++++++++++++++ .../pdfbox/examples/pdmodel/PrintBookmarks.java | 142 ++++++++++++++++ .../examples/pdmodel/PrintDocumentMetaData.java | 165 ++++++++++++++++++ .../pdfbox/examples/pdmodel/RemoveFirstPage.java | 98 +++++++++++ .../org/pdfbox/examples/pdmodel/ReplaceString.java | 186 +++++++++++++++++++++ .../org/pdfbox/examples/pdmodel/RubberStamp.java | 113 +++++++++++++ .../pdfbox/examples/pdmodel/ShowColorBoxes.java | 135 +++++++++++++++ .../java/org/pdfbox/examples/pdmodel/package.html | 9 + 17 files changed, 2235 insertions(+) create mode 100644 src/main/java/org/pdfbox/examples/pdmodel/AddJavascript.java create mode 100644 src/main/java/org/pdfbox/examples/pdmodel/AddMessageToEachPage.java create mode 100644 src/main/java/org/pdfbox/examples/pdmodel/AddMetadataFromDocInfo.java create mode 100644 src/main/java/org/pdfbox/examples/pdmodel/CreateBlankPDF.java create mode 100644 src/main/java/org/pdfbox/examples/pdmodel/CreateBookmarks.java create mode 100644 src/main/java/org/pdfbox/examples/pdmodel/EmbeddedFiles.java create mode 100644 src/main/java/org/pdfbox/examples/pdmodel/HelloWorld.java create mode 100644 src/main/java/org/pdfbox/examples/pdmodel/HelloWorldTTF.java create mode 100644 src/main/java/org/pdfbox/examples/pdmodel/HelloWorldType1AfmPfb.java create mode 100644 src/main/java/org/pdfbox/examples/pdmodel/ImageToPDF.java create mode 100644 src/main/java/org/pdfbox/examples/pdmodel/PrintBookmarks.java create mode 100644 src/main/java/org/pdfbox/examples/pdmodel/PrintDocumentMetaData.java create mode 100644 src/main/java/org/pdfbox/examples/pdmodel/RemoveFirstPage.java create mode 100644 src/main/java/org/pdfbox/examples/pdmodel/ReplaceString.java create mode 100644 src/main/java/org/pdfbox/examples/pdmodel/RubberStamp.java create mode 100644 src/main/java/org/pdfbox/examples/pdmodel/ShowColorBoxes.java create mode 100644 src/main/java/org/pdfbox/examples/pdmodel/package.html (limited to 'src/main/java/org/pdfbox/examples/pdmodel') diff --git a/src/main/java/org/pdfbox/examples/pdmodel/AddJavascript.java b/src/main/java/org/pdfbox/examples/pdmodel/AddJavascript.java new file mode 100644 index 0000000..d04700e --- /dev/null +++ b/src/main/java/org/pdfbox/examples/pdmodel/AddJavascript.java @@ -0,0 +1,96 @@ +/** + * Copyright (c) 2005, www.pdfbox.org + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of pdfbox; nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * http://www.pdfbox.org + * + */ +package org.pdfbox.examples.pdmodel; + +import org.pdfbox.pdmodel.PDDocument; +import org.pdfbox.pdmodel.interactive.action.type.PDActionJavaScript; + +import java.io.IOException; + +/** + * This is an example of how to set some javascript in the document. + * + * @author Ben Litchfield (ben@benlitchfield.com) + * @version $Revision: 1.2 $ + */ +public class AddJavascript +{ + private AddJavascript() + { + //static class, should not be instantiated. + } + + /** + * This will print the documents data. + * + * @param args The command line arguments. + * + * @throws Exception If there is an error parsing the document. + */ + public static void main( String[] args ) throws Exception + { + if( args.length != 2 ) + { + usage(); + } + else + { + PDDocument document = null; + try + { + document = PDDocument.load( 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] ); + } + finally + { + if( document != null ) + { + document.close(); + } + } + } + } + + /** + * This will print the usage for this document. + */ + private static void usage() + { + System.err.println( "Usage: java org.pdfbox.examples.pdmodel.AddJavascript " ); + } +} \ No newline at end of file diff --git a/src/main/java/org/pdfbox/examples/pdmodel/AddMessageToEachPage.java b/src/main/java/org/pdfbox/examples/pdmodel/AddMessageToEachPage.java new file mode 100644 index 0000000..efb6ec1 --- /dev/null +++ b/src/main/java/org/pdfbox/examples/pdmodel/AddMessageToEachPage.java @@ -0,0 +1,148 @@ +/** + * Copyright (c) 2005, www.pdfbox.org + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of pdfbox; nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * http://www.pdfbox.org + * + */ +package org.pdfbox.examples.pdmodel; + +import java.io.IOException; +import java.util.List; + +import org.pdfbox.exceptions.COSVisitorException; + +import org.pdfbox.pdmodel.PDDocument; +import org.pdfbox.pdmodel.PDPage; + +import org.pdfbox.pdmodel.common.PDRectangle; +import org.pdfbox.pdmodel.edit.PDPageContentStream; + +import org.pdfbox.pdmodel.font.PDFont; +import org.pdfbox.pdmodel.font.PDType1Font; + + +/** + * This is an example of how to add a message to every page + * in a pdf document. + * + * @author Ben Litchfield (ben@benlitchfield.com) + * @version $Revision: 1.2 $ + */ +public class AddMessageToEachPage +{ + /** + * Constructor. + */ + public AddMessageToEachPage() + { + super(); + } + + /** + * create the second sample document from the PDF file format specification. + * + * @param file The file to write the PDF to. + * @param message The message to write in the file. + * @param outfile The resulting PDF. + * + * @throws IOException If there is an error writing the data. + * @throws COSVisitorException If there is an error writing the PDF. + */ + public void doIt( String file, String message, String outfile ) throws IOException, COSVisitorException + { + // the document + PDDocument doc = null; + try + { + doc = PDDocument.load( file ); + + List allPages = doc.getDocumentCatalog().getAllPages(); + PDFont font = PDType1Font.HELVETICA_BOLD; + float fontSize = 12.0f; + + for( int i=0; i + * see usage() for commandline + * + * @param args Command line arguments. + */ + public static void main(String[] args) + { + AddMessageToEachPage app = new AddMessageToEachPage(); + try + { + if( args.length != 3 ) + { + app.usage(); + } + else + { + app.doIt( args[0], args[1], args[2] ); + } + } + catch (Exception e) + { + e.printStackTrace(); + } + } + + /** + * This will print out a message telling how to use this example. + */ + private void usage() + { + System.err.println( "usage: " + this.getClass().getName() + " " ); + } +} \ No newline at end of file diff --git a/src/main/java/org/pdfbox/examples/pdmodel/AddMetadataFromDocInfo.java b/src/main/java/org/pdfbox/examples/pdmodel/AddMetadataFromDocInfo.java new file mode 100644 index 0000000..35080cd --- /dev/null +++ b/src/main/java/org/pdfbox/examples/pdmodel/AddMetadataFromDocInfo.java @@ -0,0 +1,180 @@ +/** + * Copyright (c) 2005, www.pdfbox.org + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of pdfbox; nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * http://www.pdfbox.org + * + */ +package org.pdfbox.examples.pdmodel; + +import org.pdfbox.pdmodel.PDDocument; +import org.pdfbox.pdmodel.PDDocumentCatalog; +import org.pdfbox.pdmodel.PDDocumentInformation; +import org.pdfbox.pdmodel.common.PDMetadata; +import org.pdfbox.util.DateConverter; + +import java.io.ByteArrayInputStream; +import java.util.Calendar; +import java.util.GregorianCalendar; + +/** + * This is an example on how to add metadata to a document. + * + * Usage: java org.pdfbox.examples.pdmodel.AddMetadataToDocument <input-pdf> <output-pdf> + * + * @author Ben Litchfield (ben@benlitchfield.com) + * @version $Revision: 1.2 $ + */ +public class AddMetadataFromDocInfo +{ + private static final String PADDING = + " " + + " " + + " " + + " " + + " " + + " " + + " " + + " " + + " " + + " " + + " " + + " "; + + + + private AddMetadataFromDocInfo() + { + //utility class + } + + /** + * This will print the documents data. + * + * @param args The command line arguments. + * + * @throws Exception If there is an error parsing the document. + */ + public static void main( String[] args ) throws Exception + { + if( args.length != 2 ) + { + usage(); + } + else + { + PDDocument document = null; + + try + { + document = PDDocument.load( args[0] ); + if( document.isEncrypted() ) + { + System.err.println( "Error: Cannot add metadata to encrypted document." ); + System.exit( 1 ); + } + PDDocumentCatalog catalog = document.getDocumentCatalog(); + PDDocumentInformation info = document.getDocumentInformation(); + + //Right now, PDFBox does not have any XMP library, so we will + //just consruct the XML by hand. + StringBuffer xmp= new StringBuffer(); + xmp.append( + "\n" + + "\n" + + "\n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " " + fixNull( info.getTitle() ) +"\n" + + " \n" + + " \n" + + " \n" + + " \n" + + " PDFBox.org\n" + + " \n" + + " \n" + + " \n" + + " \n" + + " " + fixNull( info.getSubject() ) +"\n" + + " \n" + + " \n" + + " \n" + + " \n" + + "\n" ); + + //xmp spec says we should put padding, so that the metadata can be appended to + //in place + xmp.append( PADDING ); + xmp.append( PADDING ); + xmp.append( PADDING ); + xmp.append( "\n" ); + ByteArrayInputStream mdInput = new ByteArrayInputStream( xmp.toString().getBytes() ); + PDMetadata metadataStream = new PDMetadata(document, mdInput, false ); + catalog.setMetadata( metadataStream ); + + + document.save( args[1] ); + } + finally + { + if( document != null ) + { + document.close(); + } + } + } + } + + private static String fixNull( String string ) + { + return string == null ? "" : string; + } + + private static String fixNull( Calendar cal ) + { + return cal == null ? "" : DateConverter.toISO8601( cal ); + } + + /** + * This will print the usage for this document. + */ + private static void usage() + { + System.err.println( "Usage: java org.pdfbox.examples.pdmodel.AddMetadata " ); + } +} \ No newline at end of file diff --git a/src/main/java/org/pdfbox/examples/pdmodel/CreateBlankPDF.java b/src/main/java/org/pdfbox/examples/pdmodel/CreateBlankPDF.java new file mode 100644 index 0000000..6d942ac --- /dev/null +++ b/src/main/java/org/pdfbox/examples/pdmodel/CreateBlankPDF.java @@ -0,0 +1,123 @@ +/** + * Copyright (c) 2003, www.pdfbox.org + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of pdfbox; nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * http://www.pdfbox.org + * + */ +package org.pdfbox.examples.pdmodel; + +import java.io.FileOutputStream; +import java.io.IOException; + +import org.pdfbox.exceptions.COSVisitorException; + +import org.pdfbox.pdfwriter.COSWriter; + +import org.pdfbox.pdmodel.PDDocument; +import org.pdfbox.pdmodel.PDPage; + +/** + * This will create a blank PDF and write the contents to a file. + * + * usage: java org.pdfbox.examples.pdmodel.CreateBlankPDF <outputfile.pdf> + * + * @author Ben Litchfield (ben@csh.rit.edu) + * @version $Revision: 1.7 $ + */ +public class CreateBlankPDF +{ + + /** + * This will create a blank PDF and write the contents to a file. + * + * @param file The name of the file to write to. + * + * @throws IOException If there is an error writing the data. + * @throws COSVisitorException If there is an error while generating the document. + */ + public void create( String file ) throws IOException, COSVisitorException + { + PDDocument document = null; + FileOutputStream output = null; + COSWriter writer = null; + try + { + document = new PDDocument(); + //Every document requires at least one page, so we will add one + //blank page. + PDPage blankPage = new PDPage(); + document.addPage( blankPage ); + output = new FileOutputStream( file ); + writer = new COSWriter( output ); + writer.write( document.getDocument() ); + } + finally + { + if( writer != null ) + { + writer.close(); + } + if( output != null ) + { + output.close(); + } + if( document != null ) + { + document.close(); + } + } + } + + /** + * This will create a blank document. + * + * @param args The command line arguments. + * + * @throws IOException If there is an error writing the document data. + * @throws COSVisitorException If there is an error generating the data. + */ + public static void main( String[] args ) throws IOException, COSVisitorException + { + if( args.length != 1 ) + { + usage(); + } + else + { + CreateBlankPDF creator = new CreateBlankPDF(); + creator.create( args[0] ); + } + } + + /** + * This will print the usage of this class. + */ + private static void usage() + { + System.err.println( "usage: java org.pdfbox.examples.pdmodel.CreateBlankPDF " ); + } +} \ No newline at end of file diff --git a/src/main/java/org/pdfbox/examples/pdmodel/CreateBookmarks.java b/src/main/java/org/pdfbox/examples/pdmodel/CreateBookmarks.java new file mode 100644 index 0000000..88c09fb --- /dev/null +++ b/src/main/java/org/pdfbox/examples/pdmodel/CreateBookmarks.java @@ -0,0 +1,119 @@ +/** + * Copyright (c) 2005, www.pdfbox.org + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of pdfbox; nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * http://www.pdfbox.org + * + */ +package org.pdfbox.examples.pdmodel; + +import org.pdfbox.pdmodel.PDDocument; +import org.pdfbox.pdmodel.PDPage; +import org.pdfbox.pdmodel.interactive.documentnavigation.destination.PDPageFitWidthDestination; +import org.pdfbox.pdmodel.interactive.documentnavigation.outline.PDDocumentOutline; +import org.pdfbox.pdmodel.interactive.documentnavigation.outline.PDOutlineItem; + +import java.util.List; + +/** + * This is an example on how to add bookmarks to a PDF document. It simply + * adds 1 bookmark for every page. + * + * Usage: java org.pdfbox.examples.pdmodel.CreateBookmarks <input-pdf> <output-pdf> + * + * @author Ben Litchfield (ben@csh.rit.edu) + * @version $Revision: 1.1 $ + */ +public class CreateBookmarks +{ + private CreateBookmarks() + { + //utility class + } + + /** + * This will print the documents data. + * + * @param args The command line arguments. + * + * @throws Exception If there is an error parsing the document. + */ + public static void main( String[] args ) throws Exception + { + if( args.length != 2 ) + { + usage(); + } + else + { + PDDocument document = null; + try + { + document = PDDocument.load( args[0] ); + if( document.isEncrypted() ) + { + System.err.println( "Error: Cannot add bookmarks to encrypted document." ); + System.exit( 1 ); + } + PDDocumentOutline outline = new PDDocumentOutline(); + document.getDocumentCatalog().setDocumentOutline( outline ); + PDOutlineItem pagesOutline = new PDOutlineItem(); + pagesOutline.setTitle( "All Pages" ); + outline.appendChild( pagesOutline ); + List pages = document.getDocumentCatalog().getAllPages(); + for( int i=0; i " ); + } +} \ No newline at end of file diff --git a/src/main/java/org/pdfbox/examples/pdmodel/EmbeddedFiles.java b/src/main/java/org/pdfbox/examples/pdmodel/EmbeddedFiles.java new file mode 100644 index 0000000..86bc447 --- /dev/null +++ b/src/main/java/org/pdfbox/examples/pdmodel/EmbeddedFiles.java @@ -0,0 +1,170 @@ +/** + * Copyright (c) 2004, www.pdfbox.org + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of pdfbox; nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * http://www.pdfbox.org + * + */ +package org.pdfbox.examples.pdmodel; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.util.GregorianCalendar; +import java.util.HashMap; +import java.util.Map; + +import org.pdfbox.exceptions.COSVisitorException; + +import org.pdfbox.pdmodel.PDDocument; +import org.pdfbox.pdmodel.PDDocumentNameDictionary; +import org.pdfbox.pdmodel.PDEmbeddedFilesNameTreeNode; +import org.pdfbox.pdmodel.PDPage; + +import org.pdfbox.pdmodel.common.filespecification.PDComplexFileSpecification; +import org.pdfbox.pdmodel.common.filespecification.PDEmbeddedFile; +import org.pdfbox.pdmodel.edit.PDPageContentStream; + +import org.pdfbox.pdmodel.font.PDFont; +import org.pdfbox.pdmodel.font.PDType1Font; + + +/** + * This is an example that creates a simple document and embeds a file into it.. + * + * @author Ben Litchfield (ben@benlitchfield.com) + * @version $Revision: 1.1 $ + */ +public class EmbeddedFiles +{ + /** + * Constructor. + */ + public EmbeddedFiles() + { + super(); + } + + /** + * create the second sample document from the PDF file format specification. + * + * @param file The file to write the PDF to. + * + * @throws IOException If there is an error writing the data. + * @throws COSVisitorException If there is an error writing the PDF. + */ + public void doIt( String file) throws IOException, COSVisitorException + { + // the document + PDDocument doc = null; + try + { + doc = new PDDocument(); + + PDPage page = new PDPage(); + doc.addPage( page ); + PDFont font = PDType1Font.HELVETICA_BOLD; + + PDPageContentStream contentStream = new PDPageContentStream(doc, page); + contentStream.beginText(); + contentStream.setFont( font, 12 ); + contentStream.moveTextPositionByAmount( 100, 700 ); + contentStream.drawString( "Go to Document->File Attachments to View Embedded Files" ); + contentStream.endText(); + contentStream.close(); + + //embedded files are stored in a named tree + PDEmbeddedFilesNameTreeNode efTree = new PDEmbeddedFilesNameTreeNode(); + + + //first create the file specification, which holds the embedded file + PDComplexFileSpecification fs = new PDComplexFileSpecification(); + fs.setFile( "Test.txt" ); + //create a dummy file stream, this would probably normally be a FileInputStream + byte[] data = "This is the contents of the embedded file".getBytes(); + ByteArrayInputStream fakeFile = + new ByteArrayInputStream( data ); + PDEmbeddedFile ef = new PDEmbeddedFile(doc, fakeFile ); + //now lets some of the optional parameters + ef.setSubtype( "test/plain" ); + ef.setSize( data.length ); + ef.setCreationDate( new GregorianCalendar() ); + fs.setEmbeddedFile( ef ); + + //now add the entry to the embedded file tree and set in the document. + Map efMap = new HashMap(); + efMap.put( "My first attachment", fs ); + efTree.setNames( efMap ); + PDDocumentNameDictionary names = new PDDocumentNameDictionary( doc.getDocumentCatalog() ); + names.setEmbeddedFiles( efTree ); + doc.getDocumentCatalog().setNames( names ); + + + doc.save( file ); + } + finally + { + if( doc != null ) + { + doc.close(); + } + } + } + + /** + * This will create a hello world PDF document with an embedded file. + *
+ * see usage() for commandline + * + * @param args Command line arguments. + */ + public static void main(String[] args) + { + EmbeddedFiles app = new EmbeddedFiles(); + try + { + if( args.length != 1 ) + { + app.usage(); + } + else + { + app.doIt( args[0] ); + } + } + catch (Exception e) + { + e.printStackTrace(); + } + } + + /** + * This will print out a message telling how to use this example. + */ + private void usage() + { + System.err.println( "usage: " + this.getClass().getName() + " " ); + } +} \ No newline at end of file diff --git a/src/main/java/org/pdfbox/examples/pdmodel/HelloWorld.java b/src/main/java/org/pdfbox/examples/pdmodel/HelloWorld.java new file mode 100644 index 0000000..9bceca7 --- /dev/null +++ b/src/main/java/org/pdfbox/examples/pdmodel/HelloWorld.java @@ -0,0 +1,137 @@ +/** + * Copyright (c) 2004, www.pdfbox.org + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of pdfbox; nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * http://www.pdfbox.org + * + */ +package org.pdfbox.examples.pdmodel; + +import java.io.IOException; + +import org.pdfbox.exceptions.COSVisitorException; + +import org.pdfbox.pdmodel.PDDocument; +import org.pdfbox.pdmodel.PDPage; + +import org.pdfbox.pdmodel.edit.PDPageContentStream; + +import org.pdfbox.pdmodel.font.PDFont; +import org.pdfbox.pdmodel.font.PDType1Font; + + +/** + * This is an example that creates a simple document. + * + * The example is taken from the pdf file format specification. + * + * @author Ben Litchfield (ben@csh.rit.edu) + * @version $Revision: 1.5 $ + */ +public class HelloWorld +{ + /** + * Constructor. + */ + public HelloWorld() + { + super(); + } + + /** + * create the second sample document from the PDF file format specification. + * + * @param file The file to write the PDF to. + * @param message The message to write in the file. + * + * @throws IOException If there is an error writing the data. + * @throws COSVisitorException If there is an error writing the PDF. + */ + public void doIt( String file, String message) throws IOException, COSVisitorException + { + // the document + PDDocument doc = null; + try + { + doc = new PDDocument(); + + PDPage page = new PDPage(); + doc.addPage( page ); + PDFont font = PDType1Font.HELVETICA_BOLD; + + PDPageContentStream contentStream = new PDPageContentStream(doc, page); + contentStream.beginText(); + contentStream.setFont( font, 12 ); + contentStream.moveTextPositionByAmount( 100, 700 ); + contentStream.drawString( message ); + contentStream.endText(); + contentStream.close(); + doc.save( file ); + } + finally + { + if( doc != null ) + { + doc.close(); + } + } + } + + /** + * This will create a hello world PDF document. + *
+ * see usage() for commandline + * + * @param args Command line arguments. + */ + public static void main(String[] args) + { + HelloWorld app = new HelloWorld(); + try + { + if( args.length != 2 ) + { + app.usage(); + } + else + { + app.doIt( args[0], args[1] ); + } + } + catch (Exception e) + { + e.printStackTrace(); + } + } + + /** + * This will print out a message telling how to use this example. + */ + private void usage() + { + System.err.println( "usage: " + this.getClass().getName() + " " ); + } +} \ No newline at end of file diff --git a/src/main/java/org/pdfbox/examples/pdmodel/HelloWorldTTF.java b/src/main/java/org/pdfbox/examples/pdmodel/HelloWorldTTF.java new file mode 100644 index 0000000..16b1423 --- /dev/null +++ b/src/main/java/org/pdfbox/examples/pdmodel/HelloWorldTTF.java @@ -0,0 +1,134 @@ +/** + * Copyright (c) 2005, www.pdfbox.org + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of pdfbox; nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * http://www.pdfbox.org + * + */ + +package org.pdfbox.examples.pdmodel; + +import java.io.IOException; + +import org.pdfbox.exceptions.COSVisitorException; +import org.pdfbox.pdmodel.PDDocument; +import org.pdfbox.pdmodel.PDPage; +import org.pdfbox.pdmodel.edit.PDPageContentStream; +import org.pdfbox.pdmodel.font.PDFont; +import org.pdfbox.pdmodel.font.PDTrueTypeFont; + +/** + * This is an example that creates a simple document + * with a ttf-font. + * + * @author Michael Niedermair + * @version $Revision: 1.2 $ + */ +public class HelloWorldTTF +{ + + /** + * create the second sample document from the PDF file format specification. + * + * @param file The file to write the PDF to. + * @param message The message to write in the file. + * @param fontfile The ttf-font file. + * + * @throws IOException If there is an error writing the data. + * @throws COSVisitorException If there is an error writing the PDF. + */ + public void doIt(final String file, final String message, + final String fontfile) throws IOException, COSVisitorException + { + + // the document + PDDocument doc = null; + try + { + doc = new PDDocument(); + + PDPage page = new PDPage(); + doc.addPage(page); + PDFont font = PDTrueTypeFont.loadTTF(doc, fontfile); + + PDPageContentStream contentStream = new PDPageContentStream(doc, + page); + contentStream.beginText(); + contentStream.setFont(font, 12); + contentStream.moveTextPositionByAmount(100, 700); + contentStream.drawString(message); + contentStream.endText(); + contentStream.close(); + doc.save(file); + System.out.println(file + " created!"); + } + finally + { + if (doc != null) + { + doc.close(); + } + } + } + + /** + * This will create a hello world PDF document + * with a ttf-font. + *
+ * see usage() for commandline + * + * @param args Command line arguments. + */ + public static void main(String[] args) + { + + HelloWorldTTF app = new HelloWorldTTF(); + try + { + if (args.length != 3) + { + app.usage(); + } + else + { + app.doIt(args[0], args[1], args[2]); + } + } + catch (Exception e) + { + e.printStackTrace(); + } + } + + /** + * This will print out a message telling how to use this example. + */ + private void usage() + { + System.err.println("usage: " + this.getClass().getName() + + " "); + } +} \ No newline at end of file diff --git a/src/main/java/org/pdfbox/examples/pdmodel/HelloWorldType1AfmPfb.java b/src/main/java/org/pdfbox/examples/pdmodel/HelloWorldType1AfmPfb.java new file mode 100644 index 0000000..2457a2e --- /dev/null +++ b/src/main/java/org/pdfbox/examples/pdmodel/HelloWorldType1AfmPfb.java @@ -0,0 +1,134 @@ +/** + * Copyright (c) 2005, www.pdfbox.org + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of pdfbox; nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * http://www.pdfbox.org + * + */ + +package org.pdfbox.examples.pdmodel; + +import java.io.IOException; + +import org.pdfbox.exceptions.COSVisitorException; +import org.pdfbox.pdmodel.PDDocument; +import org.pdfbox.pdmodel.PDPage; +import org.pdfbox.pdmodel.edit.PDPageContentStream; +import org.pdfbox.pdmodel.font.PDFont; +import org.pdfbox.pdmodel.font.PDType1AfmPfbFont; + +/** + * This is an example that creates a simple document + * with a Type 1 font (afm + pfb). + * + * @author Michael Niedermair + * @version $Revision: 1.2 $ + */ +public class HelloWorldType1AfmPfb +{ + + /** + * create the second sample document from the PDF file format specification. + * + * @param file The file to write the PDF to. + * @param message The message to write in the file. + * @param fontfile The ttf-font file. + * + * @throws IOException If there is an error writing the data. + * @throws COSVisitorException If there is an error writing the PDF. + */ + public void doIt(final String file, final String message, + final String fontfile) throws IOException, COSVisitorException + { + + // the document + PDDocument doc = null; + try + { + doc = new PDDocument(); + + PDPage page = new PDPage(); + doc.addPage(page); + PDFont font = new PDType1AfmPfbFont(doc,fontfile); + + PDPageContentStream contentStream = new PDPageContentStream(doc, + page); + contentStream.beginText(); + contentStream.setFont(font, 12); + contentStream.moveTextPositionByAmount(100, 700); + contentStream.drawString(message); + contentStream.endText(); + contentStream.close(); + doc.save(file); + System.out.println(file + " created!"); + } + finally + { + if (doc != null) + { + doc.close(); + } + } + } + + /** + * This will create a hello world PDF document + * with a ttf-font. + *
+ * see usage() for commandline + * + * @param args Command line arguments. + */ + public static void main(String[] args) + { + + HelloWorldType1AfmPfb app = new HelloWorldType1AfmPfb(); + try + { + if (args.length != 3) + { + app.usage(); + } + else + { + app.doIt(args[0], args[1], args[2]); + } + } + catch (Exception e) + { + e.printStackTrace(); + } + } + + /** + * This will print out a message telling how to use this example. + */ + private void usage() + { + System.err.println("usage: " + this.getClass().getName() + + " "); + } +} \ No newline at end of file diff --git a/src/main/java/org/pdfbox/examples/pdmodel/ImageToPDF.java b/src/main/java/org/pdfbox/examples/pdmodel/ImageToPDF.java new file mode 100644 index 0000000..9faf79d --- /dev/null +++ b/src/main/java/org/pdfbox/examples/pdmodel/ImageToPDF.java @@ -0,0 +1,146 @@ +/** + * Copyright (c) 2005, www.pdfbox.org + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of pdfbox; nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * http://www.pdfbox.org + * + */ +package org.pdfbox.examples.pdmodel; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.RandomAccessFile; + +import org.pdfbox.exceptions.COSVisitorException; + +import org.pdfbox.pdmodel.PDDocument; +import org.pdfbox.pdmodel.PDPage; + +import org.pdfbox.pdmodel.edit.PDPageContentStream; + +import org.pdfbox.pdmodel.graphics.xobject.PDCcitt; +import org.pdfbox.pdmodel.graphics.xobject.PDJpeg; +import org.pdfbox.pdmodel.graphics.xobject.PDXObjectImage; + + +/** + * This is an example that creates a simple document. + * + * The example is taken from the pdf file format specification. + * + * @author Ben Litchfield (ben@csh.rit.edu) + * @version $Revision: 1.5 $ + */ +public class ImageToPDF +{ + + /** + * create the second sample document from the PDF file format specification. + * + * @param file The file to write the PDF to. + * @param image The filename of the image to put in the PDF. + * + * @throws IOException If there is an error writing the data. + * @throws COSVisitorException If there is an error writing the PDF. + */ + public void createPDFFromImage( String file, String image) throws IOException, COSVisitorException + { + // the document + PDDocument doc = null; + try + { + doc = new PDDocument(); + + PDPage page = new PDPage(); + doc.addPage( page ); + + PDXObjectImage ximage = null; + if( image.toLowerCase().endsWith( ".jpg" ) ) + { + ximage = new PDJpeg(doc, new FileInputStream( image ) ); + } + else if (image.toLowerCase().endsWith(".tif") || image.toLowerCase().endsWith(".tiff")) + { + ximage = new PDCcitt(doc, new RandomAccessFile(new File(image),"r")); + } + else + { + //BufferedImage awtImage = ImageIO.read( new File( image ) ); + //ximage = new PDPixelMap(doc, awtImage); + throw new IOException( "Image type not supported:" + image ); + } + PDPageContentStream contentStream = new PDPageContentStream(doc, page); + + contentStream.drawImage( ximage, 20, 20 ); + + contentStream.close(); + doc.save( file ); + } + finally + { + if( doc != null ) + { + doc.close(); + } + } + } + + /** + * This will create a PDF document with a single image on it. + *
+ * see usage() for commandline + * + * @param args Command line arguments. + */ + public static void main(String[] args) + { + ImageToPDF app = new ImageToPDF(); + try + { + if( args.length != 2 ) + { + app.usage(); + } + else + { + app.createPDFFromImage( args[0], args[1] ); + } + } + catch (Exception e) + { + e.printStackTrace(); + } + } + + /** + * This will print out a message telling how to use this example. + */ + private void usage() + { + System.err.println( "usage: " + this.getClass().getName() + " " ); + } +} \ No newline at end of file diff --git a/src/main/java/org/pdfbox/examples/pdmodel/PrintBookmarks.java b/src/main/java/org/pdfbox/examples/pdmodel/PrintBookmarks.java new file mode 100644 index 0000000..0a1a647 --- /dev/null +++ b/src/main/java/org/pdfbox/examples/pdmodel/PrintBookmarks.java @@ -0,0 +1,142 @@ +/** + * Copyright (c) 2005, www.pdfbox.org + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of pdfbox; nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * http://www.pdfbox.org + * + */ +package org.pdfbox.examples.pdmodel; + +import org.pdfbox.exceptions.InvalidPasswordException; + +import org.pdfbox.pdfparser.PDFParser; + +import org.pdfbox.pdmodel.PDDocument; +import org.pdfbox.pdmodel.interactive.documentnavigation.outline.PDDocumentOutline; +import org.pdfbox.pdmodel.interactive.documentnavigation.outline.PDOutlineItem; +import org.pdfbox.pdmodel.interactive.documentnavigation.outline.PDOutlineNode; + +import java.io.FileInputStream; +import java.io.IOException; + +/** + * This is an example on how to access the bookmarks that are part of a pdf document. + * + * Usage: java org.pdfbox.examples.pdmodel.PrintBookmarks <input-pdf> + * + * @author Ben Litchfield (ben@csh.rit.edu) + * @version $Revision: 1.1 $ + */ +public class PrintBookmarks +{ + /** + * This will print the documents data. + * + * @param args The command line arguments. + * + * @throws Exception If there is an error parsing the document. + */ + public static void main( String[] args ) throws Exception + { + if( args.length != 1 ) + { + usage(); + } + else + { + PDDocument document = null; + FileInputStream file = null; + try + { + file = new FileInputStream( args[0] ); + PDFParser parser = new PDFParser( file ); + parser.parse(); + document = parser.getPDDocument(); + if( document.isEncrypted() ) + { + try + { + document.decrypt( "" ); + } + catch( InvalidPasswordException e ) + { + System.err.println( "Error: Document is encrypted with a password." ); + System.exit( 1 ); + } + } + PrintBookmarks meta = new PrintBookmarks(); + PDDocumentOutline outline = document.getDocumentCatalog().getDocumentOutline(); + if( outline != null ) + { + meta.printBookmark( outline, "" ); + } + else + { + System.out.println( "This document does not contain any bookmarks" ); + } + } + finally + { + if( file != null ) + { + file.close(); + } + if( document != null ) + { + document.close(); + } + } + } + } + + /** + * This will print the usage for this document. + */ + private static void usage() + { + System.err.println( "Usage: java org.pdfbox.examples.pdmodel.PrintBookmarks " ); + } + + /** + * This will print the documents bookmarks to System.out. + * + * @param bookmark The bookmark to print out. + * @param indentation A pretty printing parameter + * + * @throws IOException If there is an error getting the page count. + */ + public void printBookmark( PDOutlineNode bookmark, String indentation ) throws IOException + { + PDOutlineItem current = bookmark.getFirstChild(); + while( current != null ) + { + System.out.println( indentation + current.getTitle() ); + printBookmark( current, indentation + " " ); + current = current.getNextSibling(); + } + + } +} \ No newline at end of file diff --git a/src/main/java/org/pdfbox/examples/pdmodel/PrintDocumentMetaData.java b/src/main/java/org/pdfbox/examples/pdmodel/PrintDocumentMetaData.java new file mode 100644 index 0000000..c096113 --- /dev/null +++ b/src/main/java/org/pdfbox/examples/pdmodel/PrintDocumentMetaData.java @@ -0,0 +1,165 @@ +/** + * Copyright (c) 2003-2005, www.pdfbox.org + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of pdfbox; nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * http://www.pdfbox.org + * + */ +package org.pdfbox.examples.pdmodel; + +import org.pdfbox.exceptions.InvalidPasswordException; + +import org.pdfbox.pdfparser.PDFParser; + +import org.pdfbox.pdmodel.PDDocument; +import org.pdfbox.pdmodel.PDDocumentCatalog; +import org.pdfbox.pdmodel.PDDocumentInformation; +import org.pdfbox.pdmodel.common.PDMetadata; + +import java.io.FileInputStream; +import java.io.IOException; + +import java.text.SimpleDateFormat; + +import java.util.Calendar; + +/** + * This is an example on how to get a documents metadata information. + * + * Usage: java org.pdfbox.examples.pdmodel.PrintDocumentMetaData <input-pdf> + * + * @author Ben Litchfield (ben@benlitchfield.com) + * @version $Revision: 1.10 $ + */ +public class PrintDocumentMetaData +{ + /** + * This will print the documents data. + * + * @param args The command line arguments. + * + * @throws Exception If there is an error parsing the document. + */ + public static void main( String[] args ) throws Exception + { + if( args.length != 1 ) + { + usage(); + } + else + { + PDDocument document = null; + FileInputStream file = null; + try + { + file = new FileInputStream( args[0] ); + PDFParser parser = new PDFParser( file ); + parser.parse(); + document = parser.getPDDocument(); + if( document.isEncrypted() ) + { + try + { + document.decrypt( "" ); + } + catch( InvalidPasswordException e ) + { + System.err.println( "Error: Document is encrypted with a password." ); + System.exit( 1 ); + } + } + PrintDocumentMetaData meta = new PrintDocumentMetaData(); + meta.printMetadata( document ); + } + finally + { + if( file != null ) + { + file.close(); + } + if( document != null ) + { + document.close(); + } + } + } + } + + /** + * This will print the usage for this document. + */ + private static void usage() + { + System.err.println( "Usage: java org.pdfbox.examples.pdmodel.PrintDocumentMetaData " ); + } + + /** + * This will print the documents data to System.out. + * + * @param document The document to get the metadata from. + * + * @throws IOException If there is an error getting the page count. + */ + public void printMetadata( PDDocument document ) throws IOException + { + PDDocumentInformation info = document.getDocumentInformation(); + PDDocumentCatalog cat = document.getDocumentCatalog(); + PDMetadata metadata = cat.getMetadata(); + System.out.println( "Page Count=" + document.getNumberOfPages() ); + System.out.println( "Title=" + info.getTitle() ); + System.out.println( "Author=" + info.getAuthor() ); + System.out.println( "Subject=" + info.getSubject() ); + System.out.println( "Keywords=" + info.getKeywords() ); + System.out.println( "Creator=" + info.getCreator() ); + System.out.println( "Producer=" + info.getProducer() ); + System.out.println( "Creation Date=" + formatDate( info.getCreationDate() ) ); + System.out.println( "Modification Date=" + formatDate( info.getModificationDate() ) ); + System.out.println( "Trapped=" + info.getTrapped() ); + if( metadata != null ) + { + System.out.println( "Metadata=" + metadata.getInputStreamAsString() ); + } + } + + /** + * This will format a date object. + * + * @param date The date to format. + * + * @return A string representation of the date. + */ + private String formatDate( Calendar date ) + { + String retval = null; + if( date != null ) + { + SimpleDateFormat formatter = new SimpleDateFormat(); + retval = formatter.format( date.getTime() ); + } + + return retval; + } +} \ No newline at end of file diff --git a/src/main/java/org/pdfbox/examples/pdmodel/RemoveFirstPage.java b/src/main/java/org/pdfbox/examples/pdmodel/RemoveFirstPage.java new file mode 100644 index 0000000..2b7242b --- /dev/null +++ b/src/main/java/org/pdfbox/examples/pdmodel/RemoveFirstPage.java @@ -0,0 +1,98 @@ +/** + * Copyright (c) 2005, www.pdfbox.org + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of pdfbox; nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * http://www.pdfbox.org + * + */ +package org.pdfbox.examples.pdmodel; + +import org.pdfbox.pdmodel.PDDocument; + +import java.io.IOException; + +/** + * This is an example on how to remove pages from a PDF document. + * + * @author Ben Litchfield (ben@benlitchfield.com) + * @version $Revision: 1.4 $ + */ +public class RemoveFirstPage +{ + private RemoveFirstPage() + { + //utility class, should not be instantiated. + } + + /** + * This will print the documents data. + * + * @param args The command line arguments. + * + * @throws Exception If there is an error parsing the document. + */ + public static void main( String[] args ) throws Exception + { + if( args.length != 2 ) + { + usage(); + } + else + { + PDDocument document = null; + try + { + document = PDDocument.load( args[0] ); + if( document.isEncrypted() ) + { + throw new IOException( "Encrypted documents are not supported for this example" ); + } + if( document.getNumberOfPages() <= 1 ) + { + throw new IOException( "Error: A PDF document must have at least one page, " + + "cannot remove the last page!"); + } + document.removePage( 0 ); + document.save( args[1] ); + } + finally + { + if( document != null ) + { + document.close(); + } + } + } + } + + /** + * This will print the usage for this document. + */ + private static void usage() + { + System.err.println( "Usage: java org.pdfbox.examples.pdmodel.RemoveFirstPage " ); + } +} \ No newline at end of file diff --git a/src/main/java/org/pdfbox/examples/pdmodel/ReplaceString.java b/src/main/java/org/pdfbox/examples/pdmodel/ReplaceString.java new file mode 100644 index 0000000..bbf8688 --- /dev/null +++ b/src/main/java/org/pdfbox/examples/pdmodel/ReplaceString.java @@ -0,0 +1,186 @@ +/** + * Copyright (c) 2005, www.pdfbox.org + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of pdfbox; nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * http://www.pdfbox.org + * + */ +package org.pdfbox.examples.pdmodel; + +import java.io.IOException; +import java.io.OutputStream; +import java.util.List; + +import org.pdfbox.cos.COSArray; +import org.pdfbox.cos.COSString; +import org.pdfbox.exceptions.COSVisitorException; + +import org.pdfbox.pdfparser.PDFStreamParser; +import org.pdfbox.pdfwriter.ContentStreamWriter; +import org.pdfbox.pdmodel.PDDocument; +import org.pdfbox.pdmodel.PDPage; + +import org.pdfbox.pdmodel.common.PDStream; + +import org.pdfbox.util.PDFOperator; + + +/** + * This is an example that will replace a string in a PDF with a new one. + * + * The example is taken from the pdf file format specification. + * + * @author Ben Litchfield (ben@benlitchfield.com) + * @version $Revision: 1.2 $ + */ +public class ReplaceString +{ + /** + * Constructor. + */ + public ReplaceString() + { + super(); + } + + /** + * Locate a string in a PDF and replace it with a new string. + * + * @param inputFile The PDF to open. + * @param outputFile The PDF to write to. + * @param strToFind The string to find in the PDF document. + * @param message The message to write in the file. + * + * @throws IOException If there is an error writing the data. + * @throws COSVisitorException If there is an error writing the PDF. + */ + public void doIt( String inputFile, String outputFile, String strToFind, String message) + throws IOException, COSVisitorException + { + // the document + PDDocument doc = null; + try + { + doc = PDDocument.load( inputFile ); + List pages = doc.getDocumentCatalog().getAllPages(); + for( int i=0; i + * see usage() for commandline + * + * @param args Command line arguments. + */ + public static void main(String[] args) + { + ReplaceString app = new ReplaceString(); + try + { + if( args.length != 4 ) + { + app.usage(); + } + else + { + app.doIt( args[0], args[1], args[2], args[3] ); + } + } + catch (Exception e) + { + e.printStackTrace(); + } + } + + /** + * This will print out a message telling how to use this example. + */ + private void usage() + { + System.err.println( "usage: " + this.getClass().getName() + + " " ); + } +} \ No newline at end of file diff --git a/src/main/java/org/pdfbox/examples/pdmodel/RubberStamp.java b/src/main/java/org/pdfbox/examples/pdmodel/RubberStamp.java new file mode 100644 index 0000000..158e0f8 --- /dev/null +++ b/src/main/java/org/pdfbox/examples/pdmodel/RubberStamp.java @@ -0,0 +1,113 @@ +/** + * Copyright (c) 2005, www.pdfbox.org + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of pdfbox; nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * http://www.pdfbox.org + * + */ +package org.pdfbox.examples.pdmodel; + +import org.pdfbox.pdmodel.PDDocument; +import org.pdfbox.pdmodel.PDPage; +import org.pdfbox.pdmodel.common.PDRectangle; +import org.pdfbox.pdmodel.interactive.annotation.PDAnnotationRubberStamp; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +/** + * This is an example on how to add annotations to pages of a PDF document. + * + * @author Paul King + * @version $Revision: 1.1 $ + */ +public class RubberStamp +{ + private RubberStamp() + { + //utility class, should not be instantiated. + } + + /** + * This will print the documents data. + * + * @param args The command line arguments. + * + * @throws Exception If there is an error parsing the document. + */ + public static void main( String[] args ) throws Exception + { + if( args.length != 2 ) + { + usage(); + } + else + { + PDDocument document = null; + try + { + document = PDDocument.load( args[0] ); + if( document.isEncrypted() ) + { + throw new IOException( "Encrypted documents are not supported for this example" ); + } + List allpages = new ArrayList(); + document.getDocumentCatalog().getPages().getAllKids(allpages); + + for (int i=0; i < allpages.size(); i++) + { + PDPage apage = (PDPage) allpages.get(i); + List annotations = apage.getAnnotations(); + + PDAnnotationRubberStamp rs = new PDAnnotationRubberStamp(); + rs.setName(PDAnnotationRubberStamp.NAME_TOP_SECRET); + rs.setRectangle(new PDRectangle(100,100)); + rs.setContents("A top secret note"); + + annotations.add(rs); + } + + document.save( args[1] ); + } + finally + { + if( document != null ) + { + document.close(); + } + } + } + } + + /** + * This will print the usage for this document. + */ + private static void usage() + { + System.err.println( "Usage: java org.pdfbox.examples.pdmodel.RubberStamp " ); + } +} \ No newline at end of file diff --git a/src/main/java/org/pdfbox/examples/pdmodel/ShowColorBoxes.java b/src/main/java/org/pdfbox/examples/pdmodel/ShowColorBoxes.java new file mode 100644 index 0000000..f936642 --- /dev/null +++ b/src/main/java/org/pdfbox/examples/pdmodel/ShowColorBoxes.java @@ -0,0 +1,135 @@ +/** + * Copyright (c) 2004, www.pdfbox.org + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of pdfbox; nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * http://www.pdfbox.org + * + */ +package org.pdfbox.examples.pdmodel; + +import java.awt.Color; +import java.io.IOException; + +import org.pdfbox.exceptions.COSVisitorException; + +import org.pdfbox.pdmodel.PDDocument; +import org.pdfbox.pdmodel.PDPage; + +import org.pdfbox.pdmodel.edit.PDPageContentStream; + +/** + * This is an example that creates a simple document. + * + * The example is taken from the pdf file format specification. + * + * @author Ben Litchfield (ben@csh.rit.edu) + * @version $Revision: 1.1 $ + */ +public class ShowColorBoxes +{ + /** + * Constructor. + */ + public ShowColorBoxes() + { + super(); + } + + /** + * create the second sample document from the PDF file format specification. + * + * @param file The file to write the PDF to. + * + * @throws IOException If there is an error writing the data. + * @throws COSVisitorException If there is an error writing the PDF. + */ + public void doIt( String file) throws IOException, COSVisitorException + { + // the document + PDDocument doc = null; + try + { + doc = new PDDocument(); + + PDPage page = new PDPage(); + doc.addPage( page ); + + PDPageContentStream contentStream = new PDPageContentStream(doc, page); + //first fill the entire background with cyan + contentStream.setNonStrokingColor( Color.CYAN ); + contentStream.fillRect( 0,0, page.findMediaBox().getWidth(), page.findMediaBox().getHeight() ); + + //then draw a red box in the lower left hand corner + contentStream.setNonStrokingColor( Color.RED ); + contentStream.fillRect( 10, 10, 100, 100 ); + + contentStream.close(); + doc.save( file ); + } + finally + { + if( doc != null ) + { + doc.close(); + } + } + } + + /** + * This will create a hello world PDF document. + *
+ * see usage() for commandline + * + * @param args Command line arguments. + */ + public static void main(String[] args) + { + ShowColorBoxes app = new ShowColorBoxes(); + try + { + if( args.length != 1 ) + { + app.usage(); + } + else + { + app.doIt( args[0] ); + } + } + catch (Exception e) + { + e.printStackTrace(); + } + } + + /** + * This will print out a message telling how to use this example. + */ + private void usage() + { + System.err.println( "usage: " + this.getClass().getName() + " " ); + } +} \ No newline at end of file diff --git a/src/main/java/org/pdfbox/examples/pdmodel/package.html b/src/main/java/org/pdfbox/examples/pdmodel/package.html new file mode 100644 index 0000000..b9beefa --- /dev/null +++ b/src/main/java/org/pdfbox/examples/pdmodel/package.html @@ -0,0 +1,9 @@ + + + + + + +These examples show how to use the classes in the PDModel package. + + -- cgit v1.2.3