繁体   English   中英

使用 Java 编辑 PDF 文本

[英]Editing PDF text using Java

有没有办法可以从 Java 编辑 PDF?
我有一个 PDF 文档,其中包含需要使用 Java 替换的文本占位符,但我看到的所有库都是从头开始创建 PDF 和小的编辑功能。
无论如何我可以编辑PDF还是这是不可能的?

你可以用iText做到这一点。 我用以下代码对其进行了测试。 它会在现有 PDF 的每一页上添加一段文本和一个红色圆圈。

/* requires itextpdf-5.1.2.jar or similar */
import java.io.*;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.*;

public class AddContentToPDF {

    public static void main(String[] args) throws IOException, DocumentException {

        /* example inspired from "iText in action" (2006), chapter 2 */

        PdfReader reader = new PdfReader("C:/temp/Bubi.pdf"); // input PDF
        PdfStamper stamper = new PdfStamper(reader,
          new FileOutputStream("C:/temp/Bubi_modified.pdf")); // output PDF
        BaseFont bf = BaseFont.createFont(
                BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED); // set font

        //loop on pages (1-based)
        for (int i=1; i<=reader.getNumberOfPages(); i++){

            // get object for writing over the existing content;
            // you can also use getUnderContent for writing in the bottom layer
            PdfContentByte over = stamper.getOverContent(i);

            // write text
            over.beginText();
            over.setFontAndSize(bf, 10);    // set font and size
            over.setTextMatrix(107, 740);   // set x,y position (0,0 is at the bottom left)
            over.showText("I can write at page " + i);  // set text
            over.endText();

            // draw a red circle
            over.setRGBColorStroke(0xFF, 0x00, 0x00);
            over.setLineWidth(5f);
            over.ellipse(250, 450, 350, 550);
            over.stroke();
        }

        stamper.close();

    }
}

我修改了发现的代码,它的工作方式如下

public class Principal {
public static final String SRC = "C:/tmp/244558.pdf";
public static final String DEST = "C:/tmp/244558-2.pdf";

public static void main(String[] args) throws IOException, DocumentException {
    File file = new File(DEST);
    file.getParentFile().mkdirs();
    new Principal().manipulatePdf(SRC, DEST);
}

public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
    PdfReader reader = new PdfReader(src);
    PdfDictionary dict = reader.getPageN(1);
    PdfObject object = dict.getDirectObject(PdfName.CONTENTS);
    PdfArray refs = null;
    if (dict.get(PdfName.CONTENTS).isArray()) {
        refs = dict.getAsArray(PdfName.CONTENTS);
    } else if (dict.get(PdfName.CONTENTS).isIndirect()) {
        refs = new PdfArray(dict.get(PdfName.CONTENTS));
    }
    for (int i = 0; i < refs.getArrayList().size(); i++) {
        PRStream stream = (PRStream) refs.getDirectObject(i);
        byte[] data = PdfReader.getStreamBytes(stream);
        stream.setData(new String(data).replace("NULA", "Nulo").getBytes());
    }
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
    stamper.close();
    reader.close();
}

}

看看aspose和这个示例代码

您可以使用Itext进行有限的编辑,但PDF是一种结束文件格式,因此您无法执行任何过于复杂的操作。 我写了一篇文章解释了一些限制: PDF格式和样式信息

我已经使用LibreOffice Draw完成了这项工作。

您首先在 Draw 中手动打开一个 pdf,检查其渲染是否正常,然后将其保存为 Draw .odg 文件。

这是一个压缩的 xml 文件,因此您可以在代码中对其进行修改以查找和替换占位符。

接下来(从代码中)您使用命令行调用 Draw 来生成 pdf。

成功!

主要问题是 Draw 不处理嵌入在 pdf 中的字体。 如果您的系统上还没有安装该字体 - 那么它的渲染会很奇怪,因为 Draw 会用不可避免地具有不同大小的标准字体替换它。

如果对这种方法感兴趣,我将整理一些可共享的代码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM