I'm signing a pdf document with iTextSharp. It works fine, but I want to protect this file, I want that this pdf signed can not be printed, copied or signed again, how can I do it?
I have tried with
PdfEncryptor.Encrypt(
reader,
new FileStream(this.outputPDF, FileMode.Create, FileAccess.Write),
true,
null,
null,
PdfWriter.AllowFillIn | PdfWriter.AllowScreenReaders);
but doesnt work, it says
iTextSharp.text.DocumentException: 'The original document was reused. Read it again from file.'
public void Sign(string SigReason, string SigContact, string SigLocation, bool visible)
{
PdfReader reader = new PdfReader(this.inputPDF);
/*ERROR-->*/PdfEncryptor.Encrypt(reader, new FileStream(this.outputPDF, FileMode.Create, FileAccess.Write), true, null, null, PdfWriter.AllowFillIn | PdfWriter.AllowScreenReaders);
PdfStamper st = PdfStamper.CreateSignature(reader, new FileStream(this.outputPDF, FileMode.Create, FileAccess.Write), '\0', null, true);
st.MoreInfo = this.metadata.getMetaData();
st.XmpMetadata = this.metadata.getStreamedMetaData();
PdfSignatureAppearance sap = st.SignatureAppearance;
sap.SetCrypto(this.myCert.Akp, this.myCert.Chain, null, PdfSignatureAppearance.WINCER_SIGNED);
sap.Reason = SigReason;
sap.Contact = SigContact;
sap.Location = SigLocation;
if (visible)
sap.SetVisibleSignature(new iTextSharp.text.Rectangle(100, 100, 250, 150), 1, null);
st.Close();
}
Related
In my PDF I am having one box I want to place digital signature in that box..How can I do it using iTextSharp.
iTextSharp.text.pdf.PdfStamper stamper = iTextSharp.text.pdf.PdfStamper.CreateSignature(reader, fout, '\0');
iTextSharp.text.pdf.PdfSignatureAppearance appearance = stamper.SignatureAppearance;
iTextSharp.text.pdf.BaseFont bf = iTextSharp.text.pdf.BaseFont.CreateFont(System.Web.HttpContext.Current.Server.MapPath("~/files/Arial.ttf"), iTextSharp.text.pdf.BaseFont.IDENTITY_H, iTextSharp.text.pdf.BaseFont.EMBEDDED);
iTextSharp.text.Font font = new iTextSharp.text.Font(bf, 11);
appearance.Layer2Font = font;
//appearance.Image = new iTextSharp.text.pdf.PdfImage();
appearance.Reason = "Test";
appearance.Location = "Pune";
appearance.SetVisibleSignature("SignHere");
IExternalSignature es = new PrivateKeySignature(pk, "SHA-256");
MakeSignature.SignDetached(appearance, es, new Org.BouncyCastle.X509.X509Certificate[] { pk12.GetCertificate(alias).Certificate }, null, null, null, 0, CryptoStandard.CMS);
stamper.Close();
I´m signing a pdf document with digital certificate, using itextsharp 3.1.
Everything works fine, but I want to make some aspect changes that I don't know how they are made.
Currently, the logo (jpg image) is shown in the background of the signature, what I want is that the logo to appear to the left of the signature.
Thanks.
The code I am using is:
public void Sign(string SigReason, string SigContact, string SigLocation, bool visible)
{
PdfReader reader = new PdfReader(this.inputPDF);
PdfStamper st = PdfStamper.CreateSignature(reader, new FileStream(this.outputPDF, FileMode.Create, FileAccess.Write), '\0', null, true);
st.MoreInfo = this.metadata.getMetaData();
st.XmpMetadata = this.metadata.getStreamedMetaData();
PdfSignatureAppearance sap = st.SignatureAppearance;
sap.SetCrypto(this.myCert.Akp, this.myCert.Chain, null, PdfSignatureAppearance.WINCER_SIGNED);
sap.Reason = SigReason;
sap.Contact = SigContact;
sap.Location = SigLocation;
sap.Image = Image.GetInstance(#"Logo.jpg");
sap.SetVisibleSignature(new iTextSharp.text.Rectangle(100, 100, 250, 150), st.Reader.NumberOfPages, null);
st.Close();
}
The actual result is:
The expected result is:
This question already has answers here:
iTextSharp vertical SignatureAppearance
(2 answers)
Closed 3 years ago.
I have code which digitally signs pdf file which displays the signature horizontally by default, but need the signature to be place Vertical position.
I have tried rotating the rectangle to 90 or 270 but didn't worked
Working code(Print signature horizontal position)
X509Certificate2 signatureCert = new X509Certificate2(cert);
if (!cert.HasPrivateKey)
{
MessageBox.Show("Certificate does not have Private Key");
}
else
{
RSACryptoServiceProvider rsa = (RSACryptoServiceProvider)cert.PrivateKey;
if (rsa == null)
{
if (rsa.CspKeyContainerInfo.HardwareDevice) //smartcard
{
if ((rsa.CspKeyContainerInfo.KeyContainerName == CertificateDetails.KeyContainerName) && (rsa.CspKeyContainerInfo.ProviderName == CertificateDetails.ProviderName))
{
// MessageBox.Show("Certificate Match");
}
else
{
MessageBox.Show("Incorrect Certificate Provider Details.");
}
}
}
}
X509CertificateParser cp = new X509CertificateParser();
X509Certificate[] chain = new X509Certificate[] { cp.ReadCertificate(cert.RawData) };
IExternalSignature externalSignature = new X509Certificate2Signature(cert, "SHA-1");
PdfReader pdfReader = new PdfReader(source);
PdfStamper pdfStamper = PdfStamper.CreateSignature(pdfReader, new
FileStream(dest, FileMode.Create, FileAccess.Write), '\0', null, true);
PdfSignatureAppearance signatureAppearance = pdfStamper.SignatureAppearance;
signatureAppearance.SignatureRenderingMode = PdfSignatureAppearance.RenderingMode.DESCRIPTION;
signatureAppearance.Reason = reason;
signatureAppearance.Location = location;
signatureAppearance.SetVisibleSignature(new iTextSharp.text.Rectangle(50, 250, 250, 30), 1, signatureName);
MakeSignature.SignDetached(signatureAppearance, externalSignature, chain, null, null, null, 0, CryptoStandard.CMS);
pdfStamper.Close();
Expecting the output to display signature in vertical position.
Thanks in advance.
The answer was given earlier.
https://stackoverflow.com/a/28042136/12237843
Digital signature on PDF - Complete certificate chain does not show in Acrobat reader DC using Itextsharp 5 with .net
On FOXIT reader certificate complete chain is showing.
PDF download Link : PDF_SAMPLE
private void SignWithCertificate(X509Certificate2 cert)
{
ICollection<X509Certificate> chain = new List<X509Certificate>();
X509Chain x509chain = new X509Chain();
x509chain.Build(cert);
foreach (X509ChainElement x509ChainElement in x509chain.ChainElements)
{
chain.Add(DotNetUtilities.FromX509Certificate(x509ChainElement.Certificate));
}
IExternalSignature externalSignature = new X509Certificate2Signature(cert, "SHA-256");
PdfReader pdfReader = new PdfReader(_sourceFile);
FileStream signedPdf = new FileStream(_targetFile, FileMode.Create); //the output pdf file
PdfStamper pdfStamper = PdfStamper.CreateSignature(pdfReader, signedPdf, '\0');
PdfSignatureAppearance signatureAppearance = pdfStamper.SignatureAppearance;
//here set signatureAppearance at your will
signatureAppearance.Reason = _reason;
signatureAppearance.Location = _location;
signatureAppearance.SignatureRenderingMode = PdfSignatureAppearance.RenderingMode.DESCRIPTION;
signatureAppearance.Acro6Layers = true;
signatureAppearance.Layer4Text = "";
signatureAppearance.SetVisibleSignature(new Rectangle(_coordinates), _pageNo, "Sig");
MakeSignature.SignDetached(signatureAppearance, externalSignature, chain, null, null, null, 0, CryptoStandard.CADES);
}
We are sucessfully signing PDF documents, using the C# sample C2_01_SignHelloWorld:
http://sourceforge.net/p/itextsharp/code/HEAD/tree/tutorial/signatures/chapter2/C2_01_SignHelloWorld/C2_01_SignHelloWorld.cs#l21
For a just about every PDF we sign, this works fine. However, as soon as there is one or more attachments, MakeSignature.SignDetached throws a NullReference exception.
Does someone hava an idea why this is happening?
Thanks!
Here is our code snippet:
PdfReader reader = new PdfReader(pdfFileName);
PdfSignatureAppearance signatureAppearance;
String pdfFileNameDest = pdfFileName.Substring(0, pdfFileName.LastIndexOf('.')) + "-signed" + pdfFileName.Substring(pdfFileName.LastIndexOf('.'));
FileStream fileStream = new FileStream(certFileName, FileMode.Open);
FileStream fileStreamDest = new FileStream(pdfFileNameDest, FileMode.Create);
Pkcs12Store store = new Pkcs12Store(fileStream, certPassword.ToCharArray());
String alias = "";
ICollection<X509Certificate> chain = new List<X509Certificate>();
try
{
// Search private key
foreach (string al in store.Aliases)
if (store.IsKeyEntry(al) && store.GetKey(al).Key.IsPrivate)
{
alias = al;
break;
}
AsymmetricKeyEntry pk = store.GetKey(alias);
foreach (X509CertificateEntry c in store.GetCertificateChain(alias))
chain.Add(c.Certificate);
RsaPrivateCrtKeyParameters parameters = pk.Key as RsaPrivateCrtKeyParameters;
PdfStamper stamper = PdfStamper.CreateSignature(reader, fileStreamDest, '\0');
signatureAppearance = stamper.SignatureAppearance;
signatureAppearance.Reason = "Because we need to sign it";
signatureAppearance.Contact = "We";
signatureAppearance.Location = "Server";
IExternalSignature pks = new PrivateKeySignature(parameters, DigestAlgorithms.SHA256);
MakeSignature.SignDetached(signatureAppearance, pks, chain, null, null, null, 0, CryptoStandard.CMS);
signatureAppearance.SetVisibleSignature(new iTextSharp.text.Rectangle(100, 100, 250, 150), 1, null);
stamper.Close();
}
catch (Exception ex)
{
MessageBox.Show("Exception: " + ex.ToString());
}