0

I'm trying to add a watermark over a PDF that I am generating using HTML/CSS. My initial approach was to create a transparent PNG the size of the page and overlay that image on top of each page of the generated document. Since ABCpdf does not consider PNG as a suitable image file, I'm loading the PNG as an XImage and preserving the transparency. However, the image is still appearing opauqe over the formatted HTML/CSS layer.

    Private Shared _watermark As Byte()
    Private Shared ReadOnly Property Watermark() As Byte()
        Get
            If _watermark Is Nothing Then
                Using memoryStream As New MemoryStream()
                    My.Resources.watermark.Save(memoryStream, Imaging.ImageFormat.Png)

                    _watermark = memoryStream.ToArray()
                End Using
            End If

            Return _watermark
        End Get
    End Property

Private Function CreatePreviewPdf(html As String) As Byte()
        Dim generatedStream = New MemoryStream()

        Using pdfDoc = New Doc()
            pdfDoc.HtmlOptions.Engine = EngineType.Gecko
            pdfDoc.Rect.Inset(25, 25)
            'Adds a margin
            pdfDoc.Page = pdfDoc.AddPage()

            Dim options As New XReadOptions
            options.PreserveTransparency = True
            Dim imgwater = XImage.FromData(Watermark, options)

            Dim id = pdfDoc.AddImageHtml(html)
            While True
                If Not pdfDoc.Chainable(id) Then
                    Exit While
                End If

                pdfDoc.Page = pdfDoc.AddPage()
                id = pdfDoc.AddImageObject(imgwater, 1)
            End While

            For i As Object = 1 To pdfDoc.PageCount
                pdfDoc.PageNumber = i
                pdfDoc.Flatten()
            Next

            If pdfDoc.PageCount > 0 Then
                pdfDoc.PageNumber = 1
            End If

            Dim theId As Integer = 0
            For i As Integer = 1 To pdfDoc.PageCount
                pdfDoc.PageNumber = i
                pdfDoc.Layer = pdfDoc.LayerCount + 1
                If i = 1 Then
                    theId = pdfDoc.AddImageToChain(theId)
                Else
                    pdfDoc.AddImageCopy(theId)
                End If
            Next

            pdfDoc.Save(generatedStream)
        End Using

        generatedStream.Position = 0

        Return generatedStream.ToArray()
    End Function

How can I add a semitransparent watermark to every page?

bjb568
  • 11,089
  • 11
  • 50
  • 71
cdalto
  • 797
  • 1
  • 15
  • 33
  • "Since ABCpdf does not consider PNG as a suitable image file" Say what? I use PNGs every day with ABCpdf. What version are you using? – Ross Presser Feb 04 '15 at 18:09
  • I was using 8.1 at the time. – cdalto Mar 03 '15 at 21:29
  • I've used PNG with abcPDF since about version 6.5. – Ross Presser Mar 04 '15 at 21:40
  • Interesting. Well I never found a way to resolve the issue, so I ended up using HTML/CSS to get what I needed done at the time. Looking at WebSupergoo's website, I found this in their FAQ: "Why doesn't AddImage work with my GIF images? You can pass AddImage a path to a JPEG, TIFF or EMF file. This inserts the raw compressed data into your PDF document. Other image types (e.g. GIF) cannot be inserted direct - they must be drawn into an Image object and then the Image object can be added to the PDF document. There are examples in the ABCpdf documentation." I was using AddImage with a PNG file. – cdalto Mar 05 '15 at 19:45
  • 1
    Ah! It's been so long since I wrote that code, I'd forgotten. For PNG, I pull the PNG into an XImage, then use AddImageObject. For reasons of caching (this is on a website), I actually use a byte array to hold the PNG data before I pass it to XImage. See http://pastebin.com/aS1HsY8M for a snippet that takes in a PNG file and puts out a Doc file. In my workflow I generally place the Doc into another Doc next. – Ross Presser Mar 09 '15 at 20:03

0 Answers0