说明
iText 是Java 操作 PDF 的工具,提供编辑 PDF,包括添加文字、图片等功能
maven 依赖
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| <dependency> <groupId>com.itextpdf</groupId> <artifactId>itextpdf</artifactId> <version>5.5.13</version> </dependency>
<dependency> <groupId>com.itextpdf</groupId> <artifactId>itext-asian</artifactId> <version>5.2.0</version> </dependency>
<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency>
|
PDFUtil.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
| package com.example.pdfjava;
import com.itextpdf.text.Font; import com.itextpdf.text.Image; import com.itextpdf.text.pdf.BaseFont; import com.itextpdf.text.pdf.PdfContentByte; import com.itextpdf.text.pdf.PdfReader; import com.itextpdf.text.pdf.PdfStamper; import lombok.Data; import lombok.experimental.Accessors;
import java.io.FileOutputStream; import java.util.ArrayList; import java.util.List;
public class PDFUtil {
public static void main(String[] args) { String pdfPath_in = "/Users/wangwz/Downloads/1.pdf"; String pdfPath_out = "/Users/wangwz/Downloads/2.pdf"; List<ImageParams> imageParams = new ArrayList<>(2); imageParams.add(new ImageParams().setPage(1).setType(0).setX(200f).setY(300f).setImageFilePath("/Users/wangwz/Downloads/1.jpg")); imageParams.add(new ImageParams().setPage(2).setType(1).setX(100f).setY(400f).setImageFilePath("/Users/wangwz/Downloads/3.png")); generatePDF(pdfPath_in, pdfPath_out, imageParams); }
public static final Integer IMAGEWIDTH = 64; public static final Integer IMAGEHEIGHT = 90;
public static void generatePDF(String pdfPath_in, String pdfPath_out, List<ImageParams> imageParams) { PdfReader reader = null; PdfStamper stamper = null; try { reader = new PdfReader(pdfPath_in); stamper = new PdfStamper(reader, new FileOutputStream(pdfPath_out)); BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); Font font = new Font(bf, 10); font.setStyle(Font.BOLD); font.getBaseFont(); PdfContentByte over;
for (ImageParams imageParam : imageParams) { over = stamper.getOverContent(imageParam.getPage());
Image image = Image.getInstance(imageParam.getImageFilePath()); image.setAbsolutePosition(imageParam.getX(), imageParam.getY()); float width = image.getWidth(); System.out.println("width = " + width); float height = image.getHeight(); System.out.println("height = " + height); image.scaleToFit(width, height); over.addImage(image); }
stamper.close(); reader.close(); } catch (Exception e) { e.printStackTrace(); } }
public static void mergePDF(String[] files, String outputPath) { String sep = java.io.File.separator; Document document = null; PdfCopy copy = null; PdfReader reader = null; try { document = new Document(new PdfReader(files[0]).getPageSize(1)); copy = new PdfCopy(document, new FileOutputStream(outputPath)); document.open(); for (int i = 0; i < files.length; i++) { reader = new PdfReader(files[i]); int numberOfPages = reader.getNumberOfPages(); for (int j = 1; j <= numberOfPages; j++) { document.newPage(); PdfImportedPage page = copy.getImportedPage(reader, j); copy.addPage(page); } } } catch (IOException e) { } catch (DocumentException e) { } finally { if (document != null) document.close(); if (reader != null) reader.close(); if (copy != null) copy.close(); } } }
@Accessors(chain = true) @Data class ImageParams { private Float x; private Float y; private Integer page; private Integer type; private String picdata; private String imageFilePath; }
|