iText7——画发票PDF(完整)

显示描述:
1、每页显示必须带有发票头、“销售方和购买方信息”
2、明细填充为:当n≤8 行时,发票总高度140mm,每条发票明细行款高度4.375mm;
当8<n≤12行时,发票高度增加17.5mm,不换页;
以此类推
根据实际情况能放下“应税明细信息”和“票尾”信息后换页,注意是否为最后一行

显示效果:
1、只有一页的情况:
在这里插入图片描述
2、有两页的且刚好能放下所有明细的情况(超过一页后,开票日期下显示页数):
这是第一页
在这里插入图片描述

代码实现:

步骤一、创建主方法,可传入需要的参数模型

public static void InvoicePage(NaturalSystemPdfModel model) //NaturalSystemPdfModel为传入的需要用到的参数模型
{//创建pdf的保存位置//model.filePath为传入模型中存放的文件保存路径,model.kprq为开票日期,model.fileName为文件名//model.filePath = Path.Combine(_webHostEnvironment.WebRootPath, “.pdf”);获取当前服务器下的文件路径string outputPath = Path.Combine(model.filePath,model.kprq.ToString("yyyyMMdd"), model.fileName + ".pdf");//判断文件夹是否存在,不存在则创建一个新的if (!Directory.Exists(Path.Combine(model.filePath, model.kprq.ToString("yyyyMMdd"))))Directory.CreateDirectory(Path.Combine(model.filePath, model.kprq.ToString("yyyyMMdd")));// 创建一个新的PDF文档var cc = new PdfWriter(outputPath);//设置 PdfStream默认压缩级别。cc.SetCompressionLevel(CompressionConstants.BEST_COMPRESSION);PdfDocument pdf = new PdfDocument(cc);model.jshjdx = MoneyToUpper(model.jshj.ToString()); //价税合计大写转换(转换方法MoneyToUpper())model.gmfmc = model.gmfmc == "个人" ? model.gmfmc + "(个人)" : model.gmfmc;//购方名称字体//model.webHost为模型中的发布服务器中的文件所在位置:IWebHostEnvironment _webHostEnvironment.WebRootPathPdfFont KT = PdfFontFactory.CreateFont(System.IO.Path.Combine(model.webHost, "fonts", "SIMKAI.TTF"), PdfEncodings.IDENTITY_H, PdfFontFactory.EmbeddingStrategy.PREFER_NOT_EMBEDDED);PdfFont ST = PdfFontFactory.CreateFont(System.IO.Path.Combine(model.webHost, "fonts", "SIMFANG.TTF"), PdfEncodings.IDENTITY_H, PdfFontFactory.EmbeddingStrategy.PREFER_NOT_EMBEDDED);PdfFont CN = PdfFontFactory.CreateFont(System.IO.Path.Combine(model.webHost, "fonts", "COUR.TTF"), PdfEncodings.IDENTITY_H, PdfFontFactory.EmbeddingStrategy.PREFER_NOT_EMBEDDED);//添加页眉页脚||或者需要每页显示的票头//创建类PdfEventHandler,在步骤二中PdfEventHandler handler = new PdfEventHandler(model, KT, ST, CN);//发票中的票头、购买方信息、表头都放在PdfEventHandler类中pdf.AddEventHandler(PdfDocumentEvent.START_PAGE, handler);ComputeValue computeValue = new ComputeValue();try{using (Document document = new Document(pdf, iText.Kernel.Geom.PageSize.A4, false)){//document.SetMargins(0, 0, 0, 0);//默认宽210mm=8.2677英寸=595磅//默认高29.7mm=11.6929英寸=842磅//默认边距36磅=0.5英寸=12.7mm// 定义自定义RGB颜色(例如,红色)DeviceRgb customColor = new DeviceRgb(128, 0, 0);//发票固定高度pt:票头,ht:购买方信息,et:票尾备注,kp:开票人float pt = 30, ht = 22, et = 20, kp = 8.5f;//下面代码中大量用到的computeValue.computeUnit()方法作用为:毫米转换磅#region 表中列表 //添加表格Table BodyTable = new Table(9, false);Cell cel11 = new Cell(1, 1).SetTextAlignment(TextAlignment.CENTER).SetWidth(computeValue.computeUnit(37)).SetHeight(computeValue.computeUnit(4.5f)).SetBorderRight(Border.NO_BORDER).SetBorderBottom(Border.NO_BORDER).SetBorderTop(Border.NO_BORDER).SetFont(KT).SetFontSize(9).SetFontColor(customColor).Add(new Paragraph("项目名称"));Cell cel12 = new Cell(1, 2).SetTextAlignment(TextAlignment.LEFT).SetWidth(computeValue.computeUnit(24)).SetBorder(Border.NO_BORDER).SetFont(KT).SetFontSize(9).SetFontColor(customColor).Add(new Paragraph("规格型号"));Cell cel13 = new Cell(1, 1).SetTextAlignment(TextAlignment.CENTER).SetWidth(computeValue.computeUnit(12)).SetBorder(Border.NO_BORDER).SetFont(KT).SetFontSize(9).SetFontColor(customColor).Add(new Paragraph("单位"));Cell cel14 = new Cell(1, 1).SetTextAlignment(TextAlignment.RIGHT).SetWidth(computeValue.computeUnit(25)).SetBorder(Border.NO_BORDER).SetFont(KT).SetFontSize(9).SetFontColor(customColor).Add(new Paragraph("数量"));Cell cel15 = new Cell(1, 1).SetTextAlignment(TextAlignment.RIGHT).SetWidth(computeValue.computeUnit(25)).SetBorder(Border.NO_BORDER).SetFont(KT).SetFontSize(9).SetFontColor(customColor).Add(new Paragraph("单价"));Cell cel16 = new Cell(1, 1).SetTextAlignment(TextAlignment.RIGHT).SetWidth(computeValue.computeUnit(26)).SetBorder(Border.NO_BORDER).SetFont(KT).SetFontSize(9).SetFontColor(customColor).Add(new Paragraph("金额"));Cell cel17 = new Cell(1, 1).SetTextAlignment(TextAlignment.CENTER).SetBorder(Border.NO_BORDER).SetWidth(computeValue.computeUnit(25)).SetFont(KT).SetFontSize(9).SetFontColor(customColor).Add(new Paragraph("税率/征收率"));Cell cel18 = new Cell(1, 1).SetTextAlignment(TextAlignment.RIGHT).SetWidth(computeValue.computeUnit(27)).SetBorderLeft(Border.NO_BORDER).SetBorderBottom(Border.NO_BORDER).SetBorderTop(Border.NO_BORDER).SetFont(KT).SetFontSize(9).SetFontColor(customColor).Add(new Paragraph("税额"));BodyTable.AddCell(cel11.SetBorder(new SolidBorder(customColor, 1)));BodyTable.AddCell(cel12);BodyTable.AddCell(cel13);BodyTable.AddCell(cel14);BodyTable.AddCell(cel15);BodyTable.AddCell(cel16);BodyTable.AddCell(cel17);BodyTable.AddCell(cel18.SetBorder(new SolidBorder(customColor, 1)));BodyTable.StartNewRow();//合计小计列//合计Cell cel41 = new Cell(1, 2).SetTextAlignment(TextAlignment.CENTER).SetHeight(computeValue.computeUnit(4.5f)).SetPaddingBottom(-5).SetFont(KT).SetFontSize(9).SetFontColor(customColor).Add(new Paragraph("合\t\t计").SetFixedLeading(11));Cell cel42 = new Cell(1, 5).SetTextAlignment(TextAlignment.RIGHT).SetHeight(computeValue.computeUnit(4.5f)).SetFont(ST).SetFontSize(9).Add(new Paragraph("¥" + model.hjje).SetFixedLeading(11));Cell cel43 = new Cell(1, 2).SetTextAlignment(TextAlignment.RIGHT).SetHeight(computeValue.computeUnit(4.5f)).SetFont(ST).SetFontSize(9).Add(new Paragraph("¥" + model.hjse).SetFixedLeading(11));//价税合计大小写ImageData data = ImageDataFactory.Create(Path.Combine(model.webHost, "images", "jiashuiheji.png"));Image img = new Image(data).SetWidth(15).SetHeight(15);Cell cel51 = new Cell(1, 2).SetHeight(computeValue.computeUnit(8)).SetWidth(computeValue.computeUnit(50)).SetBorderBottom(Border.NO_BORDER).SetVerticalAlignment(VerticalAlignment.MIDDLE).SetTextAlignment(TextAlignment.CENTER).SetFont(KT).SetFontSize(9).SetFontColor(customColor).Add(new Paragraph("价税合计(大写)"));Cell cel53 = new Cell(1, 4).SetBorderLeft(Border.NO_BORDER).SetBorderRight(Border.NO_BORDER).SetBorderBottom(Border.NO_BORDER).SetHeight(computeValue.computeUnit(8)).SetVerticalAlignment(VerticalAlignment.MIDDLE).SetTextAlignment(TextAlignment.LEFT).Add(new Paragraph(model.jshjdx).SetFont(ST).SetFontSize(10).SetFirstLineIndent(18)).Add(new Image(ImageDataFactory.Create(Path.Combine(model.webHost, "images", "jiashuiheji.png"))).SetWidth(15).SetHeight(15));Cell cel55 = new Cell(1, 1).SetBorderLeft(Border.NO_BORDER).SetBorderRight(Border.NO_BORDER).SetBorderBottom(Border.NO_BORDER).SetVerticalAlignment(VerticalAlignment.MIDDLE).SetHeight(computeValue.computeUnit(8)).SetTextAlignment(TextAlignment.RIGHT).Add(new Paragraph("(小写)").SetFont(KT).SetFontSize(9).SetFontColor(customColor));Cell cel54 = new Cell(1, 2).SetBorderLeft(Border.NO_BORDER).SetHeight(computeValue.computeUnit(8)).SetBorderBottom(Border.NO_BORDER).SetVerticalAlignment(VerticalAlignment.MIDDLE).SetTextAlignment(TextAlignment.LEFT).Add(new Paragraph("¥" + model.jshj).SetFont(ST).SetFontSize(10));#region 表尾//添加表格Table endTable = new Table(2, false);Cell cel61 = new Cell(1, 1).SetWidth(computeValue.computeUnit(6)).SetHeight(computeValue.computeUnit(20)).SetVerticalAlignment(VerticalAlignment.MIDDLE).SetFont(KT).SetFontSize(9).SetFontColor(customColor).Add(new Paragraph("备注"));Cell cel62 = new Cell(1, 8).SetTextAlignment(TextAlignment.LEFT).SetVerticalAlignment(VerticalAlignment.MIDDLE).SetWidth(computeValue.computeUnit(195)).SetHeight(computeValue.computeUnit(20)).SetFont(ST).SetFontSize(10).Add(new Paragraph(model.bz));#endregion#region 开票人// 添加发票内容Paragraph ending = new Paragraph().Add("开票人:").SetFont(KT).SetFontSize(9).SetFontColor(customColor).SetTextAlignment(TextAlignment.LEFT);Paragraph endingname = new Paragraph().Add( model.kpr).SetFont(ST).SetFontSize(10).SetTextAlignment(TextAlignment.LEFT);#endregion//循环存放数据int i = 0, j = model.medis.Count;float XJJE=0,XJSE=0,sumh=0,oldsumh=0;float gh = pt + ht + et + kp;bool flag=false;for (i = 0; i < j; i++){//计算每行高度//商品名长度int mcleng =computeValue.GetStrLength(model.medis[i].xmmc)-1;//方法computeValue.GetStrLength()作用为获取字符串长度int ggleng= computeValue.GetStrLength(model.medis[i].ggxh);float higth = (mcleng % 8 == 0 ? mcleng / 8 : mcleng / 8 + 1)>(ggleng % 6 == 0 ? ggleng / 6 : ggleng / 6 + 1)? (mcleng % 8 == 0 ? mcleng / 8 : mcleng / 8 + 1): (ggleng % 6 == 0 ? ggleng / 6 : ggleng / 6 + 1);sumh += higth;Cell cel121 = new Cell(1, 1).SetTextAlignment(TextAlignment.CENTER).SetWidth(computeValue.computeUnit(37)).SetHeight(computeValue.computeUnit((float)(higth * 4.375))).SetFont(ST).SetFontSize(10).Add(new Paragraph(model.medis[i].xmmc).SetFixedLeading(12));Cell cel122 = new Cell(1, 2).SetTextAlignment(TextAlignment.LEFT).SetWidth(computeValue.computeUnit(24)).SetHeight(computeValue.computeUnit((float)(higth * 4.375))).SetFont(ST).SetFontSize(10).SetSplitCharacters(new KeepAllSplitCharacters()).Add(new Paragraph(model.medis[i].ggxh).SetFixedLeading(12).SetWidth(computeValue.computeUnit(24)));Cell cel123 = new Cell(1, 1).SetTextAlignment(TextAlignment.CENTER).SetWidth(computeValue.computeUnit(12)).SetHeight(computeValue.computeUnit((float)(higth * 4.375))).SetFont(ST).SetFontSize(10).Add(new Paragraph(model.medis[i].dw).SetFixedLeading(12));Cell cel124 = new Cell(1, 1).SetTextAlignment(TextAlignment.RIGHT).SetWidth(computeValue.computeUnit(25)).SetHeight(computeValue.computeUnit((float)(higth * 4.375))).SetFont(ST).SetFontSize(10).Add(new Paragraph(model.medis[i].sl).SetFixedLeading(12));Cell cel125 = new Cell(1, 1).SetTextAlignment(TextAlignment.RIGHT).SetWidth(computeValue.computeUnit(25)).SetHeight(computeValue.computeUnit((float)(higth * 4.375))).SetFont(ST).SetFontSize(10).Add(new Paragraph(model.medis[i].dj).SetFixedLeading(12));Cell cel126 = new Cell(1, 1).SetTextAlignment(TextAlignment.RIGHT).SetWidth(computeValue.computeUnit(26)).SetHeight(computeValue.computeUnit((float)(higth * 4.375))).SetFont(ST).SetFontSize(10).Add(new Paragraph(model.medis[i].je).SetFixedLeading(12));Cell cel127 = new Cell(1, 1).SetTextAlignment(TextAlignment.CENTER).SetWidth(computeValue.computeUnit(25)).SetHeight(computeValue.computeUnit((float)(higth * 4.375))).SetFont(ST).SetFontSize(10).Add(new Paragraph(model.medis[i].slv).SetFixedLeading(12));Cell cel128 = new Cell(1, 1).SetTextAlignment(TextAlignment.RIGHT).SetWidth(computeValue.computeUnit(27)).SetHeight(computeValue.computeUnit((float)(higth * 4.375))).SetFont(ST).SetFontSize(10).Add(new Paragraph(model.medis[i].se).SetFixedLeading(12));BodyTable.AddCell(cel121.SetBorder(new SolidBorder(customColor, 1)).SetBorderRight(Border.NO_BORDER).SetBorderBottom(Border.NO_BORDER).SetBorderTop(Border.NO_BORDER));BodyTable.AddCell(cel122.SetBorder(Border.NO_BORDER));BodyTable.AddCell(cel123.SetBorder(Border.NO_BORDER));BodyTable.AddCell(cel124.SetBorder(Border.NO_BORDER));BodyTable.AddCell(cel125.SetBorder(Border.NO_BORDER));BodyTable.AddCell(cel126.SetBorder(Border.NO_BORDER));BodyTable.AddCell(cel127.SetBorder(Border.NO_BORDER));BodyTable.AddCell(cel128.SetBorder(new SolidBorder(customColor, 1)).SetBorderLeft(Border.NO_BORDER).SetBorderBottom(Border.NO_BORDER).SetBorderTop(Border.NO_BORDER));BodyTable.StartNewRow();//创建新行// 添加分页//不足一页或刚好一页得情况//小计XJJE += float.Parse(model.medis[i].je);XJSE+= float.Parse(model.medis[i].se);if (sumh <= 35&& i+1==j){//中间高度float nh = 140 - gh + 25;//有分页的情况if (flag){//中间高度加8//小计Cell cel411 = new Cell(1, 2).SetTextAlignment(TextAlignment.CENTER).SetHeight(computeValue.computeUnit(4.5f)).SetFont(KT).SetFontSize(9).SetFontColor(customColor).SetPaddingBottom(-5).Add(new Paragraph("小\t\t计").SetFixedLeading(11));Cell cel421 = new Cell(1, 5).SetBorder(Border.NO_BORDER).SetTextAlignment(TextAlignment.RIGHT).SetHeight(computeValue.computeUnit(4.5f)).SetFont(ST).SetFontSize(9).Add(new Paragraph("¥" + XJJE).SetFixedLeading(11));Cell cel431 = new Cell(1, 2).SetTextAlignment(TextAlignment.RIGHT).SetHeight(computeValue.computeUnit(4.5f)).SetFont(ST).SetFontSize(9).Add(new Paragraph("¥" + XJSE).SetFixedLeading(11));//合计小计列BodyTable.AddCell(cel411.SetBorder(new SolidBorder(customColor, 1)).SetBorderBottom(Border.NO_BORDER).SetBorderTop(Border.NO_BORDER).SetBorderRight(Border.NO_BORDER));BodyTable.AddCell(cel421.SetBorder(Border.NO_BORDER));BodyTable.AddCell(cel431.SetBorder(new SolidBorder(customColor, 1)).SetBorderLeft(Border.NO_BORDER).SetBorderBottom(Border.NO_BORDER).SetBorderTop(Border.NO_BORDER));BodyTable.StartNewRow();}//不够一页的情况//合计列BodyTable.AddCell(cel41.SetBorder(new SolidBorder(customColor, 1)).SetBorderBottom(Border.NO_BORDER).SetBorderTop(Border.NO_BORDER).SetBorderRight(Border.NO_BORDER));BodyTable.AddCell(cel42.SetBorder(Border.NO_BORDER));BodyTable.AddCell(cel43.SetBorder(new SolidBorder(customColor, 1)).SetBorderLeft(Border.NO_BORDER).SetBorderBottom(Border.NO_BORDER).SetBorderTop(Border.NO_BORDER));BodyTable.StartNewRow();//新增行//价税合计大小写BodyTable.AddCell(cel51.SetBorder(new SolidBorder(customColor, 1)));//BodyTable.AddCell(cel52.SetBorder(new SolidBorder(customColor, 1)));BodyTable.AddCell(cel53.SetBorder(new SolidBorder(customColor, 1)));BodyTable.AddCell(cel55.SetBorder(new SolidBorder(customColor, 1)));BodyTable.AddCell(cel54.SetBorder(new SolidBorder(customColor, 1)));BodyTable.StartNewRow();//新增行float lasth=0;if (sumh <= 8){lasth = 8-sumh;}else if (sumh <= 12){nh += 17.5f*1;lasth = 12 - sumh;}else if (sumh <= 16){nh += 17.5f * 2;lasth = 16 - sumh;}else if (sumh <= 20){nh += 17.5f * 3;lasth = 20 - sumh;}else if (sumh <= 24){nh += 17.5f * 4;lasth = 24 - sumh;}else if (sumh <= 28){nh += 17.5f * 5;lasth = 28 - sumh;}else if (sumh <= 32){nh += 17.5f * 6;lasth = 32 - sumh;}else if (sumh <= 36){nh += 17.5f * 7;lasth = 36 - sumh;}else if (sumh <= 40){nh += 17.5f * 8;lasth = 40 - sumh;}//设置最后一行行高,因为不足一页时,最后一行可能需要占用多行行高var bzh = computeValue.computeUnit((higth + lasth) * 4.375f);cel121.SetHeight(bzh);cel122.SetHeight(bzh);cel123.SetHeight(bzh);cel124.SetHeight(bzh);cel125.SetHeight(bzh);cel126.SetHeight(bzh);cel127.SetHeight(bzh);cel128.SetHeight(bzh);BodyTable.SetHeight(computeValue.computeUnit(nh));//给表设置高度document.Add(BodyTable.SetFixedPosition(computeValue.computeUnit(4.5f), computeValue.computeUnit(243- nh) , computeValue.computeUnit(201)));//表尾备注endTable.AddCell(cel61.SetBorder(new SolidBorder(customColor, 1)));endTable.AddCell(cel62.SetBorder(new SolidBorder(customColor, 1)));float b = computeValue.computeUnit(241 - 20- nh) ;document.Add(endTable.SetFixedPosition(computeValue.computeUnit(4.5f), b, computeValue.computeUnit(201)));//价税合计图片//document.Add(img.SetFixedPosition(computeValue.computeUnit(51.5f), b + computeValue.computeUnit(24)));//开票人document.Add(ending.SetFixedPosition(computeValue.computeUnit(18), b - computeValue.computeUnit((float)8.5), computeValue.computeUnit(183)));document.Add(endingname.SetFixedPosition(computeValue.computeUnit(29), b - computeValue.computeUnit((float)8.5), computeValue.computeUnit(183)));sumh = 0;}//分页的情况else if (((sumh-higth >= 36) && (sumh - higth <= 39)&& i != 0 && i + 1 != j)|| (sumh>= 33)&& i + 2 == j){//小计Cell cel411 = new Cell(1, 2).SetTextAlignment(TextAlignment.CENTER).SetHeight(computeValue.computeUnit(4.5f)).SetFont(KT).SetFontSize(9).SetFontColor(customColor).SetPaddingBottom(-5).Add(new Paragraph("小\t\t计").SetFixedLeading(11));Cell cel421 = new Cell(1, 5).SetBorder(Border.NO_BORDER).SetTextAlignment(TextAlignment.RIGHT).SetHeight(computeValue.computeUnit(4.5f)).SetFont(ST).SetFontSize(9).Add(new Paragraph("¥" + XJJE).SetFixedLeading(11));Cell cel431 = new Cell(1, 2).SetTextAlignment(TextAlignment.RIGHT).SetHeight(computeValue.computeUnit(4.5f)).SetFont(ST).SetFontSize(9).Add(new Paragraph("¥" +XJSE).SetFixedLeading(11));//合计小计列BodyTable.AddCell(cel411.SetBorder(new SolidBorder(customColor, 1)).SetBorderBottom(Border.NO_BORDER).SetBorderTop(Border.NO_BORDER).SetBorderRight(Border.NO_BORDER));BodyTable.AddCell(cel421.SetBorder(Border.NO_BORDER));BodyTable.AddCell(cel431.SetBorder(new SolidBorder(customColor, 1)).SetBorderLeft(Border.NO_BORDER).SetBorderBottom(Border.NO_BORDER).SetBorderTop(Border.NO_BORDER));BodyTable.StartNewRow();BodyTable.AddCell(cel41.SetBorder(new SolidBorder(customColor, 1)).SetBorderTop(Border.NO_BORDER).SetBorderRight(Border.NO_BORDER));BodyTable.AddCell(cel42.SetBorder(new SolidBorder(customColor, 1)).SetBorderLeft(Border.NO_BORDER).SetBorderTop(Border.NO_BORDER).SetBorderRight(Border.NO_BORDER));BodyTable.AddCell(cel43.SetBorder(new SolidBorder(customColor, 1)).SetBorderLeft(Border.NO_BORDER).SetBorderTop(Border.NO_BORDER));BodyTable.StartNewRow();//float nh =(sumh-oldsumh)*4.375f+13.5f+38;//判断中间列表占用高度--> i * computeValue.computeUnit(10):每行数据的高度;+ computeValue.computeUnit(10):表头高度+合计行的高度;(i*15):每行数据的行间距float nh = (sumh) * 4.375f + 13.5f + 38;BodyTable.SetHeight(computeValue.computeUnit(nh));BodyTable.SetBorderBottom(new SolidBorder(customColor, 1));document.Add(BodyTable.SetFixedPosition(computeValue.computeUnit(4.5f), computeValue.computeUnit(243- nh) , computeValue.computeUnit(201)));//开票人document.Add(ending.SetFixedPosition(computeValue.computeUnit(18), computeValue.computeUnit(243- nh- 9) , computeValue.computeUnit(183)));document.Add(endingname.SetFixedPosition(computeValue.computeUnit(29), computeValue.computeUnit(243- nh- 9) , computeValue.computeUnit(183)));//分页document.Add(new AreaBreak());BodyTable = new Table(9, false);#region 表头Cell cel110 = new Cell(1, 1).SetTextAlignment(TextAlignment.CENTER).SetWidth(computeValue.computeUnit(37)).SetHeight(computeValue.computeUnit(4.5f)).SetBorderRight(Border.NO_BORDER).SetBorderBottom(Border.NO_BORDER).SetBorderTop(Border.NO_BORDER).SetFont(KT).SetFontSize(9).SetFontColor(customColor).Add(new Paragraph("项目名称"));Cell cel120 = new Cell(1, 2).SetTextAlignment(TextAlignment.LEFT).SetWidth(computeValue.computeUnit(24)).SetBorder(Border.NO_BORDER).SetFont(KT).SetFontSize(9).SetFontColor(customColor).Add(new Paragraph("规格型号"));Cell cel130 = new Cell(1, 1).SetTextAlignment(TextAlignment.CENTER).SetWidth(computeValue.computeUnit(12)).SetBorder(Border.NO_BORDER).SetFont(KT).SetFontSize(9).SetFontColor(customColor).Add(new Paragraph("单位"));Cell cel140 = new Cell(1, 1).SetTextAlignment(TextAlignment.RIGHT).SetWidth(computeValue.computeUnit(25)).SetBorder(Border.NO_BORDER).SetFont(KT).SetFontSize(9).SetFontColor(customColor).Add(new Paragraph("数量"));Cell cel150 = new Cell(1, 1).SetTextAlignment(TextAlignment.RIGHT).SetWidth(computeValue.computeUnit(25)).SetBorder(Border.NO_BORDER).SetFont(KT).SetFontSize(9).SetFontColor(customColor).Add(new Paragraph("单价"));Cell cel160 = new Cell(1, 1).SetTextAlignment(TextAlignment.RIGHT).SetWidth(computeValue.computeUnit(26)).SetBorder(Border.NO_BORDER).SetFont(KT).SetFontSize(9).SetFontColor(customColor).Add(new Paragraph("金额"));Cell cel170 = new Cell(1, 1).SetTextAlignment(TextAlignment.CENTER).SetBorder(Border.NO_BORDER).SetWidth(computeValue.computeUnit(25)).SetFont(KT).SetFontSize(9).SetFontColor(customColor).Add(new Paragraph("税率/征收率"));Cell cel180 = new Cell(1, 1).SetTextAlignment(TextAlignment.RIGHT).SetWidth(computeValue.computeUnit(27)).SetBorderLeft(Border.NO_BORDER).SetBorderBottom(Border.NO_BORDER).SetBorderTop(Border.NO_BORDER).SetFont(KT).SetFontSize(9).SetFontColor(customColor).Add(new Paragraph("税额"));BodyTable.AddCell(cel110.SetBorder(new SolidBorder(customColor, 1)));BodyTable.AddCell(cel120);BodyTable.AddCell(cel130);BodyTable.AddCell(cel140);BodyTable.AddCell(cel150);BodyTable.AddCell(cel160);BodyTable.AddCell(cel170);BodyTable.AddCell(cel180.SetBorder(new SolidBorder(customColor, 1)));BodyTable.StartNewRow();#endregionXJJE = 0; XJSE = 0;//oldsumh = sumh;sumh = 0;flag = true;continue;}//添加页码int n=pdf.GetNumberOfPages();if (n>1){for (int p = 2; p <= n; p++){document.ShowTextAligned(new Paragraph(String.Format("共" + n + "页 第" + p+"页")).SetFontSize(10).SetFont(ST),computeValue.computeUnit(200), computeValue.computeUnit(272), p, TextAlignment.RIGHT,VerticalAlignment.TOP, 0);}}// 关闭文档document.Close();Console.WriteLine("PDF发票已生成:" + outputPath);}}catch (Exception e){Console.WriteLine(e.Message);throw;}}/// <summary>
/// 金额转换成中文大写金额
/// </summary>
/// <param name="LowerMoney">eg:10.74</param>
/// <returns></returns>
private static string MoneyToUpper(string LowerMoney)
{string functionReturnValue = null;bool IsNegative = false; // 是否是负数if (LowerMoney.Trim().Substring(0, 1) == "-"){// 是负数则先转为正数LowerMoney = LowerMoney.Trim().Remove(0, 1);IsNegative = true;}string strLower = null;string strUpart = null;string strUpper = null;int iTemp = 0;// 保留两位小数 123.489→123.49  123.4→123.4LowerMoney = Math.Round(double.Parse(LowerMoney), 2).ToString();if (LowerMoney.IndexOf(".") > 0){if (LowerMoney.IndexOf(".") == LowerMoney.Length - 2){LowerMoney = LowerMoney + "0";}}else{LowerMoney = LowerMoney + ".00";}strLower = LowerMoney;iTemp = 1;strUpper = "";while (iTemp <= strLower.Length){switch (strLower.Substring(strLower.Length - iTemp, 1)){case ".":strUpart = "圆";break;case "0":strUpart = "零";break;case "1":strUpart = "壹";break;case "2":strUpart = "贰";break;case "3":strUpart = "叁";break;case "4":strUpart = "肆";break;case "5":strUpart = "伍";break;case "6":strUpart = "陆";break;case "7":strUpart = "柒";break;case "8":strUpart = "捌";break;case "9":strUpart = "玖";break;}switch (iTemp){case 1:strUpart = strUpart + "分";break;case 2:strUpart = strUpart + "角";break;case 3:strUpart = strUpart + "";break;case 4:strUpart = strUpart + "";break;case 5:strUpart = strUpart + "拾";break;case 6:strUpart = strUpart + "佰";break;case 7:strUpart = strUpart + "仟";break;case 8:strUpart = strUpart + "万";break;case 9:strUpart = strUpart + "拾";break;case 10:strUpart = strUpart + "佰";break;case 11:strUpart = strUpart + "仟";break;case 12:strUpart = strUpart + "亿";break;case 13:strUpart = strUpart + "拾";break;case 14:strUpart = strUpart + "佰";break;case 15:strUpart = strUpart + "仟";break;case 16:strUpart = strUpart + "万";break;default:strUpart = strUpart + "";break;}strUpper = strUpart + strUpper;iTemp = iTemp + 1;}strUpper = strUpper.Replace("零拾", "零");strUpper = strUpper.Replace("零佰", "零");strUpper = strUpper.Replace("零仟", "零");strUpper = strUpper.Replace("零零零", "零");strUpper = strUpper.Replace("零零", "零");strUpper = strUpper.Replace("零角零分", "整");strUpper = strUpper.Replace("零分", "整");strUpper = strUpper.Replace("零角", "零");strUpper = strUpper.Replace("零亿零万零圆", "亿圆");strUpper = strUpper.Replace("亿零万零圆", "亿圆");strUpper = strUpper.Replace("零亿零万", "亿");strUpper = strUpper.Replace("零万零圆", "万圆");strUpper = strUpper.Replace("零亿", "亿");strUpper = strUpper.Replace("零万", "万");strUpper = strUpper.Replace("零圆", "圆");strUpper = strUpper.Replace("零零", "零");Console.WriteLine("零角零分");// 对壹圆以下的金额的处理if (strUpper.Substring(0, 1) == "圆"){strUpper = strUpper.Substring(1, strUpper.Length - 1);}if (strUpper.Substring(0, 1) == "零"){strUpper = strUpper.Substring(1, strUpper.Length - 1);}if (strUpper.Substring(0, 1) == "角"){strUpper = strUpper.Substring(1, strUpper.Length - 1);}if (strUpper.Substring(0, 1) == "分"){strUpper = strUpper.Substring(1, strUpper.Length - 1);}if (strUpper.Substring(0, 1) == "整"){strUpper = "零圆整";}functionReturnValue = strUpper;if (IsNegative == true){return "负" + functionReturnValue;}else{return string.Format(functionReturnValue, Encoding.GetEncoding("GB2312"));}
}public class ComputeValue
{/// <summary>/// 毫米转换磅/// </summary>/// <param name="millimetre"></param>/// <returns></returns>public float computeUnit(float millimetre){return millimetre / 10 / 2.54f * 72;}/// <summary>/// 获取字符串长度/// </summary>/// <param name="str"></param>/// <returns></returns>public int GetStrLength(string str){double length = 0;str = str.Replace(" ", "");for (int i = 0; i < str.Length; i++) {if (str[i] >= 0x4E00 && str[i] <= 0x9FA5){length += 1;}else{length += 0.5;}}return (int)Math.Ceiling(length);}
}

步骤二:创建PdfEventHandler类:

/// <summary>
/// 需要继承IEventHandler
/// </summary>
public class PdfEventHandler : IEventHandler
{private NaturalSystemPdfModel _model;private PdfFont KT;private PdfFont ST;private PdfFont CN;//构造方法:带入模型以及字体public PdfEventHandler(NaturalSystemPdfModel model, PdfFont kt, PdfFont st, PdfFont cn) {_model = model;KT = kt;ST = st;CN = cn;}public void HandleEvent(Event e){PdfDocumentEvent docEvent = (PdfDocumentEvent)e;PdfDocument pdfDoc = docEvent.GetDocument();PdfPage page = docEvent.GetPage();PdfCanvas pdfCanvas = new PdfCanvas(page.NewContentStreamBefore(), page.GetResources(), pdfDoc);Rectangle pageSize = page.GetPageSize();ComputeValue computeValue = new ComputeValue();// 定义自定义RGB颜色(例如,红色)DeviceRgb customColor = new DeviceRgb(128, 0, 0);Canvas canvas = new Canvas(pdfCanvas, pageSize);#region 票头#region 双横线//添加横线pdfCanvas.MoveTo(computeValue.computeUnit(141), computeValue.computeUnit(278.5f));pdfCanvas.LineTo(computeValue.computeUnit(69), computeValue.computeUnit(278.5f));pdfCanvas.SetStrokeColor(new DeviceRgb(128, 0, 0));pdfCanvas.MoveTo(computeValue.computeUnit(141), computeValue.computeUnit(277.5f));pdfCanvas.LineTo(computeValue.computeUnit(69), computeValue.computeUnit(277.5f));pdfCanvas.SetStrokeColor(new DeviceRgb(128, 0, 0));pdfCanvas.ClosePathStroke();#endregion#region 票头左边 二维码、标签码// 添加动态二维码Image EWM = new Image(ImageDataFactory.Create(_model.qrCode)).SetWidth(computeValue.computeUnit(20)).SetHeight(computeValue.computeUnit(20));canvas.Add(EWM.SetFixedPosition(1, computeValue.computeUnit(7), computeValue.computeUnit(271)));//二维码中间的“税”字图标Image S = new Image(ImageDataFactory.Create(System.IO.Path.Combine(_model.webHost, "images", "Shui.png"))).SetWidth(computeValue.computeUnit(4)).SetHeight(computeValue.computeUnit(4));canvas.Add(S.SetFixedPosition(1, computeValue.computeUnit(15), computeValue.computeUnit((float)279)));//Image EWM = new BarcodeQRCode("", 54, 56, null);//添加标签//Image BQM = new Image(ImageDataFactory.Create("E://111.png"))//    .SetWidth(computeValue.computeUnit(28))//    .SetHeight(computeValue.computeUnit(20));//canvas.Add(BQM.SetFixedPosition(1, computeValue.computeUnit(29), computeValue.computeUnit(271)));#endregion#region 票头文字及印章// 添加发票内容Paragraph heading = new Paragraph(_model.type).SetTextAlignment(TextAlignment.CENTER).SetFont(KT).SetFontColor(customColor).SetFontSize(19);if (_model.type.Contains("增值税")){canvas.Add(heading.SetFixedPosition(computeValue.computeUnit(56), computeValue.computeUnit(280.35f), computeValue.computeUnit(100)));}else {canvas.Add(heading.SetFixedPosition(computeValue.computeUnit(72), computeValue.computeUnit(280.35f), computeValue.computeUnit(70)));}// 添加发票章图片./Images/fapiaozhang.pngImage stamp = new Image(ImageDataFactory.Create(System.IO.Path.Combine(_model.webHost, "images", "fapiaozhang.png"))).SetWidth(computeValue.computeUnit(28)).SetHeight(computeValue.computeUnit(20));canvas.Add(stamp.SetFixedPosition(1, computeValue.computeUnit(92), computeValue.computeUnit(269)));#endregion#region 票头右上角信息// 发票号码:Paragraph invoiceInfo_Num = new Paragraph().Add("发票号码:").SetFont(KT).SetFontSize(9).SetFontColor(customColor).SetTextAlignment(TextAlignment.LEFT);canvas.Add(invoiceInfo_Num.SetFixedPosition(computeValue.computeUnit(155), computeValue.computeUnit(280.65f), computeValue.computeUnit(19.06f)));Paragraph invoiceInfo_Numc = new Paragraph().Add(_model.fphm).SetFont(ST).SetFontSize(9).SetTextAlignment(TextAlignment.LEFT);canvas.Add(invoiceInfo_Numc.SetFixedPosition(computeValue.computeUnit(170), computeValue.computeUnit(280.65f), computeValue.computeUnit(33.36f)));// 开票日期:Paragraph invoiceInfo_Date = new Paragraph().Add("开票日期: ").SetFont(KT).SetFontSize(9).SetFontColor(customColor).SetTextAlignment(TextAlignment.LEFT);canvas.Add(invoiceInfo_Date.SetFixedPosition(computeValue.computeUnit(155), computeValue.computeUnit(274.5f), computeValue.computeUnit(19.06f)));Paragraph invoiceInfo_Datec = new Paragraph().Add(_model.kprq.ToString("yyyy年MM月dd日")).SetFont(ST).SetFontSize(9).SetTextAlignment(TextAlignment.LEFT);canvas.Add(invoiceInfo_Datec.SetFixedPosition(computeValue.computeUnit(170), computeValue.computeUnit(274.5f), computeValue.computeUnit(33.36f)));#endregion#endregion#region 购买方信息//添加表格Table HeadTable = new Table(4, false);Cell cel01 = new Cell(1, 1).SetTextAlignment((TextAlignment)TextAlignment.CENTER).SetWidth(computeValue.computeUnit(6)).SetHeight(computeValue.computeUnit(22)).Add(new Paragraph("购买方信息").SetFixedLeading(12).SetFontColor(customColor).SetFont(KT).SetFontSize(9).SetFontColor(customColor));Cell cel02 = new Cell(1, 1).SetTextAlignment(TextAlignment.LEFT).SetVerticalAlignment(VerticalAlignment.MIDDLE).SetWidth(computeValue.computeUnit(94.5f)).SetHeight(computeValue.computeUnit(22)).Add(new Paragraph("名称:").SetFontColor(customColor).SetFont(KT).SetFontSize(9).SetFontColor(customColor).Add(new Text(_model.gmfmc).SetFontSize(10).SetFont(ST).SetFontColor(ColorConstants.BLACK))).Add(new Paragraph("统一社会信用代码/纳税人识别号:").SetFontColor(customColor).SetFont(KT).SetFontSize(9).SetFontColor(customColor).Add(new Text(_model.gmfnsrsbh).SetFontSize(10).SetFont(ST).SetFontColor(ColorConstants.BLACK)));Cell cel03 = new Cell(1, 1).SetTextAlignment(TextAlignment.CENTER).SetWidth(computeValue.computeUnit(6)).SetHeight(computeValue.computeUnit(22)).Add(new Paragraph("销售方信息").SetFixedLeading(12).SetFontColor(customColor).SetFont(KT).SetFontSize(9).SetFontColor(customColor));Cell cel04 = new Cell(1, 1).SetTextAlignment(TextAlignment.LEFT).SetVerticalAlignment(VerticalAlignment.MIDDLE).SetWidth(computeValue.computeUnit(94.5f)).SetHeight(computeValue.computeUnit(22)).Add(new Paragraph("名称:").SetFontColor(customColor).SetFont(KT).SetFontSize(9).SetFontColor(customColor).Add(new Text(_model.xsfmc).SetFontSize(10).SetFont(ST).SetFontColor(ColorConstants.BLACK))).Add(new Paragraph("统一社会信用代码/纳税人识别号:").SetFontColor(customColor).SetFont(KT).SetFontSize(9).SetFontColor(customColor).Add(new Text(_model.xsfnsrsbh).SetFontSize(10).SetFont(ST).SetFontColor(ColorConstants.BLACK)));HeadTable.AddCell(cel01.SetBorder(new SolidBorder(customColor, 1)));HeadTable.AddCell(cel02.SetBorder(new SolidBorder(customColor, 1)));HeadTable.AddCell(cel03.SetBorder(new SolidBorder(customColor, 1)));HeadTable.AddCell(cel04.SetBorder(new SolidBorder(customColor, 1)));canvas.Add(HeadTable.SetFixedPosition(computeValue.computeUnit(4.5f), computeValue.computeUnit(243), computeValue.computeUnit(201)));#endregion// 添加页脚页码//if (pdfDoc.GetNumberOfPages() > 1)//{//    #region 页眉//    Paragraph p = new Paragraph("共" + pdfDoc.GetNumberOfPages() + "页 第" + pdfDoc.GetPageNumber(page) + "页")//    .SetFontSize(9)//    .SetFont(ST)//    .SetFixedPosition(computeValue.computeUnit(186), computeValue.computeUnit(266.5f), computeValue.computeUnit(141));//    canvas.Add(p);//    #endregion//}//canvas.Add(p);canvas.Close();}
}

完成!(创作不易,点赞鼓励_

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/26447.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【模拟-BM100 设计LRU缓存结构】

题目 BM100 设计LRU缓存结构 描述 设计LRU(最近最少使用)缓存结构&#xff0c;该结构在构造时确定大小&#xff0c;假设大小为 capacity &#xff0c;操作次数是 n &#xff0c;并有如下功能: Solution(int capacity) 以正整数作为容量 capacity 初始化 LRU 缓存get(key)&am…

【PyTorch 新手基础】Regularization -- 减轻过拟合 overfitting

Overfit 过拟合&#xff0c;效果如最右图所示 常见应对方案如下&#xff1a; 增大数据集入手&#xff1a;More data or data argumentation简化模型参数入手&#xff1a;Constraint model complexity (shallow model, regularization) or dropout dropout: torch.nn.Dropout(0…

搭建一个好玩的 RSS 订阅网站记录

全文相关链接 Github仓库创建链接Railway官网Supabase官网f-droid上的co.appreactor.news应用下载链接Railway账户使用量估算链接 全文相关代码 原文地址: https://blog.taoshuge.eu.org/p/270/ Dockerfile FROM docker.io/miniflux/miniflux:2.1.3环境变量 DATABASE_URL…

Java线程池参数和处理流程

线程池是一种管理和重用线程资源的机制&#xff0c;是利用池化思想设置和管理多线程的工具。线程池维护一定数量的线程&#xff0c;当有任务需要时&#xff0c;就从中选择一个的线程用来执行任务&#xff0c;当使用完成后该线程就会被重新放回线程池中&#xff0c;通过这样循环…

Apollo配置中心最佳实践

携程配置中心地址&#xff1a;GitCode - 全球开发者的开源社区,开源代码托管平台 1.1 Apollo配置中心介绍 Apollo&#xff08;阿波罗&#xff09;是开源配置管理中心&#xff0c;能够集中化管理应用不同环境、不同集群的配置&#xff0c;配置修改后能够实时推送到应用端…

ASM字节码插桩实现点击防抖

思路&#xff1a;在点击事件onclick的时候&#xff0c;将view的onclick在给定的时间给拦截掉。以前我们可能都是用一个util来拦截&#xff0c;这样在每个点击事件都得去判断&#xff0c;那么这里就用字节码插桩的形式来实现一下。 ASM的引入 dependencies {implementation gr…

QT day01

思维导图 QT编程 实现一个账号登录界面 代码&#xff1a; myweidget.h #ifndef MYWEIDGET_H #define MYWEIDGET_H#include <QWidget> #include <QIcon> //图标类 #include <QLineEdit> //行编辑器类 #include <QLabel> //标签类 #…

【Redis】安装和命令行客户端

https://www.bilibili.com/video/BV1cr4y1671t https://www.oz6.cn/articles/58 redis 非结构化有&#xff1a; 键值类型(Redis)文档类型(MongoDB)列类型(HBase)Graph:类型(Neo4j) 扩展性&#xff1a;水平即为分布式扩展 redis特征 键值&#xff08;key-value&#xff09;型…

【springBoot学习篇】springBoot集成mybatis

目录 第一步&#xff1a;新建spring项目的时候&#xff0c;需要勾选mybatis框架和jdbc连接数据库的包 第二步&#xff1a;在resource目录下面的配置文件当中添加以下的内容&#xff1a;配置数据源 第三步&#xff1a;配置实体类 第四步&#xff1a;添加一个对象的增删改查方…

上位机图像处理和嵌入式模块部署(h750 mcu和图像处理)

【 声明&#xff1a;版权所有&#xff0c;欢迎转载&#xff0c;请勿用于商业用途。 联系信箱&#xff1a;feixiaoxing 163.com】 前面我们说过&#xff0c;h750和之前的103、407相比较&#xff0c;本身cpu频率比较高&#xff0c;flash大小一般&#xff0c;但是ram比较大&#x…

群辉其它方案远程访问(ZeroTier篇)

目录 1、注册ZeroTier 2、创建网络 3、下载安装客户端 (1)Windows (2)移动端 i.Android i.iOS (a)注册新ID (b)登陆苹果应用商店 iii.群辉NAS 4、客户端加入网络 (a)Windows (b)Android (c)群辉NAS 5、使用 群辉的远程访问,最标准的做法就是使用…

自动控制原理【期末复习】(二)

无人机上桨之后可以在调试架上先调试&#xff1a; 1.根轨迹的绘制 /// 前面针对的是时域分析&#xff0c;下面针对频域分析&#xff1a; 2.波特图 3.奈维斯特图绘制 1.奈氏稳定判据 2.对数稳定判据 3.相位裕度和幅值裕度

【全篇】Python从零基础到入门

文章目录 第一章 基础语法1.字面量2.注释3.变量4.数据类型5.数据类型转换6.标识符7.运算符8.字符串拓展1.字符串的三种定义方式2.字符串拼接&#xff08;不用&#xff09;3.字符串格式化&#xff08;了解&#xff09;4.格式化的精度控制5.字符串格式化2&#xff08;常用&#x…

跟《经济学人》学英文:2024年6月8日这期:Part 01

本文是对《经济学人》杂志2024.6.8这期的英文学习。 Narendra Modi looks likely to serve a third term as India’s prime minister, after his Bharatiya Janata Party and its allies won a slim majority. The ruling alliance won 293 seats, compared with the opposi…

【代码随想录】【算法训练营】【第36天】[452]用最少数量的箭引爆气球 [435]无重叠区间 [763]划分字母区间

前言 思路及算法思维&#xff0c;指路 代码随想录。 题目来自 LeetCode。 day 36&#xff0c;周三&#xff0c;最难坚持的一天~ 题目详情 [452] 用最少数量的箭引爆气球 题目描述 452 用最少数量的箭引爆气球 解题思路 前提&#xff1a;区间可能重叠 思路&#xff1a;…

YOLO系列理论解读 v1 v2 v3

YOLO系列理论解读 YOLO v1&#xff08;You Only Look Once:Unified, Real-Time Object Detection&#xff09; YOLO v1实现步骤 将一幅图像分成SxS个网格(grid cell)&#xff0c;如果某个object的中心落在这个网格中&#xff0c;则这个网格就负责预测这个object。 通常情况…

服务器无法远程桌面连接,解决服务器进行无法远程桌面连接方法有哪些

当服务器无法建立远程桌面连接时&#xff0c;通常涉及多个层面的排查和修复。下面将详细列举一些专业的解决方法&#xff0c;以应对服务器远程桌面连接问题。 一、基础排查与验证 1. 确认网络连通性&#xff1a; - 使用ping命令检查客户端与服务器之间的网络连通性。 - …

数组(C语言)(详细过程!!!)

目录 数组的概念 一维数组 sizeof计算数组元素个数 二维数组 C99中的变⻓数组 数组的概念 数组是⼀组相同类型元素的集合。 数组分为⼀维数组和多维数组&#xff0c;多维数组⼀般比较多见的是二维数组。 从这个概念中我们就可以发现2个有价值的信息&#xff1a;(1)数…

什么是相对路径?什么是绝对路径?打包时路径怎么搞?

简单点说&#xff1a; 绝对路径&#xff1a;绝对路径是一个完整的路径&#xff0c;从根目录开始一直到目标文件或目录的路径。通常我们直接使用"/ "代表从根目录开始的目录路径。它提供了文件或目录在文件系统中的确切位置&#xff0c;与当前工作目录无关。绝对路径…

AMS深入浅出

目标&#xff1a; 1. 一、AMS启动流程 ActivityManagerService是 安卓10 以后&#xff0c;将AMS拆分出ActivityTaskManagerService。 1.1 启动入口 AMS是由SystemServer进程启动&#xff0c;在启动过程 startBootStripService&#xff0c;会启动AMS和ATMS服务。 SystemSe…