I am using iTextSharp to place an image and some text on every page of a PDF. This works perfectly for some PDF's but not for others. Strangely enough it does not work for instance on a PDF I created with Word 2010's 'Save as pdf' function. I use GetOverContent, which should everything on top.
With Acrobat I can see that my layer has been added. So it must be under the word text somewhere.
How can I set a z-index for my stamp?
And is there a way (tool) to see clearly what objects are in a PDF? I find Acrobat not really helpfull in that regard, but maybe I need training ;-)
Example not working PDF here. (link updated)
Stamping code here, PDF comes from database and is stored in database:
Protected Sub cbTekening_Callback(source As Object, e As DevExpress.Web.CallbackEventArgs) Handles cbTekening.Callback
Dim document As New iTextSharp.text.Document()
Dim intTekID As Integer
Try
Dim fieldValues As List(Of Object) = gridTekeningen.GetSelectedFieldValues(New String() {"TekeningID"})
For Each item As Object In fieldValues
Using ms As MemoryStream = New MemoryStream()
intTekID = item
Dim pdfr = New PdfReader(GetPDFFromDatabase(intTekID).ToArray())
Dim pdfs As New iTextSharp.text.pdf.PdfStamper(pdfr, ms)
Dim image__1 As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(New System.Uri(Session("ORG_Website") & Session("ORG_Tekeninglogo")))
Dim image__2 As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(New System.Uri(Session("ORG_Website") & "Images/contactid_" & Session("ContactID") & ".png"))
Dim rect As Rectangle
Dim PageCount As Integer = pdfr.NumberOfPages
Dim content As iTextSharp.text.pdf.PdfContentByte
For x = 1 To PageCount
rect = pdfr.GetPageSize(x)
content = pdfs.GetOverContent(x)
image__1.SetAbsolutePosition(50.0F, 50.0F)
image__1.ScalePercent(30.0F, 30.0F)
image__2.SetAbsolutePosition(100.0F, 100.0F)
image__2.ScalePercent(30.0F, 30.0F)
Content.AddImage(image__1)
Content.AddImage(image__2)
Dim layer As New PdfLayer("Goedkeurlaag" & x.ToString, pdfs.Writer)
'Tell the cb that the next commands should be "bound" to this new layer
Content.BeginLayer(layer)
Content.SetFontAndSize(BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED), 20)
Dim strWatermerkText1 As String = Session("ORG_Tekeningtekst")
Dim strWatermerkText2 As String = Format(Now, "dd-MM-yyyy")
Dim strWatermerkText3 As String = Session("VolNaam")
Content.SetColorFill(BaseColor.RED)
Content.BeginText()
Content.ShowTextAligned(PdfContentByte.ALIGN_LEFT, strWatermerkText3, 60, 160, 0.0F)
Content.ShowTextAligned(PdfContentByte.ALIGN_LEFT, strWatermerkText2, 60, 185, 0.0F)
Content.ShowTextAligned(PdfContentByte.ALIGN_LEFT, strWatermerkText1, 60, 210, 0.0F)
Content.EndText()
'// Close the layer
Content.EndLayer()
Next
pdfs.Close()
StoreToDatabase(ms, intTekID)
End Using
Next item
imgPubliceerTekening.ClientVisible = False
gridTekeningen.DataBind()
Catch ex As Exception
End Try
End Sub