pdfbox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From til...@apache.org
Subject svn commit: r1855650 - /pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/EmbeddedFiles.java
Date Sat, 16 Mar 2019 14:31:55 GMT
Author: tilman
Date: Sat Mar 16 14:31:55 2019
New Revision: 1855650

URL: http://svn.apache.org/viewvc?rev=1855650&view=rev
Log:
PDFBOX-4485: improve example for backwards, cross-platform and cross-language compatibility;
improve format; enable show attachments panel; remove typo

Modified:
    pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/EmbeddedFiles.java

Modified: 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=1855650&r1=1855649&r2=1855650&view=diff
==============================================================================
--- pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/EmbeddedFiles.java
(original)
+++ pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/EmbeddedFiles.java
Sat Mar 16 14:31:55 2019
@@ -27,11 +27,10 @@ import org.apache.pdfbox.pdmodel.PDDocum
 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.PDPageContentStream;
-
+import org.apache.pdfbox.pdmodel.PageMode;
 import org.apache.pdfbox.pdmodel.font.PDFont;
 import org.apache.pdfbox.pdmodel.font.PDType1Font;
 
@@ -47,7 +46,6 @@ public class EmbeddedFiles
      */
     public EmbeddedFiles()
     {
-        super();
     }
 
     /**
@@ -59,8 +57,7 @@ public class EmbeddedFiles
      */
     public void doIt( String file) throws IOException
     {
-        try ( // the document
-                PDDocument doc = new PDDocument())
+        try (PDDocument doc = new PDDocument())
         {
             PDPage page = new PDPage();
             doc.addPage( page );
@@ -87,16 +84,18 @@ public class EmbeddedFiles
 
             //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 );
+            ByteArrayInputStream fakeFile = new ByteArrayInputStream(data);
             PDEmbeddedFile ef = new PDEmbeddedFile(doc, fakeFile );
             //now lets some of the optional parameters
-            ef.setSubtype( "test/plain" );
+            ef.setSubtype( "text/plain" );
             ef.setSize( data.length );
             ef.setCreationDate( new GregorianCalendar() );
+
+            // use both methods for backwards, cross-platform and cross-language compatibility.
             fs.setEmbeddedFile( ef );
+            fs.setEmbeddedFileUnicode(ef);
 
-            // create a new tree node and add the embedded file 
+            // create a new tree node and add the embedded file
             PDEmbeddedFilesNameTreeNode treeNode = new PDEmbeddedFilesNameTreeNode();
             treeNode.setNames( Collections.singletonMap( "My first attachment",  fs ) );
             // add the new node as kid to the root node
@@ -108,6 +107,8 @@ public class EmbeddedFiles
             names.setEmbeddedFiles( efTree );
             doc.getDocumentCatalog().setNames( names );
 
+            // show attachments panel in some viewers 
+            doc.getDocumentCatalog().setPageMode(PageMode.USE_ATTACHMENTS);
 
             doc.save( file );
         }



Mime
View raw message