iTextSharp (C#): Add text (over everything) at position
<p>I'm trying to use iTextSharp in a WPF project to add text to existing PDFs over everything else and at a given point.  Basically the digital equivalent of writing on a paper document</p>

<p>I've tried multiple methods and nothing seems to actually write the text to the new PDF. My code is messy from testing various things (left in so I know what I've tried - yes, I'll be clearing it out after resolution), so I'll try to get all of the important lines.</p>

<pre><code>var stream = new FileStream(targetFilePath, FileMode.Create);
var doc = new iTextSharp.text.Document();
var target = new PdfCopy(doc, stream);

doc.Open();

var baseFont = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, false);

foreach (var vm in Pages) // Cycles through pages in WPF object...
{
    var srcDoc = vm.DocTmpFName;

    if (Path.GetExtension(srcDoc).ToUpperInvariant() == ".PDF")
    {
        var reader = new iTextSharp.text.pdf.PdfReader(srcDoc);
        var page = reader.GetPageN(vm.Number);
        var stamper = new PdfStamper(reader, stream);

        target.AddPage(target.GetImportedPage(reader, vm.Number));

        page.Put(PdfName.ROTATE, new PdfNumber(vm.Rotation % 360f));

        // Inside this 'foreach' loop is what isn't working.  The rest works fine.
        foreach(var str in vm.Strings) // str is a container class (just a string and two 'double' vars for positioning)
        {
            // This isn't writing to new PDF even though it gets run (verified with breakpoints)
            var contentByte = stamper.GetOverContent(vm.Number);

            contentByte.BeginText();
            contentByte.SetTextMatrix((float)str.X, (float)str.Y);
            contentByte.SetFontAndSize(baseFont, 72f); //Making it stupid-big so I don't miss it
            contentByte.ShowText(str.String);
            contentByte.EndText();
        }

        target.FreeReader(reader);
        reader.Close();

    }
}

target.Close();
doc.Close();
stream.Close();
</code></pre>

<p>If you feel I've missed something let me know.  I tried to keep the context of the problem area, so hopefully it's not too horrible of a hack-job.</p>

<p><strong>- Edited to correct code -</strong></p>

<hr>

<p><strong>Edit</strong></p>

<p>I renamed some variables to make it easier to discern what they're for. Also, I've included the new fixed (albeit buggy) code that adds text at a position - for anyone stumbling across this later. =)</p>

<h2>Working code after @mkl's answer:</h2>

<pre><code>var stream = new FileStream(targetFilePath, FileMode.Create);
var doc = new iTextSharp.text.Document();
var targetPdf = new PdfCopy(doc, stream);

doc.Open();

foreach (ViewModels.PageViewModel vm in document.Pages)
{
    var srcDocPath = FileIO.ToTempFileName(vm.DocName);

    // Copy pageDict from source...
    if (Path.GetExtension(srcDocPath).ToUpperInvariant() == ".PDF")
    {
        var srcReader = new iTextSharp.text.pdf.PdfReader(srcDocPath);
        var pageDict = srcReader.GetPageN(vm.Number);
        var importedPage = targetPdf.GetImportedPage(srcReader, vm.Number);
        var pageStamp = targetPdf.CreatePageStamp(importedPage);

        foreach(var str in vm.Strings)
        {
            ColumnText.ShowTextAligned(pageStamp.GetOverContent(),
                iTextSharp.text.Element.ALIGN_LEFT,
                new iTextSharp.text.Phrase(str.String),
                (float) str.X,
                // pdf-origin is bottom-left, not top-left
                (float)(importedPage.Height - str.Y),
                0);
        }

        pageDict.Put(PdfName.ROTATE, new PdfNumber(vm.Rotation % 360f));
        pageStamp.AlterContents();
        targetPdf.AddPage(importedPage);

        targetPdf.FreeReader(srcReader);
        srcReader.Close();
    }
}
targetPdf.Close();
doc.Close();
stream.Close();
</code></pre>