Edit pdf page using pdfbox

How can I edit a pdf page using java and pdfbox by writing in a specific position that I already know 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(); } 

Thanks.

+6
source share
3 answers

I understand how to do this, 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(); } } 
+2
source

Perhaps you used PDFBox, everything that you are missing is added to the page. Just change this line:

 PDPageContentStream contentStream = new PDPageContentStream(document, page); 

in

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

Starting with PDFBox 2.0, boolean appendContent been replaced with AppendMode APPEND so that the equivalent of the previous code is now:

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

Anita is correct. It actually works very well. I would add that the line

 page.getContents().getStream(); 

it may be a stranger, and PDPage is amortized in favor of PDPageable in newer versions (and is mainly used for printing), but the code will work for your purpose without switching from iText (and in the end you initially asked about PDFBox).

Remember to include the fix that Anita gave to create extra bits in the creation of the contentstream:

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

You should also remember that you are likely to create and close streams for each print section that you place on top of the pdf on which you overlay the text. You need to close both streams and the document so that the buffers are written, otherwise you will not see the changes.

In addition, for those who are trying to find out, there are several options for downloading libraries from apache for pdfbox. I find it easiest to use (for now) a file called pdfbox-app-1.8.10.jar (which I use in my JSF applications). It already includes other libraries that are hard-wired to pdfbox, which you also need to download to do anything meaningful.

+1
source

Source: https://habr.com/ru/post/949590/


All Articles