java 绘制pdf_java生成PDF文件

该博客介绍了如何使用Java的iText库来创建PDF文件,包括设置文档尺寸、处理中文显示、创建表格、设置单元格样式以及插入图片等内容,最后展示了生成的PDF文件包含了表格和图片。
摘要由CSDN通过智能技术生成

1 importjava.awt.Color;2 importjava.io.File;3 importjava.io.FileOutputStream;4 importjava.io.IOException;5 importjava.util.ArrayList;6 importjava.util.List;7

8 importcom.lowagie.text.Cell;9 importcom.lowagie.text.Document;10 importcom.lowagie.text.DocumentException;11 importcom.lowagie.text.Element;12 importcom.lowagie.text.Font;13 importcom.lowagie.text.Image;14 importcom.lowagie.text.PageSize;15 importcom.lowagie.text.Paragraph;16 importcom.lowagie.text.Table;17 importcom.lowagie.text.pdf.BaseFont;18 importcom.lowagie.text.pdf.PdfPCell;19 importcom.lowagie.text.pdf.PdfPTable;20 importcom.lowagie.text.pdf.PdfWriter;21

22

23 public classMySelf {24

25 /**

26 *@paramargs27 *@throwsIOException28 *@throwsDocumentException29 */

30 public static void main(String[] args) throwsDocumentException, IOException {31 //TODO Auto-generated method stub

32 float head= (float)50.0;33 float lineHeight1 = (float)50.0;34 float lineHeight2 = (float)50.0;35 float lineHeight3 = (float)51.0;36 //创建文档,设置页面大小, 左、右、上和下页边距。

37 Document document = new Document(PageSize.A4, 10, 10, 50, 50);38 //处理中文显示问题,使用计算机自带字体

39 BaseFont bfChinese=BaseFont.createFont("c://windows//fonts//simkai.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);40 //方法二:使用iTextAsian.jar中的字体41 //BaseFont baseFont = BaseFont.createFont("STSong-Light",BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);42

43 //方法三:使用资源字体(ClassPath)

44 BaseFont baseFont = BaseFont.createFont("/SIMYOU.TTF",BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);

45

46 Font headfont=new Font(bfChinese,10,Font.BOLD);//设置字体大小 样式

47 Font keyfont=new Font(bfChinese,12,Font.BOLD);//文字加粗

48 Font title=new Font(bfChinese,18,Font.BOLD);//文字加粗

49 Font textfont=new Font(bfChinese,16,Font.NORMAL);//正常文字50

51 //document是创建的文档,FileOutputStream是向文档中输入

52 PdfWriter.getInstance(document, new FileOutputStream(new File("D:\\MySelf.pdf")));53 //打开文档

54 document.open();55 //开始生成一个2列的表格

56 PdfPTable table=new PdfPTable(2);57 PdfPTable table1=new PdfPTable(6);58 //定义每个单元格的宽度

59 float[] widthsHeader={20f,5f};60 float[] widthsHeade1={3f,3f,3f,4f,3f,6f};61 //设置表格每一各的宽度

62 table.setWidths(widthsHeader);63 table1.setWidths(widthsHeade1);64 //设置边距65 //设置单元格间距66 //table.setSpacing(1f);67 //设置表格的总体宽度

68 table.setWidthPercentage(100);69

70

71 //如果表格格式一样采用遍历的方式读取添加

72 /*List list=new ArrayList();73 list.add("姓名");74 list.add("姓名");75 list.add("姓名");76 list.add("姓名");77 list.add("姓名");78 list.add("姓名");79 list.add("姓名");80 for(int i=0;i

91 }92 document.add(table);93 document.close();94 System.out.println("表格创建成功");95 return;*/

96

97 //单元格对象

98 PdfPCell cell=null;99 //设置单元格内容

100 cell=new PdfPCell(new Paragraph("文档标题栏",title));101 //合并单元格列

102 cell.setColspan(2);103 //设置垂直居中

104 cell.setVerticalAlignment(Element.ALIGN_MIDDLE);105 //设置水平居中

106 cell.setHorizontalAlignment(Element.ALIGN_CENTER);107 cell.setFixedHeight(head);108 //将单元格内容添加到表格中去

109 table.addCell(cell);110

111

112

113 cell=new PdfPCell(new Paragraph("A",keyfont));114 //设置单元格背景颜色

115 cell.setBackgroundColor(Color.lightGray);116 //设置水平居中

117 cell.setHorizontalAlignment(Element.ALIGN_CENTER);118 //设置垂直居中

119 cell.setVerticalAlignment(Element.ALIGN_MIDDLE);120 cell.setFixedHeight(lineHeight1);121 table1.addCell(cell);122

123 cell=new PdfPCell(new Paragraph("B",keyfont));124 cell.setHorizontalAlignment(Element.ALIGN_CENTER);125 cell.setVerticalAlignment(Element.ALIGN_MIDDLE);126 cell.setFixedHeight(lineHeight1);127 table1.addCell(cell);128 cell=new PdfPCell(new Paragraph("C",keyfont));129 cell.setBackgroundColor(Color.lightGray);130 cell.setHorizontalAlignment(Element.ALIGN_CENTER);131 cell.setVerticalAlignment(Element.ALIGN_MIDDLE);132 cell.setFixedHeight(lineHeight1);133 table1.addCell(cell);134 cell=new PdfPCell(new Paragraph("D",keyfont));135 cell.setHorizontalAlignment(Element.ALIGN_CENTER);136 cell.setVerticalAlignment(Element.ALIGN_MIDDLE);137 cell.setFixedHeight(lineHeight1);138 table1.addCell(cell);139 cell=new PdfPCell(new Paragraph("E",keyfont));140 cell.setBackgroundColor(Color.lightGray);141 cell.setHorizontalAlignment(Element.ALIGN_CENTER);142 cell.setVerticalAlignment(Element.ALIGN_MIDDLE);143 cell.setFixedHeight(lineHeight1);144 table1.addCell(cell);145 cell=new PdfPCell(new Paragraph("F",keyfont));146 cell.setHorizontalAlignment(Element.ALIGN_CENTER);147 cell.setVerticalAlignment(Element.ALIGN_MIDDLE);148 cell.setFixedHeight(lineHeight1);149 table1.addCell(cell);150

151 cell=new PdfPCell(new Paragraph("G",keyfont));152 cell.setBackgroundColor(Color.lightGray);153 cell.setHorizontalAlignment(Element.ALIGN_CENTER);154 cell.setVerticalAlignment(Element.ALIGN_MIDDLE);155 cell.setFixedHeight(lineHeight2);156 table1.addCell(cell);157 cell=new PdfPCell(new Paragraph("H",keyfont));158 cell.setHorizontalAlignment(Element.ALIGN_CENTER);159 cell.setVerticalAlignment(Element.ALIGN_MIDDLE);160 cell.setFixedHeight(lineHeight2);161 table1.addCell(cell);162 cell=new PdfPCell(new Paragraph("I",keyfont));163 cell.setBackgroundColor(Color.lightGray);164 cell.setHorizontalAlignment(Element.ALIGN_CENTER);165 cell.setVerticalAlignment(Element.ALIGN_MIDDLE);166 cell.setFixedHeight(lineHeight2);167 table1.addCell(cell);168 cell=new PdfPCell(new Paragraph("J",keyfont));169 cell.setHorizontalAlignment(Element.ALIGN_CENTER);170 cell.setVerticalAlignment(Element.ALIGN_MIDDLE);171 cell.setFixedHeight(lineHeight2);172 table1.addCell(cell);173 cell=new PdfPCell(new Paragraph("K",keyfont));174 cell.setBackgroundColor(Color.lightGray);175 cell.setHorizontalAlignment(Element.ALIGN_CENTER);176 cell.setVerticalAlignment(Element.ALIGN_MIDDLE);177 cell.setFixedHeight(lineHeight2);178 table1.addCell(cell);179 cell=new PdfPCell(new Paragraph("L",keyfont));180 cell.setHorizontalAlignment(Element.ALIGN_CENTER);181 cell.setVerticalAlignment(Element.ALIGN_MIDDLE);182 cell.setFixedHeight(lineHeight2);183 table1.addCell(cell);184

185 cell=new PdfPCell(new Paragraph("M",keyfont));186 cell.setBackgroundColor(Color.lightGray);187 cell.setHorizontalAlignment(Element.ALIGN_CENTER);188 cell.setVerticalAlignment(Element.ALIGN_MIDDLE);189 cell.setFixedHeight(lineHeight3);190 table1.addCell(cell);191 cell=new PdfPCell(new Paragraph("N",keyfont));192 cell.setHorizontalAlignment(Element.ALIGN_CENTER);193 cell.setVerticalAlignment(Element.ALIGN_MIDDLE);194 cell.setFixedHeight(lineHeight3);195 table1.addCell(cell);196 cell=new PdfPCell(new Paragraph("O",keyfont));197 cell.setBackgroundColor(Color.lightGray);198 cell.setHorizontalAlignment(Element.ALIGN_CENTER);199 cell.setVerticalAlignment(Element.ALIGN_MIDDLE);200 cell.setFixedHeight(lineHeight3);201 table1.addCell(cell);202 cell=new PdfPCell(new Paragraph("P",keyfont));203 cell.setHorizontalAlignment(Element.ALIGN_CENTER);204 cell.setVerticalAlignment(Element.ALIGN_MIDDLE);205 cell.setFixedHeight(lineHeight3);206 table1.addCell(cell);207 cell=new PdfPCell(new Paragraph("Q",keyfont));208 cell.setBackgroundColor(Color.lightGray);209 cell.setHorizontalAlignment(Element.ALIGN_CENTER);210 cell.setVerticalAlignment(Element.ALIGN_MIDDLE);211 cell.setFixedHeight(lineHeight3);212 table1.addCell(cell);213 cell=new PdfPCell(new Paragraph("R",keyfont));214 cell.setHorizontalAlignment(Element.ALIGN_CENTER);215 cell.setVerticalAlignment(Element.ALIGN_MIDDLE);216 cell.setFixedHeight(lineHeight3);217 table1.addCell(cell);218

219 PdfPCell pdfpcell=newPdfPCell(table1);220 pdfpcell.setPadding(0);221 table.addCell(pdfpcell);222 //加载图片添加到指定位置

223 Image image = Image.getInstance ("C:/Users/admin/Desktop/日志文件/IMG_0696.JPG");224 float height=image.height();225 float width=image.width();226 System.out.println(height);227 System.out.println(width);228

229 cell=newPdfPCell(image);230 cell.setHorizontalAlignment(Element.ALIGN_CENTER);231 cell.setVerticalAlignment(Element.ALIGN_MIDDLE);232 cell.setFixedHeight(lineHeight1+lineHeight2+lineHeight3);233 table.addCell(cell);234

235 document.add(table);236 document.close();237

238

239 }240

241 }242

243 生成PDF代码

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的示例代码,用于生成并打印 PDF 文件: ```java import java.io.FileOutputStream; import com.itextpdf.text.Document; import com.itextpdf.text.Paragraph; import com.itextpdf.text.pdf.PdfWriter; import java.awt.print.PrinterJob; import javax.print.PrintService; public class PDFGenerator { public static void main(String[] args) { try { // 创建文档对象 Document document = new Document(); // 设置输出路径 String outputPath = "C:/pdf/demo.pdf"; PdfWriter.getInstance(document, new FileOutputStream(outputPath)); // 打开文档对象 document.open(); // 添加段落 document.add(new Paragraph("Hello World!")); // 关闭文档对象 document.close(); // 输出提示信息 System.out.println("PDF文件生成并保存到:" + outputPath); // 打印PDF文件 printPDF(outputPath); } catch (Exception e) { e.printStackTrace(); } } // 打印PDF文件 public static void printPDF(String filePath) throws Exception { // 获取打印机服务 PrintService printService = PrinterJob.getPrinterJob().getPrintService(); // 创建打印任务 PrinterJob job = PrinterJob.getPrinterJob(); // 设置打印机 job.setPrintService(printService); // 设置打印文件 job.setPrintable(new PDFPrintable(filePath)); // 执行打印任务 job.print(); } } // PDF打印器 class PDFPrintable implements Printable { private PDDocument document; public PDFPrintable(String filePath) throws IOException { // 加载PDF文件 this.document = PDDocument.load(new File(filePath)); } @Override public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException { // 获取PDF页码 int pageCount = document.getNumberOfPages(); if (pageIndex >= pageCount) { return NO_SUCH_PAGE; } // 获取PDF页面 PDPage page = document.getPage(pageIndex); // 创建Graphics2D对象 Graphics2D graphics2D = (Graphics2D) graphics; // 获取打印区域大小 Rectangle2D.Double rect = new Rectangle2D.Double( pageFormat.getImageableX(), pageFormat.getImageableY(), pageFormat.getImageableWidth(), pageFormat.getImageableHeight()); // 渲染PDF页面 PDFRenderer renderer = new PDFRenderer(page.getDocument()); BufferedImage image = renderer.renderImage(pageIndex, 1.0f); // 绘制PDF页面 graphics2D.drawImage(image, null, (int) rect.x, (int) rect.y); // 返回打印结果 return PAGE_EXISTS; } } ``` 这个示例使用了 iText 库来生成 PDF 文件,使用了 Java 的打印机 API 来打印 PDF 文件。示例中首先创建了一个文档对象,然后使用 PdfWriter 将其输出到指定的本地目录,接着添加了一个段落,最后关闭文档对象。示例中使用了字符串类型的 outputPath 变量来指定生成 PDF 文件的路径,你可以根据自己的需求修改该变量的值,指定生成 PDF 文件的具体路径。 当程序运行完成后,生成PDF 文件会自动保存到指定的本地目录中,并输出一条提示信息。接着,程序调用了 printPDF 方法,该方法使用了 Java 的打印机 API 来打印 PDF 文件。printPDF 方法中首先获取了系统中的打印机服务,然后创建了一个打印任务对象,设置了打印机和打印文件,最后调用了打印任务的 print 方法来执行打印任务。 注意,如果你要使用这个示例代码来打印 PDF 文件,你需要先在系统中安装一个 PDF 打印机服务,例如 Adobe PDF 或者 Foxit Reader PDF Printer 等,否则程序会抛出异常。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值