java 使用 pdfbox 编辑 pdf 页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17685132/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 14:39:59  来源:igfitidea点击:

Edit pdf page using pdfbox

javaeditpdfbox

提问by Monssef

How can i edit a pdf page with java and pdfbox by writing in a specific position that i know already in pixels ?

如何通过在我已经知道的像素位置书写来使用 java 和 pdfbox 编辑 pdf 页面?

I tried this but it overwrites :

我试过这个,但它会覆盖:

PDDocument document = null;
try {
    document = PDDocument.load(new File("/x/x/x/mypdf.pdf"));
    PDPage page = (PDPage) document.getDocumentCatalog().getAllPages().get(0);
    PDFont font = PDType1Font.HELVETICA_BOLD;
    PDPageContentStream contentStream = new PDPageContentStream(document, page);
    page.getContents().getStream();
    contentStream.beginText();
    contentStream.setFont(font, 12);
    contentStream.moveTextPositionByAmount(100, 100);
    contentStream.drawString("Hello");
    contentStream.endText();
    contentStream.close();
    document.save("/x/x/x/mypdf.pdf");
    document.close();
} catch (IOException e) {
    e.printStackTrace();
} catch (COSVisitorException e) {
    e.printStackTrace();
}

Thank you.

谢谢你。

回答by Anita Kulkarni

You could have used PDFBox, all you are missing is appending to the page. Just change this line:

您可以使用 PDFBox,您所缺少的只是附加到页面。只需更改这一行:

PDPageContentStream contentStream = new PDPageContentStream(document, page);

to:

到:

PDPageContentStream contentStream = new PDPageContentStream(document, page, true, true);

Starting from PDFBox 2.0, the booleanappendContenthas been replaced by the AppendModeAPPENDsuch that the equivalent of the previous code is now:

从 PDFBox 2.0 开始,booleanappendContent已被替换为AppendModeAPPEND,因此之前代码的等价物现在为:

PDPageContentStream contentStream = new PDPageContentStream(
    document, page, PDPageContentStream.AppendMode.APPEND, true
);

回答by Monssef

I figure it out how to do it, instead of using pdfbox i used iTextpdf, this is the java code i used :

我想出了如何做到这一点,而不是使用 pdfbox 我使用iTextpdf,这是我使用的 Java 代码:

package ma;

import java.io.*;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.*;

public class editPdf {

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

    PdfReader reader = new PdfReader("/Users/Monssef/Desktop/mypdf.pdf");
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(
            "/Users/Leonidas/Desktop/mypdfmodified.pdf"));
    BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252,
            BaseFont.NOT_EMBEDDED);

        PdfContentByte over = stamper.getOverContent(1);

        over.beginText();
        over.setFontAndSize(bf, 10);
        over.setTextMatrix(107, 107);
        over.showText("page updated");
        over.endText();

    stamper.close();
}

}

回答by FreedomRings

Anita is correct. In fact it works quite well. I would add that the line

安妮塔是对的。事实上,它工作得很好。我想补充一点

page.getContents().getStream();

is possibly extraneous, and PDPage is being depreciated in favor of PDPageable in the newer releases (and is used primarily for printing), but the code will work for your purpose without going to the expense of iText (and after all, you originally asked about PDFBox).

可能是无关紧要的,并且在较新的版本中 PDPage 正在贬值以支持 PDPageable(并且主要用于打印),但是代码将用于您的目的,而无需花费 iText(毕竟,您最初询问的是PDFBox)。

Don't forget to include the fix Anita gave to create the extra bits in the creation of contentstream:

不要忘记在内容流的创建中包含 Anita 为创建额外位而提供的修复:

PDPageContentStream contentStream = new PDPageContentStream(
        document, page, true, true);

You should also remember that you will likely be creating and closing streams for each section of print that you place on top of the pdf you are overlaying text upon. You will need to be sure to close both the streams and the document so that the buffers are written, otherwise you will not see your changes.

您还应该记住,您可能会为放置在覆盖文本的 pdf 顶部的每个打印部分创建和关闭流。您需要确保关闭流和文档,以便写入缓冲区,否则您将看不到更改。

Also, for those trying this out, there are several options of downloading libraries from apache for pdfbox. The easiest one to use, I think, is (currently) the one named pdfbox-app-1.8.10.jar(which I am currently using even in my JSF apps). It already includes the other libraries that are hard-wired into pdfbox that you would also need to download to do anything meaningful.

此外,对于那些尝试这样做的人,有几种从 apache 下载 pdfbox 库的选项。我认为,最容易使用的是(目前)名为pdfbox-app-1.8.10.jar 的(我目前甚至在我的 JSF 应用程序中也在使用)。它已经包含硬连接到 pdfbox 的其他库,您还需要下载这些库才能执行任何有意义的操作。