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:
Related
I am using iTextSharp library for digital signature. In my PDF file having a QR code and once we applied signature the QR code became a gray color.
The figure (A) shows a original PDF and figure (B) shows the PDF file after digital signature.
following is the code to apply the signature. Please help me, why the QR code became gray color.
public void Sign(PDFSignatureAP sigAP, bool encrypt, PdfEncryption Enc)
{
PdfReader reader = new PdfReader(this.inputPDF);
reader.RemoveUsageRights();
PdfStamper st;
if (this.myCert == null) //No signature just write meta-data and quit
{
st = new PdfStamper(reader, new FileStream(this.outputPDF,
FileMode.Create,FileAccess.Write));
}
else
{
st = PdfStamper.CreateSignature(reader, new FileStream(this.outputPDF, FileMode.Create,
FileAccess.Write), '\0', null, sigAP.Multi);
}
if (encrypt && Enc != null) Enc.Encrypt(st);
st.MoreInfo = this.metadata.getMetaData();
st.XmpMetadata = this.metadata.getStreamedMetaData();
if (this.myCert == null) //No signature just write meta-data and quit
{
st.Close();
return;
}
var pass = new SecureString();
char[] array = myCert.Password.ToCharArray();
foreach (char ch in array)
{
pass.AppendChar(ch);
}
var privateKey = myCert.SCertificate.PrivateKey as RSACryptoServiceProvider;
CspParameters cspParameters = new CspParameters(privateKey.CspKeyContainerInfo.ProviderType,
privateKey.CspKeyContainerInfo.ProviderName,
privateKey.CspKeyContainerInfo.KeyContainerName,
new System.Security.AccessControl.CryptoKeySecurity(),
pass);
RSACryptoServiceProvider rsaCsp;
if (string.IsNullOrEmpty(myCert.Path))
rsaCsp = new RSACryptoServiceProvider(cspParameters);
PdfSignatureAppearance sap = st.SignatureAppearance;
sap.Reason = sigAP.SigReason;
sap.Contact = sigAP.SigContact;
sap.Location = sigAP.SigLocation;
if (sigAP.Visible)
{
iTextSharp.text.Rectangle rect = st.Reader.GetPageSize(sigAP.Page);
sap.Image = sigAP.RawData == null ? null :
iTextSharp.text.Image.GetInstance(sigAP.RawData);
if(!string.IsNullOrEmpty(sigAP.CustomText))
sap.Layer2Text = sigAP.CustomText + Environment.NewLine + "Date :" + DateTime.Now;
sap.Acro6Layers = false;
sap.Layer4Text = PdfSignatureAppearance.questionMark;
sap.SignatureRenderingMode = PdfSignatureAppearance.RenderingMode.DESCRIPTION;
sap.SetVisibleSignature(new iTextSharp.text.Rectangle(sigAP.SigX, sigAP.SigY, sigAP.SigX + sigAP.SigW, sigAP.SigY + sigAP.SigH), sigAP.Page, "Signature");
}
IExternalSignature externalSignature = new X509Certificate2Signature(myCert.SCertificate, "SHA1");
MakeSignature.SignDetached(sap, externalSignature, myCert.Chain, null, null,myCert.Tsc, 0, CryptoStandard.CMS);
st.Close();
reader.Close();
}
Figure A (original)
Figure B (after digital signature applied)
Link for Pdf files
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();
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
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();
}
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);
}