生成简单的pdf
引用iTextSharp.LGPLv2.Core
----------------------------------------------------------------
iTextSharp.text.Rectangle pageSize = PageSize.A4;
Document doc = new Document(pageSize);
MemoryStream ms = new MemoryStream();//将PDF写入内存流
PdfWriter writer = PdfWriter.GetInstance(doc, ms);
doc.Open(); //打开文件
#region 头部
//定义table行列数据
PdfPTable tableRow = new PdfPTable(1); //生成只有一列的行数据
tableRow.DefaultCell.Border = iTextSharp.text.Rectangle.NO_BORDER; //无边框
tableRow.DefaultCell.MinimumHeight = 60f; //高度
float[] headWidths = new float[] { 500f }; //宽度
tableRow.SetWidths(headWidths);
BaseFont.AddToResourceSearch("iTextAsian.dll");
BaseFont.AddToResourceSearch("iTextAsianCmaps.dll");
BaseFont bf = BaseFont.CreateFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
//定义字体样式
var headerStyle = new iTextSharp.text.Font(bf, 14)
{
Color = new BaseColor(System.Drawing.Color.Black),
Size = 14,
};
//设置字体颜色样式
var baseFont = new iTextSharp.text.Font(bf, 12)
{
Color = new BaseColor(System.Drawing.Color.Black), //设置字体颜色
Size = 12 //字体大小
};
var headRow = new PdfPCell(new iTextSharp.text.Paragraph("***WISTRON***", headerStyle));
headRow.HorizontalAlignment = iTextSharp.text.Element.ALIGN_CENTER;//居中
headRow.Border = iTextSharp.text.Rectangle.NO_BORDER;
tableRow.AddCell(headRow);
headRow = new PdfPCell(new iTextSharp.text.Paragraph("***保稅生產性貨品出區統計表***", headerStyle));
headRow.HorizontalAlignment = iTextSharp.text.Element.ALIGN_CENTER;
headRow.Border = iTextSharp.text.Rectangle.NO_BORDER;
tableRow.AddCell(headRow);
var time = "Date Time: " + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
headRow = new PdfPCell(new iTextSharp.text.Paragraph(time, baseFont));
headRow.HorizontalAlignment = iTextSharp.text.Element.ALIGN_LEFT;//居左
headRow.Border = iTextSharp.text.Rectangle.NO_BORDER;
tableRow.AddCell(headRow);
var fromdate = "From Date:" + QueryRequest.ApproveDate[0]?.ToString("yyyy/MM/dd") + " To Date:" + QueryRequest.ApproveDate[1]?.ToString("yyyy/MM/dd");
headRow = new PdfPCell(new iTextSharp.text.Paragraph(fromdate, baseFont));
headRow.HorizontalAlignment = iTextSharp.text.Element.ALIGN_LEFT;
headRow.Border = iTextSharp.text.Rectangle.NO_BORDER;
tableRow.AddCell(headRow);
doc.Add(tableRow);
PdfPTable tablerow = new PdfPTable(2);
tablerow.DefaultCell.Border = iTextSharp.text.Rectangle.NO_BORDER;
tablerow.DefaultCell.MinimumHeight = 60f;
float[] headwidths = new float[] { 100f, 80f, };
tablerow.SetWidths(headwidths);
var contentRow = new PdfPCell(new iTextSharp.text.Paragraph(LanguageService.BondedStatisticalTable, baseFont));
contentRow.HorizontalAlignment = iTextSharp.text.Element.ALIGN_CENTER;
contentRow.Border = iTextSharp.text.Rectangle.NO_BORDER;
tablerow.AddCell(contentRow);
contentRow = new PdfPCell(new iTextSharp.text.Paragraph(BondedExitStatisticsTables.Sum(t => t.Count).ToString(), baseFont));
contentRow.HorizontalAlignment = iTextSharp.text.Element.ALIGN_CENTER;
contentRow.Border = iTextSharp.text.Rectangle.NO_BORDER;
tablerow.AddCell(contentRow);
foreach (var item in BondedExitStatisticsTables)
{
contentRow = new PdfPCell(new iTextSharp.text.Paragraph(item.Description, baseFont));
contentRow.HorizontalAlignment = iTextSharp.text.Element.ALIGN_CENTER;
contentRow.Border = iTextSharp.text.Rectangle.NO_BORDER;
tablerow.AddCell(contentRow);
contentRow = new PdfPCell(new iTextSharp.text.Paragraph(item.Count.ToString(), baseFont));
contentRow.HorizontalAlignment = iTextSharp.text.Element.ALIGN_CENTER;
contentRow.Border = iTextSharp.text.Rectangle.NO_BORDER;
tablerow.AddCell(contentRow);
}
doc.Add(tablerow);
#endregion
//关闭文件
doc.Close();
byte[] bytes = ms.ToArray();
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架