-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
146 lines (132 loc) · 6.99 KB
/
Program.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
136
137
138
139
140
141
142
143
144
145
146
using System;
using System.IO;
using iText.Forms;
using iText.Forms.Fields;
using iText.IO;
using iText.Kernel.Pdf;
using System.Collections.Generic;
using System.Linq;
using ChoETL;
using Newtonsoft.Json;
namespace pdffillerdncore
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Begin PDF Processing");
//string srcPdf = @"resources\sf3101_template.pdf"; //Set template form as source
string srcPdf = @"resources\SF1150-77.pdf"; //Set template form as source
string destPdf = @"output\mergedsf1150_" + DateTime.Now.ToString("MMddyyyyHHmmss") + ".pdf"; //Set named output pdf
var myclass = new SF1150();
myclass.GenerateSF1150(srcPdf,@"C:\DEV\PDFFormFillDemo-dotnet\resources\par166p1_revised.csv",destPdf);
//var myclass = new testClass();
//myclass.GenerateTest();
//Program.discoverPDFFields(srcPdf);
//Program.testFillSF1150(srcPdf,destPdf);
//Program.createPocoFromPDF(srcPdf);
//Program.FixFieldNamesPDF(srcPdf);
//Read CSV File into POCO using ChoETL Lib
/*
var recs = new ChoCSVReader<SF3101>(@"C:\DEV\pdffillerdncore\resources\sf3101_sample_data.csv").WithFirstLineHeader();
byte[] result = createPdf(recs, srcPdf); //Create multi-page pdf using the template for each record.
File.WriteAllBytes(destPdf, result); //Write the pdf in memory out to file.
*/
Console.WriteLine("End PDF Processing");
}
private static 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;
recs.Configuration.MayContainEOLInData = true; //Handling for multi-line values in CSV
foreach (var rec in recs)
{
MemoryStream m = new MemoryStream(fillFormSF3101(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();
}
}
private static byte[] fillFormSF3101(SF3101 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();
form.RemoveField("SAVE"); //remove save button
form.RemoveField("PRINT"); //remove print button
form.RemoveField("RESET"); //remove reset button
//form.GetField("Reason").SetVisibility(3);
//Since we control the template, we can directly set all the fields
fields["SSN"].SetValue(rec.SSN);
fields["Name"].SetValue(rec.Name);
fields["DOB"].SetValue(rec.DOB);
fields["Agency"].SetValue(rec.Agency);
fields["Payroll Office No"].SetValue(rec.PayrollOfficeNo);
if (!String.IsNullOrEmpty(rec.Location)) { fields["Location"].SetValue(rec.Location); }
if (!String.IsNullOrEmpty(rec.Reason)) { fields["Reason"].SetValue(rec.Reason); }
fields["Data on SF 3100"].SetValue(rec.DataOnSF3100);
fields["Total culmulative"].SetValue(rec.TotalCulmulative);
fields["Corrected Data"].SetValue(rec.CorrectedData);
fields["Overstatement"].SetValue(rec.Overstatement);
fields["Total culmulative deductions"].SetValue(rec.TotalCulmulativeDeductions);
fields["SF 3100 Data 2"].SetValue(rec.SF3100Data2);
fields["SF 3100 Corrected Data 2"].SetValue(rec.SF3100CorrectedData2);
fields["SF 3100 Data"].SetValue(rec.SF3100Data);
fields["SF 3100 Corrected Data"].SetValue(rec.SF3100CorrectedData);
fields["SF 3100 Corrected Data 3"].SetValue(rec.SF3100CorrectedData3);
fields["SF 2812 Number"].SetValue(rec.SF2812Number);
fields["Register"].SetValue(rec.Register);
fields["SF2812 - dated"].SetValue(rec.SF2812Dated);
fields["SF 3100 Data 3"].SetValue(rec.SF3100Data3);
fields["Title"].SetValue(rec.Title);
fields["Telephone Number"].SetValue(rec.TelephoneNumber);
fields["Register - dated"].SetValue(rec.RegisterDated);
fields["Date signed"].SetValue(rec.DateSigned);
form.FlattenFields();
pdfDoc.Close();
return memoryStream.ToArray();
}
}
private static void createPocoFromPDF(string pdf)
{
PdfDocument pdfDoc = new PdfDocument(new PdfReader(pdf));
PdfAcroForm form = PdfAcroForm.GetAcroForm(pdfDoc, true);
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($"public string {l.Replace(" ",string.Empty).Replace(".",string.Empty)} {{ get; set; }}");
}
}
private static void FixFieldNamesPDF(string pdf)
{
PdfDocument pdfDoc = new PdfDocument(new PdfReader(pdf),new PdfWriter(@"output\3100_new.pdf"));
PdfAcroForm form = PdfAcroForm.GetAcroForm(pdfDoc, true);
var fields = form.GetFormFields();
var lines = fields.Select(kvp => kvp.Key); //Grab PDF Fields from document
//int count = 0;
foreach (var l in lines) //Iterate through the fields to build the set value map
{
//Console.WriteLine($"public string {l.Replace(" ",string.Empty).Replace(".",string.Empty)} {{ get; set; }}");
if (l.Contains("Year", StringComparison.OrdinalIgnoreCase))
{
//form.GetFormFields().Put($"EffectiveDate{count}", f);
//fields[l].SetFieldName($"EffectiveDate{count}");
//count++;
}
}
pdfDoc.Close();
}
}
}