Example usage for org.apache.pdfbox.pdmodel PDDocument close

List of usage examples for org.apache.pdfbox.pdmodel PDDocument close

Introduction

In this page you can find the example usage for org.apache.pdfbox.pdmodel PDDocument close.

Prototype

@Override
public void close() throws IOException 

Source Link

Document

This will close the underlying COSDocument object.

Usage

From source file:org.apache.fop.render.pdf.PDFBoxAdapterTestCase.java

License:Apache License

@Test
public void testTTSubset() throws Exception {
    PDDocument doc = getResource(TTSubset1);
    FOPPDFSingleByteFont mbfont = new FOPPDFSingleByteFont(getFont(doc, "R9"), "TimesNewRomanPSMT_TrueType");
    mbfont.addFont(getFont(doc, "R9"));
    //        Assert.assertEquals(mbfont.mapChar('t'), 116);

    PDDocument doc2 = getResource(TTSubset2);
    String name = mbfont.addFont(getFont(doc2, "R9"));
    Assert.assertEquals(name, "TimesNewRomanPSMT_TrueType");
    Assert.assertEquals(mbfont.getFontName(), "TimesNewRomanPSMT_TrueType");
    byte[] is = IOUtils.toByteArray(mbfont.getInputStream());
    Assert.assertEquals(is.length, 41112);
    doc.close();
    doc2.close();//from w  ww  .  j  av a2 s .c o  m
}

From source file:org.apache.fop.render.pdf.PDFBoxAdapterTestCase.java

License:Apache License

@Test
public void testType1Subset() throws Exception {
    PDDocument doc = getResource(Type1Subset1);
    FOPPDFSingleByteFont mbfont = new FOPPDFSingleByteFont(getFont(doc, "F15"), "");
    mbfont.addFont(getFont(doc, "F15"));
    PDDocument doc2 = getResource(Type1Subset2);
    mbfont.addFont(getFont(doc2, "F15"));
    Type1Font f = Type1Font.createWithPFB(mbfont.getInputStream());
    Set<String> csDict = new TreeSet<String>(f.getCharStringsDict().keySet());
    Assert.assertEquals(csDict.toString(), "[.notdef, a, d, e, hyphen, l, m, n, p, s, space, t, two, x]");
    Assert.assertEquals(f.getSubrsArray().size(), 518);
    Assert.assertEquals(f.getFamilyName(), "Verdana");
    doc.close();
    doc2.close();/*from  w  w  w  .  ja  v  a  2 s . c  om*/
}

From source file:org.apache.fop.render.pdf.PDFBoxAdapterTestCase.java

License:Apache License

@Test
public void testStream() throws Exception {
    PDFDocument pdfdoc = new PDFDocument("");
    PDFPage pdfpage = new PDFPage(new PDFResources(pdfdoc), 0, r, r, r, r);
    pdfpage.setDocument(pdfdoc);/*from www.  ja v  a  2  s . c  om*/
    PDFBoxAdapter adapter = new PDFBoxAdapter(pdfpage, new HashMap(), new HashMap<Integer, PDFArray>());
    PDDocument doc = getResource(ROTATE);
    PDPage page = (PDPage) doc.getDocumentCatalog().getPages().get(0);
    AffineTransform at = new AffineTransform();
    Rectangle r = new Rectangle(0, 1650, 842000, 595000);
    String stream = adapter.createStreamFromPDFBoxPage(doc, page, "key", at, null, r);
    Assert.assertEquals(at, new AffineTransform(-0.0, 1.0000000554888686, 1.0000000554888686, 0.0, 0.0,
            -2.0742416381835938E-5));
    Assert.assertTrue(stream.contains("/GS0106079 gs"));
    Assert.assertTrue(stream.contains("/TT0106079 1 Tf"));
    doc.close();
}

From source file:org.apache.fop.render.pdf.PDFBoxAdapterTestCase.java

License:Apache License

@Test
public void testTaggedPDFWriter() throws IOException {
    PDFDocument pdfdoc = new PDFDocument("");
    PDFPage pdfpage = new PDFPage(new PDFResources(pdfdoc), 0, r, r, r, r);
    pdfpage.setDocument(pdfdoc);//w w w  . j a va  2  s  .c  o  m
    PDFBoxAdapter adapter = new PDFBoxAdapter(pdfpage, new HashMap(), new HashMap<Integer, PDFArray>());
    adapter.setCurrentMCID(5);
    PDDocument doc = getResource(HELLOTagged);
    PDPage page = (PDPage) doc.getDocumentCatalog().getPages().get(0);
    AffineTransform at = new AffineTransform();
    Rectangle r = new Rectangle(0, 1650, 842000, 595000);
    String stream = adapter.createStreamFromPDFBoxPage(doc, page, "key", at, null, r);
    Assert.assertTrue(stream, stream.contains("/P <</MCID 5 >>BDC"));
    doc.close();
}

From source file:org.apache.fop.render.pdf.PDFBoxAdapterTestCase.java

License:Apache License

@Test
public void testLink() throws Exception {
    PDFDocument pdfdoc = new PDFDocument("");
    PDFPage pdfpage = new PDFPage(new PDFResources(pdfdoc), 0, r, r, r, r);
    pdfpage.setDocument(pdfdoc);/*from w ww  .ja va2 s  .c o  m*/
    pdfpage.setObjectNumber(1);
    Map<Integer, PDFArray> pageNumbers = new HashMap<Integer, PDFArray>();
    PDFBoxAdapter adapter = new PDFBoxAdapter(pdfpage, new HashMap(), pageNumbers);
    PDDocument doc = getResource(LINK);
    PDPage page = (PDPage) doc.getDocumentCatalog().getPages().get(0);
    AffineTransform at = new AffineTransform();
    Rectangle r = new Rectangle(0, 1650, 842000, 595000);
    String stream = adapter.createStreamFromPDFBoxPage(doc, page, "key", at, null, r);
    Assert.assertTrue(stream.contains("/Link <</MCID 5 >>BDC"));
    Assert.assertEquals(pageNumbers.size(), 4);
    PDFAnnotList annots = (PDFAnnotList) pdfpage.get("Annots");
    Assert.assertEquals(annots.toPDFString(), "[\n1 0 R\n2 0 R\n]");
    doc.close();
}

From source file:org.apache.fop.render.pdf.PDFBoxAdapterTestCase.java

License:Apache License

@Test
public void testXform() throws Exception {
    PDFDocument pdfdoc = new PDFDocument("");
    pdfdoc.getFilterMap().put(PDFFilterList.DEFAULT_FILTER, Arrays.asList("null"));
    pdfdoc.setMergeFontsEnabled(true);/* w  w  w. j a va 2  s .  c  o m*/
    PDFPage pdfpage = new PDFPage(new PDFResources(pdfdoc), 0, r, r, r, r);
    pdfpage.setDocument(pdfdoc);
    pdfpage.setObjectNumber(1);
    Map<Integer, PDFArray> pageNumbers = new HashMap<Integer, PDFArray>();
    PDFBoxAdapter adapter = new PDFBoxAdapter(pdfpage, new HashMap(), pageNumbers);
    PDDocument doc = getResource(XFORM);
    PDPage page = (PDPage) doc.getDocumentCatalog().getPages().get(0);
    AffineTransform at = new AffineTransform();
    Rectangle r = new Rectangle(0, 1650, 842000, 595000);
    adapter.createStreamFromPDFBoxPage(doc, page, "key", at, new FontInfo(), r);
    doc.close();
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    pdfdoc.output(bos);
    Assert.assertFalse(bos.toString("UTF-8").contains("/W 5 /H 5 /BPC 8 /CS /RGB ID "));
}

From source file:org.apache.fop.render.pdf.PDFBoxAdapterTestCase.java

License:Apache License

private ByteArrayOutputStream pdfToPS(String pdf) throws IOException, ImageException {
    ImageConverterPDF2G2D i = new ImageConverterPDF2G2D();
    ImageInfo imgi = new ImageInfo("a", "b");
    PDDocument doc = getResource(pdf);
    org.apache.xmlgraphics.image.loader.Image img = new ImagePDF(imgi, doc);
    ImageGraphics2D ig = (ImageGraphics2D) i.convert(img, null);
    GeneralGraphics2DImagePainter g = (GeneralGraphics2DImagePainter) ig.getGraphics2DImagePainter();
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    PSPDFGraphics2D g2d = (PSPDFGraphics2D) g.getGraphics(true, new FOPPSGeneratorImpl(stream));
    Rectangle2D rect = new Rectangle2D.Float(0, 0, 100, 100);
    GraphicContext gc = new GraphicContext();
    g2d.setGraphicContext(gc);/*from   ww  w .  ja  v a2  s  .  c o m*/
    ig.getGraphics2DImagePainter().paint(g2d, rect);
    doc.close();
    return stream;
}

From source file:org.apache.pdflens.example.HelloWorld.java

License:Apache License

/**
 * create the second sample document from the PDF file format specification.
 *
 * @param file The file to write the PDF to.
 * @param message The message to write in the file.
 *
 * @throws IOException If there is an error writing the data.
 * @throws COSVisitorException If there is an error writing the PDF.
 *///from   ww  w .  jav  a  2 s  .c  om
public void doIt(String file, String message) throws IOException, COSVisitorException {
    // the document
    PDDocument doc = null;
    try {
        doc = new PDDocument();

        PDPage page = new PDPage();
        doc.addPage(page);
        PDFont font = PDType1Font.HELVETICA_BOLD;

        PDPageContentStream contentStream = new PDPageContentStream(doc, page);
        contentStream.beginText();
        contentStream.setFont(font, 12);
        contentStream.moveTextPositionByAmount(100, 700);
        contentStream.drawString(message);
        contentStream.endText();
        contentStream.close();
        doc.save(file);

    } finally {
        if (doc != null) {
            doc.close();
        }
    }
}

From source file:org.apache.pdflens.example.HelloWorldMutiPage.java

License:Apache License

/**
 * create the second sample document from the PDF file format specification.
 *
 * @param file The file to write the PDF to.
 * @param message The message to write in the file.
 *
 * @throws IOException If there is an error writing the data.
 * @throws COSVisitorException If there is an error writing the PDF.
 *//*from  ww  w  .j  a v a 2s.  co m*/
public void doIt(String file, String message) throws IOException, COSVisitorException {
    // the document
    PDDocument doc = null;
    try {
        doc = new PDDocument();

        for (int i = 0; i < 10; i++) {
            PDPage page = generatePage(message + i, doc);
            doc.addPage(page);
        }

        doc.save(file);

    } finally {
        if (doc != null) {
            doc.close();
        }
    }
}

From source file:org.apache.tika.parser.pdf.EnhancedPDFParser.java

License:Apache License

public void parse(InputStream stream, ContentHandler handler, Metadata metadata, ParseContext context)
        throws IOException, SAXException, TikaException {

    PDDocument pdfDocument = null;
    //config from context, or default if not set via context
    PDFParserConfig localConfig = context.get(PDFParserConfig.class, defaultConfig);
    String password = "";
    try {// w  w w.  j  av a 2  s. c o  m
        // PDFBox can process entirely in memory, or can use a temp file
        //  for unpacked / processed resources
        // Decide which to do based on if we're reading from a file or not already
        TikaInputStream tstream = TikaInputStream.cast(stream);
        password = getPassword(metadata, context);
        if (tstream != null && tstream.hasFile()) {
            // File based, take that as a cue to use a temporary file
            if (localConfig.getUseNonSequentialParser() == true) {
                pdfDocument = PDDocument.load(new CloseShieldInputStream(stream), password);
            } else {
                pdfDocument = PDDocument.load(new CloseShieldInputStream(stream), true);
            }
        } else {
            // Go for the normal, stream based in-memory parsing
            if (localConfig.getUseNonSequentialParser() == true) {
                pdfDocument = PDDocument.load(new CloseShieldInputStream(stream), password);
            } else {
                pdfDocument = PDDocument.load(new CloseShieldInputStream(stream), true);
            }
        }
        metadata.set("pdf:encrypted", Boolean.toString(pdfDocument.isEncrypted()));

        pdfDocument.setAllSecurityToBeRemoved(true);

        metadata.set(Metadata.CONTENT_TYPE, "application/pdf");
        extractMetadata(pdfDocument, metadata);
        if (handler != null) {
            String xfaXml = extractXFAText(pdfDocument);
            if (xfaXml != null) {
                try (BufferedInputStream is = new BufferedInputStream(
                        new ByteArrayInputStream(xfaXml.getBytes()))) {
                    new TXTParser().parse(is, handler, metadata, context);
                }
                metadata.set(Metadata.CONTENT_TYPE, "application/pdf");
            } else {
                EnhancedPDF2XHTML.process(pdfDocument, handler, context, metadata, localConfig);
            }
        }
    } finally {
        if (pdfDocument != null) {
            pdfDocument.close();
        }
    }
}