-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathPdfService.cs
225 lines (182 loc) · 8.68 KB
/
PdfService.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
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
using System;
using System.Text;
using iText.Kernel.Pdf;
using iText.Signatures;
using iText.Kernel.Geom;
using Org.BouncyCastle.X509;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Pkcs;
using System.Globalization;
using iText.Kernel.Font;
using iText.IO.Image;
using System.IO;
using iText.Bouncycastle.X509;
using iText.Commons.Bouncycastle.Cert;
using iText.Bouncycastle.Crypto;
namespace NetPdfService
{
public class PdfService
{
private string errorText = "";
public void Firmar(string inFile, string outFile, string certFile, string password, string reason, string location, string contact, string imgeFile, int x1, int y1, int x2, int y2, string nombre, string dni)
{
const bool isVisible = true;
if (String.IsNullOrEmpty(imgeFile))
{
errorText = "Image File cannot be null";
throw new ArgumentNullException(paramName: nameof(imgeFile), message: errorText);
}
if (String.IsNullOrWhiteSpace(nombre)) { nombre = ""; }
if (String.IsNullOrWhiteSpace(nombre)) { dni = ""; }
Firmar(inFile, outFile, certFile, password, reason, location, contact, imgeFile, x1, y1, x2, y2, nombre, dni, isVisible);
}
public void Firmar(string inFile, string outFile, string certFile, string password, string reason, string location, string contact)
{
const string imgeFile = "";
const int x1 = 0;
const int y1 = 0;
const int x2 = 0;
const int y2 = 0;
const string nombre = "";
const string dni = "";
const bool isVisible = false;
Firmar(inFile, outFile, certFile, password, reason, location, contact, imgeFile, x1, y1, x2, y2, nombre, dni, isVisible);
}
internal void Firmar(string inFile, string outFile, string certFile, string password, string reason, string location, string contact, string imgeFile, int x1, int y1, int x2, int y2, string nombre, string dni, bool isVisible)
{
if (String.IsNullOrEmpty(inFile))
{
errorText = "Input File cannot be null";
throw new ArgumentNullException(paramName: nameof(inFile), message: errorText);
}
if (System.IO.Path.GetExtension(inFile) != ".pdf")
{
errorText = "Input File Extension is not PDF";
throw new ArgumentException(paramName: nameof(inFile), message: errorText);
}
if (String.IsNullOrEmpty(outFile))
{
errorText = "Output File cannot be null";
throw new ArgumentNullException(paramName: nameof(outFile), message: errorText);
}
if (System.IO.Path.GetExtension(outFile) != ".pdf")
{
errorText = "Output File Extension is not PDF";
throw new ArgumentException(paramName: nameof(outFile), message: errorText);
}
if (String.IsNullOrEmpty(certFile))
{
errorText = "Certificate File File cannot be null";
throw new ArgumentNullException(paramName: nameof(certFile), message: errorText);
}
if (System.IO.Path.GetExtension(certFile) != ".pfx")
{
errorText = "Certificate File Extension is not PFX";
throw new ArgumentException(paramName: nameof(certFile), message: errorText);
}
if (String.IsNullOrEmpty(password))
{
errorText = "Password cannot be null";
throw new ArgumentNullException(paramName: nameof(password), message: errorText);
}
if (String.IsNullOrWhiteSpace(reason)) { reason = "proof of authenticity"; }
ResetError();
try
{
Sign(inFile, outFile, certFile, password, reason, location, contact, imgeFile, x1, y1, x2, y2, nombre, dni, isVisible, null, null, null, 0);
}
catch (Exception ex)
{
errorText = ex.Message;
}
}
internal void Sign(string inFile, string outFile, string certFile, string password, string reason, string location, string contact, string imgeFile, int x1, int y1, int x2, int y2, string nombre, string dni, bool isVisible,
ICollection<ICrlClient>? crlList, IOcspClient? ocspClient, ITSAClient? tsaClient, int estimatedSize)
{
PdfSigner signer = new PdfSigner(new PdfReader(inFile), new FileStream(outFile, FileMode.Create), new StampingProperties());
signer.SetCertificationLevel(PdfSigner.NOT_CERTIFIED);
//Determinamos la Fecha de la certFile
DateTime fechaFirma = DateTime.Now;
signer.SetSignDate(fechaFirma);
signer.SetFieldName("sig_" + dni + "_" + fechaFirma.ToString("U", DateTimeFormatInfo.InvariantInfo).Replace(" ", "_").Replace(",", "").Replace(":", ""));
signer.SetCertificationLevel(PdfSigner.CERTIFIED_FORM_FILLING);
signer.SetLocation(@location);
signer.SetReason(@reason);
signer.SetContact(@contact);
// Create the signature appearance
PdfSignatureAppearance appearance = signer.GetSignatureAppearance();
if (isVisible)
{
//La Firma será Visible y en la Ultima página del PDF
Rectangle rect = new Rectangle(x1, y1, x2, y2);
int numberOfPages = GetNumberOfPages(inFile);
ImageData imageData = ImageDataFactory.Create(imgeFile);
appearance.SetImage(imageData);
appearance.SetReuseAppearance(false);
appearance.SetPageRect(rect);
appearance.SetPageNumber(numberOfPages);
appearance.SetImageScale(0.22f);
//Añadimos el Nombre y el DNI en la firma como Texto.
StringBuilder buf = new StringBuilder();
buf.Append('\n').Append('\n').Append('\n').Append('\n').Append(@nombre).Append('\n').Append(@dni);
string text = buf.ToString();
//PdfFont font = PdfFontFactory.CreateFont(iText.IO.Font.Constants.StandardFonts.HELVETICA_BOLD);
//appearance.SetLayer2Font(font);
appearance.SetLayer2Text(text);
}
IX509Certificate[]? chain = null;
IExternalSignature? pks = null;
CreateChainFromFile(certFile, password, DigestAlgorithms.SHA256, ref chain, ref pks);
// Aquí es donde necesitas convertir el arreglo de certificados al tipo esperado por iText
var convertedChain = chain.Select(x => (iText.Commons.Bouncycastle.Cert.IX509Certificate)x).ToArray();
signer.SignDetached(pks, convertedChain, crlList, ocspClient, tsaClient, estimatedSize, PdfSigner.CryptoStandard.CMS);
}
internal void CreateChainFromFile(String certFile, String password, String digestAlgorithm, ref IX509Certificate[]? chain, ref IExternalSignature? pks)
{
FileStream certStream = new FileStream(certFile, FileMode.Open, FileAccess.Read);
try
{
Pkcs12Store pk12 = new Pkcs12StoreBuilder().Build();
pk12.Load(certStream, password.ToCharArray());
String alias = "";
foreach (String tAlias in pk12.Aliases)
{
if (pk12.IsKeyEntry(tAlias))
{
alias = tAlias;
break;
}
}
ICipherParameters pk = pk12.GetKey(alias).Key;
X509CertificateEntry[] ce = pk12.GetCertificateChain(alias);
chain = new IX509Certificate[ce.Length];
for (int k = 0; k < ce.Length; ++k)
{
chain[k] = new X509CertificateBC(ce[k].Certificate);
}
pks = new PrivateKeySignature(new PrivateKeyBC(pk), digestAlgorithm);
}
finally
{
certStream.Close();
}
}
internal int GetNumberOfPages(string inputFile)
{
PdfReader reader = new PdfReader(inputFile);
PdfDocument srcDoc = new PdfDocument(reader);
int numberOfPages = srcDoc.GetNumberOfPages();
srcDoc.Close();
reader.Close();
return numberOfPages;
}
public string GetLastError()
{
return errorText;
}
internal void ResetError()
{
errorText = "";
}
}
}