PDFBox

Posted by Bruce Tsai
09/26/2016

Apache PDFbox 是一個基於 Java 的開放原始 PDF 文件的類別庫,它可以用於創建新的 PDF 文件,修改現有的 PDF 文件,還可以從 PDF 文件中提取所需的內容。 Apache PDFBox 還包含了數個命令列工具。

範例

PDDocument doc = new PDDocument();
try
{
    PDPage page = new PDPage();
    doc.addPage(page);

    // load the font as this needs to be embedded
    PDFont font = PDType0Font.load(doc, new File(fontfile));

    // create a page with the message
    PDPageContentStream contents = new PDPageContentStream(doc, page);
    contents.beginText();
    contents.setFont(font, 12);
    contents.newLineAtOffset(100, 700);
    contents.showText(message);
    contents.endText();
    contents.saveGraphicsState();
    contents.close();

    // add XMP metadata
    XMPMetadata xmp = XMPMetadata.createXMPMetadata();

    try
    {
        DublinCoreSchema dc = xmp.createAndAddDublinCoreSchema();
        dc.setTitle(file);

        PDFAIdentificationSchema id = xmp.createAndAddPFAIdentificationSchema();
        id.setPart(1);
        id.setConformance("B");

        XmpSerializer serializer = new XmpSerializer();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        serializer.serialize(xmp, baos, true);

        PDMetadata metadata = new PDMetadata(doc);
        metadata.importXMPMetadata(baos.toByteArray());
        doc.getDocumentCatalog().setMetadata(metadata);
    }
    catch(BadFieldValueException e)
    {
        // won't happen here, as the provided value is valid
        throw new IllegalArgumentException(e);
    }

    // sRGB output intent
    InputStream colorProfile = CreatePDFA.class.getResourceAsStream(
            "/org/apache/pdfbox/resources/pdfa/sRGB Color Space Profile.icm");
    PDOutputIntent intent = new PDOutputIntent(doc, colorProfile);
    intent.setInfo("sRGB IEC61966-2.1");
    intent.setOutputCondition("sRGB IEC61966-2.1");
    intent.setOutputConditionIdentifier("sRGB IEC61966-2.1");
    intent.setRegistryName("http://www.color.org");
    doc.getDocumentCatalog().addOutputIntent(intent);

    doc.save(file);
}
finally
{
    doc.close();
}

results matching ""

    No results matching ""