目录pdfSharp下载 涉及知识点 文档示例图 核心代码 PdfSharp一款开源的用于创建,操作PDF文档的.net类库,本文以一个简单的小例子,简述如何通过PdfSharp进行
PdfSharp一款开源的用于创建,操作PDF文档的.net类库,本文以一个简单的小例子,简述如何通过PdfSharp进行创建PDF文档,仅供学习分享使用,如有不足之处,还请指正。
在本例中,主要通过NuGet包管理器进行下载安装,目前PdfSharp版本为v1.5.0.5147,如下所示:
在生成PDF文档过程中,主要知识点如下:
在本例中,主要是将页面内容写入PDF文件中,页面如下所示:
生成的PDF文件,如下所示:
在本例中,核心代码主要包括如下几个部分:
具体代码,如下所示:
/// <summary>
/// 生成Pdf
/// </summary>
/// <param name="filePath"></param>
/// <param name="bo"></param>
/// <returns></returns>
public bool GeneratePdf(string filePath, PdfBo bo) {
int margin_left_right = 30;//左右边距
int margin_top_bottom = 30;//上下边距
//1. 定义文档对象
PdfDocument document = new PdfDocument();
//2. 新增一页
PdfPage page = document.AddPage();
// 设置纸张大小
page.Size = PageSize.A4;
//3. 创建一个绘图对象
XGraphics gfx = XGraphics.FromPdfPage(page);
XFont font = new XFont("华文宋体", 40, XFontStyle.Bold);
//定制化内容开始
int cur_x = 0 + margin_left_right;
int cur_y = 0 + margin_top_bottom;
//标题1
gfx.DrawString(bo.Head1, font, XBrushes.Red, new XRect(cur_x, cur_y, page.Width-2*cur_x, 80), XStringFormats.Center);
//序号
font = new XFont("华文宋体", 12, XFontStyle.Regular);
cur_y = cur_y + 80;
gfx.DrawString(bo.No, font, XBrushes.Black, new XRect(cur_x, cur_y, 100, 20), XStringFormats.CenterLeft);
//密级
cur_x = cur_x + 200;
gfx.DrawString(string.Format("密级[{0}]",bo.Private), font, XBrushes.Black, new XRect(cur_x, cur_y, 100, 20), XStringFormats.CenterLeft);
//缓级
cur_x = cur_x + 100;
gfx.DrawString(string.Format("缓级[{0}]", bo.Speed), font, XBrushes.Black, new XRect(cur_x, cur_y, 100, 20), XStringFormats.CenterLeft);
//签发人
cur_x = cur_x + 100;
gfx.DrawString(string.Format("签发人:{0}", bo.Person), font, XBrushes.Black, new XRect(cur_x, cur_y, 100, 20), XStringFormats.CenterLeft);
//一条横线
cur_x = 0 + margin_left_right;
cur_y = cur_y + 20;
XPen pen = new XPen(XColor.FromKnownColor(XKnownColor.Black), 1);
gfx.DrawLine(pen, cur_x, cur_y, page.Width-cur_x, cur_y+2);
//标题2
font = new XFont("华文宋体", 20, XFontStyle.Regular);
cur_y = cur_y + 10;
gfx.DrawString(bo.Head2, font, XBrushes.Black, new XRect(cur_x, cur_y, page.Width-2*cur_x, 40), XStringFormats.Center);
//抬头
font = new XFont("华文宋体", 15, XFontStyle.Bold);
cur_y = cur_y + 40;
gfx.DrawString(bo.Title, font, XBrushes.Black, new XRect(cur_x, cur_y, page.Width, 40), XStringFormats.CenterLeft);
//正文 ,自动换行
cur_y = cur_y + 40;
XTextFormatter tf = new XTextFormatter(gfx);
font = new XFont("华文宋体", 12, XFontStyle.Regular);
//测量当前内容下,一行可以多少个汉字
int cnt = 0;
int height = 0;
for (int i = 0; i < bo.Content.Length; i++) {
XSize xsize=gfx.MeasureString(bo.Content.Substring(0,i+1), font, XStringFormats.TopLeft);
double width = xsize.Width;
if (width >= page.Width - 2 * cur_x) {
cnt = i; //表示一行可以放多少个汉字。
height =(int) xsize.Height;
break;
}
}
cnt = cnt > 0 ? cnt : bo.Content.Length;//每一行多少汉字
string[] arrContent = bo.Content.Split('\n');
string new_content = "";
int total_lines = 0;
foreach (string content in arrContent) {
if (content.Length <= cnt)
{
new_content+=string.Format("{0}\n",content);
total_lines++;
}
else {
string tmpContent = content;
int lines = content.Length / cnt + 1;
for (int j = 0; j < lines; j++) {
tmpContent = tmpContent.Insert(j * cnt, "\n");
total_lines++;
}
new_content += string.Format("{0}\n", tmpContent);
}
}
int num = new_content.Length - new_content.Replace("\r", "").Length;
//计算矩形
XRect rect = new XRect(cur_x, cur_y, page.Width - 2 * cur_x, (total_lines+num)*(height+2));
tf.DrawString(new_content, font, XBrushes.Black, rect, XStringFormats.TopLeft);
//主题词
cur_y = cur_y + (total_lines + num) * (height + 2) + 20;
font = new XFont("华文宋体", 12, XFontStyle.Bold);
gfx.DrawString(string.Format("主题词:{0}",bo.KeyWord), font, XBrushes.Black, new XRect(cur_x, cur_y, page.Width, 40), XStringFormats.CenterLeft);
//再加一条横线
cur_y = cur_y + 40;
gfx.DrawLine(pen, cur_x, cur_y, page.Width - cur_x, cur_y + 2);
cur_y = cur_y + 2;
font = new XFont("华文宋体", 10, XFontStyle.Regular);
gfx.DrawString(string.Format("{0}{1}",bo.Company, bo.Dept), font, XBrushes.Black, new XRect(cur_x, cur_y, page.Width-2*cur_x, 40), XStringFormats.CenterLeft);
gfx.DrawString(DateTime.Now.ToString("yyyy 年 MM 月 dd 日 印发"), font, XBrushes.Black, new XRect(cur_x, cur_y, page.Width-2*cur_x, 40), XStringFormats.CenterRight);
//水印开始
font = new XFont("华文宋体", 20, XFontStyle.BoldItalic);
// 计算长度
var size = gfx.MeasureString(bo.Watermark, font);
// 定义旋转中心
gfx.TranslateTransform(page.Width / 2, page.Height / 2);
gfx.RotateTransform(-Math.Atan(page.Height / page.Width) * 180 / Math.PI);
gfx.TranslateTransform(-page.Width / 2, -page.Height / 2);
// 字符样式
var format = new XStringFormat();
format.Alignment = XStringAlignment.Near;
format.LineAlignment = XLineAlignment.Near;
//画刷
XBrush brush = new XSolidBrush(XColor.FromArgb(128, 255, 0, 0));
for (int i = 0; i < 3; i++) {
gfx.DrawString(bo.Watermark, font, brush,
new XPoint((page.Width - size.Width) / (1.5+i*0.5), (page.Height - size.Height) / (1.5 + i * 0.5)),
format);
}
//水印结束
//6. 保存文档
document.Save(filePath);
return true;
}
以上就是C# 利用PdfSharp生成Pdf文件的示例的详细内容,更多关于c# 生成Pdf文件的资料请关注编程网其它相关文章!
--结束END--
本文标题: C# 利用PdfSharp生成Pdf文件的示例
本文链接: https://www.lsjlt.com/news/122778.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0