-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPdfTools.cs
92 lines (84 loc) · 3.65 KB
/
PdfTools.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
using System;
using System.IO;
using System.Text.RegularExpressions;
using iText.Forms;
using iText.Forms.Fields;
using iText.IO;
using iText.Kernel.Pdf;
using System.Collections.Generic;
using System.Linq;
using ChoETL;
namespace pdffillerdncore
{
public class PdfFieldAttribute : Attribute
{
public string FieldName{ get; }
public PdfFieldAttribute(string fieldName)
{
FieldName = fieldName;
}
}
public class PdfTools
{
public byte[] CreatePdf(dynamic recs, string templatePdfFile)
{
// create clone page for each user in users
using (MemoryStream memoryStream = new MemoryStream())
{
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(memoryStream).SetSmartMode(true));
pdfDoc.InitializeOutlines();
PdfDocument srcDoc;
foreach (var rec in recs)
{
MemoryStream m = new MemoryStream(FillForm(rec,templatePdfFile));
srcDoc = new PdfDocument(new PdfReader(m));
// copy content to the resulting PDF
srcDoc.CopyPagesTo(1, srcDoc.GetNumberOfPages(), pdfDoc);
}
pdfDoc.Close();
return memoryStream.ToArray();
}
}
internal static byte[] FillForm<T>(T rec, string templatePdfFile)
{
using (var memoryStream = new MemoryStream())
{
PdfReader reader = new PdfReader(templatePdfFile); //Iput
PdfWriter writer = new PdfWriter(memoryStream); //output
PdfDocument pdfDoc = new PdfDocument(reader, writer);
PdfAcroForm form = PdfAcroForm.GetAcroForm(pdfDoc, true);
var fields = form.GetFormFields();
// var fields =
//var fields = GetFormFieldsForTempalte(templatePdfFile);
var properties = typeof(T).GetProperties().Where(x => x.GetCustomAttributes(typeof(PdfFieldAttribute),true).Any());
foreach (var prop in properties)
{
var attr = prop.GetCustomAttribute<PdfFieldAttribute>();
if (!fields.TryGetValue(attr.FieldName, out var pdfField))
continue;
pdfField.SetValue(prop.GetValue(rec)?.ToString() ?? String.Empty);
}
form.FlattenFields();
pdfDoc.Close();
return memoryStream.ToArray();
}
}
private static void discoverPDFFields(string pdf)
{
PdfDocument pdfDoc = new PdfDocument(new PdfReader(pdf));
PdfAcroForm form = PdfAcroForm.GetAcroForm(pdfDoc, true);
StreamWriter sw = new StreamWriter(@"output\fields.txt");
var fields = form.GetFormFields();
var lines = fields.Select(kvp => kvp.Key); //Grab PDF Fields from document
foreach (var l in lines) //Iterate through the fields to build the set value map
{
//Console.WriteLine($"fields[\"{l}\"].SetValue();");
//Console.WriteLine(l);
sw.WriteLine(l);
//var fld = l.Replace("form1[0].#subform[0].","").Replace("[0]","");
//sw.WriteLine($"fields.First(kvp => kvp.Key.Contains(\"{fld}\")).Value.SetValue()");
}
sw.Close();
}
}
}