What do you mean by "it doesn't work". Does it produce a PDF? Is it
valid? Is the signature invalid (you cannot get around that)?
--Mark Storer
Senior Software Engineer
Cardiff.com
import legalese.Disclaimer;
Disclaimer<Cardiff> DisCard = null;
________________________________
From: Kiran Nepal [mailto:[email protected]]
Sent: Thursday, April 28, 2011 9:27 AM
To: [email protected]
Subject: [iText-questions] How to watermark a digitally signed
pdfprogramatically
Hi Friends,
I urgently need to add watermark on a digitally signed pdf
programatically using C#. I can watermark normal pdf using the code
below but, it doesn't work for a pdf that with digital signature. Can
you please help me to solve this issue.
The following code works and adds watermark on a normal pdf and doesn't
work for digitally signed pdf
public void AddWatermarkText(string sourceFile, string
outputFile, string watermarkText, iTextSharp.text.pdf.BaseFont
watermarkFont, float watermarkFontSize, iTextSharp.text.Color
watermarkFontColor, float watermarkFontOpacity, float
watermarkRotation,string waterMarkWord)
{
iTextSharp.text.pdf.PdfReader reader = null;
iTextSharp.text.pdf.PdfStamper stamper = null;
iTextSharp.text.pdf.PdfGState gstate = null;
iTextSharp.text.pdf.PdfContentByte underContent = null;
iTextSharp.text.Rectangle rect = null;
int pageCount = 0;
try
{
{
reader = new
iTextSharp.text.pdf.PdfReader(sourceFile);
rect = reader.GetPageSizeWithRotation(1);
//stamper = new
iTextSharp.text.pdf.PdfStamper(reader, new
System.IO.FileStream(outputFile, System.IO.FileMode.Create));//original
working but not for signed certificate
stamper = new PdfStamper(reader, new
System.IO.FileStream(outputFile, System.IO.FileMode.CreateNew), '\0',
true); //Runs but doesn't work for signed pdf
//PdfStamper stamp = new PdfStamper(reader, new
BinaryReader("c:\\my_dir\\" + SignedPdf + "-watermarked.pdf"), '\0',
true); //Google for writing in Signed Pdf
if (watermarkFont == null)
{
watermarkFont =
iTextSharp.text.pdf.BaseFont.CreateFont(iTextSharp.text.pdf.BaseFont.HEL
VETICA, iTextSharp.text.pdf.BaseFont.CP1252,
iTextSharp.text.pdf.BaseFont.NOT_EMBEDDED);
}
if (watermarkFontColor == null)
{
watermarkFontColor = iTextSharp.text.Color.BLUE;
}
gstate = new iTextSharp.text.pdf.PdfGState();
gstate.FillOpacity = watermarkFontOpacity;
gstate.StrokeOpacity = watermarkFontOpacity;
pageCount = reader.NumberOfPages;
for (int i = 1; i <= pageCount; i++)
{
//underContent = stamper.GetUnderContent(1);
//If we need just one page, comment for loop and pagecount and write (1)
instead of i
underContent = stamper.GetUnderContent(i);
//write uder the other text///GetOverContent for above the existing page
//_with1 = underContent;
underContent.SaveState();
underContent.SetGState(gstate);
underContent.SetColorFill(watermarkFontColor);
underContent.BeginText();
underContent.SetFontAndSize(watermarkFont,
watermarkFontSize);
underContent.SetTextMatrix(30, 30);
underContent.ShowTextAligned(iTextSharp.text.Element.ALIGN_CENTER,
watermarkText, rect.Width / 2, rect.Height / 2, watermarkRotation);
underContent.EndText();
underContent.RestoreState();
}
}
stamper.Close();
reader.Close();
}
catch (Exception ex)
{
throw ex;
}
}
Thanks
Kiran
------------------------------------------------------------------------------
WhatsUp Gold - Download Free Network Management Software
The most intuitive, comprehensive, and cost-effective network
management toolset available today. Delivers lowest initial
acquisition cost and overall TCO of any competing solution.
http://p.sf.net/sfu/whatsupgold-sd
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions
iText(R) is a registered trademark of 1T3XT BVBA.
Many questions posted to this list can (and will) be answered with a reference
to the iText book: http://www.itextpdf.com/book/
Please check the keywords list before you ask for examples:
http://itextpdf.com/themes/keywords.php