7

I am using following code:

PdfReader PDFReader = new PdfReader("C:\\file.pdf");

FileStream Stream = new FileStream("C:\\new.pdf", FileMode.Create, FileAccess.Write);

PdfStamper PDFStamper = new PdfStamper(PDFReader, Stream);

for (int iCount = 0; iCount < PDFStamper.Reader.NumberOfPages; iCount++)
{
    PdfContentByte PDFData = PDFStamper.GetOverContent(iCount + 1);
    BaseFont baseFont = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.EMBEDDED);
    PDFData.BeginText();
    PDFData.SetColorFill(CMYKColor.LIGHT_GRAY);
    PDFData.SetFontAndSize(baseFont, 80);
    PDFData.ShowTextAligned(PdfContentByte.ALIGN_CENTER, "SAMPLE DOCUMENT", 300, 400, 45);
    PDFData.EndText();
}

Stream.Close();
PDFReader.Close();
PDFStamper.Close();

But PDFStamper.Close(); throws error. Also, I am not sure whether to call PDFReader.Close(); before or after PDFStamper.Close();

And watermark is not added in PDF file.

1 Answer 1

3

Your order of Close calls is all wrong:

Stream.Close();
PDFReader.Close();
PDFStamper.Close();

In particular the PDFStamper requires both the PDFReader and the Stream to still be open when it is getting closed.

Furthermore, unless an exception is thrown, the Stream automatically is closed during PDFStamper.Close().

Thus, use

PDFStamper.Close();
PDFReader.Close();

instead.

If you want to make sure that the Stream is getting closed in case of an exception, use a using statement.

9
  • Thanks, it is working fine now. But one question, I want my text to appera inside the page, it is not covering the whole page, sometimes it is smaller than the page and sometimes larger. Dec 26, 2015 at 7:18
  • 1
    You get this behavior because you are using hard coded coordinates (x = 300 and y = 400). You should calculate the offset. See for instance How to add an image watermark to a PDF file? In your case, you also want to check pagesize.getRight() and pagesize.getTop() to find out if your watermark exceeds the available space. See also How to position text relative to page? Those are Java examples. You'll have to make minimal changes to make this work in iTextSharp Dec 26, 2015 at 7:27
  • @BrunoLowagie Hi Bruno Lowagie, thanks for the reply. I am honored to speak to you. I will try to set the coordinates. I am trying to put a watermark in diagonal way as "Sample Document" in the PDF file. I am sure you must have seen such in many documents. I want to have it as exactly starting from bottom left to top right of the page irrespective of page coordinates etc. So, I will try to use some maths to get. and will look into the link that you gave. Thanks. Dec 26, 2015 at 7:52
  • 2
    @ComputerUser you also want to check pagesize.getRight() and pagesize.getTop() to find out if your watermark exceeds the available space. - And also check pagesize.Left and pagesize.Bottom: They don't need to be 0 (even if they often are)!
    – mkl
    Dec 26, 2015 at 7:52
  • 1
    Is pagesize.getTop(0) same as pagesize.Top? and similarly for other coordinates like pagesize.getLeft(0) and pagesize.Left? - yes. The method GetTop(float) allows you to automatically have a margin value applied to the border, and for a margin of 0 you get the original border.
    – mkl
    Dec 26, 2015 at 9:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.