Search in sources :

Example 66 with PDDocument

use of com.tom_roush.pdfbox.pdmodel.PDDocument in project PdfBox-Android by TomRoush.

the class TestCOSName method PDFBox4076.

/**
 * PDFBOX-4076: Check that characters outside of US_ASCII are not replaced with "?".
 *
 * @throws IOException
 */
@Test
public void PDFBox4076() throws IOException {
    String special = "中国你好!";
    PDDocument document = new PDDocument();
    PDPage page = new PDPage();
    document.addPage(page);
    document.getDocumentCatalog().getCOSObject().setString(COSName.getPDFName(special), special);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    document.save(baos);
    document.close();
    document = PDDocument.load(baos.toByteArray());
    COSDictionary catalogDict = document.getDocumentCatalog().getCOSObject();
    Assert.assertTrue(catalogDict.containsKey(special));
    Assert.assertEquals(special, catalogDict.getString(special));
    document.close();
}
Also used : PDPage(com.tom_roush.pdfbox.pdmodel.PDPage) PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 67 with PDDocument

use of com.tom_roush.pdfbox.pdmodel.PDDocument in project PdfBox-Android by TomRoush.

the class TestPDFParser method testPDFBox3208.

/**
 * Test whether /Info dictionary is retrieved correctly when rebuilding the trailer of a corrupt
 * file. An incorrect algorithm would result in an outline dictionary being mistaken for an
 * /Info.
 *
 * @throws IOException
 */
// TODO: PdfBox-Android - provide test file
@Test
public void testPDFBox3208() throws IOException {
    File testPdf = new File(TARGETPDFDIR, "PDFBOX-3208-L33MUTT2SVCWGCS6UIYL5TH3PNPXHIS6.pdf");
    assumeTrue(testPdf.exists());
    PDDocument doc = PDDocument.load(testPdf);
    PDDocumentInformation di = doc.getDocumentInformation();
    assertEquals("Liquent Enterprise Services", di.getAuthor());
    assertEquals("Liquent services server", di.getCreator());
    assertEquals("Amyuni PDF Converter version 4.0.0.9", di.getProducer());
    assertEquals("", di.getKeywords());
    assertEquals("", di.getSubject());
    assertEquals("892B77DE781B4E71A1BEFB81A51A5ABC_20140326022424.docx", di.getTitle());
    assertEquals(DateConverter.toCalendar("D:20140326142505-02'00'"), di.getCreationDate());
    assertEquals(DateConverter.toCalendar("20140326172513Z"), di.getModificationDate());
    doc.close();
}
Also used : PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) File(java.io.File) ScratchFile(com.tom_roush.pdfbox.io.ScratchFile) PDDocumentInformation(com.tom_roush.pdfbox.pdmodel.PDDocumentInformation) Test(org.junit.Test)

Example 68 with PDDocument

use of com.tom_roush.pdfbox.pdmodel.PDDocument in project PdfBox-Android by TomRoush.

the class TestPDFParser method testPDFBox3940.

/**
 * Test whether the /Info is retrieved correctly when rebuilding the trailer of a corrupt file,
 * despite the /Info dictionary not having a modification date.
 *
 * @throws IOException
 */
// TODO: PdfBox-Android - provide test file
@Test
public void testPDFBox3940() throws IOException {
    File testPdf = new File(TARGETPDFDIR, "PDFBOX-3940-079977.pdf");
    assumeTrue(testPdf.exists());
    PDDocument doc = PDDocument.load(testPdf);
    PDDocumentInformation di = doc.getDocumentInformation();
    assertEquals("Unknown", di.getAuthor());
    assertEquals("C:REGULA~1IREGSFR_EQ_EM.WP", di.getCreator());
    assertEquals("Acrobat PDFWriter 3.02 for Windows", di.getProducer());
    assertEquals("", di.getKeywords());
    assertEquals("", di.getSubject());
    assertEquals("C:REGULA~1IREGSFR_EQ_EM.PDF", di.getTitle());
    assertEquals(DateConverter.toCalendar("Tuesday, July 28, 1998 4:00:09 PM"), di.getCreationDate());
    doc.close();
}
Also used : PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) File(java.io.File) ScratchFile(com.tom_roush.pdfbox.io.ScratchFile) PDDocumentInformation(com.tom_roush.pdfbox.pdmodel.PDDocumentInformation) Test(org.junit.Test)

Example 69 with PDDocument

use of com.tom_roush.pdfbox.pdmodel.PDDocument in project PdfBox-Android by TomRoush.

the class COSWriterTest method testPDFBox4241.

/**
 * PDFBOX-4241: check whether the output stream is closed twice.
 *
 * @throws IOException
 */
@Test
public void testPDFBox4241() throws IOException {
    PDDocument doc = new PDDocument();
    PDPage page = new PDPage();
    doc.addPage(page);
    doc.save(new BufferedOutputStream(new ByteArrayOutputStream(1024) {

        private boolean open = true;

        @Override
        public void close() throws IOException {
            // Thread.dumpStack();
            open = false;
            super.close();
        }

        @Override
        public void flush() throws IOException {
            if (!open) {
                throw new IOException("Stream already closed");
            }
        // Thread.dumpStack();
        }
    }));
    doc.close();
}
Also used : PDPage(com.tom_roush.pdfbox.pdmodel.PDPage) PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) BufferedOutputStream(java.io.BufferedOutputStream) Test(org.junit.Test)

Example 70 with PDDocument

use of com.tom_roush.pdfbox.pdmodel.PDDocument in project PdfBox-Android by TomRoush.

the class PageExtractorTest method testExtract.

/**
 * Test of extract method, of class com.tom_roush.pdfbox.util.PageExtractor.
 */
public void testExtract() throws Exception {
    PDDocument sourcePdf = null;
    PDDocument result = null;
    try {
        // this should work for most users
        sourcePdf = PDDocument.load(new File("src/test/resources/pdfbox/input/cweb.pdf"));
        PageExtractor instance = new PageExtractor(sourcePdf);
        result = instance.extract();
        assertEquals(sourcePdf.getNumberOfPages(), result.getNumberOfPages());
        closeDoc(result);
        instance = new PageExtractor(sourcePdf, 1, 1);
        result = instance.extract();
        assertEquals(1, result.getNumberOfPages());
        closeDoc(result);
        instance = new PageExtractor(sourcePdf, 1, 5);
        result = instance.extract();
        assertEquals(5, result.getNumberOfPages());
        closeDoc(result);
        instance = new PageExtractor(sourcePdf, 5, 10);
        result = instance.extract();
        assertEquals(6, result.getNumberOfPages());
        closeDoc(result);
        instance = new PageExtractor(sourcePdf, 2, 1);
        result = instance.extract();
        assertEquals(0, result.getNumberOfPages());
        closeDoc(result);
    } finally {
        closeDoc(sourcePdf);
        closeDoc(result);
    }
}
Also used : 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