首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用Java编辑PDF文本

使用Java编辑PDF文本
EN

Stack Overflow用户
提问于 2010-11-09 14:23:19
回答 5查看 59.9K关注 0票数 28

有没有办法可以从Java编辑PDF?

我有一个PDF文档,其中包含我需要使用Java替换的文本的占位符,但我看到的所有库都是从头开始创建PDF的,并且具有小的编辑功能。

有没有什么办法可以编辑PDF,或者这是不可能的?

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2012-02-22 18:47:09

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

代码语言:javascript
复制
/* 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();

    }
}
票数 14
EN

Stack Overflow用户

发布于 2018-10-12 04:32:43

我稍微修改了一下代码,它的工作方式如下

代码语言:javascript
复制
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();
}

}

票数 4
EN

Stack Overflow用户

发布于 2010-11-09 14:43:41

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4131031

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档