使用Java编辑PDF文本
有没有办法从Java编辑PDF?使用Java编辑PDF文本,java,pdf,Java,Pdf,有没有办法从Java编辑PDF? 我有一个PDF文档,其中包含需要使用Java替换的文本占位符,但我看到的所有库都从头创建了PDF和小编辑功能。 我可以编辑PDF吗?或者这是不可能的吗?看看,你可以用Itext进行有限的编辑,但是PDF是一种结束文件格式,所以你不能做太复杂的事情。我写了一篇文章解释了其中的一些限制:。你可以用它来做。我用下面的代码测试了它。它在现有PDF的每一页上添加一块文本和一个红色圆圈 /* requires itextpdf-5.1.2.jar or similar */
我有一个PDF文档,其中包含需要使用Java替换的文本占位符,但我看到的所有库都从头创建了PDF和小编辑功能。
我可以编辑PDF吗?或者这是不可能的吗?看看,你可以用Itext进行有限的编辑,但是PDF是一种结束文件格式,所以你不能做太复杂的事情。我写了一篇文章解释了其中的一些限制:。你可以用它来做。我用下面的代码测试了它。它在现有PDF的每一页上添加一块文本和一个红色圆圈
/* requires itextpdf-5.1.2.jar or similar */
import java.io.*;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.*;
public class AddContentToPDF {
public static void main(String[] args) throws IOException, DocumentException {
/* example inspired from "iText in action" (2006), chapter 2 */
PdfReader reader = new PdfReader("C:/temp/Bubi.pdf"); // input PDF
PdfStamper stamper = new PdfStamper(reader,
new FileOutputStream("C:/temp/Bubi_modified.pdf")); // output PDF
BaseFont bf = BaseFont.createFont(
BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED); // set font
//loop on pages (1-based)
for (int i=1; i<=reader.getNumberOfPages(); i++){
// get object for writing over the existing content;
// you can also use getUnderContent for writing in the bottom layer
PdfContentByte over = stamper.getOverContent(i);
// write text
over.beginText();
over.setFontAndSize(bf, 10); // set font and size
over.setTextMatrix(107, 740); // set x,y position (0,0 is at the bottom left)
over.showText("I can write at page " + i); // set text
over.endText();
// draw a red circle
over.setRGBColorStroke(0xFF, 0x00, 0x00);
over.setLineWidth(5f);
over.ellipse(250, 450, 350, 550);
over.stroke();
}
stamper.close();
}
}
/*需要itextpdf-5.1.2.jar或类似文件*/
导入java.io.*;
导入com.itextpdf.text.DocumentException;
导入com.itextpdf.text.pdf.*;
公共类AddContentToPDF{
公共静态void main(字符串[]args)引发IOException、DocumentException{
/*示例灵感来源于“iText in action”(2006),第2章*/
PdfReader=newpdfReader(“C:/temp/Bubi.pdf”);//输入pdf
PdfStamper压模=新PdfStamper(读卡器、,
新文件outputstream(“C:/temp/Bubi_modified.pdf”);//输出pdf
BaseFont bf=BaseFont.createFont(
BaseFont.HELVETICA,BaseFont.CP1252,BaseFont.NOT_EMBEDDED);//设置字体
//页上循环(基于1)
对于(inti=1;i看一看,这个我修改了找到的代码,它工作如下
public class Principal {
public static final String SRC = "C:/tmp/244558.pdf";
public static final String DEST = "C:/tmp/244558-2.pdf";
public static void main(String[] args) throws IOException, DocumentException {
File file = new File(DEST);
file.getParentFile().mkdirs();
new Principal().manipulatePdf(SRC, DEST);
}
public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
PdfReader reader = new PdfReader(src);
PdfDictionary dict = reader.getPageN(1);
PdfObject object = dict.getDirectObject(PdfName.CONTENTS);
PdfArray refs = null;
if (dict.get(PdfName.CONTENTS).isArray()) {
refs = dict.getAsArray(PdfName.CONTENTS);
} else if (dict.get(PdfName.CONTENTS).isIndirect()) {
refs = new PdfArray(dict.get(PdfName.CONTENTS));
}
for (int i = 0; i < refs.getArrayList().size(); i++) {
PRStream stream = (PRStream) refs.getDirectObject(i);
byte[] data = PdfReader.getStreamBytes(stream);
stream.setData(new String(data).replace("NULA", "Nulo").getBytes());
}
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
stamper.close();
reader.close();
}
公共类主体{
公共静态最终字符串SRC=“C:/tmp/244558.pdf”;
公共静态最终字符串DEST=“C:/tmp/244558-2.pdf”;
公共静态void main(字符串[]args)引发IOException、DocumentException{
文件文件=新文件(DEST);
文件.getParentFile().mkdirs();
新主体().操纵EPDF(SRC、DEST);
}
public void manipulatedpf(String src,String dest)抛出IOException,DocumentException{
PdfReader读取器=新PdfReader(src);
PdfDictionary dict=reader.getPageN(1);
PdfObject object=dict.getDirectObject(PdfName.CONTENTS);
PdfArray refs=null;
if(dict.get(PdfName.CONTENTS.isArray()){
refs=dict.getAsArray(PdfName.CONTENTS);
}else if(dict.get(PdfName.CONTENTS.isIndirect()){
refs=新的PdfArray(dict.get(PdfName.CONTENTS));
}
对于(int i=0;i
}不幸的是,此示例需要现有PDF中的字段。请参阅我的答案。此示例使用新文本和椭圆覆盖页面。是否有方法修改文本?是否有方法搜索(例如标记)并将其替换为我的文本而不是覆盖?错误…….致命信号7(SIGBUS),代码2,故障地址0xa290903f(在tid 25590(om.pdfgenerator)中)我无法编辑现有pdf中的文本。提取的文本是一组类似于“(>)Tj”的值。教程链接:@IgorG。关于“教程链接”:您肯定已经看到了JavaDoc顶部的链接。它指出Bruno特别指出,如果您的pdf相对简单,您可以使用该代码,但在现实生活中,pdf从来没有这么简单……因此,您无法编辑现有pdf中的文本,这是意料之中的!如果您只想编辑非常简单的pdf具体的文档,从中创建自己的问题并提供示例。不过,不要期望有一个通用的解决方案!嗨,在PHP中有什么方法可以做到这一点吗?两个链接都是断开的第一个链接的第二个:我还发现:这种文本替换只在非常特定的情况下有效(pdf中的字体必须使用类似ASCII的编码,并且不得仅作为不完整的子集嵌入;此外,生成器不得应用字距调整或将文本行拆分为单独块的类似技术)。此外,您还只能更改页面内容流的内容。