Editing PDF from Java Using PDFBox

天大地大妈咪最大 提交于 2019-12-13 16:46:24

问题


I am currently getting a page from a PDF and then trying to edit that particular page. When i do the edit it is not saving within the PDF itself.

This is my code could someone please help.

PDPage page = (PDPage) allPages.get(f);
System.out.println(page);
PDRectangle pageSize = page.findMediaBox();
float stringWidth = font.getStringWidth( "AAA" );
float centeredPosition = (pageSize.getWidth() - (stringWidth*fontSize)/1000f)/2f;

PDPageContentStream contentStream = new PDPageContentStream(pdoc,page,true,false);

contentStream.beginText();
contentStream.setFont( font, fontSize );
//contentStream.addLine(700, 700, 700, 1000);
contentStream.moveTextPositionByAmount(0 , 0);
contentStream.drawString( "AAA" );
contentStream.endText();
contentStream.close();

pdoc.save("C:/1/1.pdf");
pdoc.close();

回答1:


The code is with which I am doing this is correct.

The problem is not the code but the way the pdfs are being generated are at a Version 1.2. I would need to be able know what I can do to alter PDFs that are one V 1.2




回答2:


please use/update below code

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

Its working for me



来源:https://stackoverflow.com/questions/7269257/editing-pdf-from-java-using-pdfbox

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!