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