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:pdf.WorkOrderPDF.java

public void createWorkOrderPNG(String path) throws IOException {
    File file = new File(path);
    PDDocument doc = PDDocument.load(file);
    PDFRenderer renderer = new PDFRenderer(doc);
    renderer.renderImageWithDPI(0, 600);
    BufferedImage image = renderer.renderImage(0);
    SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy.");
    ImageIO.write(image, "PNG", new File("pdf_docs\\work_orders_picture\\" + "wopic-"
            + numOfWorkOrder.split("\\/")[0] + "_" + numOfWorkOrder.split("/")[1] + ".pdf"));
    doc.close();

}

From source file:pdf.WorkOrderPDF.java

public void createWorkOrder() throws IOException {
    PDDocument document = PDDocument.load(template);
    //prva strana
    PDPage page = document.getPage(0);/*w  w  w  .  ja  va 2 s  .co m*/
    contentStream = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.PREPEND, false);

    this.insertProducts();
    this.insertSupplies();
    this.workOrderInfo();

    document.save("pdf_docs\\work_orders\\" + "work_order_" + numOfWorkOrder.split("\\/")[0] + "_"
            + numOfWorkOrder.split("/")[1] + ".pdf");
    document.close();

}

From source file:pdf.WriteOffMaterialsPDF.java

public void createWriteOffMaterialsPNG(String path) throws IOException {
    File file = new File(path);
    PDDocument doc = PDDocument.load(file);
    PDFRenderer renderer = new PDFRenderer(doc);
    BufferedImage image = renderer.renderImage(0);
    SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy.");
    ImageIO.write(image, "PNG", new File(
            "pdf_docs\\write_off_materials_picture\\" + "wompic-" + sdf.format(dateOfWriteOff) + ".png"));
    doc.close();
}

From source file:pdf.WriteOffMaterialsPDF.java

public void createWriteOffMaterials() throws IOException {
    PDDocument document = PDDocument.load(template);
    //prva strana
    PDPage page = document.getPage(0);//ww  w.j  av  a 2s . c om
    contentStream = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.PREPEND, false);

    this.insertSupplies();
    this.writeOffMaterialsInfo();
    contentStream.close();
    SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy.");
    String path = "pdf_docs\\write_off_materials\\" + "write_off_materials-" + sdf.format(dateOfWriteOff)
            + ".pdf";
    document.save(path);
    createWriteOffMaterialsPNG(path);
    document.close();

}

From source file:pdf.WriteOffProductsPDF.java

public void createWriteOffProductsPNG(String path) throws IOException {
    File file = new File(path);
    PDDocument doc = PDDocument.load(file);
    PDFRenderer renderer = new PDFRenderer(doc);
    BufferedImage image = renderer.renderImage(0);
    SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy.");
    ImageIO.write(image, "PNG", new File(
            "pdf_docs\\write_off_products_picture\\" + "woppic-" + sdf.format(dateOfWriteOff) + ".png"));
    doc.close();
}

From source file:pdf.WriteOffProductsPDF.java

public void createWriteOffProducts() throws IOException {
    PDDocument document = PDDocument.load(template);
    //prva strana
    PDPage page = document.getPage(0);//  w  ww  .j  a v a  2  s.  c  o  m
    contentStream = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.PREPEND, false);

    this.insertProducts();
    this.writeOffProductsInfo();
    contentStream.close();
    SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy.");
    String path = "pdf_docs\\write_off_products\\" + "write_off_products-" + sdf.format(dateOfWriteOff)
            + ".pdf";
    document.save(path);
    createWriteOffProductsPNG(path);
    document.close();

}

From source file:pdfbox.PDFA3File.java

License:Apache License

/**
 * Create a simple PDF/A-3 document./*www  .ja v  a2s. c o m*/
 * This example is based on HelloWorld example.
 * As it is a simple case, to conform the PDF/A norm, are added : - the font
 * used in the document - the sRGB color profile - a light xmp block with only
 * PDF identification schema (the only mandatory) - an output intent To
 * conform to A/3 - the mandatory MarkInfo dictionary displays tagged PDF
 * support - and optional producer and - optional creator info is added
 * 
 * @param file
 *          The file to write the PDF to.
 * @param message
 *          The message to write in the file.
 * @throws Exception
 *           If something bad occurs
 */
public void doIt(String file, String message) throws Exception {
    // the document
    PDDocument doc = null;
    try {
        doc = new PDDocument();

        // now create the page and add content
        PDPage page = new PDPage();

        doc.addPage(page);

        InputStream fontStream = PDFA3File.class.getResourceAsStream("/Ubuntu-R.ttf");
        PDFont font = PDTrueTypeFont.loadTTF(doc, fontStream);

        // create a page with the message where needed
        PDPageContentStream contentStream = new PDPageContentStream(doc, page);
        contentStream.beginText();
        contentStream.setFont(font, 12);
        contentStream.moveTextPositionByAmount(100, 700);
        contentStream.drawString(message);
        contentStream.endText();
        contentStream.saveGraphicsState();
        contentStream.close();

        PDDocumentCatalog cat = makeA3compliant(doc);

        InputStream colorProfile = PDFA3File.class.getResourceAsStream("/sRGB Color Space Profile.icm");

        // create output intent
        PDOutputIntent oi = new PDOutputIntent(doc, colorProfile);
        oi.setInfo("sRGB IEC61966-2.1");
        oi.setOutputCondition("sRGB IEC61966-2.1");
        oi.setOutputConditionIdentifier("sRGB IEC61966-2.1");
        oi.setRegistryName("http://www.color.org");
        cat.addOutputIntent(oi);

        doc.save(file);

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

From source file:pdfbox.PDFA3FileAttachment.java

License:Apache License

/**
 * Create a simple PDF/A-3 document./*from   w  ww.j  a  v a 2  s  .  com*/
 * This example is based on HelloWorld example.
 * As it is a simple case, to conform the PDF/A norm, are added : - the font
 * used in the document - the sRGB color profile - a light xmp block with only
 * PDF identification schema (the only mandatory) - an output intent To
 * conform to A/3 - the mandatory MarkInfo dictionary displays tagged PDF
 * support - and optional producer and - optional creator info is added
 * 
 * @param file
 *          The file to write the PDF to.
 * @param message
 *          The message to write in the file.
 * @throws Exception
 *           If something bad occurs
 */
public void doIt(String file, String message) throws Exception {
    // the document
    PDDocument doc = null;
    try {
        doc = new PDDocument();

        // now create the page and add content
        PDPage page = new PDPage();

        doc.addPage(page);

        InputStream fontStream = PDFA3FileAttachment.class.getResourceAsStream("/Ubuntu-R.ttf");
        PDFont font = PDTrueTypeFont.loadTTF(doc, fontStream);

        // create a page with the message where needed
        PDPageContentStream contentStream = new PDPageContentStream(doc, page);
        contentStream.beginText();
        contentStream.setFont(font, 12);
        contentStream.moveTextPositionByAmount(100, 700);
        contentStream.drawString(message);
        contentStream.endText();
        contentStream.saveGraphicsState();
        contentStream.close();

        PDDocumentCatalog cat = makeA3compliant(doc);
        attachSampleFile(doc);

        InputStream colorProfile = PDFA3FileAttachment.class
                .getResourceAsStream("/sRGB Color Space Profile.icm");
        // create output intent
        PDOutputIntent oi = new PDOutputIntent(doc, colorProfile);
        oi.setInfo("sRGB IEC61966-2.1");
        oi.setOutputCondition("sRGB IEC61966-2.1");
        oi.setOutputConditionIdentifier("sRGB IEC61966-2.1");
        oi.setRegistryName("http://www.color.org");
        cat.addOutputIntent(oi);

        doc.save(file);

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

From source file:pdfbox.PDFAFile.java

License:Apache License

/**
 * Create a simple PDF/A document./*w  w w  .j  av a  2 s .  co  m*/
 * This example is based on HelloWorld example.
 * As it is a simple case, to conform the PDF/A norm, are added : - the font
 * used in the document - a light xmp block with only PDF identification
 * schema (the only mandatory) - an output intent
 * 
 * @param file
 *          The file to write the PDF to.
 * @param message
 *          The message to write in the file.
 * @throws Exception
 *           If something bad occurs
 */
public void doIt(String file, String message) throws Exception {
    // the document
    PDDocument doc = null;
    try {
        doc = new PDDocument();

        PDPage page = new PDPage();
        doc.addPage(page);

        InputStream fontStream = PDFA3File.class.getResourceAsStream("/Ubuntu-R.ttf");
        PDFont font = PDTrueTypeFont.loadTTF(doc, fontStream);

        // create a page with the message where needed
        PDPageContentStream contentStream = new PDPageContentStream(doc, page);
        contentStream.beginText();
        contentStream.setFont(font, 12);
        contentStream.moveTextPositionByAmount(100, 700);
        contentStream.drawString(message);
        contentStream.endText();
        contentStream.saveGraphicsState();
        contentStream.close();

        PDDocumentCatalog cat = doc.getDocumentCatalog();
        PDMetadata metadata = new PDMetadata(doc);
        cat.setMetadata(metadata);

        // jempbox version
        XMPMetadata xmp = new XMPMetadata();
        XMPSchemaPDFAId pdfaid = new XMPSchemaPDFAId(xmp);
        xmp.addSchema(pdfaid);
        pdfaid.setConformance("B");
        pdfaid.setPart(1);
        pdfaid.setAbout("");
        metadata.importXMPMetadata(xmp.asByteArray());

        InputStream colorProfile = PDFA3File.class.getResourceAsStream("/sRGB Color Space Profile.icm");
        // create output intent
        PDOutputIntent oi = new PDOutputIntent(doc, colorProfile);
        oi.setInfo("sRGB IEC61966-2.1");
        oi.setOutputCondition("sRGB IEC61966-2.1");
        oi.setOutputConditionIdentifier("sRGB IEC61966-2.1");
        oi.setRegistryName("http://www.color.org");
        cat.addOutputIntent(oi);

        doc.save(file);

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

From source file:pdfboxtest.PDFBoxTest.java

/**
 *///  w  w w . j  a  v a2s  .c  o  m
public static void main(String[] args) throws Exception {

    GetFundamentals go = new GetFundamentals();
    // Create a document and add a page to it
    go.ticker = "AAN";
    String outputFileName = go.ticker + ".pdf";
    PDDocument document = new PDDocument();
    PDPage page1 = new PDPage(PDPage.PAGE_SIZE_LETTER);
    PDPage page2 = new PDPage(PDPage.PAGE_SIZE_LETTER);
    PDPage page3 = new PDPage(PDPage.PAGE_SIZE_LETTER);
    PDPage page4 = new PDPage(PDPage.PAGE_SIZE_LETTER);
    // PDPage.PAGE_SIZE_LETTER is also possible
    // rect can be used to get the page width and height
    document.addPage(page1);
    document.addPage(page2);
    document.addPage(page3);
    document.addPage(page4);

    // Create a new font object selecting one of the PDF base fonts
    PDFont fontPlain = PDType1Font.HELVETICA_BOLD;

    // Start a new content stream which will "hold" the to be created content
    PDPageContentStream cos = new PDPageContentStream(document, page1);

    // Define a text content stream using the selected font, move the cursor and draw some text
    setupDefault(cos, fontPlain, document, go);
    setupP1(cos, fontPlain, document, go.ticker);
    cos.close();
    cos = new PDPageContentStream(document, page2);
    setupDefault(cos, fontPlain, document, go);
    setupP2(cos, fontPlain, document);
    cos.close();
    cos = new PDPageContentStream(document, page3);
    setupDefault(cos, fontPlain, document, go);
    setupP3(cos, fontPlain, document);
    cos.close();
    cos = new PDPageContentStream(document, page4);
    setupDefault(cos, fontPlain, document, go);
    setupP4(cos, fontPlain, document);
    cos.close();

    // Make sure that the content stream is closed:

    // Save the results and ensure that the document is properly closed:
    document.save(outputFileName);
    document.close();
}