You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by gb...@apache.org on 2011/12/06 21:15:20 UTC
svn commit: r1211081 [2/3] - in /pdfbox/trunk: ./ examples/ examples/src/
examples/src/main/ examples/src/main/java/ examples/src/main/java/org/
examples/src/main/java/org/apache/
examples/src/main/java/org/apache/pdfbox/ examples/src/main/java/org/apa...
Added: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/HelloWorld.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/HelloWorld.java?rev=1211081&view=auto
==============================================================================
--- pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/HelloWorld.java (added)
+++ pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/HelloWorld.java Tue Dec 6 20:15:18 2011
@@ -0,0 +1,123 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.pdfbox.examples.pdmodel;
+
+import java.io.IOException;
+
+import org.apache.pdfbox.exceptions.COSVisitorException;
+
+import org.apache.pdfbox.pdmodel.PDDocument;
+import org.apache.pdfbox.pdmodel.PDPage;
+
+import org.apache.pdfbox.pdmodel.edit.PDPageContentStream;
+
+import org.apache.pdfbox.pdmodel.font.PDFont;
+import org.apache.pdfbox.pdmodel.font.PDType1Font;
+
+
+/**
+ * This is an example that creates a simple document.
+ *
+ * The example is taken from the pdf file format specification.
+ *
+ * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
+ * @version $Revision: 1.6 $
+ */
+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>" );
+ }
+}
Propchange: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/HelloWorld.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/HelloWorldTTF.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/HelloWorldTTF.java?rev=1211081&view=auto
==============================================================================
--- pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/HelloWorldTTF.java (added)
+++ pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/HelloWorldTTF.java Tue Dec 6 20:15:18 2011
@@ -0,0 +1,120 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.pdfbox.examples.pdmodel;
+
+import java.io.IOException;
+
+import org.apache.pdfbox.exceptions.COSVisitorException;
+import org.apache.pdfbox.pdmodel.PDDocument;
+import org.apache.pdfbox.pdmodel.PDPage;
+import org.apache.pdfbox.pdmodel.edit.PDPageContentStream;
+import org.apache.pdfbox.pdmodel.font.PDFont;
+import org.apache.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>");
+ }
+}
Propchange: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/HelloWorldTTF.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/HelloWorldType1AfmPfb.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/HelloWorldType1AfmPfb.java?rev=1211081&view=auto
==============================================================================
--- pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/HelloWorldType1AfmPfb.java (added)
+++ pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/HelloWorldType1AfmPfb.java Tue Dec 6 20:15:18 2011
@@ -0,0 +1,120 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.pdfbox.examples.pdmodel;
+
+import java.io.IOException;
+
+import org.apache.pdfbox.exceptions.COSVisitorException;
+import org.apache.pdfbox.pdmodel.PDDocument;
+import org.apache.pdfbox.pdmodel.PDPage;
+import org.apache.pdfbox.pdmodel.edit.PDPageContentStream;
+import org.apache.pdfbox.pdmodel.font.PDFont;
+import org.apache.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>");
+ }
+}
Propchange: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/HelloWorldType1AfmPfb.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/ImageToPDF.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/ImageToPDF.java?rev=1211081&view=auto
==============================================================================
--- pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/ImageToPDF.java (added)
+++ pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/ImageToPDF.java Tue Dec 6 20:15:18 2011
@@ -0,0 +1,132 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.pdfbox.examples.pdmodel;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+
+import org.apache.pdfbox.exceptions.COSVisitorException;
+import org.apache.pdfbox.io.RandomAccessFile;
+
+import org.apache.pdfbox.pdmodel.PDDocument;
+import org.apache.pdfbox.pdmodel.PDPage;
+
+import org.apache.pdfbox.pdmodel.edit.PDPageContentStream;
+
+import org.apache.pdfbox.pdmodel.graphics.xobject.PDCcitt;
+import org.apache.pdfbox.pdmodel.graphics.xobject.PDJpeg;
+import org.apache.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 <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
+ * @version $Revision: 1.7 $
+ */
+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>" );
+ }
+}
Propchange: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/ImageToPDF.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/PrintBookmarks.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/PrintBookmarks.java?rev=1211081&view=auto
==============================================================================
--- pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/PrintBookmarks.java (added)
+++ pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/PrintBookmarks.java Tue Dec 6 20:15:18 2011
@@ -0,0 +1,128 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.pdfbox.examples.pdmodel;
+
+import org.apache.pdfbox.exceptions.InvalidPasswordException;
+
+import org.apache.pdfbox.pdfparser.PDFParser;
+
+import org.apache.pdfbox.pdmodel.PDDocument;
+import org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline.PDDocumentOutline;
+import org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline.PDOutlineItem;
+import org.apache.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.apache.pdfbox.examples.pdmodel.PrintBookmarks <input-pdf>
+ *
+ * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
+ * @version $Revision: 1.2 $
+ */
+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.apache.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();
+ }
+
+ }
+}
Propchange: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/PrintBookmarks.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/PrintDocumentMetaData.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/PrintDocumentMetaData.java?rev=1211081&view=auto
==============================================================================
--- pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/PrintDocumentMetaData.java (added)
+++ pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/PrintDocumentMetaData.java Tue Dec 6 20:15:18 2011
@@ -0,0 +1,151 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.pdfbox.examples.pdmodel;
+
+import org.apache.pdfbox.exceptions.InvalidPasswordException;
+
+import org.apache.pdfbox.pdfparser.PDFParser;
+
+import org.apache.pdfbox.pdmodel.PDDocument;
+import org.apache.pdfbox.pdmodel.PDDocumentCatalog;
+import org.apache.pdfbox.pdmodel.PDDocumentInformation;
+import org.apache.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.apache.pdfbox.examples.pdmodel.PrintDocumentMetaData <input-pdf>
+ *
+ * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
+ * @version $Revision: 1.11 $
+ */
+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.apache.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;
+ }
+}
Propchange: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/PrintDocumentMetaData.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/PrintURLs.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/PrintURLs.java?rev=1211081&view=auto
==============================================================================
--- pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/PrintURLs.java (added)
+++ pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/PrintURLs.java Tue Dec 6 20:15:18 2011
@@ -0,0 +1,141 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.pdfbox.examples.pdmodel;
+
+import java.awt.geom.Rectangle2D;
+import java.util.List;
+
+import org.apache.pdfbox.pdmodel.PDDocument;
+import org.apache.pdfbox.pdmodel.PDPage;
+
+import org.apache.pdfbox.pdmodel.common.PDRectangle;
+import org.apache.pdfbox.pdmodel.interactive.action.type.PDAction;
+import org.apache.pdfbox.pdmodel.interactive.action.type.PDActionURI;
+import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotation;
+import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationLink;
+import org.apache.pdfbox.util.PDFTextStripperByArea;
+
+
+/**
+ * This is an example of how to access a URL in a PDF document.
+ *
+ * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
+ * @version $Revision: 1.3 $
+ */
+public class PrintURLs
+{
+ /**
+ * Constructor.
+ */
+ private PrintURLs()
+ {
+ //utility class
+ }
+
+ /**
+ * This will create a hello world PDF document.
+ * <br />
+ * see usage() for commandline
+ *
+ * @param args Command line arguments.
+ *
+ * @throws Exception If there is an error extracting the URLs.
+ */
+ public static void main(String[] args) throws Exception
+ {
+ PDDocument doc = null;
+ try
+ {
+ if( args.length != 1 )
+ {
+ usage();
+ }
+ else
+ {
+ doc = PDDocument.load( args[0] );
+ List allPages = doc.getDocumentCatalog().getAllPages();
+ for( int i=0; i<allPages.size(); i++ )
+ {
+ PDFTextStripperByArea stripper = new PDFTextStripperByArea();
+ PDPage page = (PDPage)allPages.get( i );
+ List annotations = page.getAnnotations();
+ //first setup text extraction regions
+ for( int j=0; j<annotations.size(); j++ )
+ {
+ PDAnnotation annot = (PDAnnotation)annotations.get( j );
+ if( annot instanceof PDAnnotationLink )
+ {
+ PDAnnotationLink link = (PDAnnotationLink)annot;
+ PDRectangle rect = link.getRectangle();
+ //need to reposition link rectangle to match text space
+ float x = rect.getLowerLeftX();
+ float y = rect.getUpperRightY();
+ float width = rect.getWidth();
+ float height = rect.getHeight();
+ int rotation = page.findRotation();
+ if( rotation == 0 )
+ {
+ PDRectangle pageSize = page.findMediaBox();
+ y = pageSize.getHeight() - y;
+ }
+ else if( rotation == 90 )
+ {
+ //do nothing
+ }
+
+ Rectangle2D.Float awtRect = new Rectangle2D.Float( x,y,width,height );
+ stripper.addRegion( "" + j, awtRect );
+ }
+ }
+
+ stripper.extractRegions( page );
+
+ for( int j=0; j<annotations.size(); j++ )
+ {
+ PDAnnotation annot = (PDAnnotation)annotations.get( j );
+ if( annot instanceof PDAnnotationLink )
+ {
+ PDAnnotationLink link = (PDAnnotationLink)annot;
+ PDAction action = link.getAction();
+ String urlText = stripper.getTextForRegion( "" + j );
+ if( action instanceof PDActionURI )
+ {
+ PDActionURI uri = (PDActionURI)action;
+ System.out.println( "Page " + (i+1) +":'" + urlText + "'=" + uri.getURI() );
+ }
+ }
+ }
+ }
+ }
+ }
+ finally
+ {
+ if( doc != null )
+ {
+ doc.close();
+ }
+ }
+ }
+
+ /**
+ * This will print out a message telling how to use this example.
+ */
+ private static void usage()
+ {
+ System.err.println( "usage: " + PrintURLs.class.getName() + " <input-file>" );
+ }
+}
Propchange: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/PrintURLs.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/RemoveFirstPage.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/RemoveFirstPage.java?rev=1211081&view=auto
==============================================================================
--- pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/RemoveFirstPage.java (added)
+++ pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/RemoveFirstPage.java Tue Dec 6 20:15:18 2011
@@ -0,0 +1,84 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.pdfbox.examples.pdmodel;
+
+import org.apache.pdfbox.pdmodel.PDDocument;
+
+import java.io.IOException;
+
+/**
+ * This is an example on how to remove pages from a PDF document.
+ *
+ * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
+ * @version $Revision: 1.5 $
+ */
+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.apache.pdfbox.examples.pdmodel.RemoveFirstPage <input-pdf> <output-pdf>" );
+ }
+}
Propchange: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/RemoveFirstPage.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/ReplaceString.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/ReplaceString.java?rev=1211081&view=auto
==============================================================================
--- pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/ReplaceString.java (added)
+++ pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/ReplaceString.java Tue Dec 6 20:15:18 2011
@@ -0,0 +1,172 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.pdfbox.examples.pdmodel;
+
+import java.io.IOException;
+import java.io.OutputStream;
+import java.util.List;
+
+import org.apache.pdfbox.cos.COSArray;
+import org.apache.pdfbox.cos.COSString;
+import org.apache.pdfbox.exceptions.COSVisitorException;
+
+import org.apache.pdfbox.pdfparser.PDFStreamParser;
+import org.apache.pdfbox.pdfwriter.ContentStreamWriter;
+import org.apache.pdfbox.pdmodel.PDDocument;
+import org.apache.pdfbox.pdmodel.PDPage;
+
+import org.apache.pdfbox.pdmodel.common.PDStream;
+
+import org.apache.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 <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
+ * @version $Revision: 1.3 $
+ */
+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("ISO-8859-1") );
+ }
+ 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("ISO-8859-1") );
+ }
+ }
+ }
+ }
+ }
+ //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>" );
+ }
+}
Propchange: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/ReplaceString.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/ReplaceURLs.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/ReplaceURLs.java?rev=1211081&view=auto
==============================================================================
--- pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/ReplaceURLs.java (added)
+++ pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/ReplaceURLs.java Tue Dec 6 20:15:18 2011
@@ -0,0 +1,113 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.pdfbox.examples.pdmodel;
+
+import java.util.List;
+
+import org.apache.pdfbox.pdmodel.PDDocument;
+import org.apache.pdfbox.pdmodel.PDPage;
+
+import org.apache.pdfbox.pdmodel.interactive.action.type.PDAction;
+import org.apache.pdfbox.pdmodel.interactive.action.type.PDActionURI;
+import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotation;
+import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationLink;
+
+
+/**
+ * This is an example of how to replace a URL in a PDF document. This
+ * will only replace the URL that the text refers to and not the text
+ * itself.
+ *
+ * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
+ * @version $Revision: 1.2 $
+ */
+public class ReplaceURLs
+{
+ /**
+ * Constructor.
+ */
+ private ReplaceURLs()
+ {
+ //utility class
+ }
+
+ /**
+ * This will read in a document and replace all of the urls with
+ * http://www.pdfbox.org.
+ * <br />
+ * see usage() for commandline
+ *
+ * @param args Command line arguments.
+ *
+ * @throws Exception If there is an error during the process.
+ */
+ public static void main(String[] args) throws Exception
+ {
+ PDDocument doc = null;
+ try
+ {
+ if( args.length != 2 )
+ {
+ usage();
+ }
+ else
+ {
+ doc = PDDocument.load( args[0] );
+ List allPages = doc.getDocumentCatalog().getAllPages();
+ for( int i=0; i<allPages.size(); i++ )
+ {
+ PDPage page = (PDPage)allPages.get( i );
+ List annotations = page.getAnnotations();
+
+ for( int j=0; j<annotations.size(); j++ )
+ {
+ PDAnnotation annot = (PDAnnotation)annotations.get( j );
+ if( annot instanceof PDAnnotationLink )
+ {
+ PDAnnotationLink link = (PDAnnotationLink)annot;
+ PDAction action = link.getAction();
+ if( action instanceof PDActionURI )
+ {
+ PDActionURI uri = (PDActionURI)action;
+ String oldURI = uri.getURI();
+ String newURI = "http://www.pdfbox.org";
+ System.out.println( "Page " + (i+1) +": Replacing " + oldURI + " with " + newURI );
+ uri.setURI( newURI );
+ }
+ }
+ }
+ }
+ doc.save( args[1] );
+ }
+ }
+ finally
+ {
+ if( doc != null )
+ {
+ doc.close();
+ }
+ }
+ }
+
+ /**
+ * This will print out a message telling how to use this example.
+ */
+ private static void usage()
+ {
+ System.err.println( "usage: " + ReplaceURLs.class.getName() + " <input-file> <output-file>" );
+ }
+}
Propchange: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/ReplaceURLs.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/RubberStamp.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/RubberStamp.java?rev=1211081&view=auto
==============================================================================
--- pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/RubberStamp.java (added)
+++ pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/RubberStamp.java Tue Dec 6 20:15:18 2011
@@ -0,0 +1,99 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.pdfbox.examples.pdmodel;
+
+import org.apache.pdfbox.pdmodel.PDDocument;
+import org.apache.pdfbox.pdmodel.PDPage;
+import org.apache.pdfbox.pdmodel.common.PDRectangle;
+import org.apache.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.apache.pdfbox.examples.pdmodel.RubberStamp <input-pdf> <output-pdf>" );
+ }
+}
Propchange: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/RubberStamp.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/RubberStampWithImage.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/RubberStampWithImage.java?rev=1211081&view=auto
==============================================================================
--- pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/RubberStampWithImage.java (added)
+++ pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/RubberStampWithImage.java Tue Dec 6 20:15:18 2011
@@ -0,0 +1,197 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.pdfbox.examples.pdmodel;
+
+import org.apache.pdfbox.cos.COSDictionary;
+import org.apache.pdfbox.exceptions.COSVisitorException;
+import org.apache.pdfbox.pdmodel.PDDocument;
+import org.apache.pdfbox.pdmodel.PDPage;
+import org.apache.pdfbox.pdmodel.PDResources;
+import org.apache.pdfbox.pdmodel.common.PDRectangle;
+import org.apache.pdfbox.pdmodel.common.PDStream;
+import org.apache.pdfbox.pdmodel.graphics.xobject.PDJpeg;
+import org.apache.pdfbox.pdmodel.graphics.xobject.PDXObjectForm;
+import org.apache.pdfbox.pdmodel.graphics.xobject.PDXObjectImage;
+import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationRubberStamp;
+import org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceDictionary;
+import org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceStream;
+import org.apache.pdfbox.util.MapUtil;
+
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.text.NumberFormat;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Locale;
+
+/**
+ * This is an example on how to add a rubber stamp with an image to pages of a PDF document.
+ *
+ * @version $Revision: 1.0 $
+ */
+public class RubberStampWithImage
+{
+
+ private static final String SAVE_GRAPHICS_STATE = "q\n";
+ private static final String RESTORE_GRAPHICS_STATE = "Q\n";
+ private static final String CONCATENATE_MATRIX = "cm\n";
+ private static final String XOBJECT_DO = "Do\n";
+ private static final String SPACE = " ";
+
+ private static NumberFormat formatDecimal = NumberFormat.getNumberInstance( Locale.US );
+
+ /**
+ * Add a rubber stamp with an jpg image to every page of the given document.
+ * @param args the command line arguments
+ * @throws IOException an exception is thrown if something went wrong
+ */
+ public void doIt( String[] args ) throws IOException
+ {
+ if( args.length != 3 )
+ {
+ 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);
+ int numberOfPages = allpages.size();
+
+ for (int i=0; i < numberOfPages; i++)
+ {
+ PDPage apage = (PDPage) allpages.get(i);
+ List annotations = apage.getAnnotations();
+ PDAnnotationRubberStamp rubberStamp = new PDAnnotationRubberStamp();
+ rubberStamp.setName(PDAnnotationRubberStamp.NAME_TOP_SECRET);
+ rubberStamp.setRectangle(new PDRectangle(100,100));
+ rubberStamp.setContents("A top secret note");
+
+ // Create a PDXObjectImage with the given jpg
+ FileInputStream fin = new FileInputStream( args[2] );
+ PDJpeg mypic = new PDJpeg(document,fin);
+
+ //Define and set the target rectangle
+ PDRectangle myrect = new PDRectangle();
+ myrect.setUpperRightX(275);
+ myrect.setUpperRightY(575);
+ myrect.setLowerLeftX(250);
+ myrect.setLowerLeftY(550);
+
+ // Create a PDXObjectForm
+ PDStream formstream = new PDStream(document);
+ OutputStream os = formstream.createOutputStream();
+ PDXObjectForm form = new PDXObjectForm(formstream);
+ form.setResources(new PDResources());
+ form.setBBox(myrect);
+ form.setFormType(1);
+ // adjust the image to the target rectangle and add it to the stream
+ drawXObject(mypic, form.getResources(), os, 250, 550, 25, 25);
+ os.close();
+
+ PDAppearanceStream myDic = new PDAppearanceStream(form.getCOSStream());
+ PDAppearanceDictionary appearance = new PDAppearanceDictionary(new COSDictionary());
+ appearance.setNormalAppearance(myDic);
+ rubberStamp.setAppearance(appearance);
+ rubberStamp.setRectangle(myrect);
+
+ //Add the new RubberStamp to the document
+ annotations.add(rubberStamp);
+
+ }
+ document.save( args[1] );
+ }
+ catch(COSVisitorException exception)
+ {
+ System.err.println("An error occured during saving the document.");
+ System.err.println("Exception:"+exception);
+ }
+ finally
+ {
+ if( document != null )
+ {
+ document.close();
+ }
+ }
+ }
+ }
+
+ private void drawXObject( PDXObjectImage xobject, PDResources resources, OutputStream os,
+ float x, float y, float width, float height ) throws IOException
+ {
+ // This is similar to PDPageContentStream.drawXObject()
+ String xObjectPrefix = "Im";
+ String objMapping = MapUtil.getNextUniqueKey( resources.getImages(), xObjectPrefix );
+ resources.getXObjects().put( objMapping, xobject );
+
+ appendRawCommands( os, SAVE_GRAPHICS_STATE );
+ appendRawCommands( os, formatDecimal.format( width ) );
+ appendRawCommands( os, SPACE );
+ appendRawCommands( os, formatDecimal.format( 0 ) );
+ appendRawCommands( os, SPACE );
+ appendRawCommands( os, formatDecimal.format( 0 ) );
+ appendRawCommands( os, SPACE );
+ appendRawCommands( os, formatDecimal.format( height ) );
+ appendRawCommands( os, SPACE );
+ appendRawCommands( os, formatDecimal.format( x ) );
+ appendRawCommands( os, SPACE );
+ appendRawCommands( os, formatDecimal.format( y ) );
+ appendRawCommands( os, SPACE );
+ appendRawCommands( os, CONCATENATE_MATRIX );
+ appendRawCommands( os, SPACE );
+ appendRawCommands( os, "/" );
+ appendRawCommands( os, objMapping );
+ appendRawCommands( os, SPACE );
+ appendRawCommands( os, XOBJECT_DO );
+ appendRawCommands( os, SPACE );
+ appendRawCommands( os, RESTORE_GRAPHICS_STATE );
+ }
+
+ private void appendRawCommands(OutputStream os, String commands) throws IOException
+ {
+ os.write( commands.getBytes("ISO-8859-1"));
+ }
+
+ /**
+ * This creates an instance of RubberStampWithImage.
+ *
+ * @param args The command line arguments.
+ *
+ * @throws Exception If there is an error parsing the document.
+ */
+ public static void main( String[] args ) throws Exception
+ {
+ RubberStampWithImage rubberStamp = new RubberStampWithImage();
+ rubberStamp.doIt(args);
+ }
+
+ /**
+ * This will print the usage for this example.
+ */
+ private void usage()
+ {
+ System.err.println( "Usage: java "+getClass().getName()+" <input-pdf> <output-pdf> <jpeg-filename>" );
+ }
+}
Propchange: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/RubberStampWithImage.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/ShowColorBoxes.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/ShowColorBoxes.java?rev=1211081&view=auto
==============================================================================
--- pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/ShowColorBoxes.java (added)
+++ pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/ShowColorBoxes.java Tue Dec 6 20:15:18 2011
@@ -0,0 +1,121 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.pdfbox.examples.pdmodel;
+
+import java.awt.Color;
+import java.io.IOException;
+
+import org.apache.pdfbox.exceptions.COSVisitorException;
+
+import org.apache.pdfbox.pdmodel.PDDocument;
+import org.apache.pdfbox.pdmodel.PDPage;
+
+import org.apache.pdfbox.pdmodel.edit.PDPageContentStream;
+
+/**
+ * This is an example that creates a simple document.
+ *
+ * The example is taken from the pdf file format specification.
+ *
+ * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
+ * @version $Revision: 1.2 $
+ */
+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>" );
+ }
+}
Propchange: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/ShowColorBoxes.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/UsingTextMatrix.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/UsingTextMatrix.java?rev=1211081&view=auto
==============================================================================
--- pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/UsingTextMatrix.java (added)
+++ pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/UsingTextMatrix.java Tue Dec 6 20:15:18 2011
@@ -0,0 +1,182 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.pdfbox.examples.pdmodel;
+
+import java.io.IOException;
+
+import org.apache.pdfbox.exceptions.COSVisitorException;
+import org.apache.pdfbox.pdmodel.PDDocument;
+import org.apache.pdfbox.pdmodel.PDPage;
+import org.apache.pdfbox.pdmodel.common.PDRectangle;
+import org.apache.pdfbox.pdmodel.edit.PDPageContentStream;
+import org.apache.pdfbox.pdmodel.font.PDFont;
+import org.apache.pdfbox.pdmodel.font.PDType1Font;
+
+
+/**
+ * This is an example of how to use a text matrix.
+ * @version $Revision: 1.0 $
+ */
+public class UsingTextMatrix
+{
+ /**
+ * Constructor.
+ */
+ public UsingTextMatrix()
+ {
+ super();
+ }
+
+ /**
+ * creates a sample document with some text using a text matrix.
+ *
+ * @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 message, String outfile ) throws IOException, COSVisitorException
+ {
+ // the document
+ PDDocument doc = null;
+ try
+ {
+ doc = new PDDocument();
+
+ // Page 1
+ PDFont font = PDType1Font.HELVETICA;
+ PDPage page = new PDPage();
+ page.setMediaBox(PDPage.PAGE_SIZE_A4);
+ doc.addPage(page);
+ float fontSize = 12.0f;
+
+ PDRectangle pageSize = page.findMediaBox();
+ float centeredXPosition = (pageSize.getWidth() - fontSize/1000f)/2f;
+ float stringWidth = font.getStringWidth( message );
+ float centeredYPosition = (pageSize.getHeight() - (stringWidth*fontSize)/1000f)/3f;
+
+ PDPageContentStream contentStream = new PDPageContentStream(doc, page, false, false);
+ contentStream.setFont( font, fontSize );
+ contentStream.beginText();
+ // counterclockwise rotation
+ for (int i=0;i<8;i++)
+ {
+ contentStream.setTextRotation(i*Math.PI*0.25, centeredXPosition,
+ pageSize.getHeight()-centeredYPosition);
+ contentStream.drawString( message + " " + i);
+ }
+ // clockwise rotation
+ for (int i=0;i<8;i++)
+ {
+ contentStream.setTextRotation(-i*Math.PI*0.25, centeredXPosition, centeredYPosition);
+ contentStream.drawString( message + " " + i);
+ }
+
+ contentStream.endText();
+ contentStream.close();
+
+ // Page 2
+ page = new PDPage();
+ page.setMediaBox(PDPage.PAGE_SIZE_A4);
+ doc.addPage(page);
+ fontSize = 1.0f;
+
+ contentStream = new PDPageContentStream(doc, page, false, false);
+ contentStream.setFont( font, fontSize );
+ contentStream.beginText();
+
+ // text scaling
+ for (int i=0;i<10;i++)
+ {
+ contentStream.setTextScaling(12+(i*6), 12+(i*6), 100, 100+i*50);
+ contentStream.drawString( message + " " +i);
+ }
+ contentStream.endText();
+ contentStream.close();
+
+ // Page 3
+ page = new PDPage();
+ page.setMediaBox(PDPage.PAGE_SIZE_A4);
+ doc.addPage(page);
+ fontSize = 1.0f;
+
+ contentStream = new PDPageContentStream(doc, page, false, false);
+ contentStream.setFont( font, fontSize );
+ contentStream.beginText();
+
+ int i = 0;
+ // text scaling combined with rotation
+ contentStream.setTextMatrix(12, 0, 0, 12, centeredXPosition, centeredYPosition*1.5);
+ contentStream.drawString( message + " " +i++);
+
+ contentStream.setTextMatrix(0, 18, -18, 0, centeredXPosition, centeredYPosition*1.5);
+ contentStream.drawString( message + " " +i++);
+
+ contentStream.setTextMatrix(-24, 0, 0, -24, centeredXPosition, centeredYPosition*1.5);
+ contentStream.drawString( message + " " +i++);
+
+ contentStream.setTextMatrix(0, -30, 30, 0, centeredXPosition, centeredYPosition*1.5);
+ contentStream.drawString( message + " " +i++);
+
+ contentStream.endText();
+ contentStream.close();
+
+ doc.save( outfile );
+ }
+ finally
+ {
+ if( doc != null )
+ {
+ doc.close();
+ }
+ }
+ }
+
+ /**
+ * This will create a PDF document with some examples how to use a text matrix.
+ *
+ * @param args Command line arguments.
+ */
+ public static void main(String[] args)
+ {
+ UsingTextMatrix app = new UsingTextMatrix();
+ 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() + " <Message> <output-file>" );
+ }
+}
Propchange: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/UsingTextMatrix.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/package.html
URL: http://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/package.html?rev=1211081&view=auto
==============================================================================
--- pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/package.html (added)
+++ pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/package.html Tue Dec 6 20:15:18 2011
@@ -0,0 +1,25 @@
+<!--
+ ! Licensed to the Apache Software Foundation (ASF) under one or more
+ ! contributor license agreements. See the NOTICE file distributed with
+ ! this work for additional information regarding copyright ownership.
+ ! The ASF licenses this file to You under the Apache License, Version 2.0
+ ! (the "License"); you may not use this file except in compliance with
+ ! the License. You may obtain a copy of the License at
+ !
+ ! http://www.apache.org/licenses/LICENSE-2.0
+ !
+ ! Unless required by applicable law or agreed to in writing, software
+ ! distributed under the License is distributed on an "AS IS" BASIS,
+ ! WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ ! See the License for the specific language governing permissions and
+ ! limitations under the License.
+ !-->
+<!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>
Propchange: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/package.html
------------------------------------------------------------------------------
svn:eol-style = native
Added: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/persistence/CopyDoc.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/persistence/CopyDoc.java?rev=1211081&view=auto
==============================================================================
--- pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/persistence/CopyDoc.java (added)
+++ pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/persistence/CopyDoc.java Tue Dec 6 20:15:18 2011
@@ -0,0 +1,126 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.pdfbox.examples.persistence;
+
+import java.io.IOException;
+
+import org.apache.pdfbox.cos.COSDocument;
+
+
+
+import org.apache.pdfbox.pdfparser.PDFParser;
+
+import org.apache.pdfbox.pdfwriter.COSWriter;
+import org.apache.pdfbox.exceptions.COSVisitorException;
+
+/**
+ * This is an example used to copy a documents contents from a source doc to destination doc
+ * via an in-memory document representation.
+ *
+ * @author Michael Traut
+ * @version $Revision: 1.7 $
+ */
+public class CopyDoc
+{
+ /**
+ * Constructor.
+ */
+ public CopyDoc()
+ {
+ super();
+ }
+
+ /**
+ * This will perform the document copy.
+ *
+ * @param in The filename used for input.
+ * @param out The filename used for output.
+ *
+ * @throws IOException If there is an error parsing the document.
+ * @throws COSVisitorException If there is an error while copying the document.
+ */
+ public void doIt(String in, String out) throws IOException, COSVisitorException
+ {
+ java.io.InputStream is = null;
+ java.io.OutputStream os = null;
+ COSWriter writer = null;
+ try
+ {
+ is = new java.io.FileInputStream(in);
+ PDFParser parser = new PDFParser(is);
+ parser.parse();
+
+ COSDocument doc = parser.getDocument();
+
+ os = new java.io.FileOutputStream(out);
+ writer = new COSWriter(os);
+
+ writer.write(doc);
+
+ }
+ finally
+ {
+ if( is != null )
+ {
+ is.close();
+ }
+ if( os != null )
+ {
+ os.close();
+ }
+ if( writer != null )
+ {
+ writer.close();
+ }
+ }
+ }
+
+ /**
+ * This will copy a PDF document.
+ * <br />
+ * see usage() for commandline
+ *
+ * @param args command line arguments
+ */
+ public static void main(String[] args)
+ {
+ CopyDoc app = new CopyDoc();
+ 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() + " <input-file> <output-file>" );
+ }
+}
Propchange: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/persistence/CopyDoc.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/persistence/package.html
URL: http://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/persistence/package.html?rev=1211081&view=auto
==============================================================================
--- pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/persistence/package.html (added)
+++ pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/persistence/package.html Tue Dec 6 20:15:18 2011
@@ -0,0 +1,25 @@
+<!--
+ ! Licensed to the Apache Software Foundation (ASF) under one or more
+ ! contributor license agreements. See the NOTICE file distributed with
+ ! this work for additional information regarding copyright ownership.
+ ! The ASF licenses this file to You under the Apache License, Version 2.0
+ ! (the "License"); you may not use this file except in compliance with
+ ! the License. You may obtain a copy of the License at
+ !
+ ! http://www.apache.org/licenses/LICENSE-2.0
+ !
+ ! Unless required by applicable law or agreed to in writing, software
+ ! distributed under the License is distributed on an "AS IS" BASIS,
+ ! WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ ! See the License for the specific language governing permissions and
+ ! limitations under the License.
+ !-->
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
+<html>
+<head>
+
+</head>
+<body>
+These examples will show how to use the persistence features of the PDFBox.
+</body>
+</html>
Propchange: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/persistence/package.html
------------------------------------------------------------------------------
svn:eol-style = native