Edit pdf page using pdfbox

2019-02-06 22:00发布

问题:

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

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.

回答1:

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

PDPageContentStream contentStream = new PDPageContentStream(document, page);

to:

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

Starting from PDFBox 2.0, the boolean appendContent has been replaced by the AppendMode APPEND such that the equivalent of the previous code is now:

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


回答2:

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

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();
}

}


回答3:

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).

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

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.

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.



标签: java edit pdfbox