adapt to latest pdfbox 2.0 snapshot

This commit is contained in:
Thomas Chojecki
2015-02-21 18:15:23 +01:00
parent f1de58b424
commit 1257af363a
3 changed files with 73 additions and 59 deletions

View File

@@ -31,37 +31,32 @@ import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDDocumentCatalog; import org.apache.pdfbox.pdmodel.PDDocumentCatalog;
import org.apache.pdfbox.pdmodel.PDDocumentInformation; import org.apache.pdfbox.pdmodel.PDDocumentInformation;
import org.apache.pdfbox.pdmodel.PDPage; import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.common.PDMetadata; import org.apache.pdfbox.pdmodel.common.PDMetadata;
import org.apache.pdfbox.pdmodel.documentinterchange.logicalstructure.PDMarkInfo; import org.apache.pdfbox.pdmodel.documentinterchange.logicalstructure.PDMarkInfo;
import org.apache.pdfbox.pdmodel.edit.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDFont; import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDTrueTypeFont; import org.apache.pdfbox.pdmodel.font.PDTrueTypeFont;
import org.apache.pdfbox.pdmodel.graphics.color.PDOutputIntent; import org.apache.pdfbox.pdmodel.graphics.color.PDOutputIntent;
/** /**
* This is an example that creates a simple PDF/A document. * This is an example that creates a simple PDF/A document.
*
*/ */
public class PDFA3File public class PDFA3File
{ {
/** /**
* Create a simple PDF/A-3 document. * Create a simple PDF/A-3 document.
*
* This example is based on HelloWorld example. * This example is based on HelloWorld example.
*
* As it is a simple case, to conform the PDF/A norm, are added : - the font * As it is a simple case, to conform the PDF/A norm, are added : - the font
* used in the document - the sRGB color profile - a light xmp block with only * used in the document - the sRGB color profile - a light xmp block with only
* PDF identification schema (the only mandatory) - an output intent To * PDF identification schema (the only mandatory) - an output intent To
* conform to A/3 - the mandatory MarkInfo dictionary displays tagged PDF * conform to A/3 - the mandatory MarkInfo dictionary displays tagged PDF
* support - and optional producer and - optional creator info is added * support - and optional producer and - optional creator info is added
* *
*
* @param file * @param file
* The file to write the PDF to. * The file to write the PDF to.
* @param message * @param message
* The message to write in the file. * The message to write in the file.
*
* @throws Exception * @throws Exception
* If something bad occurs * If something bad occurs
*/ */
@@ -69,7 +64,8 @@ public class PDFA3File
{ {
// the document // the document
PDDocument doc = null; PDDocument doc = null;
try { try
{
doc = new PDDocument(); doc = new PDDocument();
// now create the page and add content // now create the page and add content
@@ -104,8 +100,11 @@ public class PDFA3File
doc.save(file); doc.save(file);
} finally { }
if (doc != null) { finally
{
if (doc != null)
{
doc.close(); doc.close();
} }
} }
@@ -114,7 +113,7 @@ public class PDFA3File
/** /**
* Makes A PDF/A3a-compliant document from a PDF-A1 compliant document (on the * Makes A PDF/A3a-compliant document from a PDF-A1 compliant document (on the
* metadata level, this will not e.g. convert graphics to JPG-2000) * metadata level, this will not e.g. convert graphics to JPG-2000)
* */ */
private PDDocumentCatalog makeA3compliant(PDDocument doc) throws IOException, TransformerException private PDDocumentCatalog makeA3compliant(PDDocument doc) throws IOException, TransformerException
{ {
PDDocumentCatalog cat = doc.getDocumentCatalog(); PDDocumentCatalog cat = doc.getDocumentCatalog();
@@ -162,7 +161,7 @@ public class PDFA3File
* visually, unicode and structurally preservable * visually, unicode and structurally preservable
*/ */
pdfaid.setAbout(""); pdfaid.setAbout("");
metadata.importXMPMetadata(xmp); metadata.importXMPMetadata(xmp.asByteArray());
return cat; return cat;
} }
@@ -176,13 +175,19 @@ public class PDFA3File
public static void main(String[] args) public static void main(String[] args)
{ {
PDFA3File app = new PDFA3File(); PDFA3File app = new PDFA3File();
try { try
if (args.length != 2) { {
if (args.length != 2)
{
app.usage(); app.usage();
} else { }
else
{
app.doIt(args[0], args[1]); app.doIt(args[0], args[1]);
} }
} catch (Exception e) { }
catch (Exception e)
{
e.printStackTrace(); e.printStackTrace();
} }
} }

View File

@@ -30,7 +30,6 @@ import org.apache.jempbox.xmp.XMPSchemaBasic;
import org.apache.jempbox.xmp.XMPSchemaDublinCore; import org.apache.jempbox.xmp.XMPSchemaDublinCore;
import org.apache.jempbox.xmp.XMPSchemaPDF; import org.apache.jempbox.xmp.XMPSchemaPDF;
import org.apache.jempbox.xmp.pdfa.XMPSchemaPDFAId; import org.apache.jempbox.xmp.pdfa.XMPSchemaPDFAId;
import org.apache.pdfbox.cos.COSArray;
import org.apache.pdfbox.cos.COSDictionary; import org.apache.pdfbox.cos.COSDictionary;
import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDDocumentCatalog; import org.apache.pdfbox.pdmodel.PDDocumentCatalog;
@@ -38,18 +37,17 @@ import org.apache.pdfbox.pdmodel.PDDocumentInformation;
import org.apache.pdfbox.pdmodel.PDDocumentNameDictionary; import org.apache.pdfbox.pdmodel.PDDocumentNameDictionary;
import org.apache.pdfbox.pdmodel.PDEmbeddedFilesNameTreeNode; import org.apache.pdfbox.pdmodel.PDEmbeddedFilesNameTreeNode;
import org.apache.pdfbox.pdmodel.PDPage; import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.common.PDMetadata; import org.apache.pdfbox.pdmodel.common.PDMetadata;
import org.apache.pdfbox.pdmodel.common.filespecification.PDComplexFileSpecification; import org.apache.pdfbox.pdmodel.common.filespecification.PDComplexFileSpecification;
import org.apache.pdfbox.pdmodel.common.filespecification.PDEmbeddedFile; import org.apache.pdfbox.pdmodel.common.filespecification.PDEmbeddedFile;
import org.apache.pdfbox.pdmodel.documentinterchange.logicalstructure.PDMarkInfo; import org.apache.pdfbox.pdmodel.documentinterchange.logicalstructure.PDMarkInfo;
import org.apache.pdfbox.pdmodel.edit.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDFont; import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDTrueTypeFont; import org.apache.pdfbox.pdmodel.font.PDTrueTypeFont;
import org.apache.pdfbox.pdmodel.graphics.color.PDOutputIntent; import org.apache.pdfbox.pdmodel.graphics.color.PDOutputIntent;
/** /**
* This is an example that creates a simple PDF/A document. * This is an example that creates a simple PDF/A document.
*
*/ */
public class PDFA3FileAttachment public class PDFA3FileAttachment
{ {
@@ -107,8 +105,7 @@ public class PDFA3FileAttachment
* Validating file "RE-20131206_22.pdf" for conformance level pdfa-3a The * Validating file "RE-20131206_22.pdf" for conformance level pdfa-3a The
* key UF is required but missing. The key AFRelationship is required but * key UF is required but missing. The key AFRelationship is required but
* missing. File specification 'Test.txt' not associated with an object. * missing. File specification 'Test.txt' not associated with an object.
* */
* */
// attachments are stored as part of the "names" dictionary in the document // attachments are stored as part of the "names" dictionary in the document
// catalog // catalog
PDDocumentCatalog catalog = doc.getDocumentCatalog(); PDDocumentCatalog catalog = doc.getDocumentCatalog();
@@ -117,31 +114,27 @@ public class PDFA3FileAttachment
names.setEmbeddedFiles(efTree); names.setEmbeddedFiles(efTree);
catalog.setNames(names); catalog.setNames(names);
// AF entry (Array) in catalog with the FileSpec // // AF entry (Array) in catalog with the FileSpec
COSArray cosArray = new COSArray(); // PDAcroForm pdAcroForm = new PDAcroForm(doc);
cosArray.add(fs); // COSArray cosArray = new COSArray();
catalog.getCOSDictionary().setItem("AF", cosArray); // cosArray.add(fs);
// catalog.setItem("AF", cosArray);
} }
/** /**
* Create a simple PDF/A-3 document. * Create a simple PDF/A-3 document.
*
* This example is based on HelloWorld example. * This example is based on HelloWorld example.
*
* As it is a simple case, to conform the PDF/A norm, are added : - the font * As it is a simple case, to conform the PDF/A norm, are added : - the font
* used in the document - the sRGB color profile - a light xmp block with only * used in the document - the sRGB color profile - a light xmp block with only
* PDF identification schema (the only mandatory) - an output intent To * PDF identification schema (the only mandatory) - an output intent To
* conform to A/3 - the mandatory MarkInfo dictionary displays tagged PDF * conform to A/3 - the mandatory MarkInfo dictionary displays tagged PDF
* support - and optional producer and - optional creator info is added * support - and optional producer and - optional creator info is added
* *
*
* @param file * @param file
* The file to write the PDF to. * The file to write the PDF to.
* @param message * @param message
* The message to write in the file. * The message to write in the file.
*
* @throws Exception * @throws Exception
* If something bad occurs * If something bad occurs
*/ */
@@ -149,7 +142,8 @@ public class PDFA3FileAttachment
{ {
// the document // the document
PDDocument doc = null; PDDocument doc = null;
try { try
{
doc = new PDDocument(); doc = new PDDocument();
// now create the page and add content // now create the page and add content
@@ -184,8 +178,11 @@ public class PDFA3FileAttachment
doc.save(file); doc.save(file);
} finally { }
if (doc != null) { finally
{
if (doc != null)
{
doc.close(); doc.close();
} }
} }
@@ -194,7 +191,7 @@ public class PDFA3FileAttachment
/** /**
* Makes A PDF/A3a-compliant document from a PDF-A1 compliant document (on the * Makes A PDF/A3a-compliant document from a PDF-A1 compliant document (on the
* metadata level, this will not e.g. convert graphics to JPG-2000) * metadata level, this will not e.g. convert graphics to JPG-2000)
* */ */
private PDDocumentCatalog makeA3compliant(PDDocument doc) throws IOException, TransformerException private PDDocumentCatalog makeA3compliant(PDDocument doc) throws IOException, TransformerException
{ {
PDDocumentCatalog cat = doc.getDocumentCatalog(); PDDocumentCatalog cat = doc.getDocumentCatalog();
@@ -242,7 +239,7 @@ public class PDFA3FileAttachment
* visually, unicode and structurally preservable * visually, unicode and structurally preservable
*/ */
pdfaid.setAbout(""); pdfaid.setAbout("");
metadata.importXMPMetadata(xmp); metadata.importXMPMetadata(xmp.asByteArray());
return cat; return cat;
} }
@@ -256,13 +253,19 @@ public class PDFA3FileAttachment
public static void main(String[] args) public static void main(String[] args)
{ {
PDFA3FileAttachment app = new PDFA3FileAttachment(); PDFA3FileAttachment app = new PDFA3FileAttachment();
try { try
if (args.length != 2) { {
if (args.length != 2)
{
app.usage(); app.usage();
} else { }
else
{
app.doIt(args[0], args[1]); app.doIt(args[0], args[1]);
} }
} catch (Exception e) { }
catch (Exception e)
{
e.printStackTrace(); e.printStackTrace();
} }
} }

View File

@@ -23,15 +23,14 @@ import org.apache.jempbox.xmp.pdfa.XMPSchemaPDFAId;
import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDDocumentCatalog; import org.apache.pdfbox.pdmodel.PDDocumentCatalog;
import org.apache.pdfbox.pdmodel.PDPage; import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.common.PDMetadata; import org.apache.pdfbox.pdmodel.common.PDMetadata;
import org.apache.pdfbox.pdmodel.edit.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDFont; import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDTrueTypeFont; import org.apache.pdfbox.pdmodel.font.PDTrueTypeFont;
import org.apache.pdfbox.pdmodel.graphics.color.PDOutputIntent; import org.apache.pdfbox.pdmodel.graphics.color.PDOutputIntent;
/** /**
* This is an example that creates a simple PDF/A document. * This is an example that creates a simple PDF/A document.
*
*/ */
public class PDFAFile public class PDFAFile
{ {
@@ -45,9 +44,7 @@ public class PDFAFile
/** /**
* Create a simple PDF/A document. * Create a simple PDF/A document.
*
* This example is based on HelloWorld example. * This example is based on HelloWorld example.
*
* As it is a simple case, to conform the PDF/A norm, are added : - the font * As it is a simple case, to conform the PDF/A norm, are added : - the font
* used in the document - a light xmp block with only PDF identification * used in the document - a light xmp block with only PDF identification
* schema (the only mandatory) - an output intent * schema (the only mandatory) - an output intent
@@ -56,7 +53,6 @@ public class PDFAFile
* The file to write the PDF to. * The file to write the PDF to.
* @param message * @param message
* The message to write in the file. * The message to write in the file.
*
* @throws Exception * @throws Exception
* If something bad occurs * If something bad occurs
*/ */
@@ -64,7 +60,8 @@ public class PDFAFile
{ {
// the document // the document
PDDocument doc = null; PDDocument doc = null;
try { try
{
doc = new PDDocument(); doc = new PDDocument();
PDPage page = new PDPage(); PDPage page = new PDPage();
@@ -94,7 +91,7 @@ public class PDFAFile
pdfaid.setConformance("B"); pdfaid.setConformance("B");
pdfaid.setPart(1); pdfaid.setPart(1);
pdfaid.setAbout(""); pdfaid.setAbout("");
metadata.importXMPMetadata(xmp); metadata.importXMPMetadata(xmp.asByteArray());
InputStream colorProfile = PDFA3File.class.getResourceAsStream("/sRGB Color Space Profile.icm"); InputStream colorProfile = PDFA3File.class.getResourceAsStream("/sRGB Color Space Profile.icm");
// create output intent // create output intent
@@ -107,8 +104,11 @@ public class PDFAFile
doc.save(file); doc.save(file);
} finally { }
if (doc != null) { finally
{
if (doc != null)
{
doc.close(); doc.close();
} }
} }
@@ -124,13 +124,19 @@ public class PDFAFile
public static void main(String[] args) public static void main(String[] args)
{ {
PDFAFile app = new PDFAFile(); PDFAFile app = new PDFAFile();
try { try
if (args.length != 2) { {
if (args.length != 2)
{
app.usage(); app.usage();
} else { }
else
{
app.doIt(args[0], args[1]); app.doIt(args[0], args[1]);
} }
} catch (Exception e) { }
catch (Exception e)
{
e.printStackTrace(); e.printStackTrace();
} }
} }