我想在PDF内容的句子中添加一个单词.
例如:
This is a sample content.
Run Code Online (Sandbox Code Playgroud)
我想在这个输出中插入一个单词.
This is a nice sample content.
Run Code Online (Sandbox Code Playgroud)
这是我在互联网上找到的itextPdf示例代码.假设内容已经存在,我们想通过在句子中添加文本来修改它.
try {
//Create PdfReader instance.
PdfReader pdfReader =
new PdfReader(SRC);
//Create PdfStamper instance.
PdfStamper pdfStamper = new PdfStamper(pdfReader,
new FileOutputStream(DEST));
//Create BaseFont instance.
BaseFont baseFont = BaseFont.createFont(
BaseFont.TIMES_ROMAN,
BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
//Get the number of pages in pdf.
int pages = pdfReader.getNumberOfPages();
System.out.println(pdfStamper.getOverContent(1));
//Iterate the pdf through pages.
for(int i=1; i<=pages; i++) {
//Contain the pdf data.
PdfContentByte pageContentByte =
pdfStamper.getOverContent(i);
pageContentByte.setFlatness(89);
pageContentByte.beginText();
//Set text font and size.
pageContentByte.setFontAndSize(baseFont, 14);
pageContentByte.setTextMatrix(50, 720);
//Write text
pageContentByte.setWordSpacing(12);
pageContentByte.showText("hello world");
pageContentByte.endText();
}
//Close the pdfStamper.
pdfStamper.close();
System.out.println("PDF modified successfully.");
} catch (Exception e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
我尝试了itextPdf和PdfBox,但它们都不起作用.
我可以使用pdfbox的PDFStreamParser获取pdf文档中的对象.
PDFOperator{Td}, COSArray{[COSString{Name }, COSFloat{163.994}, COSString{____________________________________________________}, COSFloat{-8.03223}, COSString{________________________________________________________}]}, PDFOperator{TJ}, COSInt{19}, PDFOperator{TL}, PDFOperator{T*}, COSArray{[COSString{T}, COSInt{36}, COSString{itle}, COSFloat{0.997925}, COSString{ }, COSFloat{-94.9982}, COSString{_____________________________________________________________________________________________________________}]}, PDFOperator{TJ}, PDFOperator{T*}, COSArray{[
Run Code Online (Sandbox Code Playgroud)
如何实现插入文本的代码?
不.
Pdf不是一种所见即所得格式.在内部,它更像是包含代码的文件.它包含在光标周围移动,以及在光标顶端绘制文本和图形的说明.
然后就是大多数指令被打包成"对象"的事实.所有对象都放在一个字典中,该字典使用字节偏移来引用它们.
因此,在pdf文档中插入任何内容都会导致2个级别的问题.
因此,我的简短回答.你不能.这立即解释了为什么你尝试过的所有pdf工具包都无法做到.这简直是一项艰巨的任务.
| 归档时间: |
|
| 查看次数: |
309 次 |
| 最近记录: |