none
Watermark on pdf RRS feed

  • Question

  • I want to add water mark on pdf. I have applied this but not success  kindly check this code if any error so

    solve this issue.

    Private Sub Form1_Load(sender As Object, e As EventArgs)

            For Each page As PdfPageBase In doc.Pages
                Dim brush As PdfTilingBrush = New PdfTilingBrush(New SizeF((page.Canvas.ClientSize.Width / 2), (page.Canvas.ClientSize.Height / 3)))
                brush.Graphics.SetTransparency(0.3!)
                brush.Graphics.Save
                brush.Graphics.TranslateTransform((brush.Size.Width / 2), (brush.Size.Height / 2))
                brush.Graphics.RotateTransform(-45)
                brush.Graphics.DrawString("Water Mark", New PdfFont(PdfFontFamily.Helvetic, 24), PdfBrushes.Violet, 0, 0, New PdfStringFormat(PdfTextAlignment.Center))
                brush.Graphics.Restore
                brush.Graphics.SetTransparency(1)
                page.Canvas.DrawRectangle(brush, New RectangleF(New PointF(0, 0), page.Canvas.ClientSize))
            Next


        End Sub

    Monday, August 12, 2019 4:02 AM

Answers

  • Hi,

    Here is Lswhite65's code demo, please check it.

    https://1drv.ms/u/s!AqgLs2tdMyFNg2_zqov35kIYQ6Ue?e=p8Wwij.

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by CompuTechSide Tuesday, August 13, 2019 8:41 AM
    Tuesday, August 13, 2019 2:12 AM
    Moderator
  • Hi,

    This seems to be a watermark forced by the free version of Spire.Pdf, you can't remove it. So I suggested you use "iTextSharp" before.

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by CompuTechSide Tuesday, August 13, 2019 12:50 PM
    Tuesday, August 13, 2019 9:10 AM
    Moderator
  • In "iTextSharp" No  ********* (Evaluation Warning : The document was created with Spire.PDF for .NET.) ************

    You can give me "iTextSharp"  zip folder this easy for me because too much error there for requested to give me

    zip file


    Best Regards

    • Marked as answer by CompuTechSide Tuesday, August 13, 2019 12:50 PM
    Tuesday, August 13, 2019 10:13 AM
  • Hi,

    Here is the demo project that use "iTextSharp".

    https://1drv.ms/u/s!AqgLs2tdMyFNg3Af4Oj84N3xX-Z2?e=6jIoTG.

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by CompuTechSide Wednesday, August 14, 2019 6:28 AM
    Wednesday, August 14, 2019 6:15 AM
    Moderator
  • Hi,

    Here is the project.

    https://1drv.ms/u/s!AqgLs2tdMyFNg3EpMLdW_Lcur093?e=DpToRQ.

    Besides, you missed "../../".

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by CompuTechSide Wednesday, August 14, 2019 8:12 AM
    Wednesday, August 14, 2019 7:39 AM
    Moderator
  • Hi,

    Generally, one thread only discusses one question. If you have another new one, please create a new thread to post it.

    Thank you for your understanding.

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by CompuTechSide Wednesday, August 14, 2019 10:10 AM
    Wednesday, August 14, 2019 9:30 AM
    Moderator

All replies

  • Hi,

    To add watermark, you can use the Nuget package "iTextSharp". and here is the demo you can refer to.

        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            SetWatermark("D:\pdf\test.pdf", "Hello World")
        End Sub
    
        Public Shared Sub SetWatermark(ByVal filePath As String, ByVal text As String)
            Dim pdfReader As PdfReader = Nothing
            Dim pdfStamper As PdfStamper = Nothing
            Dim tempPath As String = Path.GetDirectoryName(filePath) + Path.GetFileNameWithoutExtension(filePath) & "_temp.pdf"
    
            Try
                pdfReader = New PdfReader(filePath)
                pdfStamper = New PdfStamper(pdfReader, New FileStream(tempPath, FileMode.Create))
                Dim total As Integer = pdfReader.NumberOfPages + 1
                Dim psize As iTextSharp.text.Rectangle = pdfReader.GetPageSize(1)
                Dim width As Single = psize.Width
                Dim height As Single = psize.Height
                Dim content As PdfContentByte
                Dim font As BaseFont = BaseFont.CreateFont("C:\WINDOWS\Fonts\SIMFANG.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED)
                Dim gs As PdfGState = New PdfGState()
    
                For i As Integer = 1 To total - 1
                    content = pdfStamper.GetOverContent(i)
                    gs.FillOpacity = 0.3F
                    content.SetGState(gs)
                    content.BeginText()
                    content.SetColorFill(BaseColor.BLUE)
                    content.SetFontAndSize(font, 30)
                    content.SetTextMatrix(0, 0)
                    content.ShowTextAligned(Element.ALIGN_CENTER, text, width - 120, height - 120, -45)
                    content.EndText()
                Next
    
            Catch ex As Exception
                Throw ex
            Finally
                If pdfStamper IsNot Nothing Then pdfStamper.Close()
                If pdfReader IsNot Nothing Then pdfReader.Close()
                System.IO.File.Copy(tempPath, filePath, True)
                System.IO.File.Delete(tempPath)
            End Try
        End Sub

    Hope this can help you.

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, August 12, 2019 6:04 AM
    Moderator
  • It seems you're using Spire.Pdf to add watermark. The following code works fine on my side, note you need to modify the file path according to your own case.

    Imports Spire.Pdf
    Imports Spire.Pdf.Graphics
    
    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim pdf As PdfDocument = New PdfDocument
            'load pdf file
            pdf.LoadFromFile("Input.pdf")
    
            'add text watermark
            For Each page As PdfPageBase In pdf.Pages
                Dim brush As PdfTilingBrush = New PdfTilingBrush(New SizeF((page.Canvas.ClientSize.Width / 2), (page.Canvas.ClientSize.Height / 3)))
                brush.Graphics.SetTransparency(0.3)
                brush.Graphics.Save()
                brush.Graphics.TranslateTransform((brush.Size.Width / 2), (brush.Size.Height / 2))
                brush.Graphics.RotateTransform(-45)
                Dim font As PdfTrueTypeFont = New PdfTrueTypeFont(New Font("Arial Unicode MS", 20), True)
                brush.Graphics.DrawString("Confidential", font, PdfBrushes.Red, 0, 0, New PdfStringFormat(PdfTextAlignment.Center))
                brush.Graphics.Restore()
                brush.Graphics.SetTransparency(1)
                page.Canvas.DrawRectangle(brush, New RectangleF(New PointF(0, 0), page.Canvas.ClientSize))
            Next
    
            'save 
            pdf.SaveToFile("Output.pdf")
        End Sub
    End Class






    • Edited by Lswhite65 Monday, August 12, 2019 7:00 AM
    Monday, August 12, 2019 6:50 AM
  • 'load pdf file pdf.LoadFromFile("Input.pdf")

    LoadFromFile is not a member of (PdfDocument)

    **********************************************

    'save 
            pdf.SaveToFile("Output.pdf")
    SaveToFile is not a member of (PdfDocument)

    Monday, August 12, 2019 7:19 AM
  • Hi CompuTechSide,

    I test Lswhite65's code in my PC, but it works fine. Have you install the Nuget package "Spire.Pdf"?

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, August 12, 2019 9:22 AM
    Moderator
  • Some internet issue in my side therefor i cannot upload Nuget package therefore requested you can send me Lswhite65's code in zip form

    Best Regards

    CompuTechSide
    Monday, August 12, 2019 1:42 PM
  • Hi,

    Here is Lswhite65's code demo, please check it.

    https://1drv.ms/u/s!AqgLs2tdMyFNg2_zqov35kIYQ6Ue?e=p8Wwij.

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by CompuTechSide Tuesday, August 13, 2019 8:41 AM
    Tuesday, August 13, 2019 2:12 AM
    Moderator
  • Thanks Kyle

    Issue is resolve but one issue when i open pdf one message display on right corner side you can see in pdf image this messge

    ********* (Evaluation Warning : The document was created with Spire.PDF for .NET.) ************

    without  this Evaluation Warning : message you can do something any other option without this message i hope you will do some thing

    Best Regards

    Tuesday, August 13, 2019 8:40 AM
  • Hi,

    This seems to be a watermark forced by the free version of Spire.Pdf, you can't remove it. So I suggested you use "iTextSharp" before.

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by CompuTechSide Tuesday, August 13, 2019 12:50 PM
    Tuesday, August 13, 2019 9:10 AM
    Moderator
  • In "iTextSharp" No  ********* (Evaluation Warning : The document was created with Spire.PDF for .NET.) ************

    You can give me "iTextSharp"  zip folder this easy for me because too much error there for requested to give me

    zip file


    Best Regards

    • Marked as answer by CompuTechSide Tuesday, August 13, 2019 12:50 PM
    Tuesday, August 13, 2019 10:13 AM
  • Hi,

    Here is the demo project that use "iTextSharp".

    https://1drv.ms/u/s!AqgLs2tdMyFNg3Af4Oj84N3xX-Z2?e=6jIoTG.

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by CompuTechSide Wednesday, August 14, 2019 6:28 AM
    Wednesday, August 14, 2019 6:15 AM
    Moderator
  • I want to add pdf file from application resources folder

    I don't want to use this "SetWatermark("D:\Mypdf.pdf", "Confidential")"

    Best Regards

    Wednesday, August 14, 2019 6:35 AM
  • Hi,

    You can use AppDomain.CurrentDomain.BaseDirectory + @"../../pdf folder/test.pdf" to access the pdf file.

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, August 14, 2019 6:43 AM
    Moderator
  • I am saying use pdf file from application resources folder

    I don't want to use pdf folder

    Wednesday, August 14, 2019 6:51 AM
  • Hi,

    Here is the resources folder path.

    AppDomain.CurrentDomain.BaseDirectory + @"../../Resources/newtest.pdf"

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, August 14, 2019 7:05 AM
    Moderator
  • Error on this resources folder path

    Too many arguments to 'Public overload readonly property BaseDirectory as string'
    Method aurguments must be enclosed in prentheses

    Best Regards

    Wednesday, August 14, 2019 7:22 AM
  • Hi,

    My mistake. Please remove the "@" in the path and try it again.

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, August 14, 2019 7:27 AM
    Moderator
  • Wednesday, August 14, 2019 7:34 AM
  • Hi,

    Here is the project.

    https://1drv.ms/u/s!AqgLs2tdMyFNg3EpMLdW_Lcur093?e=DpToRQ.

    Besides, you missed "../../".

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by CompuTechSide Wednesday, August 14, 2019 8:12 AM
    Wednesday, August 14, 2019 7:39 AM
    Moderator
  • Resolve issue

    Best Regards

    Wednesday, August 14, 2019 8:13 AM
  • Another issue TTF file use from resources folder

    I have applied this

     Dim bytes() As Byte = My.Resources.simfang
     Dim pResource As IntPtr = Marshal.AllocCoTaskMem(bytes.Length)
     Marshal.Copy(bytes, 0, pResource, bytes.Length)
     Dim nNumFonts As Integer = 0
     AddFontMemResourceEx(pResource, bytes.Length, IntPtr.Zero, nNumFonts)

    Dim font As BaseFont = BaseFont.CreateFont(AppDomain.CurrentDomain.BaseDirectory + "../../Resources/SIMFANG.TTF")

    Best Regards



    Wednesday, August 14, 2019 9:11 AM
  • Hi,

    Generally, one thread only discusses one question. If you have another new one, please create a new thread to post it.

    Thank you for your understanding.

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by CompuTechSide Wednesday, August 14, 2019 10:10 AM
    Wednesday, August 14, 2019 9:30 AM
    Moderator