生成简单的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();

posted @ 2023-07-18 10:55  W(王甜甜)  阅读(26)  评论(0编辑  收藏  举报