C#利用PdfSharp生成Pdf文件
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
C#利⽤PdfSharp⽣成Pdf⽂件
PdfSharp⼀款开源的⽤于创建,操作PDF⽂档的.Net类库,本⽂以⼀个简单的⼩例⼦,简述如何通过PdfSharp进⾏创建PDF⽂档,仅供学习分享使⽤,如有不⾜之处,还请指正。
PdfSharp下载
在本例中,主要通过NuGet包管理器进⾏下载安装,⽬前PdfSharp版本为v1.5.0.5147,如下所⽰:
涉及知识点
在⽣成PDF⽂档过程中,主要知识点如下:
1. PdfDocument :表⽰⼀个PDF⽂档对象,调⽤save⽅法保存⽂档到指定路径。
2. PdfPage :表⽰PDF⽂档中的⼀页。
3. XGraphics:表⽰页⾯上的绘制对象,所有的页⾯内容,都可通过此对象绘制。
如:DrawString绘制⽂本内容,DrawLine绘制直线等。
4. XFont:绘制⽂本的字体,字体名称只能取C:\Windows\Fonts⽬录下的ttf字体⽂件,不能是ttc格式的字体。
5. XTextFormatter:表⽰⼀个简单的⽂本字体格式,如识别⽂本的换⾏符⽽实现⾃动换⾏等内容。
⽂档⽰例图
在本例中,主要是将页⾯内容写⼊PDF⽂件中,页⾯如下所⽰:
⽣成的PDF⽂件,如下所⽰:
核⼼代码
在本例中,核⼼代码主要包括如下⼏个部分:
创建⽂档
绘制⽂本
绘制直线
设置纸张⼤⼩,
设置边界
⽂本的⾃动换⾏
具体代码,如下所⽰:
1///<summary>
2///⽣成Pdf
3///</summary>
4///<param name="filePath"></param>
5///<param name="bo"></param>
6///<returns></returns>
7public bool GeneratePdf(string filePath, PdfBo bo) {
8int margin_left_right = 30;//左右边距
9int margin_top_bottom = 30;//上下边距
10//1. 定义⽂档对象
11 PdfDocument document = new PdfDocument();
12//2. 新增⼀页
13 PdfPage page = document.AddPage();
14// 设置纸张⼤⼩
15 page.Size = PageSize.A4;
16//3. 创建⼀个绘图对象
17 XGraphics gfx = XGraphics.FromPdfPage(page);
18 XFont font = new XFont("华⽂宋体", 40, XFontStyle.Bold);
19//定制化内容开始
20int cur_x = 0 + margin_left_right;
21int cur_y = 0 + margin_top_bottom;
22//标题1
23 gfx.DrawString(bo.Head1, font, XBrushes.Red, new XRect(cur_x, cur_y, page.Width-2*cur_x, 80), XStringFormats.Center);
24//序号
25 font = new XFont("华⽂宋体", 12, XFontStyle.Regular);
26 cur_y = cur_y + 80;
27 gfx.DrawString(bo.No, font, XBrushes.Black, new XRect(cur_x, cur_y, 100, 20), XStringFormats.CenterLeft);
28//密级
29 cur_x = cur_x + 200;
30 gfx.DrawString(string.Format("密级[{0}]",bo.Private), font, XBrushes.Black, new XRect(cur_x, cur_y, 100, 20), XStringFormats.CenterLeft); 31//缓级
32 cur_x = cur_x + 100;
33 gfx.DrawString(string.Format("缓级[{0}]", bo.Speed), font, XBrushes.Black, new XRect(cur_x, cur_y, 100, 20), XStringFormats.CenterLeft); 34//签发⼈
35 cur_x = cur_x + 100;
36 gfx.DrawString(string.Format("签发⼈:{0}", bo.Person), font, XBrushes.Black, new XRect(cur_x, cur_y, 100, 20), XStringFormats.CenterLeft);
37//⼀条横线
38 cur_x = 0 + margin_left_right;
39 cur_y = cur_y + 20;
40 XPen pen = new XPen(XColor.FromKnownColor(XKnownColor.Black), 1);
41 gfx.DrawLine(pen, cur_x, cur_y, page.Width-cur_x, cur_y+2);
42//标题2
43 font = new XFont("华⽂宋体", 20, XFontStyle.Regular);
44 cur_y = cur_y + 10;
45 gfx.DrawString(bo.Head2, font, XBrushes.Black, new XRect(cur_x, cur_y, page.Width-2*cur_x, 40), XStringFormats.Center);
46//抬头
47 font = new XFont("华⽂宋体", 15, XFontStyle.Bold);
48 cur_y = cur_y + 40;
49 gfx.DrawString(bo.Title, font, XBrushes.Black, new XRect(cur_x, cur_y, page.Width, 40), XStringFormats.CenterLeft);
50//正⽂,⾃动换⾏
51 cur_y = cur_y + 40;
52 XTextFormatter tf = new XTextFormatter(gfx);
53 font = new XFont("华⽂宋体", 12, XFontStyle.Regular);
54
55//测量当前内容下,⼀⾏可以多少个汉字
56int cnt = 0;
57int height = 0;
58for (int i = 0; i < bo.Content.Length; i++) {
59 XSize xsize=gfx.MeasureString(bo.Content.Substring(0,i+1), font, XStringFormats.TopLeft);
60double width = xsize.Width;
61if (width >= page.Width - 2 * cur_x) {
62 cnt = i; //表⽰⼀⾏可以放多少个汉字。
63 height =(int) xsize.Height;
64break;
65 }
66 }
67 cnt = cnt > 0 ? cnt : bo.Content.Length;//每⼀⾏多少汉字
68string[] arrContent = bo.Content.Split('\n');
69string new_content = "";
70int total_lines = 0;
71foreach (string content in arrContent) {
72if (content.Length <= cnt)
73 {
74 new_content+=string.Format("{0}\n",content);
75 total_lines++;
76 }
77else {
78string tmpContent = content;
79int lines = content.Length / cnt + 1;
80for (int j = 0; j < lines; j++) {
81 tmpContent = tmpContent.Insert(j * cnt, "\n");
82 total_lines++;
83 }
84 new_content += string.Format("{0}\n", tmpContent);
85 }
86 }
87int num = new_content.Length - new_content.Replace("\r", "").Length;
88//计算矩形
89 XRect rect = new XRect(cur_x, cur_y, page.Width - 2 * cur_x, (total_lines+num)*(height+2));
90 tf.DrawString(new_content, font, XBrushes.Black, rect, XStringFormats.TopLeft);
91//主题词
92 cur_y = cur_y + (total_lines + num) * (height + 2) + 20;
93 font = new XFont("华⽂宋体", 12, XFontStyle.Bold);
94 gfx.DrawString(string.Format("主题词:{0}",bo.Keyword), font, XBrushes.Black, new XRect(cur_x, cur_y, page.Width, 40), XStringFormats.CenterLeft);
95//再加⼀条横线
96 cur_y = cur_y + 40;
97 gfx.DrawLine(pen, cur_x, cur_y, page.Width - cur_x, cur_y + 2);
98 cur_y = cur_y + 2;
99 font = new XFont("华⽂宋体", 10, XFontStyle.Regular);
100 gfx.DrawString(string.Format("{0}{1}",pany, bo.Dept), font, XBrushes.Black, new XRect(cur_x, cur_y, page.Width-2*cur_x, 40), XStringFormats.CenterLeft);
101 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); 102//⽔印开始
103 font = new XFont("华⽂宋体", 20, XFontStyle.BoldItalic);
104// 计算长度
105var size = gfx.MeasureString(bo.Watermark, font);
106
107// 定义旋转中⼼
108 gfx.TranslateTransform(page.Width / 2, page.Height / 2);
109 gfx.RotateTransform(-Math.Atan(page.Height / page.Width) * 180 / Math.PI);
110 gfx.TranslateTransform(-page.Width / 2, -page.Height / 2);
111
112// 字符样式
113var format = new XStringFormat();
114 format.Alignment = XStringAlignment.Near;
115 format.LineAlignment = XLineAlignment.Near;
116
117//画刷
118 XBrush brush = new XSolidBrush(XColor.FromArgb(128, 255, 0, 0));
119for (int i = 0; i < 3; i++) {
120 gfx.DrawString(bo.Watermark, font, brush,
121new XPoint((page.Width - size.Width) / (1.5+i*0.5), (page.Height - size.Height) / (1.5 + i * 0.5)),
122 format);
123 }
124//⽔印结束
125//6. 保存⽂档
126 document.Save(filePath);
127return true;
128 }
View Code
备注
临江仙·柳外轻雷池上⾬
【作者】欧阳修【朝代】宋
柳外轻雷池上⾬,⾬声滴碎荷声。
⼩楼西⾓断虹明。
阑⼲倚处,待得⽉华⽣。
燕⼦飞来窥画栋,⽟钩垂下帘旌。
凉波不动簟纹平。
⽔精双枕,傍有堕钗横。