itext生成pdf 博客分类: codes
程序员文章站
2024-02-12 20:12:46
...
package pdf; import java.awt.Color; import java.io.FileOutputStream; import java.io.IOException; import com.lowagie.text.Chunk; import com.lowagie.text.Document; import com.lowagie.text.DocumentException; import com.lowagie.text.Element; import com.lowagie.text.Font; import com.lowagie.text.Image; import com.lowagie.text.PageSize; import com.lowagie.text.Paragraph; import com.lowagie.text.Phrase; import com.lowagie.text.Rectangle; import com.lowagie.text.pdf.BaseFont; import com.lowagie.text.pdf.PdfAnnotation; import com.lowagie.text.pdf.PdfContentByte; import com.lowagie.text.pdf.PdfPCell; import com.lowagie.text.pdf.PdfPTable; import com.lowagie.text.pdf.PdfWriter; import com.lowagie.text.pdf.RadioCheckField; /** * * * @author ht * */ public class CreatePdfTest5 { public static void main(String[] args) throws DocumentException, IOException { // 第一步,实例化一个document对象 Document document = new Document(PageSize.A4); //document. // 第二步,设置要到出的路径 FileOutputStream out = new FileOutputStream("D:/workbook111.pdf"); // 如果是浏览器通过request请求需要在浏览器中输出则使用下面方式 // OutputStream out = response.getOutputStream(); // 第三步,设置字符 BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", false); Font fontZH = new Font(bfChinese, 12.0F, 0); Font headFont = new Font(bfChinese, 15, Font.BOLD); // 第四步,将pdf文件输出到磁盘 PdfWriter writer = PdfWriter.getInstance(document, out); writer.setEncryption(null, null, PdfWriter.ALLOW_PRINTING, PdfWriter.STANDARD_ENCRYPTION_128); // 只读 // 第五步,打开生成的pdf文件 document.open(); // 第六步,设置内容 Rectangle position = new Rectangle(220, 740, 240, 760); drawCheckBox(writer, position, "0", false); position = new Rectangle(280, 740, 300, 760); drawCheckBox(writer, position, "1", true); String title = "标题"; Paragraph titleParagraph = new Paragraph(new Chunk(title, headFont).setLocalDestination(title)); titleParagraph.setAlignment(Element.ALIGN_CENTER); document.add(titleParagraph); document.add(new Paragraph("\n")); String subTitle = "国籍:中国 其他 填表日期: 年 月 日"; Chunk k = new Chunk("kk", fontZH); Paragraph subTitleParagraph = new Paragraph(new Chunk(subTitle, fontZH).setLocalDestination(subTitle)); subTitleParagraph.setAlignment(Element.ALIGN_CENTER); document.add(subTitleParagraph); document.add(new Paragraph("\n")); // 创建table,注意这里的2是两列的意思,下面通过table.addCell添加的时候必须添加整行内容的所有列 float[] widths = new float[] {0.15f, 0.15f, 0.1f, 0.1f, 0.1f, 0.2f, 0.2f}; PdfPTable table1 = new PdfPTable(widths); table1.setWidthPercentage(100.0f); PdfPCell cell = new PdfPCell(); table1.addCell(getPdfCell("姓名", fontZH)); table1.addCell(getPdfCell("张三", fontZH)); table1.addCell(getPdfCell("性别", fontZH)); table1.addCell(getPdfCell("男", fontZH)); table1.addCell(getPdfCell("出生年月", fontZH)); table1.addCell(getPdfCell("203232323", fontZH)); cell = new PdfPCell(); cell.setRowspan(4); Image img = Image.getInstance("F:/001_ibatis 时序图.png"); cell.setImage(img); table1.addCell(cell); table1.addCell(getPdfCell("张三", fontZH)); table1.addCell(getPdfCell("张三", fontZH)); table1.addCell(getPdfCell("张三", fontZH)); table1.addCell(getPdfCell("张三", fontZH)); table1.addCell(getPdfCell("张三", fontZH)); table1.addCell(getPdfCell("张三", fontZH)); table1.addCell(getPdfCell("张三", fontZH)); table1.addCell(getPdfCell("张三", fontZH)); table1.addCell(getPdfCell("张三", fontZH)); table1.addCell(getPdfCell("张三", fontZH)); // table2 widths = new float[] {0.15f, 0.25f, 0.2f, 0.4f}; PdfPTable table2 = new PdfPTable(widths); table2.setWidthPercentage(100.0f); table2.addCell(getPdfCell("电商名称", fontZH)); table2.addCell(getPdfCell("客户量", fontZH)); table2.addCell(getPdfCell("电商名称", fontZH)); table2.addCell(getPdfCell("电商名称", fontZH)); table2.addCell(getPdfCell("简介", fontZH, 120.0f)); table2.addCell(getPdfCell("实体", fontZH, 1, 3)); cell = getPdfCell("计划书", fontZH, 1, 4); cell.setUseAscender(true); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setVerticalAlignment(Element.ALIGN_CENTER); table2.addCell(cell); // 公司简介 Paragraph dataPara = new Paragraph(); dataPara.add(new Phrase("dfjlkasjdfa1,", fontZH)); dataPara.add(new Phrase("\n")); dataPara.add(new Phrase("fdsfadewrwerwe2,", fontZH)); dataPara.add(new Phrase("\n")); Paragraph datePara = new Paragraph(); datePara.add(new Paragraph("\n\n\n\n\n\n")); datePara.add(new Paragraph(" 2018年9月9日", fontZH)); dataPara.add(datePara); cell = new PdfPCell(); cell.setRowspan(1); cell.setColspan(7); cell.setPhrase(dataPara); table2.addCell(cell); document.add(table1); document.add(table2); document.add(new Paragraph("\n")); // 第七步,关闭document document.close(); } private static RadioCheckField drawCheckBox(PdfWriter writer, Rectangle position, String fieldName, boolean flag) { float x = (position.getLeft() + position.getRight()) / 2; float y = (position.getTop() + position.getBottom()) / 2; // define the position of a check box that measures 20 by 20 //画勾 Rectangle rect = new Rectangle(x - 5, y - 5, x + 5, y + 5); RadioCheckField checkbox = new RadioCheckField(writer, rect, fieldName, "On"); checkbox.setCheckType(RadioCheckField.TYPE_CHECK); if (flag) { checkbox.setChecked(true); } else { checkbox.setChecked(false); } /* * checkbox.setBorderWidth(1.0f); checkbox.setBackgroundColor(Color.WHITE); */ //画框 PdfContentByte canvas = writer.getDirectContent(); canvas.setColorStroke(Color.BLACK); //canvas.setLineDash(1f); canvas.setLineWidth(0.5f); canvas.rectangle(x - 5, y - 5, 10,10); canvas.stroke(); try { checkbox.getRadioField().setHighlighting(PdfAnnotation.HIGHLIGHT_INVERT); writer.addAnnotation(checkbox.getCheckField()); } catch (Exception e) { e.printStackTrace(); } return checkbox; } private static PdfPCell getPdfCell(String data, Font font) { PdfPCell cell = new PdfPCell(); cell.setPhrase(new Paragraph(data, font)); return cell; } private static PdfPCell getPdfCell(String data, Font font, Float height) { PdfPCell cell = new PdfPCell(); cell.setPhrase(new Paragraph(data, font)); cell.setFixedHeight(height); return cell; } private static PdfPCell getPdfCell(String data, Font font, Integer rowspan, Integer colspan) { PdfPCell cell = new PdfPCell(); cell.setPhrase(new Paragraph(data, font)); cell.setRowspan(rowspan); cell.setColspan(colspan); return cell; } }
参考:
https://blog.csdn.net/makang456/article/details/70161037
推荐阅读
-
String与Long类型比较的陷阱 博客分类: codes
-
itext生成pdf 博客分类: codes
-
String与Long类型比较的陷阱 博客分类: codes
-
钢炮级 持久层 —— 中篇 博客分类: 持久层 javaVO实体类代码自动生成
-
mybatis oracle生成java实体类 博客分类: java mybatisoracle实体类中文注释
-
java进行pdf解析-----pdfbox 博客分类: 技术 Javalucene编程FlashIDEA
-
java 通过 list
- 实现 json 生成 博客分类: javajson java实现json拼接
-
MSSQL对表自动生成带insert语句的建表语句 博客分类: MSSQL SQL Server
-
Windows上安装OpenSSL和生成证书 博客分类: java
-
Eclipse 根据ASP.NET WSDL自动生成Axis的WS客户端 博客分类: WebService的开发应用 ASPNETDatasetWebServiceSOAPAxis