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 [1/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...
Author: gbailleul
Date: Tue Dec 6 20:15:18 2011
New Revision: 1211081
URL: http://svn.apache.org/viewvc?rev=1211081&view=rev
Log:
PDFBOX-1177: create pdfbox-examples
Added:
pdfbox/trunk/examples/
pdfbox/trunk/examples/pom.xml (with props)
pdfbox/trunk/examples/src/
pdfbox/trunk/examples/src/main/
pdfbox/trunk/examples/src/main/java/
pdfbox/trunk/examples/src/main/java/org/
pdfbox/trunk/examples/src/main/java/org/apache/
pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/
pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/
pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/AbstractExample.java (with props)
pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/fdf/
pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/fdf/PrintFields.java (with props)
pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/fdf/SetField.java (with props)
pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/fdf/package.html (with props)
pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/package.html (with props)
pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/
pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/AddImageToPDF.java (with props)
pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/AddJavascript.java (with props)
pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/AddMessageToEachPage.java (with props)
pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/AddMetadataFromDocInfo.java (with props)
pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/Annotation.java (with props)
pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/CreateBlankPDF.java (with props)
pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/CreateBookmarks.java (with props)
pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/CreateLandscapePDF.java (with props)
pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/EmbeddedFiles.java (with props)
pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/ExtractMetadata.java (with props)
pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/GoToSecondBookmarkOnOpen.java (with props)
pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/HelloWorld.java (with props)
pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/HelloWorldTTF.java (with props)
pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/HelloWorldType1AfmPfb.java (with props)
pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/ImageToPDF.java (with props)
pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/PrintBookmarks.java (with props)
pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/PrintDocumentMetaData.java (with props)
pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/PrintURLs.java (with props)
pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/RemoveFirstPage.java (with props)
pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/ReplaceString.java (with props)
pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/ReplaceURLs.java (with props)
pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/RubberStamp.java (with props)
pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/RubberStampWithImage.java (with props)
pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/ShowColorBoxes.java (with props)
pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/UsingTextMatrix.java (with props)
pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/package.html (with props)
pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/persistence/
pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/persistence/CopyDoc.java (with props)
pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/persistence/package.html (with props)
pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/signature/
pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/signature/ShowSignature.java (with props)
pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/signature/package.html (with props)
pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/util/
pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/util/ExtractTextByArea.java (with props)
pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/util/PrintImageLocations.java (with props)
pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/util/PrintTextLocations.java (with props)
pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/util/RemoveAllText.java (with props)
pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/util/package.html (with props)
Modified:
pdfbox/trunk/pom.xml
Added: pdfbox/trunk/examples/pom.xml
URL: http://svn.apache.org/viewvc/pdfbox/trunk/examples/pom.xml?rev=1211081&view=auto
==============================================================================
--- pdfbox/trunk/examples/pom.xml (added)
+++ pdfbox/trunk/examples/pom.xml Tue Dec 6 20:15:18 2011
@@ -0,0 +1,60 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!--
+ ! 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.
+ !-->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+
+ <parent>
+ <groupId>org.apache.pdfbox</groupId>
+ <artifactId>pdfbox-parent</artifactId>
+ <version>1.7.0-SNAPSHOT</version>
+ <relativePath>../parent/pom.xml</relativePath>
+ </parent>
+
+ <artifactId>pdfbox-examples</artifactId>
+
+ <packaging>jar</packaging>
+ <name>Apache PDFBox</name>
+ <description>
+ The Apache PDFBox library is an open source Java tool for working with PDF documents.
+ This artefact contains examples on how the library can be used.
+ </description>
+ <inceptionYear>2002</inceptionYear>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.pdfbox</groupId>
+ <artifactId>pdfbox</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+ </dependencies>
+
+ <build>
+ <resources>
+ <resource>
+ <directory>src/main/resources</directory>
+ <filtering>true</filtering>
+ </resource>
+ </resources>
+ <plugins>
+ </plugins>
+ </build>
+
+</project>
+
Propchange: pdfbox/trunk/examples/pom.xml
------------------------------------------------------------------------------
svn:eol-style = native
Added: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/AbstractExample.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/AbstractExample.java?rev=1211081&view=auto
==============================================================================
--- pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/AbstractExample.java (added)
+++ pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/AbstractExample.java Tue Dec 6 20:15:18 2011
@@ -0,0 +1,111 @@
+/*
+ * 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;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+import org.apache.pdfbox.cos.COSDocument;
+
+import org.apache.pdfbox.pdmodel.PDDocument;
+
+import org.apache.pdfbox.pdfwriter.COSWriter;
+
+/**
+ * A simple class which has some methods used by all examples.
+ *
+ * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
+ * @version $Revision: 1.4 $
+ */
+public abstract class AbstractExample
+{
+ /**
+ * Close the stream.
+ *
+ * @param stream The stream to close.
+ *
+ * @throws IOException If there is an error closing the stream.
+ */
+ public void close( InputStream stream ) throws IOException
+ {
+ if( stream != null )
+ {
+ stream.close();
+ }
+ }
+
+ /**
+ * Close the stream.
+ *
+ * @param stream The stream to close.
+ *
+ * @throws IOException If there is an error closing the stream.
+ */
+ public void close( OutputStream stream ) throws IOException
+ {
+ if( stream != null )
+ {
+ stream.close();
+ }
+ }
+
+ /**
+ * Close the document.
+ *
+ * @param doc The doc to close.
+ *
+ * @throws IOException If there is an error closing the document.
+ */
+ public void close( COSDocument doc ) throws IOException
+ {
+ if( doc != null )
+ {
+ doc.close();
+ }
+ }
+
+ /**
+ * Close the document.
+ *
+ * @param doc The doc to close.
+ *
+ * @throws IOException If there is an error closing the document.
+ */
+ public void close( PDDocument doc ) throws IOException
+ {
+ if( doc != null )
+ {
+ doc.close();
+ }
+ }
+
+ /**
+ * Close the writer.
+ *
+ * @param writer The writer to close.
+ *
+ * @throws IOException If there is an error closing the writer.
+ */
+ public static void close( COSWriter writer ) throws IOException
+ {
+ if( writer != null )
+ {
+ writer.close();
+ }
+ }
+}
Propchange: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/AbstractExample.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/fdf/PrintFields.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/fdf/PrintFields.java?rev=1211081&view=auto
==============================================================================
--- pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/fdf/PrintFields.java (added)
+++ pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/fdf/PrintFields.java Tue Dec 6 20:15:18 2011
@@ -0,0 +1,149 @@
+/*
+ * 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.fdf;
+
+import java.io.IOException;
+
+import java.util.Iterator;
+import java.util.List;
+
+import org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm;
+import org.apache.pdfbox.pdmodel.interactive.form.PDField;
+
+import org.apache.pdfbox.exceptions.CryptographyException;
+import org.apache.pdfbox.exceptions.InvalidPasswordException;
+
+import org.apache.pdfbox.pdmodel.PDDocument;
+import org.apache.pdfbox.pdmodel.PDDocumentCatalog;
+
+/**
+ * This example will take a PDF document and print all the fields from the file.
+ *
+ * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
+ * @version $Revision: 1.16 $
+ */
+public class PrintFields
+{
+
+ /**
+ * This will print all the fields from the document.
+ *
+ * @param pdfDocument The PDF to get the fields from.
+ *
+ * @throws IOException If there is an error getting the fields.
+ */
+ public void printFields( PDDocument pdfDocument ) throws IOException
+ {
+ PDDocumentCatalog docCatalog = pdfDocument.getDocumentCatalog();
+ PDAcroForm acroForm = docCatalog.getAcroForm();
+ List fields = acroForm.getFields();
+ Iterator fieldsIter = fields.iterator();
+
+ System.out.println(new Integer(fields.size()).toString() + " top-level fields were found on the form");
+
+ while( fieldsIter.hasNext())
+ {
+ PDField field = (PDField)fieldsIter.next();
+ processField(field, "|--", field.getPartialName());
+ }
+ }
+
+ private void processField(PDField field, String sLevel, String sParent) throws IOException
+ {
+ List kids = field.getKids();
+ if(kids != null)
+ {
+ Iterator kidsIter = kids.iterator();
+ if(!sParent.equals(field.getPartialName()))
+ {
+ sParent = sParent + "." + field.getPartialName();
+ }
+ System.out.println(sLevel + sParent);
+ //System.out.println(sParent + " is of type " + field.getClass().getName());
+ while(kidsIter.hasNext())
+ {
+ Object pdfObj = kidsIter.next();
+ if(pdfObj instanceof PDField)
+ {
+ PDField kid = (PDField)pdfObj;
+ processField(kid, "| " + sLevel, sParent);
+ }
+ }
+ }
+ else
+ {
+ String outputString = sLevel + sParent + "." + field.getPartialName() + " = " + field.getValue() +
+ ", type=" + field.getClass().getName();
+
+ System.out.println(outputString);
+ }
+ }
+
+ /**
+ * This will read a PDF file and print out the form elements.
+ * <br />
+ * see usage() for commandline
+ *
+ * @param args command line arguments
+ *
+ * @throws IOException If there is an error importing the FDF document.
+ * @throws CryptographyException If there is an error decrypting the document.
+ */
+ public static void main(String[] args) throws IOException, CryptographyException
+ {
+ PDDocument pdf = null;
+ try
+ {
+ if( args.length != 1 )
+ {
+ usage();
+ }
+ else
+ {
+ pdf = PDDocument.load( args[0] );
+ PrintFields exporter = new PrintFields();
+ if( pdf.isEncrypted() )
+ {
+ try
+ {
+ pdf.decrypt( "" );
+ }
+ catch( InvalidPasswordException e )
+ {
+ System.err.println( "Error: The document is encrypted." );
+ usage();
+ }
+ }
+ exporter.printFields( pdf );
+ }
+ }
+ finally
+ {
+ if( pdf != null )
+ {
+ pdf.close();
+ }
+ }
+ }
+ /**
+ * This will print out a message telling how to use this example.
+ */
+ private static void usage()
+ {
+ System.err.println( "usage: org.apache.pdfbox.examples.fdf.PrintFields <pdf-file>" );
+ }
+}
Propchange: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/fdf/PrintFields.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/fdf/SetField.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/fdf/SetField.java?rev=1211081&view=auto
==============================================================================
--- pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/fdf/SetField.java (added)
+++ pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/fdf/SetField.java Tue Dec 6 20:15:18 2011
@@ -0,0 +1,114 @@
+/*
+ * 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.fdf;
+
+import java.io.IOException;
+
+import org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm;
+import org.apache.pdfbox.pdmodel.interactive.form.PDField;
+
+import org.apache.pdfbox.pdmodel.PDDocument;
+import org.apache.pdfbox.pdmodel.PDDocumentCatalog;
+
+import org.apache.pdfbox.exceptions.COSVisitorException;
+
+import org.apache.pdfbox.examples.AbstractExample;
+
+/**
+ * This example will take a PDF document and set a FDF field in it.
+ *
+ * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
+ * @version $Revision: 1.7 $
+ */
+public class SetField extends AbstractExample
+{
+
+ /**
+ * This will set a single field in the document.
+ *
+ * @param pdfDocument The PDF to set the field in.
+ * @param name The name of the field to set.
+ * @param value The new value of the field.
+ *
+ * @throws IOException If there is an error setting the field.
+ */
+ public void setField( PDDocument pdfDocument, String name, String value ) throws IOException
+ {
+ PDDocumentCatalog docCatalog = pdfDocument.getDocumentCatalog();
+ PDAcroForm acroForm = docCatalog.getAcroForm();
+ PDField field = acroForm.getField( name );
+ if( field != null )
+ {
+ field.setValue( value );
+ }
+ else
+ {
+ System.err.println( "No field found with name:" + name );
+ }
+
+ }
+
+ /**
+ * This will read a PDF file and set a field and then write it the pdf out again.
+ * <br />
+ * see usage() for commandline
+ *
+ * @param args command line arguments
+ *
+ * @throws IOException If there is an error importing the FDF document.
+ * @throws COSVisitorException If there is an error writing the PDF.
+ */
+ public static void main(String[] args) throws IOException, COSVisitorException
+ {
+ SetField setter = new SetField();
+ setter.setField( args );
+ }
+
+ private void setField( String[] args ) throws IOException, COSVisitorException
+ {
+ PDDocument pdf = null;
+ try
+ {
+ if( args.length != 3 )
+ {
+ usage();
+ }
+ else
+ {
+ SetField example = new SetField();
+
+ pdf = PDDocument.load( args[0] );
+ example.setField( pdf, args[1], args[2] );
+ pdf.save( args[0] );
+ }
+ }
+ finally
+ {
+ if( pdf != null )
+ {
+ pdf.close();
+ }
+ }
+ }
+ /**
+ * This will print out a message telling how to use this example.
+ */
+ private static void usage()
+ {
+ System.err.println( "usage: org.apache.pdfbox.examples.fdf.SetField <pdf-file> <field-name> <field-value>" );
+ }
+}
Propchange: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/fdf/SetField.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/fdf/package.html
URL: http://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/fdf/package.html?rev=1211081&view=auto
==============================================================================
--- pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/fdf/package.html (added)
+++ pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/fdf/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 are examples that use the FDF features of a PDF document.
+</body>
+</html>
Propchange: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/fdf/package.html
------------------------------------------------------------------------------
svn:eol-style = native
Added: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/package.html
URL: http://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/package.html?rev=1211081&view=auto
==============================================================================
--- pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/package.html (added)
+++ pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/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>
+The packages in this package will show how to use the PDFBox API.
+</body>
+</html>
Propchange: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/package.html
------------------------------------------------------------------------------
svn:eol-style = native
Added: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/AddImageToPDF.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/AddImageToPDF.java?rev=1211081&view=auto
==============================================================================
--- pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/AddImageToPDF.java (added)
+++ pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/AddImageToPDF.java Tue Dec 6 20:15:18 2011
@@ -0,0 +1,134 @@
+/*
+ * 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 reads a document and adds an image to it..
+ *
+ * The example is taken from the pdf file format specification.
+ *
+ * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
+ * @version $Revision: 1.1 $
+ */
+public class AddImageToPDF
+{
+
+ /**
+ * Add an image to an existing PDF document.
+ *
+ * @param inputFile The input PDF to add the image to.
+ * @param image The filename of the image to put in the PDF.
+ * @param outputFile The file to write to 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 createPDFFromImage( String inputFile, String image, String outputFile )
+ throws IOException, COSVisitorException
+ {
+ // the document
+ PDDocument doc = null;
+ try
+ {
+ doc = PDDocument.load( inputFile );
+
+ //we will add the image to the first page.
+ PDPage page = (PDPage)doc.getDocumentCatalog().getAllPages().get( 0 );
+
+ 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, true, true);
+
+ contentStream.drawImage( ximage, 20, 20 );
+
+ contentStream.close();
+ doc.save( outputFile );
+ }
+ finally
+ {
+ if( doc != null )
+ {
+ doc.close();
+ }
+ }
+ }
+
+ /**
+ * This will load a PDF document and add a single image on it.
+ * <br />
+ * see usage() for commandline
+ *
+ * @param args Command line arguments.
+ */
+ public static void main(String[] args)
+ {
+ AddImageToPDF app = new AddImageToPDF();
+ try
+ {
+ if( args.length != 3 )
+ {
+ app.usage();
+ }
+ else
+ {
+ app.createPDFFromImage( 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-pdf> <image> <output-pdf>" );
+ }
+}
Propchange: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/AddImageToPDF.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/AddJavascript.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/AddJavascript.java?rev=1211081&view=auto
==============================================================================
--- pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/AddJavascript.java (added)
+++ pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/AddJavascript.java Tue Dec 6 20:15:18 2011
@@ -0,0 +1,82 @@
+/*
+ * 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.interactive.action.type.PDActionJavaScript;
+
+import java.io.IOException;
+
+/**
+ * This is an example of how to set some javascript in the document.
+ *
+ * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
+ * @version $Revision: 1.3 $
+ */
+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.apache.pdfbox.examples.pdmodel.AddJavascript <input-pdf> <output-pdf>" );
+ }
+}
Propchange: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/AddJavascript.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/AddMessageToEachPage.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/AddMessageToEachPage.java?rev=1211081&view=auto
==============================================================================
--- pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/AddMessageToEachPage.java (added)
+++ pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/AddMessageToEachPage.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 java.io.IOException;
+import java.util.List;
+
+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 add a message to every page
+ * in a pdf document.
+ *
+ * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
+ * @version $Revision: 1.3 $
+ */
+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 = 36.0f;
+
+ for( int i=0; i<allPages.size(); i++ )
+ {
+ PDPage page = (PDPage)allPages.get( i );
+ PDRectangle pageSize = page.findMediaBox();
+ float stringWidth = font.getStringWidth( message )*fontSize/1000f;
+ // calculate to center of the page
+ int rotation = page.findRotation();
+ boolean rotate = rotation == 90 || rotation == 270;
+ float pageWidth = rotate ? pageSize.getHeight() : pageSize.getWidth();
+ float pageHeight = rotate ? pageSize.getWidth() : pageSize.getHeight();
+ double centeredXPosition = rotate ? pageHeight/2f : (pageWidth - stringWidth)/2f;
+ double centeredYPosition = rotate ? (pageWidth - stringWidth)/2f : pageHeight/2f;
+ // append the content to the existing stream
+ PDPageContentStream contentStream = new PDPageContentStream(doc, page, true, true,true);
+ contentStream.beginText();
+ // set font and font size
+ contentStream.setFont( font, fontSize );
+ // set text color to red
+ contentStream.setNonStrokingColor(255, 0, 0);
+ if (rotate)
+ {
+ // rotate the text according to the page rotation
+ contentStream.setTextRotation(Math.PI/2, centeredXPosition, centeredYPosition);
+ }
+ else
+ {
+ contentStream.setTextTranslation(centeredXPosition, centeredYPosition);
+ }
+ 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>" );
+ }
+}
Propchange: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/AddMessageToEachPage.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/AddMetadataFromDocInfo.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/AddMetadataFromDocInfo.java?rev=1211081&view=auto
==============================================================================
--- pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/AddMetadataFromDocInfo.java (added)
+++ pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/AddMetadataFromDocInfo.java Tue Dec 6 20:15:18 2011
@@ -0,0 +1,115 @@
+/*
+ * 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.jempbox.xmp.XMPMetadata;
+import org.apache.jempbox.xmp.XMPSchemaBasic;
+import org.apache.jempbox.xmp.XMPSchemaDublinCore;
+import org.apache.jempbox.xmp.XMPSchemaPDF;
+
+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.util.GregorianCalendar;
+
+/**
+ * This is an example on how to add metadata to a document.
+ *
+ * Usage: java org.apache.pdfbox.examples.pdmodel.AddMetadataToDocument <input-pdf> <output-pdf>
+ *
+ * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
+ * @version $Revision: 1.4 $
+ */
+public class AddMetadataFromDocInfo
+{
+ 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();
+
+ XMPMetadata metadata = new XMPMetadata();
+
+ XMPSchemaPDF pdfSchema = metadata.addPDFSchema();
+ pdfSchema.setKeywords( info.getKeywords() );
+ pdfSchema.setProducer( info.getProducer() );
+
+ XMPSchemaBasic basicSchema = metadata.addBasicSchema();
+ basicSchema.setModifyDate( info.getModificationDate() );
+ basicSchema.setCreateDate( info.getCreationDate() );
+ basicSchema.setCreatorTool( info.getCreator() );
+ basicSchema.setMetadataDate( new GregorianCalendar() );
+
+ XMPSchemaDublinCore dcSchema = metadata.addDublinCoreSchema();
+ dcSchema.setTitle( info.getTitle() );
+ dcSchema.addCreator( "PDFBox" );
+ dcSchema.setDescription( info.getSubject() );
+
+ PDMetadata metadataStream = new PDMetadata(document);
+ metadataStream.importXMPMetadata( metadata );
+ catalog.setMetadata( metadataStream );
+
+ 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.AddMetadataFromDocInfo " +
+ "<input-pdf> <output-pdf>" );
+ }
+}
Propchange: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/AddMetadataFromDocInfo.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/Annotation.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/Annotation.java?rev=1211081&view=auto
==============================================================================
--- pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/Annotation.java (added)
+++ pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/Annotation.java Tue Dec 6 20:15:18 2011
@@ -0,0 +1,262 @@
+/*
+ * 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.edit.PDPageContentStream;
+import org.apache.pdfbox.pdmodel.font.PDFont;
+import org.apache.pdfbox.pdmodel.font.PDType1Font;
+import org.apache.pdfbox.pdmodel.graphics.color.PDGamma;
+import org.apache.pdfbox.pdmodel.interactive.action.type.PDActionURI;
+import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationLine;
+import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationSquareCircle;
+import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationTextMarkup;
+import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationLink;
+import org.apache.pdfbox.pdmodel.interactive.annotation.PDBorderStyleDictionary;
+
+
+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.2 $
+ */
+public class Annotation
+{
+ private Annotation()
+ {
+ //utility class, should not be instantiated.
+ }
+
+ /**
+ * This will create a doucument showing various annotations.
+ *
+ * @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 = new PDDocument();
+
+ try
+ {
+ PDPage page = new PDPage();
+ document.addPage(page);
+ List annotations = page.getAnnotations();
+
+ // Setup some basic reusable objects/constants
+ // Annotations themselves can only be used once!
+
+ float inch = 72;
+ PDGamma colourRed = new PDGamma();
+ colourRed.setR(1);
+ PDGamma colourBlue = new PDGamma();
+ colourBlue.setB(1);
+ PDGamma colourBlack = new PDGamma();
+
+ PDBorderStyleDictionary borderThick = new PDBorderStyleDictionary();
+ borderThick.setWidth(inch/12); // 12th inch
+ PDBorderStyleDictionary borderThin = new PDBorderStyleDictionary();
+ borderThin.setWidth(inch/72); // 1 point
+ PDBorderStyleDictionary borderULine = new PDBorderStyleDictionary();
+ borderULine.setStyle(PDBorderStyleDictionary.STYLE_UNDERLINE);
+ borderULine.setWidth(inch/72); // 1 point
+
+
+ float pw = page.getMediaBox().getUpperRightX();
+ float ph = page.getMediaBox().getUpperRightY();
+
+
+ // First add some text, two lines we'll add some annotations to this later
+
+
+ PDFont font = PDType1Font.HELVETICA_BOLD;
+
+ PDPageContentStream contentStream = new PDPageContentStream(document, page);
+ contentStream.beginText();
+ contentStream.setFont( font, 18 );
+ contentStream.moveTextPositionByAmount( inch, ph-inch-18);
+ contentStream.drawString( "PDFBox" );
+ contentStream.moveTextPositionByAmount( 0,-(inch/2));
+ contentStream.drawString( "Click Here" );
+ contentStream.endText();
+
+ contentStream.close();
+
+ // Now add the markup annotation, a highlight to PDFBox text
+ PDAnnotationTextMarkup txtMark = new PDAnnotationTextMarkup(PDAnnotationTextMarkup.SUB_TYPE_HIGHLIGHT);
+ txtMark.setColour(colourBlue);
+ txtMark.setConstantOpacity((float)0.2); // Make the highlight 20% transparent
+
+ // Set the rectangle containing the markup
+
+ float textWidth = (font.getStringWidth( "PDFBox" )/1000) * 18;
+ PDRectangle position = new PDRectangle();
+ position.setLowerLeftX(inch);
+ position.setLowerLeftY( ph-inch-18 );
+ position.setUpperRightX(72 + textWidth);
+ position.setUpperRightY(ph-inch);
+ txtMark.setRectangle(position);
+
+ // work out the points forming the four corners of the annotations
+ // set out in anti clockwise form (Completely wraps the text)
+ // OK, the below doesn't match that description.
+ // It's what acrobat 7 does and displays properly!
+ float[] quads = new float[8];
+
+ quads[0] = position.getLowerLeftX(); // x1
+ quads[1] = position.getUpperRightY()-2; // y1
+ quads[2] = position.getUpperRightX(); // x2
+ quads[3] = quads[1]; // y2
+ quads[4] = quads[0]; // x3
+ quads[5] = position.getLowerLeftY()-2; // y3
+ quads[6] = quads[2]; // x4
+ quads[7] = quads[5]; // y5
+
+ txtMark.setQuadPoints(quads);
+ txtMark.setContents("Highlighted since it's important");
+
+ annotations.add(txtMark);
+
+ // Now add the link annotation, so the clickme works
+ PDAnnotationLink txtLink = new PDAnnotationLink();
+ txtLink.setBorderStyle(borderULine);
+
+ // Set the rectangle containing the link
+
+ textWidth = (font.getStringWidth( "Click Here" )/1000) * 18;
+ position = new PDRectangle();
+ position.setLowerLeftX(inch);
+ position.setLowerLeftY( ph-(float)(1.5*inch)-20); // down a couple of points
+ position.setUpperRightX(72 + textWidth);
+ position.setUpperRightY(ph-(float)(1.5*inch));
+ txtLink.setRectangle(position);
+
+ // add an action
+ PDActionURI action = new PDActionURI();
+ action.setURI("http://www.pdfbox.org");
+ txtLink.setAction(action);
+
+ annotations.add(txtLink);
+
+
+ // Now draw a few more annotations
+
+ PDAnnotationSquareCircle aCircle =
+ new PDAnnotationSquareCircle( PDAnnotationSquareCircle.SUB_TYPE_CIRCLE);
+ aCircle.setContents("Circle Annotation");
+ aCircle.setInteriorColour(colourRed); // Fill in circle in red
+ aCircle.setColour(colourBlue); // The border itself will be blue
+ aCircle.setBorderStyle(borderThin);
+
+ // Place the annotation on the page, we'll make this 1" round
+ // 3" down, 1" in on the page
+
+ position = new PDRectangle();
+ position.setLowerLeftX(inch);
+ position.setLowerLeftY(ph-(3*inch)-inch); // 1" height, 3" down
+ position.setUpperRightX(2*inch); // 1" in, 1" width
+ position.setUpperRightY(ph-(3*inch)); // 3" down
+ aCircle.setRectangle(position);
+
+ // add to the annotations on the page
+ annotations.add(aCircle);
+
+ // Now a square annotation
+
+ PDAnnotationSquareCircle aSquare =
+ new PDAnnotationSquareCircle( PDAnnotationSquareCircle.SUB_TYPE_SQUARE);
+ aSquare.setContents("Square Annotation");
+ aSquare.setColour(colourRed); // Outline in red, not setting a fill
+ aSquare.setBorderStyle(borderThick);
+
+ // Place the annotation on the page, we'll make this 1" (72points) square
+ // 3.5" down, 1" in from the right on the page
+
+ position = new PDRectangle(); // Reuse the variable, but note it's a new object!
+ position.setLowerLeftX(pw-(2*inch)); // 1" in from right, 1" wide
+ position.setLowerLeftY(ph-(float)(3.5*inch) - inch); // 1" height, 3.5" down
+ position.setUpperRightX(pw-inch); // 1" in from right
+ position.setUpperRightY(ph-(float)(3.5*inch)); // 3.5" down
+ aSquare.setRectangle(position);
+
+ // add to the annotations on the page
+ annotations.add(aSquare);
+
+ // Now we want to draw a line between the two, one end with an open arrow
+
+ PDAnnotationLine aLine = new PDAnnotationLine();
+
+ aLine.setEndPointEndingStyle( PDAnnotationLine.LE_OPEN_ARROW );
+ aLine.setContents("Circle->Square");
+ aLine.setCaption(true); // Make the contents a caption on the line
+
+ // Set the rectangle containing the line
+
+ position = new PDRectangle(); // Reuse the variable, but note it's a new object!
+ position.setLowerLeftX(2*inch); // 1" in + width of circle
+ position.setLowerLeftY(ph-(float)(3.5*inch)-inch); // 1" height, 3.5" down
+ position.setUpperRightX(pw-inch-inch); // 1" in from right, and width of square
+ position.setUpperRightY(ph-(3*inch)); // 3" down (top of circle)
+ aLine.setRectangle(position);
+
+ // Now set the line position itself
+ float[] linepos = new float[4];
+ linepos[0] = 2*inch; // x1 = rhs of circle
+ linepos[1] = ph-(float)(3.5*inch); // y1 halfway down circle
+ linepos[2] = pw-(2*inch); // x2 = lhs of square
+ linepos[3] = ph-(4*inch); // y2 halfway down square
+ aLine.setLine(linepos);
+
+ aLine.setBorderStyle(borderThick);
+ aLine.setColour(colourBlack);
+
+ // add to the annotations on the page
+ annotations.add(aLine);
+
+
+ // Finally all done
+
+
+ document.save( args[0] );
+ }
+ finally
+ {
+ document.close();
+ }
+ }
+ }
+
+ /**
+ * This will print the usage for this document.
+ */
+ private static void usage()
+ {
+ System.err.println( "Usage: java org.apache.pdfbox.examples.pdmodel.Annotation <output-pdf>" );
+ }
+}
Propchange: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/Annotation.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/CreateBlankPDF.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/CreateBlankPDF.java?rev=1211081&view=auto
==============================================================================
--- pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/CreateBlankPDF.java (added)
+++ pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/CreateBlankPDF.java Tue Dec 6 20:15:18 2011
@@ -0,0 +1,94 @@
+/*
+ * 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;
+
+/**
+ * This will create a blank PDF and write the contents to a file.
+ *
+ * usage: java org.apache.pdfbox.examples.pdmodel.CreateBlankPDF <outputfile.pdf>
+ *
+ * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
+ * @version $Revision: 1.9 $
+ */
+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;
+ 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 );
+ document.save( file );
+ }
+ finally
+ {
+ 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.apache.pdfbox.examples.pdmodel.CreateBlankPDF <outputfile.pdf>" );
+ }
+}
Propchange: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/CreateBlankPDF.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/CreateBookmarks.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/CreateBookmarks.java?rev=1211081&view=auto
==============================================================================
--- pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/CreateBookmarks.java (added)
+++ pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/CreateBookmarks.java Tue Dec 6 20:15:18 2011
@@ -0,0 +1,105 @@
+/*
+ * 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.interactive.documentnavigation.destination.PDPageFitWidthDestination;
+import org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline.PDDocumentOutline;
+import org.apache.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.apache.pdfbox.examples.pdmodel.CreateBookmarks <input-pdf> <output-pdf>
+ *
+ * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
+ * @version $Revision: 1.2 $
+ */
+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.apache.pdfbox.examples.pdmodel.CreateBookmarks <input-pdf> <output-pdf>" );
+ }
+}
Propchange: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/CreateBookmarks.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/CreateLandscapePDF.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/CreateLandscapePDF.java?rev=1211081&view=auto
==============================================================================
--- pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/CreateLandscapePDF.java (added)
+++ pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/CreateLandscapePDF.java Tue Dec 6 20:15:18 2011
@@ -0,0 +1,135 @@
+/*
+ * 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 create a page with a landscape orientation.
+ * @version $Revision: 1.0 $
+ */
+public class CreateLandscapePDF
+{
+ /**
+ * Constructor.
+ */
+ public CreateLandscapePDF()
+ {
+ super();
+ }
+
+ /**
+ * creates a sample document with a landscape orientation and some text surrounded by a box.
+ *
+ * @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();
+
+ PDFont font = PDType1Font.HELVETICA;
+ PDPage page = new PDPage();
+ page.setMediaBox(PDPage.PAGE_SIZE_A4);
+ page.setRotation(90);
+ doc.addPage(page);
+ PDRectangle pageSize = page.findMediaBox();
+ float pageWidth = pageSize.getWidth();
+ float fontSize = 12;
+ float stringWidth = font.getStringWidth( message )*fontSize/1000f;
+ float startX = 100;
+ float startY = 100;
+ PDPageContentStream contentStream = new PDPageContentStream(doc, page, false, false);
+ // add the rotation using the current transformation matrix
+ // including a translation of pageWidth to use the lower left corner as 0,0 reference
+ contentStream.concatenate2CTM(0, 1, -1, 0, pageWidth, 0);
+ contentStream.setFont( font, fontSize );
+ contentStream.beginText();
+ contentStream.moveTextPositionByAmount(startX, startY);
+ contentStream.drawString( message);
+ contentStream.moveTextPositionByAmount(0, 100);
+ contentStream.drawString( message);
+ contentStream.moveTextPositionByAmount(100, 100);
+ contentStream.drawString( message);
+ contentStream.endText();
+
+ contentStream.drawLine(startX-2, startY-2, startX-2, startY+200+fontSize);
+ contentStream.drawLine(startX-2, startY+200+fontSize, startX+100+stringWidth+2, startY+200+fontSize);
+ contentStream.drawLine(startX+100+stringWidth+2, startY+200+fontSize, startX+100+stringWidth+2, startY-2);
+ contentStream.drawLine(startX+100+stringWidth+2, startY-2, startX-2, startY-2);
+ contentStream.close();
+
+ doc.save( outfile );
+ }
+ finally
+ {
+ if( doc != null )
+ {
+ doc.close();
+ }
+ }
+ }
+
+ /**
+ * This will create a PDF document with a landscape orientation and some text surrounded by a box.
+ *
+ * @param args Command line arguments.
+ */
+ public static void main(String[] args)
+ {
+ CreateLandscapePDF app = new CreateLandscapePDF();
+ 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/CreateLandscapePDF.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/EmbeddedFiles.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/EmbeddedFiles.java?rev=1211081&view=auto
==============================================================================
--- pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/EmbeddedFiles.java (added)
+++ pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/EmbeddedFiles.java Tue Dec 6 20:15:18 2011
@@ -0,0 +1,155 @@
+/*
+ * 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.ByteArrayInputStream;
+import java.io.IOException;
+import java.util.Collections;
+import java.util.GregorianCalendar;
+import java.util.SortedMap;
+import java.util.TreeMap;
+
+import org.apache.pdfbox.exceptions.COSVisitorException;
+
+import org.apache.pdfbox.pdmodel.PDDocument;
+import org.apache.pdfbox.pdmodel.PDDocumentNameDictionary;
+import org.apache.pdfbox.pdmodel.PDEmbeddedFilesNameTreeNode;
+import org.apache.pdfbox.pdmodel.PDPage;
+
+import org.apache.pdfbox.pdmodel.common.filespecification.PDComplexFileSpecification;
+import org.apache.pdfbox.pdmodel.common.filespecification.PDEmbeddedFile;
+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 and embeds a file into it..
+ *
+ * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
+ * @version $Revision: 1.2 $
+ */
+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("ISO-8859-1");
+ 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.
+ efTree.setNames( Collections.singletonMap( "My first attachment", fs ) );
+ 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>" );
+ }
+}
Propchange: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/EmbeddedFiles.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/ExtractMetadata.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/ExtractMetadata.java?rev=1211081&view=auto
==============================================================================
--- pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/ExtractMetadata.java (added)
+++ pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/ExtractMetadata.java Tue Dec 6 20:15:18 2011
@@ -0,0 +1,189 @@
+/*
+ * 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.jempbox.xmp.XMPMetadata;
+import org.apache.jempbox.xmp.XMPSchemaBasic;
+import org.apache.jempbox.xmp.XMPSchemaDublinCore;
+import org.apache.jempbox.xmp.XMPSchemaPDF;
+
+import org.apache.pdfbox.exceptions.InvalidPasswordException;
+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.text.DateFormat;
+import java.util.Calendar;
+import java.util.Iterator;
+import java.util.List;
+
+/**
+ * This is an example on how to extract metadata from a PDF document.
+ * <p>
+ * Usage: java org.apache.pdfbox.examples.pdmodel.ExtractDocument <input-pdf>
+ *
+ * @version $Revision$
+ */
+public class ExtractMetadata
+{
+ private ExtractMetadata()
+ {
+ //utility class
+ }
+
+ /**
+ * This is the main method.
+ *
+ * @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();
+ System.exit(1);
+ }
+ else
+ {
+ PDDocument document = null;
+
+ try
+ {
+ document = PDDocument.load( args[0] );
+ if (document.isEncrypted())
+ {
+ try
+ {
+ document.decrypt("");
+ }
+ catch( InvalidPasswordException e )
+ {
+ System.err.println( "Error: The document is encrypted." );
+ }
+ catch( org.apache.pdfbox.exceptions.CryptographyException e )
+ {
+ e.printStackTrace();
+ }
+ }
+ PDDocumentCatalog catalog = document.getDocumentCatalog();
+ PDMetadata meta = catalog.getMetadata();
+ if ( meta != null)
+ {
+ XMPMetadata metadata = meta.exportXMPMetadata();
+
+ XMPSchemaDublinCore dc = metadata.getDublinCoreSchema();
+ if (dc != null)
+ {
+ display("Title:", dc.getTitle());
+ display("Description:", dc.getDescription());
+ list("Creators: ", dc.getCreators());
+ list("Dates:", dc.getDates());
+ }
+
+ XMPSchemaPDF pdf = metadata.getPDFSchema();
+ if (pdf != null)
+ {
+ display("Keywords:", pdf.getKeywords());
+ display("PDF Version:", pdf.getPDFVersion());
+ display("PDF Producer:", pdf.getProducer());
+ }
+
+ XMPSchemaBasic basic = metadata.getBasicSchema();
+ if (basic != null)
+ {
+ display("Create Date:", basic.getCreateDate());
+ display("Modify Date:", basic.getModifyDate());
+ display("Creator Tool:", basic.getCreatorTool());
+ }
+ }
+ else
+ {
+ // The pdf doesn't contain any metadata, try to use the document information instead
+ PDDocumentInformation information = document.getDocumentInformation();
+ if ( information != null)
+ {
+ showDocumentInformation(information);
+ }
+ }
+
+ }
+ finally
+ {
+ if( document != null )
+ {
+ document.close();
+ }
+ }
+ }
+ }
+
+ private static void showDocumentInformation(PDDocumentInformation information)
+ {
+ display("Title:", information.getTitle());
+ display("Subject:", information.getSubject());
+ display("Author:", information.getAuthor());
+ display("Creator:", information.getCreator());
+ display("Producer:", information.getProducer());
+ }
+
+ private static void list(String title, List list)
+ {
+ if (list == null)
+ {
+ return;
+ }
+ System.out.println(title);
+ Iterator iter = list.iterator();
+ while (iter.hasNext())
+ {
+ Object o = iter.next();
+ System.out.println(" " + format(o));
+ }
+ }
+
+ private static String format(Object o)
+ {
+ if (o instanceof Calendar)
+ {
+ Calendar cal = (Calendar)o;
+ return DateFormat.getDateInstance().format(cal.getTime());
+ }
+ else
+ {
+ return o.toString();
+ }
+ }
+
+ private static void display(String title, Object value)
+ {
+ if (value != null)
+ {
+ System.out.println(title + " " + format(value));
+ }
+ }
+
+ /**
+ * This will print the usage for this program.
+ */
+ private static void usage()
+ {
+ System.err.println( "Usage: java " + ExtractMetadata.class.getName() + " <input-pdf>" );
+ }
+}
Propchange: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/ExtractMetadata.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/GoToSecondBookmarkOnOpen.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/GoToSecondBookmarkOnOpen.java?rev=1211081&view=auto
==============================================================================
--- pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/GoToSecondBookmarkOnOpen.java (added)
+++ pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/GoToSecondBookmarkOnOpen.java Tue Dec 6 20:15:18 2011
@@ -0,0 +1,104 @@
+/*
+ * 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.interactive.action.type.PDActionGoTo;
+import org.apache.pdfbox.pdmodel.interactive.documentnavigation.destination.PDDestination;
+import org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline.PDDocumentOutline;
+import org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline.PDOutlineItem;
+
+import java.io.IOException;
+import java.util.List;
+
+/**
+ * This is an example on how to an action to go to the second page when the PDF is opened.
+ *
+ * Usage: java org.apache.pdfbox.examples.pdmodel.GoToSecondPageOnOpen <input-pdf> <output-pdf>
+ *
+ * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
+ * @version $Revision: 1.3 $
+ */
+public class GoToSecondBookmarkOnOpen
+{
+ private GoToSecondBookmarkOnOpen()
+ {
+ //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 bookmark destination to encrypted documents." );
+ System.exit( 1 );
+ }
+
+ List pages = document.getDocumentCatalog().getAllPages();
+ if( pages.size() < 2 )
+ {
+ throw new IOException( "Error: The PDF must have at least 2 pages.");
+ }
+ PDDocumentOutline bookmarks = document.getDocumentCatalog().getDocumentOutline();
+ if( bookmarks == null )
+ {
+ throw new IOException( "Error: The PDF does not contain any bookmarks" );
+ }
+ PDOutlineItem item = bookmarks.getFirstChild().getNextSibling();
+ PDDestination dest = item.getDestination();
+ PDActionGoTo action = new PDActionGoTo();
+ action.setDestination(dest);
+ document.getDocumentCatalog().setOpenAction(action);
+
+ 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.GoToSecondBookmarkOnOpen" +
+ "<input-pdf> <output-pdf>" );
+ }
+}
Propchange: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/GoToSecondBookmarkOnOpen.java
------------------------------------------------------------------------------
svn:eol-style = native