use of com.tom_roush.pdfbox.pdmodel.PDDocument in project PdfBox-Android by TomRoush.
the class PDAcroFormFlattenTest method generateSamples.
/*
* Generate the sample images to which the PDF will be compared after flatten.
*/
private static void generateSamples(String sourceUrl, String targetFile) throws IOException {
getFromUrl(sourceUrl, targetFile);
File file = new File(IN_DIR, targetFile);
PDDocument document = PDDocument.load(file, (String) null);
String outputPrefix = file.getName() + "-";
int numPages = document.getNumberOfPages();
PDFRenderer renderer = new PDFRenderer(document);
for (int i = 0; i < numPages; i++) {
String fileName = outputPrefix + (i + 1) + ".png";
// Windows native DPI
Bitmap image = renderer.renderImageWithDPI(i, 96);
File renderFile = new File(OUT_DIR, fileName);
FileOutputStream fileOut = new FileOutputStream(renderFile);
image.compress(Bitmap.CompressFormat.PNG, 100, fileOut);
fileOut.close();
}
document.close();
}
use of com.tom_roush.pdfbox.pdmodel.PDDocument in project PdfBox-Android by TomRoush.
the class MergeAcroFormsTest method testLegacyModeMerge.
/*
* Test LegacyMode merge
*/
@Test
public void testLegacyModeMerge() throws IOException {
PDFMergerUtility merger = new PDFMergerUtility();
File toBeMerged = new File(IN_DIR, "AcroFormForMerge.pdf");
File pdfOutput = new File(OUT_DIR, "PDFBoxLegacyMerge-SameMerged.pdf");
merger.setDestinationFileName(pdfOutput.getAbsolutePath());
merger.addSource(toBeMerged);
merger.addSource(toBeMerged);
merger.mergeDocuments(null);
merger.setAcroFormMergeMode(AcroFormMergeMode.PDFBOX_LEGACY_MODE);
PDDocument compliantDocument = null;
PDDocument toBeCompared = null;
try {
compliantDocument = PDDocument.load(new File(IN_DIR, "PDFBoxLegacyMerge-SameMerged.pdf"));
toBeCompared = PDDocument.load(new File(OUT_DIR, "PDFBoxLegacyMerge-SameMerged.pdf"));
PDAcroForm compliantAcroForm = compliantDocument.getDocumentCatalog().getAcroForm();
PDAcroForm toBeComparedAcroForm = toBeCompared.getDocumentCatalog().getAcroForm();
assertEquals("There shall be the same number of root fields", compliantAcroForm.getFields().size(), toBeComparedAcroForm.getFields().size());
for (PDField compliantField : compliantAcroForm.getFieldTree()) {
assertNotNull("There shall be a field with the same FQN", toBeComparedAcroForm.getField(compliantField.getFullyQualifiedName()));
PDField toBeComparedField = toBeComparedAcroForm.getField(compliantField.getFullyQualifiedName());
compareFieldProperties(compliantField, toBeComparedField);
}
for (PDField toBeComparedField : toBeComparedAcroForm.getFieldTree()) {
assertNotNull("There shall be a field with the same FQN", compliantAcroForm.getField(toBeComparedField.getFullyQualifiedName()));
PDField compliantField = compliantAcroForm.getField(toBeComparedField.getFullyQualifiedName());
compareFieldProperties(toBeComparedField, compliantField);
}
} finally {
IOUtils.closeQuietly(compliantDocument);
IOUtils.closeQuietly(toBeCompared);
}
}
use of com.tom_roush.pdfbox.pdmodel.PDDocument in project PdfBox-Android by TomRoush.
the class MergeAcroFormsTest method testAPEntry.
/*
* PDFBOX-1100 Ensure that after merging the PDFs there is an AP and V entry.
*/
@Test
public void testAPEntry() throws IOException {
InputStream is1 = null;
InputStream is2 = null;
// Merge the PDFs form PDFBOX-1100
PDFMergerUtility merger = new PDFMergerUtility();
try {
File file1 = new File(TARGET_PDF_DIR, "PDFBOX-1100-1.pdf");
assumeTrue(file1.exists());
is1 = new FileInputStream(file1);
File file2 = new File(TARGET_PDF_DIR, "PDFBOX-1100-2.pdf");
assumeTrue(file2.exists());
is2 = new FileInputStream(file2);
File pdfOutput = new File(OUT_DIR, "PDFBOX-1100.pdf");
merger.setDestinationFileName(pdfOutput.getAbsolutePath());
merger.addSource(is1);
merger.addSource(is2);
merger.mergeDocuments(null);
// Test merge result
PDDocument mergedPDF = PDDocument.load(pdfOutput);
assertEquals("There shall be 2 pages", 2, mergedPDF.getNumberOfPages());
PDAcroForm acroForm = mergedPDF.getDocumentCatalog().getAcroForm();
PDField formField = acroForm.getField("Testfeld");
assertNotNull("There shall be an /AP entry for the field", formField.getCOSObject().getDictionaryObject(COSName.AP));
assertNotNull("There shall be a /V entry for the field", formField.getCOSObject().getDictionaryObject(COSName.V));
formField = acroForm.getField("Testfeld2");
assertNotNull("There shall be an /AP entry for the field", formField.getCOSObject().getDictionaryObject(COSName.AP));
assertNotNull("There shall be a /V entry for the field", formField.getCOSObject().getDictionaryObject(COSName.V));
mergedPDF.close();
} finally {
IOUtils.closeQuietly(is1);
IOUtils.closeQuietly(is2);
}
}
use of com.tom_roush.pdfbox.pdmodel.PDDocument in project PdfBox-Android by TomRoush.
the class MergeAcroFormsTest method testAnnotsEntry.
/*
* PDFBOX-1031 Ensure that after merging the PDFs there is an Annots entry per page.
*/
@Test
public void testAnnotsEntry() throws IOException {
InputStream s1 = null;
InputStream s2 = null;
// Merge the PDFs form PDFBOX-1031
PDFMergerUtility merger = new PDFMergerUtility();
try {
File f1 = new File(TARGET_PDF_DIR, "PDFBOX-1031-1.pdf");
assumeTrue(f1.exists());
s1 = new FileInputStream(f1);
File f2 = new File(TARGET_PDF_DIR, "PDFBOX-1031-2.pdf");
assumeTrue(f2.exists());
s2 = new FileInputStream(f2);
File pdfOutput = new File(OUT_DIR, "PDFBOX-1031.pdf");
merger.setDestinationFileName(pdfOutput.getAbsolutePath());
merger.addSource(s1);
merger.addSource(s2);
merger.mergeDocuments(null);
// Test merge result
PDDocument mergedPDF = PDDocument.load(pdfOutput);
assertEquals("There shall be 2 pages", 2, mergedPDF.getNumberOfPages());
assertNotNull("There shall be an /Annots entry for the first page", mergedPDF.getPage(0).getCOSObject().getDictionaryObject(COSName.ANNOTS));
assertEquals("There shall be 1 annotation for the first page", 1, mergedPDF.getPage(0).getAnnotations().size());
assertNotNull("There shall be an /Annots entry for the second page", mergedPDF.getPage(1).getCOSObject().getDictionaryObject(COSName.ANNOTS));
assertEquals("There shall be 1 annotation for the second page", 1, mergedPDF.getPage(0).getAnnotations().size());
mergedPDF.close();
} finally {
IOUtils.closeQuietly(s1);
IOUtils.closeQuietly(s2);
}
}
use of com.tom_roush.pdfbox.pdmodel.PDDocument in project PdfBox-Android by TomRoush.
the class PDFCloneUtilityTest method testClonePDFWithCosArrayStream.
/**
* original (minimal) test from PDFBOX-2052.
*
* @throws IOException
*/
public void testClonePDFWithCosArrayStream() throws IOException {
PDDocument srcDoc = new PDDocument();
PDDocument dstDoc = new PDDocument();
PDPage pdPage = new PDPage();
srcDoc.addPage(pdPage);
new PDPageContentStream(srcDoc, pdPage, AppendMode.APPEND, true).close();
new PDPageContentStream(srcDoc, pdPage, AppendMode.APPEND, true).close();
new PDFCloneUtility(dstDoc).cloneForNewDocument(pdPage.getCOSObject());
srcDoc.close();
dstDoc.close();
}
Aggregations