-
Notifications
You must be signed in to change notification settings - Fork 1
/
Exporter_Linux.cs
135 lines (115 loc) · 4.98 KB
/
Exporter_Linux.cs
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
using Flurl.Http;
using PdfSharpCore.Drawing;
using PdfSharpCore.Fonts;
using PdfSharpCore.Pdf;
using PdfSharpCore.Pdf.AcroForms;
using PdfSharpCore.Pdf.IO;
using System.Reflection;
namespace PdfGenerator
{
public class Exporter_Linux
{
public static PdfDocument ExportDocxByObject(Stream templateStream, object data)
{
var doc = PdfReader.Open(templateStream, PdfDocumentOpenMode.Modify);
ReplaceKeyObjetAsync(doc, data);
return doc;
}
public static PdfDocument ExportDocxByObject(string templatePath, object data)
{
var doc = PdfReader.Open(templatePath, PdfDocumentOpenMode.Modify);
ReplaceKeyObjetAsync(doc, data);
return doc;
}
private static void ReplaceKeyObjetAsync(PdfDocument doc, object model)
{
PdfAcroForm form = doc.AcroForm;
if (form.Elements.ContainsKey("/NeedAppearances"))
{
form.Elements["/NeedAppearances"] = new PdfBoolean(true);
}
else
{
form.Elements.Add("/NeedAppearances", new PdfBoolean(true));
}
string text = "";
Type t = model.GetType();
PropertyInfo[] pi = t.GetProperties();
foreach (var fieldName in form.Fields.Names)
{
var run = form.Fields[fieldName] as PdfTextField;
text = run.Name;
XPdfFontOptions options = new XPdfFontOptions(PdfFontEncoding.Unicode);
XFont font = new XFont(GlobalFontSettings.FontResolver.DefaultFontName, 18, XFontStyle.Regular, options);
run.Font=font;
foreach (PropertyInfo p in pi)
{
string key = $"${p.Name}$";
if (text.Contains(key))
{
var value = "";
try
{
value = p.GetValue(model, null).ToString();
}
catch (Exception ex)
{
}
if (value.Contains('\n'))
{
run.MultiLine = true;
}
run.Value = new PdfString(value,PdfStringEncoding.Unicode);
run.ReadOnly = true;
}
else if (text.Contains($@"#{p.Name}#"))
{
try
{
var filePath = p.GetValue(model, null) as string;
if (string.IsNullOrEmpty(filePath))
{
continue;
}
if (File.Exists(filePath))
{
using (var fileStream = new FileStream(filePath.ToString(), FileMode.Open, FileAccess.Read))
{
var rectangle = run.Elements.GetRectangle("/Rect");
var xForm = new XForm(doc, rectangle.Size);
using (var xGraphics = XGraphics.FromPdfPage(doc.Pages[0]))
{
var image = XImage.FromStream(()=>fileStream);
xGraphics.DrawImage(image, rectangle.ToXRect() +new XPoint(0, 400));
var state = xGraphics.Save();
xGraphics.Restore(state);
}
}
}
else
{
using (var fileStream = filePath.ToString().GetStreamAsync().Result)
{
var rectangle = run.Elements.GetRectangle("/Rect");
var xForm = new XForm(doc, rectangle.Size);
using (var xGraphics = XGraphics.FromPdfPage(doc.Pages[0]))
{
var image = XImage.FromStream(() => fileStream);
xGraphics.DrawImage(image, rectangle.ToXRect() +new XPoint(0, 400));
var state = xGraphics.Save();
xGraphics.Restore(state);
}
}
}
run.ReadOnly = true;
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
}
}
}
}
}