Search in sources :

Example 41 with PDDocument

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();
}
Also used : PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) IOException(java.io.IOException) File(java.io.File) PDFRenderer(com.tom_roush.pdfbox.rendering.PDFRenderer) Test(org.junit.Test)

Example 42 with PDDocument

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();
}
Also used : PDPage(com.tom_roush.pdfbox.pdmodel.PDPage) PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) PDFRenderer(com.tom_roush.pdfbox.rendering.PDFRenderer) PDFTextStripper(com.tom_roush.pdfbox.text.PDFTextStripper)

Example 43 with PDDocument

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();
}
Also used : PDPage(com.tom_roush.pdfbox.pdmodel.PDPage) PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) PDPageContentStream(com.tom_roush.pdfbox.pdmodel.PDPageContentStream) File(java.io.File) FileInputStream(java.io.FileInputStream) PDFTextStripper(com.tom_roush.pdfbox.text.PDFTextStripper) Test(org.junit.Test)

Example 44 with PDDocument

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();
}
Also used : RandomAccessBuffer(com.tom_roush.pdfbox.io.RandomAccessBuffer) PDPage(com.tom_roush.pdfbox.pdmodel.PDPage) PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) PDPageContentStream(com.tom_roush.pdfbox.pdmodel.PDPageContentStream) File(java.io.File)

Example 45 with PDDocument

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());
}
Also used : InputStream(java.io.InputStream) PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) File(java.io.File)

Aggregations

PDDocument (com.tom_roush.pdfbox.pdmodel.PDDocument)137 File (java.io.File)80 Test (org.junit.Test)69 PDPage (com.tom_roush.pdfbox.pdmodel.PDPage)37 PDPageContentStream (com.tom_roush.pdfbox.pdmodel.PDPageContentStream)22 InputStream (java.io.InputStream)21 Bitmap (android.graphics.Bitmap)18 IOException (java.io.IOException)14 PDResources (com.tom_roush.pdfbox.pdmodel.PDResources)11 PDFRenderer (com.tom_roush.pdfbox.rendering.PDFRenderer)11 ArrayList (java.util.ArrayList)11 PDDocumentCatalog (com.tom_roush.pdfbox.pdmodel.PDDocumentCatalog)10 ByteArrayOutputStream (java.io.ByteArrayOutputStream)9 FileInputStream (java.io.FileInputStream)9 FileOutputStream (java.io.FileOutputStream)9 COSArray (com.tom_roush.pdfbox.cos.COSArray)8 PDEmbeddedFile (com.tom_roush.pdfbox.pdmodel.common.filespecification.PDEmbeddedFile)8 PDFont (com.tom_roush.pdfbox.pdmodel.font.PDFont)8 COSDictionary (com.tom_roush.pdfbox.cos.COSDictionary)7 Paint (android.graphics.Paint)6