首页 > 编程知识 正文

pdf修改文本,java修改pdf

时间:2023-05-03 21:38:12 阅读:221330 作者:3443

小编xxdzjy

您可以使用iText做到这一点。我用以下代码对其进行了测试。它会在现有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 yydcc/p>

PdfStamper stamper = new PdfStamper(reader,

new FileOutputStream("C:/temp/Bubi_modified.pdf")); // output yydcc/p>

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

// 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();

}

}

2020-09-21

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。