com.itextpdf itextpdf 5.5.13 com.itextpdf html2pdf 3.0.0 com.itextpdf io 7.1.11 com.itextpdf kernel 7.1.11 bcprov-jdk15on org.bouncycastle com.itextpdf layout 7.1.11 com.itextpdf.tool xmlworker 5.5.11 org.xhtmlrenderer flying-saucer-pdf 9.1.5 org.xhtmlrenderer flying-saucer-pdf-itext5 9.1.5 itextpdf com.itextpdf org.apache.velocity velocity 1.7
@Test
public void test01 () throws IOException, DocumentException {
VelocityContext context = new VelocityContext();
context.put("name","cc");
context.put("mobile","中文111");
Properties p = new Properties();
p.setProperty("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
p.setProperty(VelocityEngine.INPUT_ENCODING,"utf-8");
p.setProperty(VelocityEngine.OUTPUT_ENCODING,"utf-8");
Velocity.init(p);
Template t = Velocity.getTemplate("hehe.vm", "utf-8");
StringWriter writer = new StringWriter();
// 将context和模板内容,渲染给writer
t.merge(context,writer);
System.out.println(writer.toString());
File file = new File("E:\hehe.pdf");
FileOutputStream outputStream = new FileOutputStream(file);
Document document = new Document();
PdfWriter pdfWriter = PdfWriter.getInstance(document, outputStream);
// 将StringWriter 变成 inputStream
ByteArrayInputStream in = new ByteArrayInputStream(writer.toString().getBytes(StandardCharsets.UTF_8));
// 生成 pdf
document.open();
XMLWorkerHelper.getInstance().parseXHtml(pdfWriter,document,in);
document.close();
// XMLWorkerFontProvider
}
引用:https://blohttps://blog.csdn.net/YINLUXIA/article/details/116915071
https://gitee.com/mu-zuo/java-generation-pdf/blob/master/src/main/java/com/muzuo/pdf/service/impl/PdfServiceImpl.java
引用:https://blog.csdn.net/qq_36623327/article/details/90765050?spm=1001.2101.3001.6661.1&utm_medium=distribute.pc_relevant_t0.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7Edefault-1-90765050-blog-118081552.pc_relevant_vip_default&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7Edefault-1-90765050-blog-118081552.pc_relevant_vip_default&utm_relevant_index=1
com.google.zxing core 2.2 com.google.zxing javase 2.2 com.itextpdf itextpdf 5.5.13 com.itextpdf itext-asian 5.2.0
public class Demo {
public static void addPdfMark(String inputFile, String outputFile, List imgList) throws IOException, DocumentException {
// 获取pdf文档信息
Map pdfMsg = getPdfMsg(inputFile);
PdfReader reader = new PdfReader(inputFile);
PdfStamper stamp = new PdfStamper(reader, new FileOutputStream(outputFile));
for (String imgPath : imgList) {
Image img = Image.getInstance(imgPath);
// 设置图片位置
// img.setAbsolutePosition(0,0);
// // 写入额外内容的页码
// Integer pageSize = Integer.valueOf(pdfMsg.get("pageSize").toString());
// System.out.println("pageSize = " + pageSize);
// // 获取要附加的pdf内容 ,,在原来pdf下面追加
// PdfContentByte under = stamp.getUnderContent(pageSize);
// under.addImage(img);
PdfContentByte content = stamp.getOverContent(1);
content.beginText();
PdfGState gs = new PdfGState();
content.setGState(gs);
content.saveState();
// 从左到右,从下到上
img.setAbsolutePosition(200,400);
content.setColorFill(BaseColor.BLACK);
content.addImage(img);
content.restoreState();
content.endText();
}
stamp.close();
}
public static Map getPdfMsg(String filePath) throws IOException {
LinkedHashMap map = new LinkedHashMap<>();
PdfReader reader = new PdfReader(new FileInputStream(filePath));
// pdf有几页
int pages = reader.getNumberOfPages();
map.put("pageSize",pages);
// 获取第几页的 页面大小
Rectangle pageSize = reader.getPageSize(1);
float width = pageSize.getWidth();
float height = pageSize.getHeight();
map.put("width",width);
map.put("height",height);
System.out.println("width = " + width);
System.out.println("height = " + height);
return map;
}
public static Map getImgMsg(String imgPath) throws IOException {
LinkedHashMap map = new LinkedHashMap<>();
File picture = new File(imgPath);
BufferedImage sourceImg = ImageIO.read(picture);
map.put("width",sourceImg.getWidth());
map.put("height",sourceImg.getHeight());
return map;
}
public static void main(String[] args) throws IOException, DocumentException {
getPdfMsg("F:\hehe.pdf");
String imgurl = FileSystemView.getFileSystemView().getHomeDirectory() + File.separator + "222.png";
String inputFile = "F:\hehe.pdf";
String outputFile = "F:\1.pdf";
Map map = getImgMsg(imgurl);
System.out.println("map = " + map);
ArrayList list = new ArrayList<>();
list.add(imgurl);
addPdfMark(inputFile,outputFile,list);
}
}



