显示描述:
1、每页显示必须带有发票头、"销售方和购买方信息"
2、明细填充为:当n≤8 行时,发票总高度140mm,每条发票明细行款高度4.375mm;
当8<n≤12行时,发票高度增加17.5mm,不换页;
以此类推
根据实际情况能放下"应税明细信息"和"票尾"信息后换页,注意是否为最后一行
3、超过一页,则每页显示小计
显示效果:
1、只有一页的情况:
2、有两页的且刚好能放下所有明细的情况(超过一页后,开票日期下显示页数):
代码实现:
步骤一、创建主方法,可传入需要的参数模型
csharp
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))
.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();
#endregion
XJJE = 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.4
LowerMoney = 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类:
csharp
/// <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.png
Image 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();
}
}
完成!(创作不易,点赞鼓励^_^)