use of com.tom_roush.pdfbox.pdmodel.PDDocument in project PdfBox-Android by TomRoush.
the class TestPDFParserInstrumentation method testPDFBox3950.
/**
* PDFBOX-3950: test parsing and rendering of truncated file with missing pages.
*
* @throws IOException
*/
@Test
public void testPDFBox3950() throws IOException {
File TARGETPDFDIR = new File(testContext.getCacheDir(), "pdfs");
TARGETPDFDIR.mkdirs();
File pdfFile = TestResourceGenerator.downloadTestResource(TARGETPDFDIR, "PDFBOX-3950-23EGDHXSBBYQLKYOKGZUOVYVNE675PRD.pdf", "https://issues.apache.org/jira/secure/attachment/12890042/23EGDHXSBBYQLKYOKGZUOVYVNE675PRD.pdf");
assumeTrue(pdfFile.exists());
PDDocument doc = PDDocument.load(pdfFile);
assertEquals(4, doc.getNumberOfPages());
PDFRenderer renderer = new PDFRenderer(doc);
for (int i = 0; i < doc.getNumberOfPages(); ++i) {
try {
renderer.renderImage(i);
} catch (IOException ex) {
if (i == 3 && ex.getMessage().equals("Missing descendant font array")) {
continue;
}
throw ex;
}
}
doc.close();
}
use of com.tom_roush.pdfbox.pdmodel.PDDocument in project PdfBox-Android by TomRoush.
the class PDFontTest method testPDFBox3826checkFonts.
private void testPDFBox3826checkFonts(byte[] byteArray, File fontFile) throws IOException {
PDDocument doc = PDDocument.load(byteArray);
PDPage page2 = doc.getPage(0);
// F1 = type0 subset
PDType0Font fontF1 = (PDType0Font) page2.getResources().getFont(COSName.getPDFName("F1"));
Assert.assertTrue(fontF1.getName().contains("+"));
Assert.assertTrue(fontFile.length() > fontF1.getFontDescriptor().getFontFile2().toByteArray().length);
// F2 = type0 full embed
PDType0Font fontF2 = (PDType0Font) page2.getResources().getFont(COSName.getPDFName("F2"));
Assert.assertFalse(fontF2.getName().contains("+"));
Assert.assertEquals(fontFile.length(), fontF2.getFontDescriptor().getFontFile2().toByteArray().length);
// F3 = tt full embed
PDTrueTypeFont fontF3 = (PDTrueTypeFont) page2.getResources().getFont(COSName.getPDFName("F3"));
Assert.assertFalse(fontF2.getName().contains("+"));
Assert.assertEquals(fontFile.length(), fontF3.getFontDescriptor().getFontFile2().toByteArray().length);
new PDFRenderer(doc).renderImage(0);
PDFTextStripper stripper = new PDFTextStripper();
stripper.setLineSeparator("\n");
String text = stripper.getText(doc);
Assert.assertEquals("testMultipleFontFileReuse1\ntestMultipleFontFileReuse2\ntestMultipleFontFileReuse3", text.trim());
doc.close();
}
use of com.tom_roush.pdfbox.pdmodel.PDDocument in project PdfBox-Android by TomRoush.
the class PDFontTest method testPDFBOX4115.
/**
* PDFBOX-4115: Test ability to create PDF with german umlaut glyphs with a type 1 font.
* Test for everything that went wrong before this was fixed.
*
* @throws IOException
*/
@Test
public void testPDFBOX4115() throws IOException {
File fontFile = TestResourceGenerator.downloadTestResource(IN_DIR, "n019003l.pfb", "https://issues.apache.org/jira/secure/attachment/12911053/n019003l.pfb");
assumeTrue(fontFile.exists());
File outputFile = new File(OUT_DIR, "FontType1.pdf");
String text = "äöüÄÖÜ";
PDDocument doc = new PDDocument();
PDPage page = new PDPage();
PDPageContentStream contentStream = new PDPageContentStream(doc, page);
PDType1Font font = new PDType1Font(doc, new FileInputStream(fontFile), WinAnsiEncoding.INSTANCE);
contentStream.beginText();
contentStream.setFont(font, 10);
contentStream.newLineAtOffset(10, 700);
contentStream.showText(text);
contentStream.endText();
contentStream.close();
doc.addPage(page);
doc.save(outputFile);
doc.close();
doc = PDDocument.load(outputFile);
font = (PDType1Font) doc.getPage(0).getResources().getFont(COSName.getPDFName("F1"));
Assert.assertEquals(font.getEncoding(), WinAnsiEncoding.INSTANCE);
for (char c : text.toCharArray()) {
String name = font.getEncoding().getName(c);
Assert.assertEquals("dieresis", name.substring(1));
Assert.assertFalse(font.getPath(name).isEmpty());
}
PDFTextStripper stripper = new PDFTextStripper();
Assert.assertEquals(text, stripper.getText(doc).trim());
doc.close();
}
use of com.tom_roush.pdfbox.pdmodel.PDDocument in project PdfBox-Android by TomRoush.
the class CCITTFactoryTest method testCreateFromRandomAccessMulti.
/**
* Tests CCITTFactory#createFromRandomAccess(PDDocument document,
* RandomAccess reader) with a multi page TIFF
*/
public void testCreateFromRandomAccessMulti() throws IOException {
String tiffPath = "pdfbox/com/tom_roush/pdfbox/pdmodel/graphics/image/ccittg4multi.tif";
// ImageInputStream is = ImageIO.createImageInputStream(new File(tiffPath));
// ImageReader imageReader = ImageIO.getImageReaders(is).next();
// imageReader.setInput(is);
// int countTiffImages = imageReader.getNumImages(true);
// assertTrue(countTiffImages > 1); TODO: PdfBox-Android
PDDocument document = new PDDocument();
int pdfPageNum = 0;
while (true) {
PDImageXObject ximage = CCITTFactory.createFromRandomAccess(document, new RandomAccessBuffer(testContext.getAssets().open(tiffPath)), pdfPageNum);
if (ximage == null) {
break;
}
// Bitmap bim = imageReader.read(pdfPageNum);
// validate(ximage, 1, bim.getWidth(), bim.getHeight(), "tiff", PDDeviceGray.INSTANCE.getName());
// checkIdent(bim, ximage.getOpaqueImage());
PDPage page = new PDPage(PDRectangle.A4);
float fX = ximage.getWidth() / page.getMediaBox().getWidth();
float fY = ximage.getHeight() / page.getMediaBox().getHeight();
float factor = Math.max(fX, fY);
document.addPage(page);
PDPageContentStream contentStream = new PDPageContentStream(document, page, AppendMode.APPEND, false);
contentStream.drawImage(ximage, 0, 0, ximage.getWidth() / factor, ximage.getHeight() / factor);
contentStream.close();
++pdfPageNum;
}
// assertEquals(countTiffImages, pdfPageNum);
document.save(testResultsDir + "/multitiff.pdf");
document.close();
document = PDDocument.load(new File(testResultsDir, "multitiff.pdf"), (String) null);
// assertEquals(countTiffImages, document.getNumberOfPages());
document.close();
// imageReader.dispose();
}
use of com.tom_roush.pdfbox.pdmodel.PDDocument in project PdfBox-Android by TomRoush.
the class CCITTFactoryTest method testCreateFromFileLock.
/**
* Tests that CCITTFactory#createFromFile(PDDocument document, File file) doesn't lock the
* source file
*/
public void testCreateFromFileLock() throws IOException {
// copy the source file to a temp directory, as we will be deleting it
String tiffG3Path = "pdfbox/com/tom_roush/pdfbox/pdmodel/graphics/image/ccittg3.tif";
InputStream sourceTiff = testContext.getAssets().open(tiffG3Path);
File copiedTiffFile = new File(testResultsDir, "ccittg3.tif");
copyFile(sourceTiff, copiedTiffFile);
PDDocument document = new PDDocument();
CCITTFactory.createFromFile(document, copiedTiffFile);
assertTrue(copiedTiffFile.delete());
}
Aggregations