Adding header/footer stamp at the correct location (VB.NET).

Q: I am using the code below (VB.NET) to stamp/watermark existing PDF
documents. The results are fine for most PDF documents, however for
some files (usually rotated or unusual dimensions) the header/footer
does not appear on the correct location. Could you please point me in
the right direction?

#Region "Stamp PDF"
Public Function Stamp() As Boolean
  Trace.Write("Starting pdf stamping at " + DateTime.Now.ToString)
  Stamp = True
   Dim PDFStampText As String = Me.m_DocProp.Watermark.Stamp
   Dim PDFStampFont As String = Me.m_DocProp.Watermark.Style.Font
   Dim PDFStampFontBold As Boolean = Me.m_DocProp.Watermark.Style.Bold
   Dim PDFStampFontItalic As Boolean =
   Dim PDFStampFontSize As String =
   Dim PDFStampTextColor As String =
   Dim PDFStampPosition as String =
   Dim PDFStampVisibility As String =

   Dim colorR As Double = CType(PDFStampTextColor.Split(",")(0),
   Dim colorG As Double = CType(PDFStampTextColor.Split(",")(1),
   Dim colorB As Double = CType(PDFStampTextColor.Split(",")(2),

   PDFStampText = SearchReplaceTokens(PDFStampText)
   trace.Write("Watermark= " + PDFStampText)
            Using Doc As New PDFDoc(Me.m_DownloadFilePath)

                Dim Builder As New ElementBuilder
                Dim Writer As New ElementWriter
' Dim pageEnd As PageIterator = Doc.PageEnd()
                Dim itr As PageIterator = Doc.GetPageIterator()
                Dim element As Element
                Dim FStyle As FontStyle = FontStyle.Regular
                If PDFStampFontBold Then FStyle = FStyle Or
                If PDFStampFontItalic Then FStyle = FStyle Or
                Dim SysFont As System.Drawing.Font = New
System.Drawing.Font(PDFStampFont, CInt(PDFStampFontSize), FStyle)
                Dim font As Font = font.CreateTrueTypeFont(Doc,
SysFont, True, True)

                While itr.HasNext()
                    element = Builder.CreateTextBegin(font,
                    Dim gs As GState = element.GetGState()
                    element = Builder.CreateTextRun(PDFStampText)
                    If PDFStampPosition.Equals("Header",
StringComparison.OrdinalIgnoreCase) Then
                        element.SetTextMatrix(1, 0, 0, 1, 20, 770)
                    ElseIf PDFStampPosition.Equals("Footer",
StringComparison.OrdinalIgnoreCase) Then
                        element.SetTextMatrix(1, 0, 0, 1, 20, 20)
                        'Assuming diagonal
                        element.SetTextMatrix(1, 1, 0, 1, 0, 20)
                    End If
                If (colorR >1) Then
                 colorR = colorR/255
                End If
                If (colorG >1) Then
                 colorG = colorG/255
                End If
                If (colorB >1) Then
                 colorB = colorB/255
                End If

                    gs.SetFillColor(New ColorPt(colorR, colorG,
                End While

                Me.m_StampedFilePath = Me.m_DownloadFolder + "/" +
Me.m_TS + "_" + STAMPED + "." + Me.m_DocType
                Trace.Write("Stamped file path = [" +
Me.m_StampedFilePath + "]")
       End Using
            Trace.Write("Ending pdf stamping at " +

  Catch ex As System.Exception
            Stamp = False
            Trace.Warn(ex.Message + " " + ex.StackTrace)
            Throw ex
            Exit Function
        End Try
End Function
#End Region
A: The problem is that you are stamping PDF page content at fixed
(hardcoded locations). In PDF format pages can have different
dimensions and as a result the stamp may not be positioned at the
correct location.

In order to account for pages with variable dimensions, you need to
use page.GetPageWidth()/GetPageHeight(). For example:

Dim page as Page = itr.Current()
If PDFStampPosition.Equals("Header",
StringComparison.OrdinalIgnoreCase) Then
  ' position the text 22 points down from the top of the page
  ' and 20 points to the right.
  element.SetTextMatrix(1, 0, 0, 1, 20, page.GetPageHeight() - 22)