打印单据(PDF)

news/2024/11/29 22:40:53/

1、首先需要下载两个包,点击: iText-5.0.5与iTextAsian包
2、正文


package com.service;import java.io.File;
import java.io.FileOutputStream;import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
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;public class BasePDFWrite {Document document = null;// 建立一个Document对象      private static Font headFont ;private static Font keyFont ;private static Font textfont_H ;private static Font textfont_B ;int maxWidth = 520;static{    BaseFont bfChinese_H;try {    /** * 新建一个字体,iText的方法 STSongStd-Light 是字体,在iTextAsian.jar 中以property为后缀 * UniGB-UCS2-H 是编码,在iTextAsian.jar 中以cmap为后缀 H 代表文字版式是 横版, 相应的 V 代表竖版  */bfChinese_H = BaseFont.createFont("STSong-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);    headFont = new Font(bfChinese_H, 10, Font.NORMAL);   keyFont = new Font(bfChinese_H, 18, Font.BOLD); textfont_H = new Font(bfChinese_H, 10, Font.NORMAL); textfont_B = new Font(bfChinese_H, 12, Font.NORMAL); } catch (Exception e) {             e.printStackTrace();    }     }/*** 设置页面属性* @param file*/public BasePDFWrite(File file) {//自定义纸张Rectangle rectPageSize = new Rectangle(350, 620);// 定义A4页面大小//Rectangle rectPageSize = new Rectangle(PageSize.A4);  rectPageSize = rectPageSize.rotate();// 加上这句可以实现页面的横置document = new Document(rectPageSize,10, 150, 10, 40);try {PdfWriter.getInstance(document,new FileOutputStream(file));    document.open();} catch (Exception e) {e.printStackTrace();    }}/*** 建表格(以列的数量建)* @param colNumber* @return*/public PdfPTable createTable(int colNumber){    PdfPTable table = new PdfPTable(colNumber);    try{    //table.setTotalWidth(maxWidth);    //table.setLockedWidth(true);    table.setHorizontalAlignment(Element.ALIGN_CENTER);         table.getDefaultCell().setBorder(1); table.setSpacingBefore(10);table.setWidthPercentage(100);}catch(Exception e){    e.printStackTrace();    }    return table;}/*** 建表格(以列的宽度比建)* @param widths* @return*/public PdfPTable createTable(float[] widths){    PdfPTable table = new PdfPTable(widths);    try{    //table.setTotalWidth(maxWidth);    //table.setLockedWidth(true);    table.setHorizontalAlignment(Element.ALIGN_CENTER);         table.getDefaultCell().setBorder(1); table.setSpacingBefore(10);table.setWidthPercentage(100);}catch(Exception e){    e.printStackTrace();    }    return table;    }/*** 表格中单元格* @param value* @param font* @param align* @return*/public PdfPCell createCell(String value,Font font,int align){    PdfPCell cell = new PdfPCell();    cell.setVerticalAlignment(Element.ALIGN_MIDDLE);            cell.setHorizontalAlignment(align);        cell.setPhrase(new Phrase(value,font));    return cell;    }/*** 表格中单元格* @param value* @param font* @param align* @param colspan* @param rowspan* @return*/public PdfPCell createCell(String value,Font font,int align_v,int align_h,int colspan,int rowspan){PdfPCell cell = new PdfPCell();    cell.setVerticalAlignment(align_v);    cell.setHorizontalAlignment(align_h);        cell.setColspan(colspan); cell.setRowspan(rowspan); cell.setPhrase(new Phrase(value,font));  return cell;}/*** 建短语* @param value* @param font* @return*/public Phrase createPhrase(String value,Font font){ Phrase phrase = new Phrase();phrase.add(value);phrase.setFont(font);return phrase;}  /*** 建段落* @param value* @param font* @param align* @return*/public Paragraph createParagraph(String value,Font font,int align){ Paragraph paragraph = new Paragraph();paragraph.add(new Phrase(value,font));paragraph.setAlignment(align);return paragraph;}public void generatePDF() throws Exception{     //页头信息document.add(createParagraph("【XXXX有限公司】",headFont,Element.ALIGN_LEFT));document.add(createParagraph("签  购  单",keyFont,Element.ALIGN_CENTER));document.add(createParagraph("编号:XD201602000003",headFont,Element.ALIGN_RIGHT));//表格信息float[] widths = {4f,10f,10f,20f,15f,8f,11f,12f,10f};PdfPTable table = createTable(widths);   table.addCell(createCell("顾客信息", textfont_H, Element.ALIGN_MIDDLE, Element.ALIGN_CENTER,1,4)); table.addCell(createCell("顾客名称", textfont_H,Element.ALIGN_MIDDLE, Element.ALIGN_LEFT,2,1));    table.addCell(createCell("XXXX有限公司", textfont_H,Element.ALIGN_MIDDLE, Element.ALIGN_LEFT,6,1));table.addCell(createCell("编码/税号", textfont_H,Element.ALIGN_MIDDLE, Element.ALIGN_LEFT,2,1)); table.addCell(createCell("", textfont_H,Element.ALIGN_MIDDLE, Element.ALIGN_LEFT,6,1)); table.addCell(createCell("联系地址", textfont_H,Element.ALIGN_MIDDLE, Element.ALIGN_LEFT,2,1));table.addCell(createCell("", textfont_H,Element.ALIGN_MIDDLE, Element.ALIGN_LEFT,6,1)); table.addCell(createCell("采购经办人", textfont_H,Element.ALIGN_MIDDLE, Element.ALIGN_CENTER,2,1));table.addCell(createCell("", textfont_H,Element.ALIGN_MIDDLE, Element.ALIGN_LEFT,1,1));table.addCell(createCell("经办人电话", textfont_H,Element.ALIGN_MIDDLE, Element.ALIGN_CENTER,1,1));table.addCell(createCell("", textfont_H,Element.ALIGN_MIDDLE, Element.ALIGN_LEFT,2,1));table.addCell(createCell("经办人手机", textfont_H,Element.ALIGN_MIDDLE, Element.ALIGN_CENTER,1,1));table.addCell(createCell("", textfont_H,Element.ALIGN_MIDDLE, Element.ALIGN_LEFT,1,1));table.addCell(createCell("产品订购清单", textfont_H, Element.ALIGN_MIDDLE, Element.ALIGN_CENTER,1,8));table.addCell(createCell("序号", textfont_H,Element.ALIGN_MIDDLE, Element.ALIGN_CENTER,1,1)); table.addCell(createCell("产品信息", textfont_H,Element.ALIGN_MIDDLE, Element.ALIGN_CENTER,2,1)); table.addCell(createCell("规格型号", textfont_H,Element.ALIGN_MIDDLE, Element.ALIGN_CENTER,1,1)); table.addCell(createCell("单位", textfont_H,Element.ALIGN_MIDDLE, Element.ALIGN_CENTER,1,1));table.addCell(createCell("单价", textfont_H,Element.ALIGN_MIDDLE, Element.ALIGN_CENTER,1,1)); table.addCell(createCell("数量", textfont_H,Element.ALIGN_MIDDLE, Element.ALIGN_CENTER,1,1));table.addCell(createCell("小计", textfont_H,Element.ALIGN_MIDDLE, Element.ALIGN_CENTER,1,1));table.addCell(createCell("1", textfont_H,Element.ALIGN_MIDDLE, Element.ALIGN_LEFT,1,1)); table.addCell(createCell("塑料纸", textfont_H,Element.ALIGN_MIDDLE, Element.ALIGN_LEFT,2,1)); table.addCell(createCell("小号", textfont_H,Element.ALIGN_MIDDLE, Element.ALIGN_LEFT,1,1)); table.addCell(createCell("个", textfont_H,Element.ALIGN_MIDDLE, Element.ALIGN_LEFT,1,1));table.addCell(createCell("20", textfont_H,Element.ALIGN_MIDDLE, Element.ALIGN_LEFT,1,1)); table.addCell(createCell("20", textfont_H,Element.ALIGN_MIDDLE, Element.ALIGN_LEFT,1,1));table.addCell(createCell("400", textfont_H,Element.ALIGN_MIDDLE, Element.ALIGN_LEFT,1,1));table.addCell(createCell("2", textfont_H,Element.ALIGN_MIDDLE, Element.ALIGN_LEFT,1,1)); table.addCell(createCell("塑料纸", textfont_H,Element.ALIGN_MIDDLE, Element.ALIGN_LEFT,2,1)); table.addCell(createCell("小号", textfont_H,Element.ALIGN_MIDDLE, Element.ALIGN_LEFT,1,1)); table.addCell(createCell("个", textfont_H,Element.ALIGN_MIDDLE, Element.ALIGN_LEFT,1,1));table.addCell(createCell("20", textfont_H,Element.ALIGN_MIDDLE, Element.ALIGN_LEFT,1,1)); table.addCell(createCell("20", textfont_H,Element.ALIGN_MIDDLE, Element.ALIGN_LEFT,1,1));table.addCell(createCell("400", textfont_H,Element.ALIGN_MIDDLE, Element.ALIGN_LEFT,1,1));table.addCell(createCell("3", textfont_H,Element.ALIGN_MIDDLE, Element.ALIGN_LEFT,1,1)); table.addCell(createCell("塑料纸", textfont_H,Element.ALIGN_MIDDLE, Element.ALIGN_LEFT,2,1)); table.addCell(createCell("小号", textfont_H,Element.ALIGN_MIDDLE, Element.ALIGN_LEFT,1,1)); table.addCell(createCell("个", textfont_H,Element.ALIGN_MIDDLE, Element.ALIGN_LEFT,1,1));table.addCell(createCell("20", textfont_H,Element.ALIGN_MIDDLE, Element.ALIGN_LEFT,1,1)); table.addCell(createCell("20", textfont_H,Element.ALIGN_MIDDLE, Element.ALIGN_LEFT,1,1));table.addCell(createCell("400", textfont_H,Element.ALIGN_MIDDLE, Element.ALIGN_LEFT,1,1));table.addCell(createCell("4", textfont_H,Element.ALIGN_MIDDLE, Element.ALIGN_LEFT,1,1)); table.addCell(createCell("合计", textfont_H,Element.ALIGN_MIDDLE, Element.ALIGN_LEFT,2,1)); table.addCell(createCell("", textfont_H,Element.ALIGN_MIDDLE, Element.ALIGN_LEFT,1,1)); table.addCell(createCell("", textfont_H,Element.ALIGN_MIDDLE, Element.ALIGN_LEFT,1,1));table.addCell(createCell("20", textfont_H,Element.ALIGN_MIDDLE, Element.ALIGN_LEFT,1,1)); table.addCell(createCell("60", textfont_H,Element.ALIGN_MIDDLE, Element.ALIGN_LEFT,1,1));table.addCell(createCell("1200", textfont_H,Element.ALIGN_MIDDLE, Element.ALIGN_LEFT,1,1));table.addCell(createCell("合计金额(元)", textfont_B,Element.ALIGN_MIDDLE, Element.ALIGN_CENTER,3,1));table.addCell(createCell("壹仟俩佰元", textfont_B,Element.ALIGN_MIDDLE, Element.ALIGN_RIGHT,6,1));table.addCell(createCell("顾客确认", textfont_H,Element.ALIGN_MIDDLE, Element.ALIGN_CENTER,1,3));table.addCell(createCell("注:本人已充分了解订购产品的功能用途,是根据实际需要自愿购买的。", textfont_H,Element.ALIGN_MIDDLE, Element.ALIGN_LEFT,8,1));table.addCell(createCell("采购经办人签字:", textfont_H,Element.ALIGN_MIDDLE, Element.ALIGN_LEFT,6,1));table.addCell(createCell("盖章:", textfont_H,Element.ALIGN_MIDDLE, Element.ALIGN_LEFT,2,1));table.addCell(createCell("签字日期", textfont_H,Element.ALIGN_MIDDLE, Element.ALIGN_LEFT,8,1));document.add(table);    document.close();    }public static void main(String[] args) throws Exception {File file = new File("d:\\PDF\\T3.pdf");file.createNewFile();new BasePDFWrite(file).generatePDF(); }
}

http://www.ppmy.cn/news/554851.html

相关文章

ant-design 打印功能/打印单据

ant项目,这里是我的单据,用js原生的打印,你会发现,你的样式基本上没了,所以我用了html2canvas先截图,然后再打印 说下思路:将单据截图,然后打印,但是打印的时候只能打印…

审核的单据才可以打印

编辑界面ActionPrintPreView,ActionPrint前置脚本实现非审核状态单据不允许打印: var easNames JavaImporter(); easNames.importPackage(Packages.com.kingdee.eas.util.client); easNames.importPackage(Packages.com.kingdee.eas.scm.common); easNa…

出入库单据小票移动打印,盘点机PDA连接蓝牙打印机打印单据小票

本期视频讲解:使用(汉码)盘点机PDA搭配蓝牙便携打印机,移动扫码入库,出库开单,并打印单据小票。实现移动的单兵神器,不用来回电脑跑,能大大提高工作效率,节省时间成本。 …

C# 利用PrintDocument定制打印单据

本文是利用PrintDocument定制打印单据的小例子,仅供学习分享使用,如果不足之处,还请指正。 涉及知识点: PrintDocument :从 Windows 窗体应用程序打印时,定义一种可重用的可发送到打印机上的对象。PrintPreviewContr…

金蝶精斗云PDA移动扫码入库出库,搭配蓝牙打印机打印单据小票

金蝶精斗云PDA移动扫码入库出库,搭配蓝牙打印机打印单据小票,单据小票打印,金蝶安卓盘点机PDA金蝶精斗云PDA轻松扫描商品条码盘点 第一步,装上单据小票纸。 打开蓝牙便携打印机的纸仓,调整里面的挡板到合适位置&#…

【C#】最全单据打印(打印模板、条形码二维码、字体样式、项目源码)

系列文章 【C#】编号生成器(定义单号规则、固定字符、流水号、业务单号) 本文链接:https://blog.csdn.net/youcheng_ge/article/details/129129787 【C#】日期范围生成器(开始日期、结束日期) 本文链接:h…

定时器的实现原理

文章目录 1.定时器的作用?2.数据结构要求3.时间轮4.分级时间轮5.业界实现方案参考文献 1.定时器的作用? 定时器的主要用途是执行定时任务。 定时任务在很多场景都需要用到,比如游戏的 Buff 实现,Redis 中的过期任务,Linux 中的定时任务&a…

团体程序设计天梯赛-练习集L2篇③

🚀欢迎来到本文🚀 🍉个人简介:Hello大家好呀,我是陈童学,一个与你一样正在慢慢前行的普通人。 🏀个人主页:陈童学哦CSDN 💡所属专栏:PTA 🎁希望各…