import com.itextpdf.text.*; import com.itextpdf.text.pdf.*; import java.io.ByteArrayOutputStream; import java.io.IOException; public class PDFUtil { /** * * @param pdfData * @param pageIndex * @param x * @param y * @param text * @return * @throws IOException * @throws DocumentException */ public static byte[] pdfWriteText(byte[] pdfData, int pageIndex, int x, int y, String text, float textSize) throws IOException, DocumentException { PdfReader reader = new PdfReader(pdfData); ByteArrayOutputStream baos = new ByteArrayOutputStream(); PdfStamper stamper = new PdfStamper(reader, baos); BaseFont baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", false); Font font = new Font(baseFont, textSize); pageIndex = handlerPageSize(reader.getNumberOfPages(), pageIndex); PdfContentByte over = stamper.getOverContent(pageIndex); ColumnText columnText = new ColumnText(over); // llx 和 urx 最小的值决定离左边的距离. lly 和 ury 最大的值决定离下边的距离 //预留一些长度 防止越界被截掉 columnText.setSimpleColumn(x, y, x + (text.length() * textSize) + 8, y + textSize + 8); Paragraph elements = new Paragraph(0, new Chunk(text)); // 设置字体,如果不设置添加的中文将无法显示 elements.setFont(font); columnText.addElement(elements); columnText.go(); stamper.close(); return baos.toByteArray(); } /** * 将 index 转为从1开始,防止越界(通过循环的方式解决),并且解析负数 * @param total * @param index 0 表示页数不存在 * @return */ public static int handlerPageSize(int total, int index) { if(total < 1) { return 0; } else if(index == 0) { return 1; } else if(index < 0) { index = index + total + 1; } int r = index % total; return r == 0 ? total : r; } /** * 多区间 * @param pdfData * @param pages int [] size must is 2 else ignore * @return * @throws IOException * @throws DocumentException */ public static byte[] splitPDFFile(byte[] pdfData, int [][] pages) throws IOException, DocumentException { PdfReader reader = new PdfReader(pdfData); int total = reader.getNumberOfPages(); Document document = new Document(reader.getPageSize(1)); ByteArrayOutputStream b = new ByteArrayOutputStream(); PdfCopy copy = new PdfCopy(document, b); document.open(); for(int[] index : pages) { if(index == null || index.length != 2) { continue; } int start = handlerPageSize(total, index[0]); int end = handlerPageSize(total, index[1]); for(int i = start; i <= end; i++) { document.newPage(); PdfImportedPage page = copy.getImportedPage(reader, i); copy.addPage(page); } } document.close(); return b.toByteArray(); } /** * 多零散页 * @param pdfData * @param pages * @return * @throws IOException * @throws DocumentException */ public static byte[] splitPDFFile(byte[] pdfData, int [] pages) throws IOException, DocumentException { PdfReader reader = new PdfReader(pdfData); int total = reader.getNumberOfPages(); Document document = new Document(reader.getPageSize(1)); ByteArrayOutputStream b = new ByteArrayOutputStream(); PdfCopy copy = new PdfCopy(document, b); document.open(); for(int index : pages) { document.newPage(); PdfImportedPage page = copy.getImportedPage(reader, handlerPageSize(total, index)); copy.addPage(page); } document.close(); return b.toByteArray(); } /** * 截取pdfFile的第from页至第end页(注意是一个闭区间),组成一个新的文件 * @param pdfData * @param from (从1开始) 负数表示从最后一页算起 * @param end 负数表示从最后一页算起 * @return * @throws IOException * @throws DocumentException */ public static byte[] splitPDFFile(byte[] pdfData, int from, int end) throws IOException, DocumentException { PdfReader reader = new PdfReader(pdfData); int total = reader.getNumberOfPages(); //保证从1开始,并且解析负数。 from = handlerPageSize(total, from); end = handlerPageSize(total, end); Document document = new Document(reader.getPageSize(1)); ByteArrayOutputStream b = new ByteArrayOutputStream(); PdfCopy copy = new PdfCopy(document, b); document.open(); for(int j = from; j <= end; j++) { document.newPage(); PdfImportedPage page = copy.getImportedPage(reader, j); copy.addPage(page); } document.close(); return b.toByteArray(); } }
<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>