已阅读5页,还剩13页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
package com.trs;import java.io.FileOutputStream;import com.itextpdf.text.BaseColor;import com.itextpdf.text.Chunk;import com.itextpdf.text.Document;import com.itextpdf.text.Element;import com.itextpdf.text.Font; import com.itextpdf.text.Image;import com.itextpdf.text.PageSize;import com.itextpdf.text.Paragraph; import com.itextpdf.text.Phrase;import com.itextpdf.text.Rectangle;import com.itextpdf.text.pdf.BaseFont; import com.itextpdf.text.pdf.PdfPCell;import com.itextpdf.text.pdf.PdfPTable;import com.itextpdf.text.pdf.PdfWriter; /* * * Title: 通过itext包生成PDF文件 * * * Description: * * * Copyright: Copyright (c) 2003 * * * Filename: PDFOpr.java * * * version 1.0 */public class PDFOprpublic static void main(String args) String pdfPath = D:/test.pdf; createPDF(pdfPath);public static void createPDF(String pdfPath) /Document(Rectangle pageSize, float marginLeft, float marginRight, float marginTop, float marginBottom) Document document = new Document(PageSize.A4.rotate(), 18f, 18f, 18f, 10f); trySystem.out.println(Generating PDF);PdfWriter.getInstance(document, new FileOutputStream(pdfPath);document.open(); BaseFont bfChinese = BaseFont.createFont(STSongStd-Light, UniGB-UCS2-H, false); Font fontChinese = new Font(bfChinese); fontChinese.setSize(10f); Font fontChinese2 = new Font(bfChinese); fontChinese2.setSize(7f); float widths = 4f,260,10f,260,10f,260f,4f; /new 一个13列的table PdfPTable table = new PdfPTable(7); /设置table每一列的宽度,widths里写的是百分比,他们加和需要是1 table.setWidths(widths); /设置表格在页面上的宽度,设成100表示可以表格填满页面,但是要去掉页面margin table.setWidthPercentage(100); /设置表格上端的空白距离,类似css中的margin-top:xxpx;这样在给表格加上标题后,标题就不会跟表格重叠在一起了。 table.setSpacingBefore(3f);table.getDefaultCell().setBorder(0);/设置表格默认为无边框BaseColor bgcolor = new BaseColor(248, 248, 255); /底色灰色/第一行(begin)PdfPCell cell0 = new PdfPCell(new Paragraph(第一行表头,fontChinese);cell0.setColspan(7); cell0.setFixedHeight(20); cell0.setPadding(0);cell0.setHorizontalAlignment(Element.ALIGN_CENTER);/ 设置内容水平居中显示cell0.setVerticalAlignment(Element.ALIGN_BOTTOM); cell0.setBorder(Rectangle.NO_BORDER); cell0.setBackgroundColor(bgcolor);table.addCell(cell0);/第一行(end)/第一行空白(begin) PdfPCell cell01 = new PdfPCell(new Paragraph(小字,fontChinese2); cell01.setColspan(7); cell01.setFixedHeight(12); cell01.setPadding(0);cell01.setHorizontalAlignment(Element.ALIGN_CENTER);/ 设置内容水平居中显示cell01.setVerticalAlignment(Element.ALIGN_TOP); cell01.setBorder(Rectangle.NO_BORDER); cell01.setBackgroundColor(bgcolor); table.addCell(cell01);/第一行空白(begin) PdfPCell cell1 = new PdfPCell(); cell1.setColspan(1); cell1.setFixedHeight(520); cell1.setPadding(0);cell1.setVerticalAlignment(Element.ALIGN_MIDDLE); cell1.setBorder(Rectangle.NO_BORDER); cell1.setBackgroundColor(bgcolor); table.addCell(cell1); PdfPCell cell2 = new PdfPCell(getTable1(); cell2.setFixedHeight(150); cell2.setColspan(1); cell2.setFixedHeight(10); cell2.setPadding(0);cell2.setVerticalAlignment(Element.ALIGN_TOP); cell2.setBorder(Rectangle.NO_BORDER); cell2.setBackgroundColor(bgcolor); /TODO:土黄色 背景#EFDB96 table.addCell(cell2);PdfPCell cell3 = new PdfPCell(new Paragraph(,fontChinese); cell3.setFixedHeight(150);cell3.setColspan(1); cell3.setFixedHeight(10); cell3.setPadding(0);cell3.setVerticalAlignment(Element.ALIGN_MIDDLE); cell3.setBorder(Rectangle.NO_BORDER); cell3.setBackgroundColor(bgcolor); table.addCell(cell3);PdfPCell cell4 = new PdfPCell(getTable2(); cell4.setFixedHeight(150);cell4.setColspan(1); cell4.setFixedHeight(10); cell4.setPadding(0);cell4.setVerticalAlignment(Element.ALIGN_TOP); cell4.setBorder(Rectangle.NO_BORDER); cell4.setBackgroundColor(bgcolor); table.addCell(cell4);PdfPCell cell5 = new PdfPCell(new Paragraph(,fontChinese); cell5.setFixedHeight(150);cell5.setColspan(1); cell5.setFixedHeight(10); cell5.setPadding(0); cell5.setVerticalAlignment(Element.ALIGN_MIDDLE); cell5.setBorder(Rectangle.NO_BORDER); cell5.setBackgroundColor(bgcolor); table.addCell(cell5);PdfPCell cell6 = new PdfPCell(getTable3(); cell6.setFixedHeight(150);cell6.setColspan(1); cell6.setFixedHeight(10); cell6.setPadding(0);cell6.setVerticalAlignment(Element.ALIGN_TOP); cell6.setBorder(Rectangle.NO_BORDER); cell6.setBackgroundColor(bgcolor); table.addCell(cell6);PdfPCell cell7 = new PdfPCell(new Paragraph(,fontChinese); cell7.setFixedHeight(150);cell7.setColspan(1); cell7.setFixedHeight(10);cell7.setPadding(0);cell7.setVerticalAlignment(Element.ALIGN_MIDDLE); cell7.setBorder(Rectangle.NO_BORDER);cell7.setBackgroundColor(bgcolor); table.addCell(cell7);/最后一行空白(begin) PdfPCell cell_end = new PdfPCell(new Paragraph(,fontChinese); cell_end.setFixedHeight(150);cell_end.setColspan(7); cell_end.setFixedHeight(10); cell_end.setPadding(0);cell_end.setHorizontalAlignment(Element.ALIGN_CENTER);/ 设置内容水平居中显示cell_end.setVerticalAlignment(Element.ALIGN_MIDDLE); cell_end.setBorder(Rectangle.NO_BORDER); cell_end.setBackgroundColor(bgcolor); table.addCell(cell_end);/最后一行空白(end) document.add(table);System.out.println(Done.);catch(Exception ex)ex.printStackTrace();finallydocument.close();/* * Table1 第一列 * return */public static PdfPTable getTable1() throws ExceptionPdfPTable iTable = new PdfPTable(1); iTable.setTotalWidth(260);/ 设置表格的宽度iTable.setLockedWidth(true);/ 设置表格的宽度固定 BaseColor bgcolor = new BaseColor(192, 192, 192); /灰色BaseColor bgcolorwrite = new BaseColor(255, 255, 255); /底色灰色BaseColor bgcolordise = new BaseColor(248, 248, 255); /底色灰色BaseFont bfChinese = BaseFont.createFont(STSongStd-Light, UniGB-UCS2-H, false); Font fontChinese = new Font(bfChinese); fontChinese.setSize(7f);Font fontChinese2 = new Font(bfChinese); fontChinese2.setSize(10f); Font fontChineseUL = new Font(bfChinese); fontChineseUL.setSize(7f); fontChineseUL.setStyle(Font.UNDERLINE); float fFixedHeight = 14f;float fFixedHeight2 = 10f;/- (begin)PdfPCell cell = new PdfPCell(new Paragraph(测试测试测试测试测试 ,fontChinese2); cell.setFixedHeight(150);cell.setColspan(1); cell.setFixedHeight(fFixedHeight); cell.setPadding(0); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setBackgroundColor(bgcolor); cell.setBorder(Rectangle.NO_BORDER);iTable.addCell(cell);Chunk chunk1 = new Chunk( ,fontChineseUL); Phrase phrase = new Phrase(chunk1);Paragraph p = new Paragraph( :,fontChinese);p.add(phrase);PdfPCell cell1 = new PdfPCell(p);cell1.setFixedHeight(150);cell1.setColspan(1); cell1.setFixedHeight(fFixedHeight2); cell1.setPadding(0); cell1.setBorder(Rectangle.NO_BORDER);cell1.setBackgroundColor(bgcolorwrite); iTable.addCell(cell1);chunk1 = new Chunk( 测试测试测试测试测试 ,fontChineseUL); phrase = new Phrase(chunk1);p = new Paragraph( 测试测试测试111 ,fontChinese);p.add(phrase);PdfPCell cell2 = new PdfPCell(p); cell2.setFixedHeight(150);cell2.setColspan(1); cell2.setFixedHeight(fFixedHeight2); cell2.setPadding(0); cell2.setBackgroundColor(bgcolorwrite); cell2.setBorder(Rectangle.NO_BORDER);iTable.addCell(cell2);cell2 = new PdfPCell(new Paragraph( ,fontChinese); /空行cell2.setFixedHeight(150);cell2.setColspan(1); cell2.setFixedHeight(4); cell2.setPadding(0); cell2.setBackgroundColor(bgcolorwrite); cell2.setBorder(Rectangle.NO_BORDER);iTable.addCell(cell2);cell2 = new PdfPCell(new Paragraph( ,fontChinese); /空行cell2.setFixedHeight(150);cell2.setColspan(1); cell2.setFixedHeight(4); cell2.setPadding(0); cell2.setBackgroundColor(bgcolordise); cell2.setBorder(Rectangle.NO_BORDER);iTable.addCell(cell2);/- (end) /- cell = new PdfPCell(new Paragraph( ,fontChinese2); cell.setFixedHeight(150);cell.setColspan(1); cell.setFixedHeight(fFixedHeight); cell.setPadding(0); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setBackgroundColor(bgcolor); cell.setBorder(Rectangle.NO_BORDER);iTable.addCell(cell);PdfPCell celltemp = new PdfPCell(new Paragraph( ,fontChinese) ; celltemp.setColspan(1); celltemp.setFixedHeight(fFixedHeight2); celltemp.setPadding(0); celltemp.setBorder(Rectangle.NO_BORDER);celltemp.setBackgroundColor(bgcolorwrite); iTable.addCell(celltemp);celltemp = new PdfPCell(new Paragraph( _,fontChinese) ; celltemp.setColspan(1); celltemp.setFixedHeight(fFixedHeight2); celltemp.setPadding(0); celltemp.setBorder(Rectangle.NO_BORDER);celltemp.setBackgroundColor(bgcolorwrite); iTable.addCell(celltemp);celltemp = new PdfPCell(new Paragraph( _ _,fontChinese) ; celltemp.setColspan(1); celltemp.setFixedHeight(fFixedHeight2); celltemp.setPadding(0); celltemp.setBorder(Rectangle.NO_BORDER);celltemp.setBackgroundColor(bgcolorwrite); iTable.addCell(celltemp);chunk1 = new Chunk(1 ,fontChineseUL); phrase = new Phrase(chunk1);p = new Paragraph( :,fontChinese);p.add(phrase);celltemp = new PdfPCell(p);celltemp.setColspan(1); celltemp.setFixedHeight(fFixedHeight2); celltemp.setPadding(0); celltemp.setBorder(Rectangle.NO_BORDER);celltemp.setBackgroundColor(bgcolorwrite); iTable.addCell(celltemp);celltemp = new PdfPCell(new Paragraph( _,fontChinese) ; celltemp.setColspan(1); celltemp.setFixedHeight(fFixedHeight2); celltemp.setPadding(0); celltemp.setBorder(Rectangle.NO_BORDER);celltemp.setBackgroundColor(bgcolorwrite); iTable.addCell(celltemp);celltemp = new PdfPCell(new Paragraph( _至_ ,fontChinese) ; celltemp.setColspan(1); celltemp.setFixedHeight(fFixedHeight2); celltemp.setPadding(0); celltemp.setBorder(Rectangle.NO_BORDER);celltemp.setBackgroundColor(bgcolorwrite); iTable.addCell(celltemp);chunk1 = new Chunk(1 ,fontChineseUL); phrase = new Phrase(chunk1);p = new Paragraph( :,fontChinese);p.add(phrase); celltemp = new PdfPCell(p); celltemp.setColspan(1); celltemp.setFixedHeight(fFixedHeight2); celltemp.setPadding(0); celltemp.setBorder(Rectangle.NO_BORDER);celltemp.setBackgroundColor(bgcolorwrite); iTable.addCell(celltemp);chunk1 = new Chunk(1 ,fontChineseUL); phrase = new Phrase(chunk1);p = new Paragraph( :,fontChinese);p.add(phrase); celltemp = new PdfPCell(p); celltemp.setColspan(1); celltemp.setFixedHeight(fFixedHeight2); celltemp.setPadding(0); celltemp.setBorder(Rectangle.NO_BORDER);celltemp.setBackgroundColor(bgcolorwrite); iTable.addCell(celltemp);celltemp = new PdfPCell(new Paragraph( _,fontChinese) ; celltemp.setColspan(1); celltemp.setFixedHeight(fFixedHeight2); celltemp.setPadding(0); celltemp.setBorder(Rectangle.NO_BORDER);celltemp.setBackgroundColor(bgcolorwrite); iTable.addCell(celltemp);celltemp = new PdfPCell(new Paragraph( _ _年_月,fontChinese) ; celltemp.setColspan(1); celltemp.setFixedHeight(fFixedHeight2); celltemp.setPadding(0); celltemp.setBorder(Rectangle.NO_BORDER);celltemp.setBackgroundColor(bgcolorwrite); iTable.addCell(celltemp);chunk1 = new Chunk(1 ,fontChineseUL); phrase = new Phrase(chunk1);p = new Paragraph( :,fontChinese);p.add(phrase); celltemp = new PdfPCell(p); celltemp.setColspan(1); celltemp.setFixedHeight(fFixedHeight2); celltemp.setPadding(0); celltemp.setBorder(Rectangle.NO_BORDER);celltemp.setBackgroundColor(bgcolorwrite); iTable.addCell(celltemp);celltemp = new PdfPCell(new Paragraph( _ _ _,fontChinese) ; celltemp.setColspan(1); celltemp.setFixedHeight(fFixedHeight2); celltemp.setPadding(0); celltemp.setBorder(Rectangle.NO_BORDER);celltemp.setBackgroundColor(bgcolorwrite); iTable.addCell(celltemp); cell2 = new PdfPCell(new Paragraph(,fontChinese); /空行cell2.setFixedHeight(150);cell2.setColspan(1); cell2.setFixedHeight(4); cell2.setPadding(0); cell2.setBackgroundColor(bgcolorwrite); cell2.setBorder(Rectangle.NO_BORDER);iTable.addCell(cell2);cell2 = new PdfPCell(new Paragraph(,fontChinese); /空行cell2.setFixedHeight(150);cell2.setColspan(1); cell2.setFixedHeight(4); cell2.setPadding(0); cell2.setBackgroundColor(bgcolordise); cell2.setBorder(Rectangle.NO_BORDER);iTable.addCell(cell2);/ cell = new PdfPCell(new Paragraph(测试 ,fontChinese2); cell.setFixedHeight(150);cell.setColspan(1); cell.setFixedHeight(fFixedHeight); cell.setPadding(0); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setBackgroundColor(bgcolor); cell.setBorder(Rectangle.NO_BORDER);iTable.addCell(cell); / cell = new PdfPCell(new Paragraph( ,fontChinese2); cell.setFixedHeight(150);cell.setColspan(1); cell.setFixedHeight(fFixedHeight); cell.setPadding(0); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setBackgroundColor(bgcolor); cell.setBorder(Rectangle.NO_BORDER);iTable.addCell(cell);celltemp = new PdfPCell(new Paragraph( ,fontChinese) ; celltemp.setColspan(1); celltemp.setFixedHeight(fFixedHeight2); celltemp.setPadding(0); celltemp.setBorder(Rectangle.NO_BORDER);celltemp.setBackgroundColor(bgcolorwrite); iTable.addCell(celltemp);celltemp = new PdfPCell(new Paragraph( ,fontChinese) ; celltemp.setColspan(1); celltemp.setFixedHeight(fFixedHeight2); celltemp.setPadding(0); celltemp.setBorder(Rectangle.NO_BORDER);celltemp.setBackgroundColor(bgcolorwrite); iTable.addCell(celltemp); / cell = new PdfPCell(new Paragraph( ,fontChinese2); cell.setFixedHeight(150);cell.setColspan(1); cell.setFixedHeight(fFixedHeight); cell.setPadding(0); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setBackgroundColor(bgcolor); cell.setBorder(Rectangle.NO_BORDER);iTable.addCell(cell);chunk1 = new Chunk( ,fontChineseUL); phrase = new Phrase(chunk1);p = new Paragraph( ,fontChinese);p.add(phrase); celltemp = new PdfPCell(p);celltemp.setColspan(1); celltemp.setFixedHeight(fFixedHeight2); celltemp.setPadding(0); celltemp.setBorder(Rectangle.NO_BORDER);celltemp.setBackgroundColor(bgcolorwrite); iTable.addCell(celltemp);return iTable; /* * Table2 第二列 * return */public static PdfPTable getTable2() throws ExceptionPdfPTable iTable = new PdfPTable(1); iTable.setTotalWidth(260);/ 设置表格的宽度iTable.setLockedWidth(true);/ 设置表格的宽度固定 return iTable; /* * Table3 第三列 * return */public static PdfPTable getTable3() throws ExceptionPdfPTable iTable = new PdfPTable(1); iTable.setTotalWidth(260);/ 设置表格的宽度iTable.setLockedWidth(true);/ 设置表格的宽度固定 BaseColor bgcolor = new BaseColor(192, 192, 192); /灰色BaseColor bgcolorwrite = new BaseColor(255, 255, 255); /底色灰色BaseColor bgcolordise = new BaseColor(248, 248, 255); /底色灰色BaseFont bfChinese = BaseFont.createFont(STSongStd-Light, UniGB-UCS2-H, false); Font fontChinese = new Font(bfChinese); fontChinese.setSize(7f);Font fontChinese2 = new Font(bfChinese); fontChinese2.setSize(10f); float fFixedHeight = 14f;float fFixedHeight2 = 12f;/- (begin)PdfPCell cell = new PdfPCell(new Paragraph(测试测试测试 ,fontChinese2); cell.setColspan(1); cell.setFixedHeight(fFixedHeight); cell.setPadding(0); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setBackgroundColor(bgcolor); cell.setBorder(Rectangle.NO_BORDER);iTable.addCell(cell); PdfPCell celltemp = new PdfPCell(new Paragraph( 测试测试测试:,fontChinese) ; celltemp.setColspan(1); celltemp.setFixedHeight(fFixedHeight2); celltemp.setPadding(0); celltemp.setBorder(Rectangle.NO_BORDER);celltemp.setBackgroundColor(bgcolorwrite); iTable.addCell(celltemp);celltemp = new PdfPCell(new Paragraph( 测试测试测试,fontChinese) ; celltemp.setColspan(1); celltemp.setFixedHeight(fFixedHeight2); celltemp.setPadding(0); celltemp.setBorder(Rectangle.NO_BORDER);celltemp.setBackgroundColor(bgcolorwrite); iTable.addCell(celltemp);celltemp = new PdfPCell(new Paragraph( 测试测试测试,fontChinese) ; celltemp.setColspan(1); celltemp.setFixedHeight(fFixedHeight2); celltemp.setPadding(0); celltemp.setBorder(Re
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 消防考试时政题库及答案
- 生猪屠宰厂自查试题带答案
- 二级经销合同协议书
- 教师公招就业协议书
- 血站护士招聘考试题库及答案
- 施工合同开工延期协议
- 亚运商品经销协议书
- 2026-2031全球及中国家用感应炉灶行业市场发展分析及前景趋势与投资发展研究报告
- 施工的安全合同范本
- 基于机械手的背俞穴阻抗检测系统:设计、实现与应用研究
- 民非单位内控管理制度
- 高一物理第一次月考卷【测试范围:人教版第1~2章】(考试版A3)
- 2025年医疗器械供货、培训方案及质保措施投标方案
- 汽车美容融资方案(3篇)
- 针刺治疗偏头痛
- 文化转译的边界与挑战-洞察及研究
- 套细胞淋巴瘤病理学讲座
- 航空航天材料供应计划及采购质量保证措施
- 2025年高级(三级)健康照护师职业技能鉴定《理论知识》真题卷(后附答案及解析)
- 国企派出董事管理办法
- 膈疝患者的护理
评论
0/150
提交评论