0

What I need to do

I'm writing tests and currently trying to check whether or not our PDF get's correctly stamped on the first page. For this, I tried to search the page for the stamp text which did not work because it appears that this stamp is not on the text layer or whatever. In short: I do not know where to look for the stamp.

What the stamping service does

PdfStamper = new PdfStamper(Stamper.mPDFSource, Stamper.mStreamOut);
PdfContentByte canvas = stamper.GetOverContent(mPage);
PdfPTable table = new PdfPTable(1);
table.SetTotalWidth(new float[] { table_width });
Font cellFont = new Font(Font.FontFamily.HELVETICA, mFontSize, Font.NORMAL, textcolor);
Phrase phrase = new Phrase(cell_text, cellFont);
PdfPCell cell = new PdfPCell(phrase);
table.AddCell(cell);
table.WriteSelectedRows(0, strRows.Length, xpos, ypos, canvas);

What I think to have found out

  • The threads I've read tell me stamps created this way basically become annotations. I've yet to find an example where a table is used though.
  • If the previous is correct, why does the following return null? This has been named the solution in different threads.

.

PdfReader.GetPdfObject(pdfDictionary.Get(PdfName.ANNOTS));
PdfReader.GetPdfObject(pdfDictionary.Get(PdfName.ANNOT));
pdfDictionary.GetAsArray(PdfName.ANNOT);
pdfDictionary.GetAsArray(PdfName.ANNOTS);

iTextSharp: 5.3.5.0 .Net: 4.0

Where/how can I find the stamps?

Regards annih

annih
  • 395
  • 2
  • 6
  • 21
  • *The threads I've read tell me stamps created this way basically become annotations.* - Those threads are wrong. Stuff added to the OverContent is added to the content. only as a side effect in some special situations, some annotations might be created. – mkl Oct 23 '13 at 16:04

1 Answers1

2

If you're just adding text and/or images then you should be able to examine the normal text stream. I think that the searches you performed returned annotation results because you talk about "stamps" which are a type of annotation, but you're not using them. You're using a PdfStamper to modify an existing document.

Below is a full-working example that shows off how to create a basic PDF, then modify it by adding a table with a unique string and then finally searching the PDF for the unique string. The comments in the code should explain everything, hopefully. Please take note of the warning at the bottom, too.

/* Setup */

//Test files
var file1 = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "file1.pdf");
var file2 = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "file2.pdf");

//Stamp Text
var stampText = "**UNIQUE_STAMP_TEXT**";

/* Step 1 */

//Create a basic simple file, nothing special here
using (var fs = new FileStream(file1, FileMode.Create, FileAccess.Write, FileShare.None)) {
    using (var doc = new Document()) {
        using (var writer = PdfWriter.GetInstance(doc, fs)) {
            doc.Open();

            doc.Add(new Paragraph("Hello World"));

            doc.Close();
        }
    }
}

/* Step 2 */

//Create our second file based on the first file
using (var reader = new PdfReader(file1)) {
    using (var fs = new FileStream(file2, FileMode.Create, FileAccess.Write, FileShare.None)) {
        using (var stamper = new PdfStamper(reader, fs)) {

            //Get the raw content stream "above" the existing content
            var canvas = stamper.GetOverContent(1);

            //Create a basic single column table
            var table = new PdfPTable(1);
            table.SetTotalWidth(new float[] { 500 });
            var cellFont = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 10, iTextSharp.text.Font.NORMAL, BaseColor.RED);

            //Add our special stamp text
            table.AddCell(new PdfPCell(new Phrase(stampText, cellFont)));

            //Draw the table onto the canvas
            table.WriteSelectedRows(0, 1, 50, 50, canvas);

        }
    }
}

/* Step 3 */

//Search the previously created PDF for the given string
bool hasStampText = false;
using (var reader = new PdfReader(file2)) {
    var text = iTextSharp.text.pdf.parser.PdfTextExtractor.GetTextFromPage(reader, 1);

    //WARNING: PdfPTable will wrap text if needed. Unless you can gaurantee that your text fits into the provided cell
    //         you might want to have some additional logic to search for your unique string.
    hasStampText = text.Contains(stampText);
}
Chris Haas
  • 53,986
  • 12
  • 141
  • 274