123
tangxu
2023-04-12 f980bc78ed10c45323a9c5edcf2441124de72241
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
using iTextSharp.text;
using iTextSharp.text.pdf;
using System;
using System.Collections.Generic;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
 
namespace IStation.DataFile
{
    /// <summary>
    /// ITextSharp辅助类
    /// </summary>
    public static class ITextSharpHelper
    {
#pragma warning disable CA1416 // 验证平台兼容性
        /// <summary>
        /// 在指定位置添加图片
        /// </summary>
        /// <param name="ws">PdfWriter</param>
        /// <param name="img"></param>
        /// <param name="x">横坐标</param>
        /// <param name="y">反向纵坐标</param>
        public static void AddImageAtPosition(this PdfWriter ws, System.Drawing.Image img, int x, int y)
        {
            var iTextImg = iTextSharp.text.Image.GetInstance(img, ImageFormat.Png);
            //若不缩放四分之三,图片显示过大。暂时未探索出图片为什么图片会变大,待解决。
            iTextImg.ScalePercent(75);
            iTextImg.SetAbsolutePosition(x, y);
            ws.DirectContent.AddImage(iTextImg);
        }
 
        /// <summary>
        /// 在指定位置添加图片
        /// </summary>
        /// <param name="ws">PdfWriter</param>
        /// <param name="x">横坐标</param>
        /// <param name="y">反向纵坐标</param>
        /// <param name="filePath">路径</param>
        public static void AddImageAtPosition(this PdfWriter ws, string filePath, int x, int y)
        {
            var iTextImg = iTextSharp.text.Image.GetInstance(filePath);
            //若不缩放四分之三,图片显示过大。暂时未探索出图片为什么图片会变大,待解决。
            iTextImg.ScalePercent(75);
            iTextImg.SetAbsolutePosition(x, y);
            ws.DirectContent.AddImage(iTextImg);
        }
 
        /// <summary>
        /// 在指定位置添加文字
        /// </summary>
        /// <param name="ws">PdfWriter</param>
        /// <param name="text">文本</param>
        /// <param name="font">字体</param>
        /// <param name="x">横坐标</param>
        /// <param name="y">反向纵坐标</param>
        /// <param name="rotation">旋转角度,逆时针</param>
        public static void AddTextAtPosition(this PdfWriter ws, string text, iTextSharp.text.Font font, float x, float y, float rotation = 0)
        {
            var cb = ws.DirectContent;
            var txt = new Phrase(text, font);
            ColumnText.ShowTextAligned(cb, Element.ALIGN_CENTER, txt, x, y, 45);
        }
 
        /// <summary>
        /// 获取PdfImage
        /// </summary>
        /// <param name="img"></param>
        /// <returns></returns>
        public static iTextSharp.text.Image CreatePdfImage(this System.Drawing.Image img)
        {
            return iTextSharp.text.Image.GetInstance(img, ImageFormat.Png);
        }
#pragma warning restore CA1416 // 验证平台兼容性
        /// <summary>
        /// 获取背景颜色为白色的PdfImage
        /// </summary>
        /// <param name="img"></param>
        /// <returns></returns>
        public static iTextSharp.text.Image CreatePdfImageInWhite(this System.Drawing.Image img)
        {
            return iTextSharp.text.Image.GetInstance(img, BaseColor.White);
        }
 
        /// <summary>
        /// 获取Pdf基础字体
        /// </summary>
        /// <returns></returns>
        public static BaseFont GetBaseFont()
        {
            return BaseFont.CreateFont(@"C:\WINDOWS\Fonts\simhei.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
        }
 
        /// <summary>
        /// 
        /// </summary>
        /// <param name="fontPath"></param>
        /// <returns></returns>
        public static BaseFont GetLocalBaseFont(string fontPath) 
        {
            if (System.IO.File.Exists(fontPath))
            {
                return BaseFont.CreateFont(fontPath, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
            }
            else 
            {
                return GetBaseFont();
            }
        }
 
        /// <summary>
        /// 字体设置
        /// </summary>
        /// <param name="baseFont"></param>
        /// <param name="size"></param>
        /// <returns></returns>
        public static iTextSharp.text.Font CreateFontBySize(this BaseFont baseFont, float size)
        {
            return new iTextSharp.text.Font(baseFont, size);
        }
 
        /// <summary>
        /// 获取特定颜色大小的Pdf字体
        /// </summary>
        /// <param name="baseFont"></param>
        /// <param name="size"></param>
        /// <param name="color"></param>
        /// <returns></returns>
        public static iTextSharp.text.Font CreateFontBySize(this BaseFont baseFont, float size, System.Drawing.Color color)
        {
            var font = new iTextSharp.text.Font(baseFont, size);
            font.Color = new BaseColor(color);
            return font;
        }
 
        /// <summary>
        /// 创建表格
        /// </summary>
        /// <param name="spanNum">表格的列数</param>
        /// <param name="percentage">表格所占界面的百分比</param>
        /// <returns></returns>
        public static PdfPTable CreateTable(int spanNum, float percentage = 100)
        {
            var pdfTable = new PdfPTable(spanNum);
            pdfTable.WidthPercentage = percentage;
            return pdfTable;
        }
 
        /// <summary>
        /// 创建表格
        /// </summary>
        /// <param name="relativeWidths"></param>
        /// <returns></returns>
        public static PdfPTable CreateTable(float[] relativeWidths)
        {
            return new PdfPTable(relativeWidths);
        }
 
        /// <summary>
        /// 获取Pdf基础颜色
        /// </summary>
        /// <param name="color"></param>
        /// <returns></returns>
        public static BaseColor GetPdfBaseColor(this System.Drawing.Color color)
        {
            return new BaseColor(color);
        }
 
        /// <summary>
        /// 添加表格单元格
        /// </summary>
        public static PdfPCell CreateTableCell
            (
            this PdfPTable table, PdfPTable cellTable,
            int colSpan, int rowSpan,
            int border, float borderWidthLeft = -1, float borderWidthRight = -1, float borderWidthTop = -1, float borderWidthBottom = -1,
            float paddingLeft = -1, float paddingRight = -1, float paddingTop = -1, float paddingBottom = -1
            )
        {
            var cell = new PdfPCell(cellTable);
            cell.Padding = 0;
            cell.Colspan = colSpan;
            cell.Rowspan = rowSpan;
            cell.UseAscender = true;
            cell.Border = border;
 
            if (borderWidthLeft > 0)
                cell.BorderWidthLeft = borderWidthLeft;
            if (borderWidthRight > 0)
                cell.BorderWidthRight = borderWidthRight;
            if (borderWidthTop > 0)
                cell.BorderWidthTop = borderWidthTop;
            if (borderWidthBottom > 0)
                cell.BorderWidthBottom = borderWidthBottom;
 
            if (paddingLeft > 0)
                cell.PaddingLeft = paddingLeft;
            if (paddingRight > 0)
                cell.PaddingRight = paddingRight;
            if (paddingTop > 0)
                cell.PaddingTop = paddingTop;
            if (paddingBottom > 0)
                cell.PaddingBottom = paddingBottom;
 
            table.AddCell(cell);
            return cell;
        }
 
        /// <summary>
        /// 创建单元格
        /// </summary>
        /// <param name="pdfTable">创建单元格的表格</param>
        /// <param name="content">单元格内容</param>
        /// <param name="font">单元格的字体</param>
        /// <param name="colSpan">单元格所占列数</param>
        /// <param name="rowSpan">单元格所占行数</param>
        /// <param name="border">边框设置</param>
        /// <param name="h_alignment">横向排列</param>
        /// <param name="v_alignment">纵向排列</param>
        /// <param name="minHeight">最低高度</param>
        /// <param name="fixHeight">固定高度</param>
        /// <param name="bc">单元格背景颜色</param>
        /// <param name="borderWidthLeft">左边框宽度</param>
        /// <param name="borderWidthRight">有边框宽度</param>
        /// <param name="borderWidthTop">上边框宽度</param>
        /// <param name="borderWidthBottom">下边框宽度</param>
        /// <returns></returns>
        public static PdfPCell CreateCell
            (
            this PdfPTable pdfTable,
            string content, iTextSharp.text.Font font,
            int colSpan, int rowSpan,
            int border,
            float minHeight = -1, float fixHeight = -1,
            int h_alignment = Element.ALIGN_CENTER, int v_alignment = Element.ALIGN_MIDDLE,
            BaseColor bc = null,
            float borderWidthLeft = -1, float borderWidthRight = -1, float borderWidthTop = -1, float borderWidthBottom = -1
            )
        {
            var cell = new PdfPCell(new Paragraph(content, font));
            cell.Colspan = colSpan;
            cell.Rowspan = rowSpan;
            cell.UseAscender = true;
            cell.Border = border;
            cell.HorizontalAlignment = h_alignment;
            cell.VerticalAlignment = v_alignment;
            if (minHeight > 0)
                cell.MinimumHeight = minHeight;
            if (fixHeight > 0)
                cell.FixedHeight = fixHeight;
            if (bc != null)
                cell.BackgroundColor = bc;
            if (borderWidthLeft > 0)
                cell.BorderWidthLeft = borderWidthLeft;
            if (borderWidthRight > 0)
                cell.BorderWidthRight = borderWidthRight;
            if (borderWidthTop > 0)
                cell.BorderWidthTop = borderWidthTop;
            if (borderWidthBottom > 0)
                cell.BorderWidthBottom = borderWidthBottom;
            pdfTable.AddCell(cell);
            return cell;
        }
 
        /// <summary>
        /// 创建图片单元格
        /// </summary>
        /// <param name="pdfTable">创建单元格的表格</param>
        /// <param name="img">单元格内的图片</param>
        /// <param name="colSpan">单元格所占列数</param>
        /// <param name="rowSpan">单元格所占行数</param>
        /// <param name="border">边框设置</param>
        /// <param name="minHeight">最低高度</param>
        /// <param name="fixHeight">固定高度</param>
        /// <param name="bc">单元格背景颜色</param>
        /// <param name="borderWidthLeft">左边框宽度</param>
        /// <param name="borderWidthRight">有边框宽度</param>
        /// <param name="borderWidthTop">上边框宽度</param>
        /// <param name="borderWidthBottom">下边框宽度</param>
        /// <param name="h_alignment"></param>
        ///  /// <param name="v_alignment"></param>
        /// <returns></returns>
        public static PdfPCell CreateImageCell
            (
            this PdfPTable pdfTable,
            System.Drawing.Image img,
            int colSpan, int rowSpan,
            int border,
            float minHeight = -1, float fixHeight = -1,
            int h_alignment = Element.ALIGN_CENTER, int v_alignment = Element.ALIGN_MIDDLE,
            BaseColor bc = null,
            float borderWidthLeft = -1, float borderWidthRight = -1, float borderWidthTop = -1, float borderWidthBottom = -1
            )
        {
            var cell = new PdfPCell();
            cell.HorizontalAlignment = h_alignment;
            cell.VerticalAlignment = v_alignment;
            var pdfImg = img.CreatePdfImage();
            pdfImg.Alignment = iTextSharp.text.Image.ALIGN_MIDDLE;
            cell.Image = pdfImg;
            cell.Colspan = colSpan;
            cell.Rowspan = rowSpan;
            cell.Border = border;
            if (minHeight > 0)
                cell.MinimumHeight = minHeight;
            if (fixHeight > 0)
                cell.FixedHeight = fixHeight;
            if (bc != null)
                cell.BackgroundColor = bc;
            if (borderWidthLeft > 0)
                cell.BorderWidthLeft = borderWidthLeft;
            if (borderWidthRight > 0)
                cell.BorderWidthRight = borderWidthRight;
            if (borderWidthTop > 0)
                cell.BorderWidthTop = borderWidthTop;
            if (borderWidthBottom > 0)
                cell.BorderWidthBottom = borderWidthBottom;
            pdfTable.AddCell(cell);
            return cell;
        }
 
 
 
    }
}