/*
IDE: VS 2022 17.5
OS: windows 10
.net 8.0
iText 8.0
System.Text.Encoding.CodePages
*/
namespace ConsoleAppPdfdemo
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text;
using iText.IO.Font;
using iText.IO.Image;
using iText.Kernel.Font;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Canvas.Draw;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;
using iText.Pdfa;
using iText.IO;
using iText.Kernel.Pdf.Xobject;
using iText.Kernel.Utils;
using iText.Kernel.Events;
using iText.Kernel.Colors;
using iText.Kernel.Geom;
using iText.Kernel.Pdf.Canvas;
using iText.Layout.Borders;
///
///
///
internal class Program
{
///
/// geovindu, Geovin Du, 涂聚文
///
///
///
static void Main(string[] args)
{
//string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
try
{
Console.WriteLine("Hello,CSharp World!");
string baseurl = Environment.CurrentDirectory.ToString() + "\\";
Console.WriteLine(baseurl);
//字体设定,c:\windows\fonts\kaiu.ttf,中文楷体
string filePath = baseurl + @"sRGB_CS_profile.icm";
string fontFile = @"c:\windows\fonts\kaiu.ttf";
string fontFile2 = baseurl + @"font\\MHeiHK-Light.TTF";
string fileName = baseurl+@"sample\\" + DateTime.Now.ToString("yyyyMMHHmmss") + ".pdf";
List orders = new List();
Orders order = new Orders();
order.productId = 1;
order.product = "中國電器";
order.qty = 50;
order.price = 52.0;
orders.Add(order);
order = new Orders();
order.productId = 2;
order.product = "日本電器";
order.qty = 150;
order.price = 252.0;
orders.Add(order);
//PdfADocument pdf = new PdfADocument(
//new PdfWriter(fileName),
//PdfAConformanceLevel.PDF_A_1B,
//new PdfOutputIntent("Custom", "", "http://www.color.org", "sRGB IEC61966-2.1",
// new FileStream(filePath, FileMode.Open, FileAccess.Read)));
PdfDocument pdf = new PdfDocument(new PdfWriter(new FileStream(fileName, FileMode.Create, FileAccess.Write)));
//FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
//PdfReader reader=new PdfReader(stream);
//PdfADocument pdf = new PdfADocument(reader, new PdfWriter(fileName));
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); //必须加这一行
//中文 字體和文字要相適應
//FontProgramFactory.CreateFont(fontFile);
PdfFont font = PdfFontFactory.CreateFont(fontFile2, PdfEncodings.IDENTITY_H);//PdfFontFactory.EmbeddingStrategy.FORCE_EMBEDDED
Document document = new Document(pdf, iText.Kernel.Geom.PageSize.A4);
document.SetFont(font);
//指定位置添加图片 images\luflog20180410145504.png
//PdfContentByte pcb1 = writer.DirectContent;
//image1.SetAbsolutePosition(12, 18); //底部
PdfImageXObject xObject = new PdfImageXObject(ImageDataFactory.Create(baseurl + "images/luflog20180410145504.png"));
iText.Layout.Element.Image image1 = new iText.Layout.Element.Image(xObject, 200);
// ColumnText ct = new ColumnText(pcb1);
image1.ScaleAbsolute(148, 50);
//pcb1.AddImage(image1);
//Resize image depend upon your need
image1.ScaleToFit(140f, 120f);
//p = new Paragraph(new Chunk(image1, 10f, 10f));
iText.Layout.Element.Table datatable = new iText.Layout.Element.Table(5, true); //5列
Cell pCTitle = new Cell(1, 5).SetBorder(iText.Layout.Borders.Border.NO_BORDER);//无边
Paragraph p = new Paragraph().SetTextAlignment(TextAlignment.CENTER);
p.Add(image1.SetTextAlignment(TextAlignment.CENTER));
pCTitle.Add(p);
datatable.AddCell(pCTitle);
document.Add(datatable);
Paragraph header = new Paragraph("ORDER DETAIL 顧客塗聚文訂單詳情").SetTextAlignment(TextAlignment.CENTER).SetFontSize(20);
document.Add(header);
Paragraph subheader = new Paragraph("C# WINDOWS FORM CREATE PDF DOCUMENT USING iTEXT7 LIBRARY").SetTextAlignment(TextAlignment.CENTER).SetFontSize(10);
document.Add(subheader);
LineSeparator ls = new LineSeparator(new SolidLine());
document.Add(ls);
Paragraph sellerHeader = new Paragraph("Sold by:").SetBold().SetTextAlignment(TextAlignment.LEFT);
Paragraph sellerDetail = new Paragraph("Seller Company").SetTextAlignment(TextAlignment.LEFT);
Paragraph sellerAddress = new Paragraph("Shenzhen city, Guangdong China").SetTextAlignment(TextAlignment.LEFT);
Paragraph sellerContact = new Paragraph("+86 1000000000").SetTextAlignment(TextAlignment.LEFT);
document.Add(sellerHeader);
document.Add(sellerDetail);
document.Add(sellerAddress);
document.Add(sellerContact);
Paragraph customerHeader = new Paragraph("Customer details:").SetBold().SetTextAlignment(TextAlignment.RIGHT);
Paragraph customerDetail = new Paragraph("Customer ABC").SetTextAlignment(TextAlignment.RIGHT);
Paragraph customerAddress1 = new Paragraph("Buxin 1008, shuibei Jewellery Builder").SetTextAlignment(TextAlignment.RIGHT);
Paragraph customerAddress2 = new Paragraph("Shenzhen 518019, Guangdong China").SetTextAlignment(TextAlignment.RIGHT);
Paragraph customerContact = new Paragraph("+85 0000000000").SetTextAlignment(TextAlignment.RIGHT);
document.Add(customerHeader);
document.Add(customerDetail);
document.Add(customerAddress1);
document.Add(customerAddress2);
document.Add(customerContact);
Paragraph orderNo = new Paragraph("Order No:15484659").SetBold().SetTextAlignment(TextAlignment.LEFT);
Paragraph invoiceNo = new Paragraph("Invoice No:MH-MU-1077").SetTextAlignment(TextAlignment.LEFT);
Paragraph invoiceTimestamp = new Paragraph("Date: 01/01/2024 04:25:37 PM").SetTextAlignment(TextAlignment.LEFT);
document.Add(orderNo);
document.Add(invoiceNo);
document.Add(invoiceTimestamp);
iText.Layout.Element.Table table = new iText.Layout.Element.Table(5, true);
table.SetFontSize(9);
Cell headerProductId = new Cell(1, 1).SetTextAlignment(TextAlignment.LEFT).Add(new Paragraph("ID"));
Cell headerProduct = new Cell(1, 1).SetTextAlignment(TextAlignment.LEFT).Add(new Paragraph("產品名稱"));
Cell headerProductPrice = new Cell(1, 1).SetTextAlignment(TextAlignment.LEFT).Add(new Paragraph("價格"));
Cell headerProductQty = new Cell(1, 1).SetTextAlignment(TextAlignment.LEFT).Add(new Paragraph("數量"));
Cell headerTotal = new Cell(1, 1).SetTextAlignment(TextAlignment.LEFT).Add(new Paragraph("合計"));
table.AddCell(headerProductId);
table.AddCell(headerProduct);
table.AddCell(headerProductPrice);
table.AddCell(headerProductQty);
table.AddCell(headerTotal);
double grandTotalVal = 0;
foreach (Orders c in orders)
{
Cell productid = new Cell(1, 1).SetTextAlignment(TextAlignment.LEFT).Add(new Paragraph(c.productId.ToString()));
Cell product = new Cell(1, 1).SetTextAlignment(TextAlignment.LEFT).Add(new Paragraph(c.product));
Cell price = new Cell(1, 1).SetTextAlignment(TextAlignment.LEFT).Add(new Paragraph(c.price.ToString()));
Cell qty = new Cell(1, 1).SetTextAlignment(TextAlignment.LEFT).Add(new Paragraph(c.qty.ToString()));
var value = c.price * c.qty;
Cell total = new Cell(1, 1).SetTextAlignment(TextAlignment.LEFT).Add(new Paragraph(value.ToString()));
grandTotalVal += value;
table.AddCell(productid);
table.AddCell(product);
table.AddCell(price);
table.AddCell(qty);
table.AddCell(total);
}
Cell grandTotalHeader = new Cell(1, 4).SetTextAlignment(TextAlignment.RIGHT).Add(new Paragraph("合計: "));
Cell grandTotal = new Cell(1, 1).SetTextAlignment(TextAlignment.LEFT).Add(new Paragraph(" " + grandTotalVal.ToString()));
table.AddCell(grandTotalHeader);
table.AddCell(grandTotal);
document.Add(table);
//底部
float[] tableWidth = { 445, 50F };
iText.Layout.Element.Table footerTable = new iText.Layout.Element.Table(5).SetFixedPosition(20F, 25F, 780F).SetBorder(iText.Layout.Borders.Border.NO_BORDER);
PdfImageXObject xObject2 = new PdfImageXObject(ImageDataFactory.Create(baseurl + "images/lufaddress_20180410145531.png"));
iText.Layout.Element.Image image2 = new iText.Layout.Element.Image(xObject2, 500);
//image2.SetMarginBottom(18); //底部
//image2.ScaleAbsolute(585, 40);
image2.SetTextAlignment(TextAlignment.LEFT);
Paragraph p2 = new Paragraph().SetTextAlignment(TextAlignment.LEFT);
p2.Add(image2.SetTextAlignment(TextAlignment.LEFT));
Cell cfoot = new Cell(1, 5).SetBorder(iText.Layout.Borders.Border.NO_BORDER); //离得远
cfoot.Add(p2);
footerTable.AddCell(cfoot).SetTextAlignment(TextAlignment.LEFT);
document.Add(footerTable);
// document.Add(image2);
table.Flush();
table.Complete();
document.Close();
Console.WriteLine(fileName);
}
catch(Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
}
///
/// 订单实体类
///
public class Orders
{
///
/// ID
///
public int productId { get; set; }
///
/// 产品名称
///
public string product { get; set; }
///
/// 数量
///
public int qty { get; set; }
///
/// 价格
///
public double price { get; set; }
}
}
输出: