Watermarks Image & Text

Looking for a way to use a watermark image/text under the text of each page. It seems as though the watermark shows but appears to be on there own page. Looking for a solution.

Hi Raymond,

I presume you’re creating a new Section object and adding it to DocumentModel.Sections collection.
For example:

DocumentModel document = ...
Section section = new Section(document);
document.Sections.Add(section);

Instead, try retrieving the existing Section object.
For example:

DocumentModel document = ...
Section section = document.Sections[0];

I hope this helps.

Regards,
Mario

Hi Mario,
I’ve tried that approach before and didn’t work and seems like I received and exception error so I tried this approach which works but only shows at the end of the document.

Dim section1 As New Section(document)
document.Sections.Add(section1)
Dim header1 As New HeaderFooter(document, HeaderFooterType.HeaderDefault)
section1.HeadersFooters.Add(header1)

Dim pictureWatermark As New PictureWatermark(document, New Picture(document, LBLCompanyPicDirectory.Text))
header1.Watermark = pictureWatermark
pictureWatermark.AutoScale()
pictureWatermark.Washout = False

What error did you get?
Please send us a small Visual Studio project that reproduces your issue so that we can investigate it.

Yes, that is expected because you’re creating a new Section object and adding it to the DocumentModel.Sections:

Dim section1 As New Section(document)
document.Sections.Add(section1)

Hi Mario,

The error we get is Severity Code Description Project File Line Suppression State
Error BC30638 Array bounds cannot appear in type specifiers.

BlockquoteDocumentModel document = …
Section section = document.Sections[0];

Dim section1 as section{0,1,2,3}
Document.Section.Add(section1)

I’ll need to see your complete code, I’m not sure what you’re writing.
Nevertheless, try this:

Dim document As DocumentModel

' ...

Dim section1 = document.Sections(0)

Dim header1 = section1.HeadersFooters.GetOrAdd(HeaderFooterType.HeaderDefault)
Dim pictureWatermark As New PictureWatermark(document,
    New Picture(document, LBLCompanyPicDirectory.Text))
header1.Watermark = pictureWatermark
pictureWatermark.AutoScale()
pictureWatermark.Washout = False

I hope this helps.

Regards,
Mario

Hi Mario,

It’s difficult to say that the above section is what we’ve tried before but what’s happening is that we have an established “.HeadersFooters.add(…)” and apparently we are faced with removing one or the other to make it work.
So in other words, if we want a typical header and footer in a word document we can’t have the watermark at the same time.

#Region "Document Parameters"
            'Watermarks
            ' Add the first section.
            Dim section1 = document.Sections(0)
            document.Sections.Add(section1)
            Dim header1 = section1.HeadersFooters.GetOrAdd(HeaderFooterType.HeaderDefault)
            section1.HeadersFooters.Add(header1)

            ' Add the second section.
            Dim section2 As New Section(document)
            document.Sections.Add(section2)
            Dim header2 As New HeaderFooter(document, HeaderFooterType.HeaderDefault)
            section2.HeadersFooters.Add(header2)


            'Setting the font and font size
            document.DefaultCharacterFormat.FontName = LblFont.Text
            document.DefaultCharacterFormat.Size = LblFontSize.Text

            document.DefaultCharacterFormat.Spacing = 0
            ' Setup LineBreakElement
            Dim lineBreakElement As New SpecialCharacter(document, SpecialCharacterType.LineBreak)

            ' Create and add "Heading 1 & Heading 2" style.
            Dim heading1Style = DirectCast(Style.CreateStyle(StyleTemplateType.Heading1, document), ParagraphStyle)
            document.Styles.Add(heading1Style)
            Dim heading2Style = DirectCast(Style.CreateStyle(StyleTemplateType.Heading2, document), ParagraphStyle)
            document.Styles.Add(heading2Style)

#End Region

#Region "Header"
            If frmMain.statbar_ClientBuilding.Text = "Residential" Then
                ' Add first header. then
                section.HeadersFooters.Add(New HeaderFooter(document, HeaderFooterType.HeaderDefault,
                    New Paragraph(document, "Ground Zero Home Inspection LLC" & vbCrLf & "Confidential - Property Inspection Report - Confidential") With
                    {.ParagraphFormat = New ParagraphFormat() With {.Alignment = HorizontalAlignment.Center}}))
            Else
                ' Add first header. then
                section.HeadersFooters.Add(New HeaderFooter(document, HeaderFooterType.HeaderDefault,
                    New Paragraph(document, "Ground Zero Commercial Inspections" & vbCrLf & "Confidential - Property Inspection Report - Confidential") With
                    {.ParagraphFormat = New ParagraphFormat() With {.Alignment = HorizontalAlignment.Center}}))
            End If

#End Region

#Region "WaterMark"

            If ChkAddWaterMark.Checked = True Then
                Try

                    If ChkWatermarkCompanyLogo.Checked = True Then
                        ' Create a picture watermark and scale it to fit the page.
                        Dim pictureWatermark As New PictureWatermark(document, New Picture(document, LBLCompanyPicDirectory.Text))
                        header1.Watermark = pictureWatermark
                        pictureWatermark.AutoScale()
                        pictureWatermark.Washout = False

                    ElseIf ChkWatermarkCompanyText.Checked = True Then
                        ' Create a text watermark and rotate it diagonally.
                        Dim textWatermark As New TextWatermark(document, "Acme corporation")
                        header2.Watermark = textWatermark
                        textWatermark.SetDiagonal()
                        textWatermark.Color = Color.Red
                        textWatermark.Semitransparent = True
                    End If

                Catch ex As Exception
                    MsgBox(ex.ToString())
                End Try
            End If

#End Region

Yes, you can.
I would just like to emphasize that in the snippet code I provided I’m not creating a new header if it already exists. I’m doing that using the HeadersFooters.GetOrAdd method.

Can you please send us a small Visual Studio project that reproduces your issue so that we can investigate it?
Also, please specify exactly what you would like to achieve.

Regards,
Mario

Hi Mario,

Per your requests we’ve uploaded the project files. To the watermarks, we still are having some difficulty getting them to show on the saved document.

I uploaded the project to this URL: Project Files

Hi Raymond,

Please take a look at this code:

Dim section As New Section(document)
document.Sections.Add(section)

'Watermarks
' Add the first section.
Dim section1 = document.Sections(0)
document.Sections.Add(section1)
Dim header1 = section1.HeadersFooters.GetOrAdd(HeaderFooterType.HeaderDefault)
section1.HeadersFooters.Add(header1)

' Add the second section.
Dim section2 As New Section(document)
document.Sections.Add(section2)
Dim header2 As New HeaderFooter(document, HeaderFooterType.HeaderDefault)
section2.HeadersFooters.Add(header2)

It creates a new Section and adds it to the document, then it retrieves that section as “section1” variable and tries to add it again.
This will result in an exception being thrown:

document.Sections.Add(section1)

Also, HeadersFooters.GetOrAdd method will already add the header to the section so there is no for you to add it:

section1.HeadersFooters.Add(header1)

Also, I see that the rest of the code only adds content to the “section”, nothing seems to be added to “section2”.
So, try using this instead:

Dim section As New Section(document)
document.Sections.Add(section)
Dim header = section.HeadersFooters.GetOrAdd(HeaderFooterType.HeaderDefault)

' ...

#Region "Header"
        ' Add first header. then
        header.Blocks.Add(
            New Paragraph(document, "Ground Zero Home Inspection LLC" & vbCrLf & "Confidential - Property Inspection Report - Confidential") With
            {.ParagraphFormat = New ParagraphFormat() With {.Alignment = HorizontalAlignment.Center}})
#End Region

#Region "WaterMark"
        Try

            If RadioButton1.Checked = True Then
                ' Create a picture watermark and scale it to fit the page.
                Dim pictureWatermark As New PictureWatermark(document, New Picture(document, TextBox1.Text))
                header.Watermark = pictureWatermark
                pictureWatermark.AutoScale()
                pictureWatermark.Washout = False

            ElseIf RadioButton2.Checked = True Then
                ' Create a text watermark and rotate it diagonally.
                Dim textWatermark As New TextWatermark(document, "Acme corporation")
                header.Watermark = textWatermark
                textWatermark.SetDiagonal()
                textWatermark.Color = Color.Red
                textWatermark.Semitransparent = True
            End If

        Catch ex As Exception
            MsgBox(ex.ToString())
        End Try
#End Region

I hope this helps.

Regards,
Mario

Thank you Mario for your help and guidance.
It’s amazing what a little watermark does for our reports.