use of com.tom_roush.pdfbox.rendering.PDFRenderer 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.rendering.PDFRenderer in project PdfBox-Android by TomRoush.
the class ValidateXImage method doWritePDF.
// write image twice (overlapped) in document, close document and re-read PDF
static void doWritePDF(PDDocument document, PDImageXObject ximage, File testResultsDir, String filename) throws IOException {
File pdfFile = new File(testResultsDir, filename);
// This part isn't really needed because this test doesn't break
// if the mask has the wrong colorspace (PDFBOX-2057), but it is still useful
// if something goes wrong in the future and we want to have a PDF to open.
PDPage page = new PDPage();
document.addPage(page);
PDPageContentStream contentStream = new PDPageContentStream(document, page, AppendMode.APPEND, false);
contentStream.drawImage(ximage, 150, 300);
contentStream.drawImage(ximage, 200, 350);
contentStream.close();
// check that the resource map is up-to-date
assertEquals(1, count(document.getPage(0).getResources().getXObjectNames()));
document.save(pdfFile);
document.close();
document = PDDocument.load(pdfFile, (String) null);
assertEquals(1, count(document.getPage(0).getResources().getXObjectNames()));
new PDFRenderer(document).renderImage(0);
document.close();
}
use of com.tom_roush.pdfbox.rendering.PDFRenderer in project PdfBox-Android by TomRoush.
the class TestSymmetricKeyEncryption method testSymmEncrForKeySize.
private void testSymmEncrForKeySize(int keyLength, boolean preferAES, int sizePriorToEncr, byte[] inputFileAsByteArray, String userpassword, String ownerpassword, AccessPermission permission) throws IOException {
PDDocument document = PDDocument.load(inputFileAsByteArray);
String prefix = "Simple-";
int numSrcPages = document.getNumberOfPages();
PDFRenderer pdfRenderer = new PDFRenderer(document);
List<Bitmap> srcImgTab = new ArrayList<Bitmap>();
List<byte[]> srcContentStreamTab = new ArrayList<byte[]>();
for (int i = 0; i < numSrcPages; ++i) {
srcImgTab.add(pdfRenderer.renderImage(i));
InputStream unfilteredStream = document.getPage(i).getContents();
byte[] bytes = IOUtils.toByteArray(unfilteredStream);
unfilteredStream.close();
srcContentStreamTab.add(bytes);
}
PDDocument encryptedDoc = encrypt(keyLength, preferAES, sizePriorToEncr, document, prefix, permission, userpassword, ownerpassword);
assertEquals(numSrcPages, encryptedDoc.getNumberOfPages());
pdfRenderer = new PDFRenderer(encryptedDoc);
for (int i = 0; i < encryptedDoc.getNumberOfPages(); ++i) {
// compare rendering
Bitmap bim = pdfRenderer.renderImage(i);
ValidateXImage.checkIdent(bim, srcImgTab.get(i));
// compare content streams
InputStream unfilteredStream = encryptedDoc.getPage(i).getContents();
byte[] bytes = IOUtils.toByteArray(unfilteredStream);
unfilteredStream.close();
Assert.assertArrayEquals("content stream of page " + i + " not identical", srcContentStreamTab.get(i), bytes);
}
File pdfFile = new File(testResultsDir, prefix + keyLength + "-bit-" + (preferAES ? "AES" : "RC4") + "-decrypted.pdf");
encryptedDoc.setAllSecurityToBeRemoved(true);
encryptedDoc.save(pdfFile);
encryptedDoc.close();
}
use of com.tom_roush.pdfbox.rendering.PDFRenderer in project PdfBox-Android by TomRoush.
the class PDFMergerUtilityTest method checkMergeIdentical.
// checks that the result file of a merge has the same rendering as the two
// source files
private void checkMergeIdentical(String filename1, String filename2, String mergeFilename, MemoryUsageSetting memUsageSetting) throws IOException {
PDDocument srcDoc1 = PDDocument.load(testContext.getAssets().open(SRCDIR + "/" + filename1), (String) null);
int src1PageCount = srcDoc1.getNumberOfPages();
PDFRenderer src1PdfRenderer = new PDFRenderer(srcDoc1);
Bitmap[] src1ImageTab = new Bitmap[src1PageCount];
for (int page = 0; page < src1PageCount; ++page) {
src1ImageTab[page] = src1PdfRenderer.renderImageWithDPI(page, DPI);
}
srcDoc1.close();
PDDocument srcDoc2 = PDDocument.load(testContext.getAssets().open(SRCDIR + "/" + filename2), (String) null);
int src2PageCount = srcDoc2.getNumberOfPages();
PDFRenderer src2PdfRenderer = new PDFRenderer(srcDoc2);
Bitmap[] src2ImageTab = new Bitmap[src2PageCount];
for (int page = 0; page < src2PageCount; ++page) {
src2ImageTab[page] = src2PdfRenderer.renderImageWithDPI(page, DPI);
}
srcDoc2.close();
PDFMergerUtility pdfMergerUtility = new PDFMergerUtility();
pdfMergerUtility.addSource(testContext.getAssets().open(SRCDIR + "/" + filename1));
pdfMergerUtility.addSource(testContext.getAssets().open(SRCDIR + "/" + filename2));
pdfMergerUtility.setDestinationFileName(TARGETTESTDIR + mergeFilename);
pdfMergerUtility.mergeDocuments(memUsageSetting);
PDDocument mergedDoc = PDDocument.load(new File(TARGETTESTDIR, mergeFilename), (String) null);
PDFRenderer mergePdfRenderer = new PDFRenderer(mergedDoc);
int mergePageCount = mergedDoc.getNumberOfPages();
assertEquals(src1PageCount + src2PageCount, mergePageCount);
for (int page = 0; page < src1PageCount; ++page) {
Bitmap bim = mergePdfRenderer.renderImageWithDPI(page, DPI);
checkImagesIdentical(bim, src1ImageTab[page]);
}
for (int page = 0; page < src2PageCount; ++page) {
int mergePage = page + src1PageCount;
Bitmap bim = mergePdfRenderer.renderImageWithDPI(mergePage, DPI);
checkImagesIdentical(bim, src2ImageTab[page]);
}
mergedDoc.close();
}
use of com.tom_roush.pdfbox.rendering.PDFRenderer in project PdfBox-Android by TomRoush.
the class PDFontTest method testPDFBox988.
/**
* Test of the error reported in PDFBOX-988
*
* @throws IOException
* @throws URISyntaxException
*/
@Test
public void testPDFBox988() throws IOException, URISyntaxException {
PDDocument doc = null;
try {
File pdf = new File(testContext.getCacheDir(), "F001u_3_7j.pdf");
OutputStream os = new FileOutputStream(pdf);
IOUtils.copy(testContext.getAssets().open("pdfbox/com/tom_roush/pdfbox/pdmodel/font/F001u_3_7j.pdf"), os);
os.close();
doc = PDDocument.load(pdf);
PDFRenderer renderer = new PDFRenderer(doc);
renderer.renderImage(0);
// the allegation is that renderImage() will crash the JVM or hang
} finally {
if (doc != null) {
doc.close();
}
}
}
Aggregations