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

  • 7
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王甜甜(.NET)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值