使用 PDFBox 从 Java 编辑 PDF
我目前正在从 PDF 获取页面,然后尝试编辑该特定页面。 当我进行编辑时,它不会保存在 PDF 本身中。
这是我的代码,有人可以帮忙吗?
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();
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();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我正在执行此操作的代码是正确的。
问题不在于代码,而在于版本 1.2 生成 pdf 的方式。
我需要知道如何更改 V 1.2 的 PDF
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
请使用/更新下面的代码
它对我有用
please use/update below code
Its working for me