Replace text and string in PDF file by PDF Parser & Modify SDK from C#, VB.NET and ASP.NET

PDF Parser & Modify SDK for .NET can be downloaded and purchased from following web page,

 

https://www.verydoc.com/pdfparsersdk.html

 

You can use following VB.NET code to replace text contents in PDF page, you can port this VB.NET code to C# code easily.

 

The entire example is included in this download package,

 

https://www.verydoc.com/pdfparsersdk.zip

 

Imports System

Imports System.IO

Imports System.Text

Imports System.Drawing

Imports System.Drawing.Imaging

 

Public Class Form1

 

    Private Declare Function VeryPDF_PDFParserSDK Lib "pdfparsersdk2.dll" (ByVal lpPDFFile As String, ByVal lpOutFile As String, ByVal lpOptions As String) As Integer

    Private Declare Function VeryPDF_PDFParserSDKFromMemory Lib "pdfparsersdk2.dll" (ByRef lpPDFData As Byte, ByVal nDataLen As Integer, ByVal lpOutFile As String, ByVal lpOptions As String) As Integer

    Private Declare Function VeryPDF_PDFParserSDK_GetHandle Lib "pdfparsersdk2.dll" (ByVal lpPDFFile As String, ByVal lpOptions As String) As Integer

    Private Declare Function VeryPDF_PDFParserSDK_GetCount Lib "pdfparsersdk2.dll" (ByVal hPDFParserData As Integer) As Integer

    Private Declare Function VeryPDF_PDFParserSDK_GetImageLength Lib "pdfparsersdk2.dll" (ByVal hPDFParserData As Integer, ByVal nIndex As Integer) As Integer

    Private Declare Function VeryPDF_PDFParserSDK_GetImageData Lib "pdfparsersdk2.dll" (ByVal hPDFParserData As Integer, ByVal nIndex As Integer, ByRef lpData As Byte, ByVal nBufLen As Integer) As Integer

    Private Declare Function VeryPDF_PDFParserSDK_GetTextInfoLength Lib "pdfparsersdk2.dll" (ByVal hPDFParserData As Integer, ByVal nIndex As Integer) As Integer

    Private Declare Function VeryPDF_PDFParserSDK_GetTextInfoData Lib "pdfparsersdk2.dll" (ByVal hPDFParserData As Integer, ByVal nIndex As Integer, ByRef lpData As Byte, ByVal nBufLen As Integer) As Integer

    Private Declare Function VeryPDF_PDFParserSDK_Free Lib "pdfparsersdk2.dll" (ByVal hPDFParserData As Integer) As Integer

    Private Declare Function VeryPDF_PDFParserSDK_GetPageCount Lib "pdfparsersdk2.dll" (ByVal lpPDFFile As String) As Integer

    Private Declare Function VeryPDF_PDFParserSDK_GetAllPagesCount Lib "pdfparsersdk2.dll" (ByVal hPDFParserData As Integer) As Integer

 

    Private Declare Function VeryPDF_ModifyPDF_OpenFile Lib "pdfparsersdk2.dll" (ByVal lpInPDFFile As String, ByVal lpOutPDFFile As String) As Integer

    Private Declare Function VeryPDF_ModifyPDF_CloseFile Lib "pdfparsersdk2.dll" (ByVal hPDF As Integer) As Integer

    Private Declare Function VeryPDF_ModifyPDF_ModifyText Lib "pdfparsersdk2.dll" (ByVal hPDF As Integer, ByVal nPage As Integer, ByVal x As Integer, ByVal y As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal lpOldText As String, ByVal lpNewText As String) As Integer

 

    Private Declare Sub VeryPDF_ModifyPDF_SetCode Lib "pdfparsersdk2.dll" (ByVal lpCode As String)

 

    Public Function Byte2Image(ByVal ByteArr() As Byte) As Image

        Dim NewImage As Image = Nothing

        Dim ImageStream As MemoryStream

        Try

            If ByteArr.GetUpperBound(0) > 0 Then

                ImageStream = New MemoryStream(ByteArr)

                NewImage = Image.FromStream(ImageStream)

            Else

                NewImage = Nothing

            End If

        Catch ex As Exception

            NewImage = Nothing

        End Try

        Byte2Image = NewImage

    End Function

 

    Public Function Image2Byte(ByRef NewImage As Image) As Byte()

        Dim ImageStream As MemoryStream

        Dim ByteArr() As Byte = Nothing

        Try

            ReDim ByteArr(0)

            If NewImage IsNot Nothing Then

                ImageStream = New MemoryStream

                NewImage.Save(ImageStream, ImageFormat.Png)

                ReDim ByteArr(CInt(ImageStream.Length - 1))

                ImageStream.Position = 0

                ImageStream.Read(ByteArr, 0, CInt(ImageStream.Length))

            End If

        Catch ex As Exception

        End Try

        Image2Byte = ByteArr

    End Function

 

   

 

    Private Sub LoadPDFFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LoadPDFFile.Click

        OpenFileDialog1.Title = "Please Select a File"

        OpenFileDialog1.InitialDirectory = Application.StartupPath()

        OpenFileDialog1.ShowDialog()

    End Sub

 

    Private Sub OpenFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk

        Dim strm As System.IO.Stream

        strm = OpenFileDialog1.OpenFile()

        PDFFileName.Text = OpenFileDialog1.FileName.ToString()

        If Not (strm Is Nothing) Then

            'insert code to read the file data

            strm.Close()

 

            ParserPDFFile()

        End If

    End Sub

 

    Private Sub ParserPDFFile()

 

        Dim nRet As Integer

        Dim strOptions As String

        Dim strInPDFFile As String

        Dim strOutFile As String

        Dim strLogMsg As String

        Dim nDPI As Integer = 300

 

        'Render PDF pages to PNG image files first

        strInPDFFile = PDFFileName.Text

        strOutFile = Application.StartupPath() & "\out.png"

        strOptions = "-r 72 -html -$ XXXXXXXXXXXXXXXXXXXXXX"

        nRet = VeryPDF_PDFParserSDK(strInPDFFile, strOutFile, strOptions)

        strLogMsg = strInPDFFile & vbCrLf & strOutFile & vbCrLf & strOptions & vbCrLf & "nRet = " & Str(nRet)

        MsgBox(strLogMsg)

 

        Dim strOutHTMLFile As String = Application.StartupPath() & "\out_pg_0001.htm"

        Dim Reader As StreamReader = File.OpenText(strOutHTMLFile)

        Dim strFileText As String = Reader.ReadToEnd()

        Reader.Close()

        HTMLContents.Text = strFileText

        WebBrowser1.Url = New Uri(Path.Combine("file://", strOutHTMLFile))

    End Sub

    Private Sub DEMO_ReplaceTextInPDFDirectly_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DEMO_ReplaceTextInPDFDirectly.Click

 

        Dim nRet As Integer

        Dim strOptions As String

        Dim strInPDFFile As String

        Dim strOutFile As String

        Dim strLogMsg As String

        Dim nDPI As Integer = 300

 

        'Render PDF pages to PNG image files first

        strInPDFFile = Application.StartupPath() & "\F_COC.pdf"

        strOutFile = Application.StartupPath() & "\out.png"

        strOptions = "-r " & CStr(nDPI) & " -html -$ XXXXXXXXXXXXXXXXXXXXXX"

        nRet = VeryPDF_PDFParserSDK(strInPDFFile, strOutFile, strOptions)

        strLogMsg = strInPDFFile & vbCrLf & strOutFile & vbCrLf & strOptions & vbCrLf & "nRet = " & Str(nRet)

        MsgBox(strLogMsg)

 

        'default DPI is 72DPI in PDF file, so you need calculate position by 72DPI,

        'you can read the text contents and position from output HTML file

        Dim x, y, w, h, hPDF, bRet, nPage As Integer

        Dim strOldText, strNewText As String

 

        VeryPDF_ModifyPDF_SetCode("Your License Key for ModifyPDF SDK")

        strOutFile = Application.StartupPath() & "\modified.pdf"

        hPDF = VeryPDF_ModifyPDF_OpenFile(strInPDFFile, strOutFile)

 

        'Replace horizontal text contents

        nPage = 1

        x = 452 * 72.0 / nDPI

        y = 809 * 72.0 / nDPI

        w = 150 * 72.0 / nDPI

        h = 35 * 72.0 / nDPI

        strOldText = "Voucher"

        strNewText = "VeryPDF1"

        bRet = VeryPDF_ModifyPDF_ModifyText(hPDF, nPage, x, y, w, h, strOldText, strNewText)

 

        'Replace vertical text contents

        nPage = 1

        x = 191 * 72.0 / nDPI

        y = 199 * 72.0 / nDPI

        w = 70 * 72.0 / nDPI

        h = 559 * 72.0 / nDPI

        strOldText = "PV91039100375"

        strNewText = "VeryPDF12"

        bRet = VeryPDF_ModifyPDF_ModifyText(hPDF, nPage, x, y, w, h, strOldText, strNewText)

 

        VeryPDF_ModifyPDF_CloseFile(hPDF)

        MsgBox("Process completed." & vbCrLf & "VeryPDF_ModifyPDF_ModifyText() return: " & CStr(bRet))

    End Sub

 

    Private Sub Replace_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Replace.Click

        'default DPI is 72DPI in PDF file, so you need calculate position by 72DPI,

        'you can read the text contents and position from output HTML file

        Dim x, y, w, h, hPDF, bRet, nPage As Integer

        Dim strOldText, strNewText As String

 

        VeryPDF_ModifyPDF_SetCode("Your License Key for ModifyPDF SDK")

        Dim strInPDFFile As String = PDFFileName.Text

        If strInPDFFile = "" Then

            MsgBox("Please select a PDF file first")

            Return

        End If

        Dim strOutFile As String = Application.StartupPath() & "\modified.pdf"

        Dim nDPI As Integer = 72

        If LeftBox.Text = "" Then

            MsgBox("Please input a correct 'Left' value for text which you want to replace")

            Return

        End If

        If TopBox.Text = "" Then

            MsgBox("Please input a correct 'Top' value for text which you want to replace")

            Return

        End If

        If WidthBox.Text = "" Then

            MsgBox("Please input a correct 'Width' value for text which you want to replace")

            Return

        End If

        If HeightBox.Text = "" Then

            MsgBox("Please input a correct 'Height' value for text which you want to replace")

            Return

        End If

 

 

        Dim left As Integer = CInt(LeftBox.Text)

        Dim top As Integer = CInt(TopBox.Text)

        Dim width As Integer = CInt(WidthBox.Text)

        Dim height As Integer = CInt(HeightBox.Text)

 

        Dim oldText As String = OldTextBox.Text

        If oldText = "" Then

            MsgBox("Please input a correct 'Old Text' value for text which you want to replace")

            Return

        End If

        Dim newText As String = NewTextBox.Text

        If newText = "" Then

            MsgBox("Please input a correct 'New Text' value for text which you want to replace")

            Return

        End If

 

        hPDF = VeryPDF_ModifyPDF_OpenFile(strInPDFFile, strOutFile)

 

        'Replace horizontal text contents

        nPage = 1

        x = left * 72.0 / nDPI

        y = top * 72.0 / nDPI

        w = width * 72.0 / nDPI

        h = height * 72.0 / nDPI

        strOldText = oldText

        strNewText = newText

        bRet = VeryPDF_ModifyPDF_ModifyText(hPDF, nPage, x, y, w, h, strOldText, strNewText)

 

        VeryPDF_ModifyPDF_CloseFile(hPDF)

        MsgBox("Replace Text in PDF file completed." & vbCrLf & "VeryPDF_ModifyPDF_ModifyText() return: " & CStr(bRet))

 

    End Sub

End Class

 

 

VN:F [1.9.20_1166]
Rating: 9.5/10 (2 votes cast)
VN:F [1.9.20_1166]
Rating: +3 (from 3 votes)
Replace text and string in PDF file by PDF Parser & Modify SDK from C#, VB.NET and ASP.NET, 9.5 out of 10 based on 2 ratings

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *


Verify Code   If you cannot see the CheckCode image,please refresh the page again!