use of org.apache.pdfbox.pdmodel.common.filespecification.PDEmbeddedFile in project pdfbox by apache.
the class TestEmbeddedFiles method testOSSpecificAttachments.
@Test
public void testOSSpecificAttachments() throws IOException {
PDEmbeddedFile nonOSFile = null;
PDEmbeddedFile macFile = null;
PDEmbeddedFile dosFile = null;
PDEmbeddedFile unixFile = null;
PDDocument doc = PDDocument.load(TestEmbeddedFiles.class.getResourceAsStream("testPDF_multiFormatEmbFiles.pdf"));
PDDocumentCatalog catalog = doc.getDocumentCatalog();
PDDocumentNameDictionary names = catalog.getNames();
PDEmbeddedFilesNameTreeNode treeNode = names.getEmbeddedFiles();
List<PDNameTreeNode<PDComplexFileSpecification>> kids = treeNode.getKids();
for (PDNameTreeNode kid : kids) {
Map<String, PDComplexFileSpecification> tmpNames = kid.getNames();
COSObjectable obj = tmpNames.get("My first attachment");
PDComplexFileSpecification spec = (PDComplexFileSpecification) obj;
nonOSFile = spec.getEmbeddedFile();
macFile = spec.getEmbeddedFileMac();
dosFile = spec.getEmbeddedFileDos();
unixFile = spec.getEmbeddedFileUnix();
}
assertTrue("non os specific", byteArrayContainsLC("non os specific", nonOSFile.toByteArray(), "ISO-8859-1"));
assertTrue("mac", byteArrayContainsLC("mac embedded", macFile.toByteArray(), "ISO-8859-1"));
assertTrue("dos", byteArrayContainsLC("dos embedded", dosFile.toByteArray(), "ISO-8859-1"));
assertTrue("unix", byteArrayContainsLC("unix embedded", unixFile.toByteArray(), "ISO-8859-1"));
}
use of org.apache.pdfbox.pdmodel.common.filespecification.PDEmbeddedFile in project pdfbox by apache.
the class TestEmbeddedFiles method testNullEmbeddedFile.
@Test
public void testNullEmbeddedFile() throws IOException {
PDEmbeddedFile embeddedFile = null;
boolean ok = false;
try {
PDDocument doc = PDDocument.load(TestEmbeddedFiles.class.getResourceAsStream("null_PDComplexFileSpecification.pdf"));
PDDocumentCatalog catalog = doc.getDocumentCatalog();
PDDocumentNameDictionary names = catalog.getNames();
assertEquals("expected two files", 2, names.getEmbeddedFiles().getNames().size());
PDEmbeddedFilesNameTreeNode embeddedFiles = names.getEmbeddedFiles();
PDComplexFileSpecification spec = embeddedFiles.getNames().get("non-existent-file.docx");
if (spec != null) {
embeddedFile = spec.getEmbeddedFile();
ok = true;
}
// now test for actual attachment
spec = embeddedFiles.getNames().get("My first attachment");
assertNotNull("one attachment actually exists", spec);
assertEquals("existing file length", 17660, spec.getEmbeddedFile().getLength());
spec = embeddedFiles.getNames().get("non-existent-file.docx");
} catch (NullPointerException e) {
assertNotNull("null pointer exception", null);
}
assertTrue("Was able to get file without exception", ok);
assertNull("EmbeddedFile was correctly null", embeddedFile);
}
use of org.apache.pdfbox.pdmodel.common.filespecification.PDEmbeddedFile in project mustangproject by ZUGFeRD.
the class ZUGFeRDExporterFromA3 method PDFAttachGenericFile.
/**
* Embeds an external file (generic - any type allowed) in the PDF.
*
* @param doc PDDocument to attach the file to.
* @param filename name of the file that will become attachment name in the PDF
* @param relationship how the file relates to the content, e.g. "Alternative"
* @param description Human-readable description of the file content
* @param subType type of the data e.g. could be "text/xml" - mime like
* @param data the binary data of the file/attachment
* @throws java.io.IOException if anything is wrong with filename
*/
public void PDFAttachGenericFile(PDDocument doc, String filename, String relationship, String description, String subType, byte[] data) throws IOException {
fileAttached = true;
PDComplexFileSpecification fs = new PDComplexFileSpecification();
fs.setFile(filename);
COSDictionary dict = fs.getCOSObject();
dict.setName("AFRelationship", relationship);
dict.setString("UF", filename);
dict.setString("Desc", description);
ByteArrayInputStream fakeFile = new ByteArrayInputStream(data);
PDEmbeddedFile ef = new PDEmbeddedFile(doc, fakeFile);
// ef.addCompression();
ef.setSubtype(subType);
ef.setSize(data.length);
ef.setCreationDate(new GregorianCalendar());
ef.setModDate(GregorianCalendar.getInstance());
fs.setEmbeddedFile(ef);
// In addition make sure the embedded file is set under /UF
dict = fs.getCOSObject();
COSDictionary efDict = (COSDictionary) dict.getDictionaryObject(COSName.EF);
COSBase lowerLevelFile = efDict.getItem(COSName.F);
efDict.setItem(COSName.UF, lowerLevelFile);
// now add the entry to the embedded file tree and set in the document.
PDDocumentNameDictionary names = new PDDocumentNameDictionary(doc.getDocumentCatalog());
PDEmbeddedFilesNameTreeNode efTree = names.getEmbeddedFiles();
if (efTree == null) {
efTree = new PDEmbeddedFilesNameTreeNode();
}
Map<String, PDComplexFileSpecification> namesMap = new HashMap<>();
Map<String, PDComplexFileSpecification> oldNamesMap = efTree.getNames();
if (oldNamesMap != null) {
for (String key : oldNamesMap.keySet()) {
namesMap.put(key, oldNamesMap.get(key));
}
}
namesMap.put(filename, fs);
efTree.setNames(namesMap);
names.setEmbeddedFiles(efTree);
doc.getDocumentCatalog().setNames(names);
// AF entry (Array) in catalog with the FileSpec
COSBase AFEntry = (COSBase) doc.getDocumentCatalog().getCOSObject().getItem("AF");
if ((AFEntry == null)) {
COSArray cosArray = new COSArray();
cosArray.add(fs);
doc.getDocumentCatalog().getCOSObject().setItem("AF", cosArray);
} else if (AFEntry instanceof COSArray) {
COSArray cosArray = (COSArray) AFEntry;
cosArray.add(fs);
doc.getDocumentCatalog().getCOSObject().setItem("AF", cosArray);
} else if ((AFEntry instanceof COSObject) && ((COSObject) AFEntry).getObject() instanceof COSArray) {
COSArray cosArray = (COSArray) ((COSObject) AFEntry).getObject();
cosArray.add(fs);
} else {
throw new IOException("Unexpected object type for PDFDocument/Catalog/COSDictionary/Item(AF)");
}
}
Aggregations