原创|使用教程|编辑:张莹心|2021-10-27 09:54:49.550|阅读 410 次
概述:自 VintaSoft Imaging .NET SDK 10.1 版以来,可以以编程方式编辑现有的 DOCX 和 XLSX 文档。本文使用此功能创建一个简单且易于定制的 PDF 文档格式发票生成器。
# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>
VintaSoftImaging.NET SDK是一个为.NET开发人员开发的,强大而易于使用的图像工具包。它可以让你加载、查看、处理、打印和保存数字图像,可将它们转换为不同的图像格式,可用多种TIFF和动态GIF文件提高您的工作效率。
PDF 文档是矢量文档,不具备内容排版功能。可以编写编程代码,使用矢量图形在 PDF 页面上绘制文档标记,但代码会很复杂,而且其开发可能需要很长时间。 经常出现以 PDF 文档格式创建发票的任务。通常代表发票的文档包含一个复杂的布局:带有徽标和公司信息的标题、有关卖方和买方的信息、包含订购项目的表格、包含附加信息的页脚。
一个复杂的任务可以通过分为 2 个部分来简化:
发票生成器应包含两个主要部分:
// 使用此代码的项目必须引用以下程序集:
// - Vintasoft.Imaging
// - Vintasoft.Imaging.Office.OpenXml
// - Vintasoft.Imaging.Pdf
// - Vintasoft.Barcode
/// <summary>
/// 生成发票,基于DOCX文档模板。
/// </summary>
public static void GenerateInvoiceUseDocxTemplate()
{
//创建DOCX文档编辑器和使用文件“Invoice_template.docx”作为文档模板
使用(Vintasoft.Imaging.Office.OpenXml.Editor。DocxDocumentEditor编辑器=
新Vintasoft.Imaging.Office.OpenXml.Editor。DocxDocumentEditor(“Invoice_template.docx” ))
{
// 生成 30 个项目的测试发票数据
InvoiceData testData = GetTestData(30);
// 填写发票数据
FillInvoiceData(editor, testData);
// 如有必要,将发票保存到 DOCX 文档
//editor.Save("Invoice.docx");
// 将发票导出到 PDF 文档
editor.Export( "Invoice_docx.pdf" );
}
}
/// <summary>
/// 使用 DOCX 文档编辑器填写发票数据。
/// </summary>
/// <param name="documentEditor">DOCX 文档编辑器。</param>
/// <param name="invoiceData">发票数据。</param>
private static void FillInvoiceData (
Vintasoft.Imaging.Office.OpenXml.Editor。DocxDocumentEditor文档编辑器,
发票数据发票数据)
{
//
使用( Vintasoft.Imaging.VintasoftImage qrCodeImage = invoiceData.GetBarcodeImage(200))创建 200x200 像素的二维码图像
{
// 将条码图像设置为索引 1 处的图像元素
documentEditor.Images[1].SetImage(qrCodeImage);
}
// 填充文档头
documentEditor.Body[ "[company_name]" ] = invoiceData.Company.CompanyName;
documentEditor.Body[ "[company_address]" ] = invoiceData.Company.Address;
documentEditor.Body[ "[company_city]" ] = invoiceData.Company.City;
documentEditor.Body[ "[company_phone]" ] = invoiceData.Company.GetPhones();
documentEditor.Body[ "[invoice_number]" ] = invoiceData.InvoiceNumber;
documentEditor.Body[ "[invoice_date]" ] = System. 日期时间.Now.ToShortDateString();
// 获取文档
Vintasoft.Imaging.Office.OpenXml.Editor 的所有表格。OpenXmlDocumentTable [] 表格 = documentEditor.Tables;
// 填充“客户信息”表
Vintasoft.Imaging.Office.OpenXml.Editor. OpenXmlDocumentTable customerInformationTable = tables[0];
SetCompanyInformation(customerInformationTable, "billing" , invoiceData.BillingAddress);
SetCompanyInformation(customerInformationTable, "shipping" , invoiceData.ShippingAddress);
// 填充“运输方式”表
Vintasoft.Imaging.Office.OpenXml.Editor. OpenXmlDocumentTable shippingMethodTable = tables[1];
ShippingMethodTable[ "[shipping_method]" ] = invoiceData.ShippingMethod;
// 填充“订单信息”表
Vintasoft.Imaging.Office.OpenXml.Editor. OpenXmlDocumentTable orderInformationTable = 表格[2];
Vintasoft.Imaging.Office.OpenXml.Editor。OpenXmlDocumentTableRow templateRow = orderInformationTable[1];
int orderItemNumber = 1;
//对于invoice
foreach中的每个项目(invoiceItem orderItem in invoiceData.OrderItems)
{
// 复制模板行并在模板行后插入副本
Vintasoft.Imaging.Office.OpenXml.Editor. OpenXmlDocumentTableRow currentRow = templateRow;
templateRow =(Vintasoft.Imaging.Office.OpenXml.Editor。OpenXmlDocumentTableRow)templateRow.InsertCopyAfterSelf();
// 填充当前行的数据
currentRow[ "[p_n]" ] = orderItemNumber.ToString();
currentRow[ "[p_description]" ] = orderItem.Product;
currentRow[ "[p_qty]" ] = orderItem.Quantity.ToString();
currentRow[ "[p_unit_price]" ] = invoiceData.GetPriceAsString(orderItem.Price);
currentRow[ "[p_price_total]" ] = invoiceData.GetPriceAsString(orderItem.TotalPrice);
orderItemNumber++;
}
// 删除模板行
templateRow.Remove();
// 填写订单信息汇总字段
orderInformationTable[ "[subtotal]" ] = invoiceData.GetPriceAsString(invoiceData.Subtotal);
orderInformationTable[ "[tax]" ] = invoiceData.GetPriceAsString(invoiceData.Tax);
orderInformationTable[ "[shipping]" ] = invoiceData.GetPriceAsString(invoiceData.Shipping);
orderInformationTable[ "[grand_total]" ] = invoiceData.GetPriceAsString(invoiceData.GrandTotal);
// 填充“Notes”表
Vintasoft.Imaging.Office.OpenXml.Editor. OpenXmlDocumentTable notesTable = 表格[3];
notesTable[ “[日期]” ] = System. 日期时间.Now.ToShortDateString();
notesTable[ “[时间]” ] = System. 日期时间.Now.ToLongTimeString();
}
/// <summary>
/// 设置公司信息。
/// </summary>
/// <param name="table">表格。</param>
/// <param name="fieldName">字段名称。</param>
/// <param name="company">公司。</param>
private static void SetCompanyInformation(
Vintasoft.Imaging.Office.OpenXml.Editor。OpenXmlDocumentTable表,字符串字段名称
,
公司公司)
{
串fieldFormat =串.Format(“[{0} _ {1}]”,fieldName的,“{0}”);
table[ string .Format(fieldFormat, "company" )] = company.CompanyName;
table[ string .Format(fieldFormat, "name" )] = company.Name;
table[ string .Format(fieldFormat, "address" )] = company.Address;
table[ string .Format(fieldFormat, "phone" )] = company.GetPhones();
table[ string .Format(fieldFormat, "city" )] = company.City;
}
以下代码为发票生成器创建测试数据:/// <summary>
/// 返回发票测试数据。
/// </summary>
/// <returns>发票测试数据。</returns>
public static InvoiceData GetTestData( int orderItemsCount)
{
公司 vintasoftCompany = new Company();
vintasoftCompany.CompanyName = "VintaSoft Ltd." ;
vintasoftCompany.Address = "M.Nagibina Ave. 33a/47" ;
vintasoftCompany.City = "Rostov-on-Don, 344068, Russia" ;
vintasoftCompany.Phones.Add( "+78632924297" );
vintasoftCompany.Phones.Add( "+78632924322 (传真)" );
公司 billingCompany = new Company();
billingCompany.CompanyName = "Billing Global Company Inc." ;
billingCompany.Name = "Q 先生" ;
billingCompany.Address = " Address1 " ;
billingCompany.City = "City1" ;
billingCompany.Phones.Add( "9876543210" );
billingCompany.Phones.Add( "7654321098 (传真)" );
公司shipingCompany = new Company();
shipingCompany.CompanyName = "Shipping Global Company Inc." ;
shipingCompany.Name = "Z 先生" ;
shipingCompany.Address = " Address2 " ;
shipingCompany.City = "City2" ;
shipingCompany.Phones.Add( " 1122334455 " );
shipingCompany.Phones.Add( "5544332211 (传真)" );
发票数据数据 =新发票数据();
系统。随机随机=新系统。随机();
data.InvoiceNumber = string .Format ( "{0}-{1}" , random.Next(100000, 999999), random.Next(0, 9));
data.Company = vintasoftCompany;
data.BillingAddress = billingCompany;
data.ShippingAddress = shipingCompany;
InvoiceItem[] availableProducts = new InvoiceItem[] {
new InvoiceItem( "VintaSoft Imaging .NET SDK, Site license for Desktop PCs" , 659.95f),
new InvoiceItem( "VintaSoft Annotation .NET Plug-in, Site license for Desktop PCs" , 449.95f),
new InvoiceItem( "VintaSoft Office .NET Plug-in, Site license for Desktop PCs" , 569.95f),
new InvoiceItem( "VintaSoft PDF .NET Plug-in (Reader+Writer), Site license for Desktop PCs" , 1499.95f),
new InvoiceItem( "VintaSoft PDF .NET Plug-in (Reader+Writer+VisualEditor),台式电脑的站点许可”, 2999.95f),
new InvoiceItem( "VintaSoft JBIG2 .NET Plug-in, Site license for Desktop PCs" , 1139.95f),
new InvoiceItem( "VintaSoft JPEG2000 .NET Plug-in, Site license for Desktop PCs" , 689.95f) ,
new InvoiceItem( "VintaSoft Document Cleaup .NET Plug-in, Site license for Desktop PCs" , 569.95f),
new InvoiceItem( "VintaSoft OCR .NET Plug-in, Site license for Desktop PCs" , 509.95f),
new InvoiceItem (“VintaSoft DICOM .NET 插件(编解码器+MPR),台式电脑的站点许可”,1199.95f),
新的InvoiceItem(“VintaSoft 表单处理 .NET 插件,台式机站点许可”,509.95f),
新的InvoiceItem(“VintaSoft Barcode .NET SDK(1D+2D 读写器),台式机站点许可”,1379.95f),
new InvoiceItem(“VintaSoft Twain .NET SDK,站点许可”,539.95f)
};
for ( int i = 0; i < orderItemsCount; i++)
{
int数量 = 1 + random.Next(10);
int index = random.Next(availableProducts.Length - 1);
data.OrderItems.Add( new InvoiceItem(availableProducts[index],quantity));
}
返回数据;
}
/// <summary>
/// 表示公司信息。
/// </summary>
公共 类公司
{
/// <summary>
/// 公司名称。
/// </summary>
公共 字符串CompanyName;
/// <summary>
/// 人名。
/// </summary>
公共 字符串名称;
/// <summary>
/// 公司所在地城市。
/// </summary>
公共 字符串城市;
/// <summary>
/// 公司地址。
/// </summary>
公共 字符串地址;
/// <summary>
/// 公司电话号码。
/// </summary>
公共System.Collections.Generic。List < string > Phones = new System.Collections.Generic。列表<字符串>();
/// <summary>
/// 返回电话号码。
/// </summary>
公共 字符串GetPhones()
{
if (Phones.Count == 1)
返回Phones[0];
系统.文本。StringBuilder结果 =新System.Text。字符串生成器();
for ( int i = 0; i < Phones.Count - 1; i++)
{
result.Append(电话[i]);
result.Append( ", " );
}
result.Append(Phones[Phones.Count - 1]);
返回结果.ToString();
}
}
/// <summary>
/// 代表发票订单项。
/// </summary>
公共 类InvoiceItem
{
/// <summary>
/// 初始化 <see cref="InvoiceItem"/> 类的新实例。
/// </summary>
/// <param name="product">产品名称。</param>
/// <param name="price">产品价格。</param>
public InvoiceItem( string product ,浮动价格)
{
产品 = 产品;
数量 = 1;
价格 = 价格;
}
/// <summary>
/// 初始化 <see cref="InvoiceItem"/> 类的新实例。
/// </summary>
/// <param name="source">来源<see cref="InvoiceItem"/>。</param>
/// <param name="quantity">产品数量。< /param>
public InvoiceItem(InvoiceItem 来源,浮动数量)
{
产品 = 来源.产品;
价格 = 来源。价格;
数量 = 数量;
}
/// <summary>
/// 产品名称。
/// </summary>
公共 字符串产品;
/// <summary>
/// 产品数量。
/// </summary>
公众 持股量;
/// <summary>
/// 产品价格。
/// </summary>
公开 浮动价格;
/// <summary>
/// 获取产品总价。
/// </summary>
公开 浮动总价
{
得到
{
退货价格 * 数量;
}
}
}
/// <summary>
/// 表示发票数据。
/// </summary>
公共 类InvoiceData
{
/// <summary>
/// 订单商品列表。
/// </summary>
公共System.Collections.Generic。List <InvoiceItem> OrderItems = new System.Collections.Generic。列表<InvoiceItem>();
/// <summary>
/// 发票编号。
/// </summary>
公共 字符串InvoiceNumber;
/// <summary>
/// 运送方式。
/// </summary>
public string ShippingMethod = "Email" ;
/// <summary>
/// 公司账单地址。
/// </summary>
public Company BillingAddress = new Company();
/// <summary>
/// 公司送货地址。
/// </summary>
public Company ShippingAddress = new Company();
/// <summary>
/// 表示公司信息的对象。
/// </summary>
public Company Company = new Company();
/// <summary>
/// 发票中使用的货币。
/// </summary>
public string Currency = "EUR" ;
/// <summary>
/// 获取或设置税值。
/// </summary>
公共 浮动税 = 0;
/// <summary>
/// 获取或设置运费。
/// </summary>
public float Shipping = 0;
/// <summary>
/// 获取小计值。
/// </summary>
公共 浮动小计
{
得到
{
浮点值 = 0;
for ( int i = 0; i < OrderItems.Count; i++)
value += OrderItems[i].TotalPrice;
返回值;
}
}
/// <summary>
/// 获取总计值。
/// </summary>
公众 持股量GrandTotal
{
得到
{
返回小计+运费+税金;
}
}
/// <summary>
/// 以字符串形式返回价格。
/// </summary>
/// <param name="price">价格。</param>
/// <returns>字符串表示的价格。</returns>
public string GetPriceAsString( float price)
{
return string .Format( "{0} {1}" , price.ToString( "f2" , System.Globalization. CultureInfo .InvariantCulture), Currency);
}
/// <summary>
/// 创建二维码图像。
/// </summary>
/// <param name="size">条形码大小。</param>
/// <returns><see cref="Vintasoft.Imaging.VintasoftImage"/> 类的一个实例包含二维码图像。</returns>
public Vintasoft.Imaging。VintasoftImage GetBarcodeImage( int size)
{
Vintasoft.Barcode.BarcodeWriter writer = new Vintasoft.Barcode.BarcodeWriter();
writer.Settings.Barcode = Vintasoft.Barcode.BarcodeType.QR;
writer.Settings.Value = string .Format ( "INVOICE={0};TOTAL={1}" , InvoiceNumber, GetPriceAsString(GrandTotal));
writer.Settings.SetWidth(size);
Vintasoft.Imaging。VintasoftImage结果 =
新的Vintasoft.Imaging。VintasoftImage (writer.GetBarcodeAsBitmap(), true );
result.Crop(新。System.Drawing中矩形(0,0,result.Width,result.Width));
返回结果;
}
}
购买最新正版授权!""
慧都年终盛典火爆开启,一年仅一次的最强促销,十八周年盛“惠”不容错过!!优惠详情点击查看>>
本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@ke049m.cn