http://git-wip-us.apache.org/repos/asf/pdfbox-docs/blob/aa929f46/content/1.8/cookbook/pdfacreation.html ---------------------------------------------------------------------- diff --git a/content/1.8/cookbook/pdfacreation.html b/content/1.8/cookbook/pdfacreation.html new file mode 100644 index 0000000..382c1fc --- /dev/null +++ b/content/1.8/cookbook/pdfacreation.html @@ -0,0 +1,239 @@ + + + + + + + + + + + + Apache PDFBox | Create a valid PDF/A document + + + + + + + + + + + + + + + + + + +
+ +
+ +
+

PDF/A Creation

+ +

The Apache PDFBox API can be used to create a PDF/A File. PDF/A is a PDF file with some constraints to ensure its +long time conservation. These constraints are described in ISO 19005.

+ +

This small sample shows what should be added during creation of a PDF file to transform it in a valid PDF/A +document. The current example creates a valid PDF/A-1b document.

+ +

Load all the fonts used in document

+ +

The PDF/A specification enforces that the fonts used in the document are present in the PDF File. You +have to load them. As an example:

+
InputStream fontStream = CreatePDFA.class.getResourceAsStream("/org/apache/pdfbox/resources/ttf/ArialMT.ttf");
+PDFont font = PDTrueTypeFont.loadTTF(doc, fontStream);
+
+

Including XMP metadata block

+ +

It is imposed to have xmp metadata defined in the PDF. At least, the PDFA Schema (giving details on the version +of PDF/A specification reached by the document) must be present. These lines create the xmp metadata for a +PDF/A-1b document:

+
XMPMetadata xmp = new XMPMetadata();
+XMPSchemaPDFAId pdfaid = new XMPSchemaPDFAId(xmp);
+xmp.addSchema(pdfaid);
+pdfaid.setConformance("B");
+pdfaid.setPart(1);
+pdfaid.setAbout("");
+metadata.importXMPMetadata(xmp);
+
+

Including color profile

+ +

It is mandatory to include the color profile used by the document. Different profiles can be used. This +example takes one present in pdfbox:

+
// create output intent
+InputStream colorProfile = CreatePDFA.class.getResourceAsStream("/org/apache/pdfbox/resources/pdfa/sRGB Color Space Profile.icm");
+PDOutputIntent oi = new PDOutputIntent(doc, colorProfile); 
+oi.setInfo("sRGB IEC61966-2.1"); 
+oi.setOutputCondition("sRGB IEC61966-2.1"); 
+oi.setOutputConditionIdentifier("sRGB IEC61966-2.1"); 
+oi.setRegistryName("http://www.color.org"); 
+cat.addOutputIntent(oi);
+
+

Complete example

+ +

The complete example can be found in pdfbox-example. The source file is

+
src/main/java/org/apache/pdfbox/examples/pdfa/CreatePDFA.java
+
+
+
+
+ + + + + + + + + + + \ No newline at end of file http://git-wip-us.apache.org/repos/asf/pdfbox-docs/blob/aa929f46/content/1.8/cookbook/pdfavalidation.html ---------------------------------------------------------------------- diff --git a/content/1.8/cookbook/pdfavalidation.html b/content/1.8/cookbook/pdfavalidation.html new file mode 100644 index 0000000..2f15089 --- /dev/null +++ b/content/1.8/cookbook/pdfavalidation.html @@ -0,0 +1,299 @@ + + + + + + + + + + + + Apache PDFBox | Cookbook - PDF/A Validation + + + + + + + + + + + + + + + + + + +
+ +
+ +
+

PDF/A Validation

+ +

The Apache Preflight library is a Java tool that implements a parser compliant with the ISO-19005 specification (aka PDF/A-1). +Check Compliance with PDF/A-1b

+ +

This small sample shows how to check the compliance of a file with the PDF/A-1b specification.

+
ValidationResult result = null;
+
+FileDataSource fd = new FileDataSource(args[0]);
+PreflightParser parser = new PreflightParser(fd);
+try
+{
+
+    /* Parse the PDF file with PreflightParser that inherits from the NonSequentialParser.
+     * Some additional controls are present to check a set of PDF/A requirements. 
+     * (Stream length consistency, EOL after some Keyword...)
+     */
+    parser.parse();
+
+    /* Once the syntax validation is done, 
+     * the parser can provide a PreflightDocument 
+     * (that inherits from PDDocument) 
+     * This document process the end of PDF/A validation.
+     */
+    PreflightDocument document = parser.getPreflightDocument();
+    document.validate();
+
+    // Get validation result
+    result = document.getResult();
+    document.close();
+
+}
+catch (SyntaxValidationException e)
+{
+    /* the parse method can throw a SyntaxValidationException 
+     * if the PDF file can't be parsed.
+     * In this case, the exception contains an instance of ValidationResult  
+     */
+    result = e.getResult();
+}
+
+// display validation result
+if (result.isValid())
+{
+    System.out.println("The file " + args[0] + " is a valid PDF/A-1b file");
+}
+else
+{
+    System.out.println("The file" + args[0] + " is not valid, error(s) :");
+    for (ValidationError error : result.getErrorsList())
+    {
+        System.out.println(error.getErrorCode() + " : " + error.getDetails());
+    }
+}
+
+

Categories of Validation Error

+ +

If a validation fails, the ValidationResult object contains all causes of the failure. +In order to help in the failure understanding, all error codes have the following form X[.Y[.Z]] where :

+ +
    +
  • 'X' is the category (ex : Font validation error...)
  • +
  • 'Y' represent a subsection of the category (ex : "Font with Glyph error")
  • +
  • 'Z' represent the cause of the error (ex : "Font with a missing Glyph")
  • +
+ +

Category ('Y') and cause ('Z') may be missing according to the difficulty to identify the error detail.

+ +

Here after, you can find all Categories (for detailed cause, see constants in the PreflightConstants interface) :

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
CategoryDescription
1[.y[.z]]Syntax Error
2[.y[.z]]Graphic Error
3[.y[.z]]Font Error
4[.y[.z]]Transparency Error
5[.y[.z]]Annotation Error
6[.y[.z]]Action Error
7[.y[.z]]Metadata Error
+ +
+
+
+ + + + + + + + + + + \ No newline at end of file http://git-wip-us.apache.org/repos/asf/pdfbox-docs/blob/aa929f46/content/1.8/cookbook/rendering.html ---------------------------------------------------------------------- diff --git a/content/1.8/cookbook/rendering.html b/content/1.8/cookbook/rendering.html new file mode 100644 index 0000000..e70c066 --- /dev/null +++ b/content/1.8/cookbook/rendering.html @@ -0,0 +1,233 @@ + + + + + + + + + + + + Apache PDFBox | Cookbook - Rendering + + + + + + + + + + + + + + + + + + +
+ +
+ +
+

Document Rendering

+ +

Convert a document to images

+ +

This small sample shows how to render (convert to images) a PDF document using PDFBox.

+
:::java
+    String filename = "YOURFILENAMEHERE.pdf";
+
+    // open the document
+    PDDocument doc = PDDocument.loadNonSeq(new File(filename), null);
+
+    boolean b;
+    List<PDPage> pages = doc.getDocumentCatalog().getAllPages();
+    for (int p = 0; p < pages.size(); ++p)
+    {
+        // RGB image with 300 dpi
+        BufferedImage bim = pages.get(p).convertToImage(BufferedImage.TYPE_INT_RGB, 300);
+
+        // save as PNG with default metadata
+        b = ImageIO.write(bim, "png", new File("rgbpage" + (p+1) + ".png"));
+        if (!b)
+        {
+            // error handling
+        }
+
+        // B/W image with 300 dpi
+        bim = pages.get(p).convertToImage(BufferedImage.TYPE_BYTE_BINARY, 300);
+
+        // save as TIF with dpi in the metadata
+        // PDFBox will choose the best compression for you - here: CCITT G4
+        // you need to add jai_imageio.jar to your classpath for this to work
+        b = ImageIOUtil.writeImage(bim, "bwpage-" + (p+1) + ".tif", 300);
+        if (!b)
+        {
+            // error handling
+        }
+    }
+
+    doc.close();
+
+
+
+
+ + + + + + + + + + + \ No newline at end of file http://git-wip-us.apache.org/repos/asf/pdfbox-docs/blob/aa929f46/content/1.8/cookbook/textextraction.html ---------------------------------------------------------------------- diff --git a/content/1.8/cookbook/textextraction.html b/content/1.8/cookbook/textextraction.html new file mode 100644 index 0000000..a7ed24b --- /dev/null +++ b/content/1.8/cookbook/textextraction.html @@ -0,0 +1,320 @@ + + + + + + + + + + + + Apache PDFBox | Cookbook - Textextraction + + + + + + + + + + + + + + + + + + +
+ +
+ +
+

Textextraction

+ +

Extracting Text

+ +

See class:org.apache.pdfbox.util.PDFTextStripper
+See class:org.apache.pdfbox.searchengine.lucene.LucenePDFDocument
+See command line app:ExtractText

+ +

One of the main features of PDFBox is its ability to quickly and accurately extract text +from a variety of PDF documents. This functionality is encapsulated in the +org.apache.pdfbox.util.PDFTextStripper and can be easily executed on the command line with +org.apache.pdfbox.ExtractText.

+ +

Lucene Integration

+ +

Lucene is an open source text search library from the Apache Jakarta Project. In order for +Lucene to be able to index a PDF document it must first be converted to text. PDFBox provides +a simple approach for adding PDF documents into a Lucene index.

+
Document luceneDocument = LucenePDFDocument.getDocument( ... );
+
+

Now that you hava a Lucene Document object, you can add it to the Lucene index just like +you would if it had been created from a text or HTML file. The LucenePDFDocument automatically +extracts a variety of metadata fields from the PDF to be added to the index, the javadoc +shows details on those fields. This approach is very simple and should be sufficient for +most users, if not then you can use some of the advanced text extraction techniques +described in the next section.

+ +

Advanced Text Extraction

+ +

Some applications will have complex text extraction requiments and neither the command +line application nor the LucenePDFDocument will be able to fulfill those requirements. +It is possible for users to utilize or extend the PDFTextStripper class to meet some of +these requirements.

+ +

Limiting The Extracted Text

+ +

There are several ways that we can limit the text that is extracted during the extraction +process. The simplest is to specify the range of pages that you want to be extracted. +For example, to only extract text from the second and third pages of the PDF document +you could do this:

+
PDFTextStripper stripper = new PDFTextStripper();
+stripper.setStartPage( 2 );
+stripper.setEndPage( 3 );
+stripper.writeText( ... );
+
+

NOTE: The startPage and endPage properties of PDFTextStripper are 1 based and inclusive.

+ +

If you wanted to start on page 2 and extract to the end of the document then you would just +set the startPage property. By default all pages in the pdf document are extracted.

+ +

It is also possible to limit the extracted text to be between two bookmarks in the page. +If you are not familiar with how to use bookmarks in PDFBox then you should review the +Bookmarks page. Similar to the startPage/endPage properties, PDFTextStripper also has +startBookmark/endBookmark properties. There are some caveats to be aware of when using this +feature of the PDFTextStripper. Not all bookmarks point to a page in the current PDF document.

+ +

The possible states of a bookmark are:

+ +
    +
  • null - The property was not set, this is the default.
  • +
  • Points to page in the PDF - The property was set and points to a valid page in the PDF
  • +
  • Bookmark does not point to anything - The property was set but the bookmark does not point to any page
  • +
  • Bookmark points to external action - The property was set, but it points to a page in a different PDF or performs an action when activated
  • +
+ +

The table below will describe how PDFBox behaves in the various scenarios:

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Start BookmarkEnd BookmarkResult
nullnullThis is the default, the properties have no effect on the text extraction.
Points to a page in the PDFnullText extraction will begin on the page that this bookmark points to and go until the end of the document.
nullPoints to a page in the PDFText extraction will begin on the first page and stop at the end of the page that this bookmark points to.
Bookmark does not point to anythingnullBecause the PDFTextStripper cannot determine a start page based on the bookmark, it will start on the first page and go until the end of the document.
nullBookmark does not point to anythingBecause the PDFTextStripper cannot determine a end page based on the bookmark, it will start on the first page and go until the end of the document.
Bookmark does not point to anythingBookmark does not point to anythingThis is a special case! If the startBookmark and endBookmark are exactly the same then no text will be extracted. If they are different then it is not possible for the PDFTextStripper to determine that pages so it will include the entire document.
Bookmark points to external actionBookmark points to external actionIf either the startBookmark or the endBookmark refer to an external page or execute an action then an OutlineNotLocalException will be thrown to indicate to the user that the bookmark is not valid.
+ +

NOTE: PDFTextStripper will check both the startPage/endPage and the startBookmark/endBookmark to determine if text should be extracted from the current page.

+ +

External Glyph List

+ +

Some PDF files need to map between glyph names and Unicode values during text extraction. +PDFBox comes with an Adobe Glyph List, but you may encounter files with glyph names that +are not in that map. To use your own glyphlist file, supply the file name to the glyphlist_ext JVM property.

+ +

Right to Left Text

+ +

Extracting text in languages whose text goes from right to left (such as Arabic and Hebrew) +in PDF files can result in text that is backwards. PDFBox can normalize and reverse the text +if the ICU4J jar file has been placed on the classpath (it is an optional dependency). +Note that you should also enable sorting with either org.apache.pdfbox.util.PDFTextStripper +or org.apache.pdfbox.ExtractText to ensure accurate output.

+ +
+
+
+ + + + + + + + + + + \ No newline at end of file http://git-wip-us.apache.org/repos/asf/pdfbox-docs/blob/aa929f46/content/1.8/cookbook/workingwithattachments.html ---------------------------------------------------------------------- diff --git a/content/1.8/cookbook/workingwithattachments.html b/content/1.8/cookbook/workingwithattachments.html new file mode 100644 index 0000000..ab8a431 --- /dev/null +++ b/content/1.8/cookbook/workingwithattachments.html @@ -0,0 +1,240 @@ + + + + + + + + + + + + Apache PDFBox | Cookbook - Working with Attachments + + + + + + + + + + + + + + + + + + +
+ +
+ +
+

Working with Attachments

+ +

The PDF File Specification

+ +

See package:org.apache.pdfbox.pdmodel.common.filespecification
+See example:EmbeddedFiles

+ +

A PDF can contain references to external files via the file system or a URL to a remote +location. It is also possible to embed a binary file into a PDF document.

+ +

There are two classes that can be used when referencing a file. PDSimpleFileSpecification +is a simple string reference to a file(e.g. "./movies/BigMovie.avi"). The simple file +specification does not allow for any parameters to be set.

+ +

The PDComplexFileSpecification is more feature rich and allows for advanced settings on +the file reference.

+ +

It is also possible to embed a file directly into a PDF. Instead of setting the file +attribute of the PDComplexFileSpecification, the EmbeddedFile attribute can be used instead.

+ +

Adding a File Attachment

+ +

PDF documents can contain file attachments that are accessed from the Document->File Attachments +menu. PDFBox allows attachments to be added to and extracted from PDF documents. +Attachments are part of the named tree that is attached to the document catalog.

+
PDEmbeddedFilesNameTreeNode efTree = new PDEmbeddedFilesNameTreeNode();
+
+//first create the file specification, which holds the embedded file
+PDComplexFileSpecification fs = new PDComplexFileSpecification();
+fs.setFile( "Test.txt" );
+InputStream is = ...;
+PDEmbeddedFile ef = new PDEmbeddedFile(doc, is );
+//set some of the attributes of the embedded file
+ef.setSubtype( "test/plain" );
+ef.setSize( data.length );
+ef.setCreationDate( new GregorianCalendar() );
+fs.setEmbeddedFile( ef );
+
+//now add the entry to the embedded file tree and set in the document.
+Map efMap = new HashMap();
+efMap.put( "My first attachment", fs );
+efTree.setNames( efMap );
+//attachments are stored as part of the "names" dictionary in the document catalog
+PDDocumentNameDictionary names = new PDDocumentNameDictionary( doc.getDocumentCatalog() );
+names.setEmbeddedFiles( efTree );
+doc.getDocumentCatalog().setNames( names );
+
+
+
+
+ + + + + + + + + + + \ No newline at end of file