iText 5(用于创建PDF):
1 2 3 4 5 6 <dependency> <groupId>com.itextpdf</groupId> <artifactId>itextpdf</artifactId> <version>5.5.13.2</version> <!-- 或者其他版本 --> </dependency>
具体代码:
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 import com.itextpdf.text.Document; import com.itextpdf.text.PageSize; import com.itextpdf.text.Paragraph; import com.itextpdf.text.Element; import com.itextpdf.text.Font; import com.itextpdf.text.pdf.BaseFont; import com.itextpdf.text.pdf.PdfPTable; import com.itextpdf.text.pdf.PdfPCell; import com.itextpdf.text.pdf.PdfWriter; import com.itextpdf.text.DocumentException; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import org.springframework.web.multipart.MultipartFile; String dest = "D:/upload/" + IdUtils.simpleUUID() + ".pdf" ; File dir = new File (dest).getParentFile();if (!dir.exists()) { dir.mkdirs(); } Document document = new Document (PageSize.A4, 50 , 50 , 50 , 50 );try { PdfWriter.getInstance(document, new FileOutputStream (dest)); document.open(); BaseFont bfChinese = BaseFont.createFont("字体路径" , BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); Font font = new Font (bfChinese, 10 , Font.NORMAL); Font boldFont = new Font (bfChinese, 10 , Font.BOLD); Paragraph title = new Paragraph ("标题" , boldFont); title.setAlignment(Element.ALIGN_CENTER); document.add(title); PdfPTable titleTable = new PdfPTable (3 ); titleTable.setWidthPercentage(100 ); titleTable.setSpacingBefore(20f ); titleTable.setSpacingAfter(20f ); float [] columnWidths = {2f , 2f , 2f }; titleTable.setWidths(columnWidths); PdfPCell cellSchool = new PdfPCell (new Paragraph ("名称" , font)); cellSchool.setHorizontalAlignment(Element.ALIGN_LEFT); cellSchool.setBorder(Rectangle.NO_BORDER); cellSchool.setPaddingTop(10f ); titleTable.addCell(cellSchool); document.add(titleTable); PdfPTable table = new PdfPTable (2 ); table.setWidthPercentage(100 ); table.setSpacingBefore(20f ); table.setWidths(new float []{1.5f , 3.5f }); addRowToTable(table, new ArrayList <>(), font); document.add(table); document.newPage(); System.out.println("PDF created and saved to: " + dest); } catch (DocumentException | IOException e) { e.printStackTrace(); System.err.println("Error creating or saving the PDF file." ); } finally { document.close(); }
addRowToTable:
1 2 3 4 5 6 7 8 private static void addRowToTable (PdfPTable table, List<String> cells, Font font) { for (String cellContent : cells) { PdfPCell cell = new PdfPCell (new Phrase (cellContent, font)); cell.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(cell); } }
加水印
调用方法(按自己需求调整字体大小,也就是50这个参数)
1 2 3 4 5 6 7 8 9 10 BaseFont baseFont=BaseFont.createFont("字体路径" , BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream (dest));document.open(); PdfContentByte waterMarkContent = writer.getDirectContentUnder();addWatermark(waterMarkContent, "水印内容" , baseFont, 50 , 45 );
部分水印
1 2 3 4 5 6 7 8 9 10 11 12 private static void addWatermark (PdfContentByte content, String text, BaseFont baseFont, float fontSize, float rotation) throws DocumentException, IOException { content.beginText(); content.setFontAndSize(baseFont, fontSize); content.setTextMatrix(0 , -rotation); content.setGrayFill(0.75f ); content.showTextAligned(Element.ALIGN_CENTER, text, 298 , 421 , 45 ); content.endText(); }
铺满水印
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 private void addWatermark (PdfContentByte content, String text, BaseFont baseFont, float fontSize, float rotation) throws DocumentException { content.beginText(); content.setFontAndSize(baseFont, fontSize); content.setTextMatrix(0 , -rotation); PdfGState gs = new PdfGState (); gs.setFillOpacity(0.3f ); content.setGState(gs); Rectangle pageSize = content.getPdfWriter().getPageSize(); float x = 50 ; float y = 50 ; float stepX = 200 ; float stepY = 200 ; while (y < pageSize.getHeight()) { x = 50 ; while (x < pageSize.getWidth()) { content.showTextAligned(Element.ALIGN_CENTER, text, x, y, rotation); x += stepX; } y += stepY; } content.endText(); }