aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/pdfbox/examples/pdmodel
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/org/pdfbox/examples/pdmodel')
-rw-r--r--src/main/java/org/pdfbox/examples/pdmodel/AddJavascript.java96
-rw-r--r--src/main/java/org/pdfbox/examples/pdmodel/AddMessageToEachPage.java148
-rw-r--r--src/main/java/org/pdfbox/examples/pdmodel/AddMetadataFromDocInfo.java180
-rw-r--r--src/main/java/org/pdfbox/examples/pdmodel/CreateBlankPDF.java123
-rw-r--r--src/main/java/org/pdfbox/examples/pdmodel/CreateBookmarks.java119
-rw-r--r--src/main/java/org/pdfbox/examples/pdmodel/EmbeddedFiles.java170
-rw-r--r--src/main/java/org/pdfbox/examples/pdmodel/HelloWorld.java137
-rw-r--r--src/main/java/org/pdfbox/examples/pdmodel/HelloWorldTTF.java134
-rw-r--r--src/main/java/org/pdfbox/examples/pdmodel/HelloWorldType1AfmPfb.java134
-rw-r--r--src/main/java/org/pdfbox/examples/pdmodel/ImageToPDF.java146
-rw-r--r--src/main/java/org/pdfbox/examples/pdmodel/PrintBookmarks.java142
-rw-r--r--src/main/java/org/pdfbox/examples/pdmodel/PrintDocumentMetaData.java165
-rw-r--r--src/main/java/org/pdfbox/examples/pdmodel/RemoveFirstPage.java98
-rw-r--r--src/main/java/org/pdfbox/examples/pdmodel/ReplaceString.java186
-rw-r--r--src/main/java/org/pdfbox/examples/pdmodel/RubberStamp.java113
-rw-r--r--src/main/java/org/pdfbox/examples/pdmodel/ShowColorBoxes.java135
-rw-r--r--src/main/java/org/pdfbox/examples/pdmodel/package.html9
17 files changed, 2235 insertions, 0 deletions
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 <input-pdf> <output-pdf>" );
+ }
+} \ 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<allPages.size(); i++ )
+ {
+ PDPage page = (PDPage)allPages.get( i );
+ PDRectangle pageSize = page.findMediaBox();
+ float stringWidth = font.getStringWidth( message );
+ float centeredPosition = (pageSize.getWidth() - (stringWidth*fontSize)/1000f)/2f;
+ PDPageContentStream contentStream = new PDPageContentStream(doc, page, true, true);
+ contentStream.beginText();
+ contentStream.setFont( font, fontSize );
+ contentStream.moveTextPositionByAmount( centeredPosition, 30 );
+ contentStream.drawString( message );
+ contentStream.endText();
+ contentStream.close();
+ }
+
+
+ doc.save( outfile );
+ }
+ finally
+ {
+ if( doc != null )
+ {
+ doc.close();
+ }
+ }
+ }
+
+ /**
+ * This will create a hello world PDF document.
+ * <br />
+ * 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() + " <input-file> <Message> <output-file>" );
+ }
+} \ 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 &lt;input-pdf&gt; &lt;output-pdf&gt;
+ *
+ * @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(
+ "<?xpacket begin='' id='W5M0MpCehiHzreSzNTczkc9d'?>\n" +
+ "<?adobe-xap-filters esc=\"CRLF\"?>\n" +
+ "<x:xmpmeta>\n" +
+ " <rdf:RDF xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'>\n" +
+ " <rdf:Description rdf:about='' xmlns:pdf='http://ns.adobe.com/pdf/1.3/' " +
+ "pdf:Keywords='" + fixNull( info.getKeywords() ) + "' " +
+ "pdf:Producer='" + fixNull( info.getProducer() ) + "'></rdf:Description>\n" +
+ " <rdf:Description rdf:about='' xmlns:xap='http://ns.adobe.com/xap/1.0/' " +
+ "xap:ModifyDate='" + fixNull( info.getModificationDate() ) + "' " +
+ "xap:CreateDate='" + fixNull( info.getCreationDate() ) + "' " +
+ "xap:CreatorTool='" + fixNull( info.getCreator() ) + "' " +
+ "xap:MetadataDate='" + fixNull( new GregorianCalendar() )+ "'>\n" +
+ " </rdf:Description>\n" +
+ " <rdf:Description rdf:about='' xmlns:dc='http://purl.org/dc/elements/1.1/' " +
+ "dc:format='application/pdf'>\n" +
+ " <dc:title>\n" +
+ " <rdf:Alt>\n" +
+ " <rdf:li xml:lang='x-default'>" + fixNull( info.getTitle() ) +"</rdf:li>\n" +
+ " </rdf:Alt>\n" +
+ " </dc:title>\n" +
+ " <dc:creator>\n" +
+ " <rdf:Seq>\n" +
+ " <rdf:li>PDFBox.org</rdf:li>\n" +
+ " </rdf:Seq>\n" +
+ " </dc:creator>\n" +
+ " <dc:description>\n" +
+ " <rdf:Alt>\n" +
+ " <rdf:li xml:lang='x-default'>" + fixNull( info.getSubject() ) +"</rdf:li>\n" +
+ " </rdf:Alt>\n" +
+ " </dc:description>\n" +
+ " </rdf:Description>\n" +
+ " </rdf:RDF>\n" +
+ "</x:xmpmeta>\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<?xpacket end='w'?>" );
+ 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 <input-pdf> <output-pdf>" );
+ }
+} \ 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 &lt;outputfile.pdf&gt;
+ *
+ * @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 <outputfile.pdf>" );
+ }
+} \ 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 &lt;input-pdf&gt; &lt;output-pdf&gt;
+ *
+ * @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<pages.size(); i++ )
+ {
+ PDPage page = (PDPage)pages.get( i );
+ PDPageFitWidthDestination dest = new PDPageFitWidthDestination();
+ dest.setPage( page );
+ PDOutlineItem bookmark = new PDOutlineItem();
+ bookmark.setDestination( dest );
+ bookmark.setTitle( "Page " + (i+1) );
+ pagesOutline.appendChild( bookmark );
+ }
+ pagesOutline.openNode();
+ outline.openNode();
+
+ 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.CreateBookmarks <input-pdf> <output-pdf>" );
+ }
+} \ 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.
+ * <br />
+ * 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() + " <output-file>" );
+ }
+} \ 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.
+ * <br />
+ * 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() + " <output-file> <Message>" );
+ }
+} \ 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 <a href="mailto:m.g.n@gmx.de">Michael Niedermair</a>
+ * @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.
+ * <br />
+ * 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()
+ + " <output-file> <Message> <ttf-file>");
+ }
+} \ 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 <a href="mailto:m.g.n@gmx.de">Michael Niedermair</a>
+ * @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.
+ * <br />
+ * 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()
+ + " <output-file> <Message> <afm-file>");
+ }
+} \ 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.
+ * <br />
+ * 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() + " <output-file> <image>" );
+ }
+} \ 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 &lt;input-pdf&gt;
+ *
+ * @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 <input-pdf>" );
+ }
+
+ /**
+ * 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 &lt;input-pdf&gt;
+ *
+ * @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 <input-pdf>" );
+ }
+
+ /**
+ * 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 <input-pdf> <output-pdf>" );
+ }
+} \ 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<pages.size(); i++ )
+ {
+ PDPage page = (PDPage)pages.get( i );
+ PDStream contents = page.getContents();
+ PDFStreamParser parser = new PDFStreamParser(contents.getStream() );
+ parser.parse();
+ List tokens = parser.getTokens();
+ for( int j=0; j<tokens.size(); j++ )
+ {
+ Object next = tokens.get( j );
+ if( next instanceof PDFOperator )
+ {
+ PDFOperator op = (PDFOperator)next;
+ //Tj and TJ are the two operators that display
+ //strings in a PDF
+ if( op.getOperation().equals( "Tj" ) )
+ {
+ //Tj takes one operator and that is the string
+ //to display so lets update that operator
+ COSString previous = (COSString)tokens.get( j-1 );
+ String string = previous.getString();
+ string = string.replaceFirst( strToFind, message );
+ previous.reset();
+ previous.append( string.getBytes() );
+ }
+ else if( op.getOperation().equals( "TJ" ) )
+ {
+ COSArray previous = (COSArray)tokens.get( j-1 );
+ for( int k=0; k<previous.size(); k++ )
+ {
+ Object arrElement = previous.getObject( k );
+ if( arrElement instanceof COSString )
+ {
+ COSString cosString = (COSString)arrElement;
+ String string = cosString.getString();
+ string = string.replaceFirst( strToFind, message );
+ cosString.reset();
+ cosString.append( string.getBytes() );
+ }
+ }
+ }
+ }
+ }
+ //now that the tokens are updated we will replace the
+ //page content stream.
+ PDStream updatedStream = new PDStream(doc);
+ OutputStream out = updatedStream.createOutputStream();
+ ContentStreamWriter tokenWriter = new ContentStreamWriter(out);
+ tokenWriter.writeTokens( tokens );
+ page.setContents( updatedStream );
+ }
+ doc.save( outputFile );
+ }
+ finally
+ {
+ if( doc != null )
+ {
+ doc.close();
+ }
+ }
+ }
+
+ /**
+ * This will open a PDF and replace a string if it finds it.
+ * <br />
+ * 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() +
+ " <input-file> <output-file> <search-string> <Message>" );
+ }
+} \ 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 <input-pdf> <output-pdf>" );
+ }
+} \ 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.
+ * <br />
+ * 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() + " <output-file>" );
+ }
+} \ 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 @@
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
+<html>
+<head>
+
+</head>
+<body>
+These examples show how to use the classes in the PDModel package.
+</body>
+</html>