The key file of VeryPDF PDF Parse & Modify Component for .NET is pdfparsersdk2.dll, which is a Win32 dynamic link library (DLL), and you should call it by APIs in your programing (e.g., C#). The following is examples showing how to use this DLL.
1. Download the package of VeryPDF PDF Parse & Modify Component for .NET, and extract it to any folder in your disk, for example, D:\temp\pdfparsersdk.
2. Run development suite, e.g., Microsoft Visual Studio 2010, and then open project file "D:\temp\pdfparsersdk\C#\CSharp_WindowsFormsApplication1\CSharp_WindowsFormsApplication1.sln".
Open demo project file of VeryPDF PDF Parse & Modify Component for .NET
In the opened file, as shown in Fig. 1, you will see the
CSharp_WindowsFormsApplication1 class and some interface functions of
pdfparsersdk2.dll.
[DllImport("pdfparsersdk2.dll")]
private static extern int VeryPDF_PDFParserSDK(string lpPDFFile, string lpOutFile, string lpOptions);
//Directly analyze PDF with this function
lpPDFFile is the input PDF, e.g., C:\test.pdf
lpOutFile is an output file, e.g., D:\out.png
lpOptions supports following arguments:
-f <int> : specify the first page to process.
-l <int> : specify the last page to process.
-r <int> : set resolution in DPI (default is 150).
-opw <string>: give owner password (for encrypted PDF).
-upw <string>:give user password (for encrypted PDF).
-html : output text information in HTML instead of CSV format.
[DllImport("pdfparsersdk2.dll")]
private static extern int VeryPDF_PDFParserSDKFromMemory(ref byte lpPDFData, int nDataLen, string lpOutFile, string lpOptions);
//Analyze PDF in memory; options and arguments are the same as those of VeryPDF_PDFParserSDK.
[DllImport("pdfparsersdk2.dll")]
private static extern int VeryPDF_PDFParserSDK_GetHandle(string lpPDFFile, string lpOptions);
//Load PDF into memory and return the handle. lpOptions arguments are the same as those of VeryPDF_PDFParserSDK.
[DllImport("pdfparsersdk2.dll")]
private static extern int VeryPDF_PDFParserSDK_GetCount(int hPDFParserData);
/*Get the page count of handle of loaded PDF. Note that the page count is the number of loaded pages but not the total page count of the PDF. For example, if lpOptions uses “-f 2 -l 4”, this function will return 3, the valid page count (2, 3, and 4) of handle. If you need the total page count, please use function VeryPDF_PDFParserSDK_GetAllPagesCount(). */
[DllImport("pdfparsersdk2.dll")]
private static extern int VeryPDF_PDFParserSDK_GetImageLength(int hPDFParserData, int nIndex);
//Get image length of the handle. The application will allocate memory by the image length value.
[DllImport("pdfparsersdk2.dll")]
private static extern int VeryPDF_PDFParserSDK_GetImageData(int hPDFParserData, int nIndex, [MarshalAs(UnmanagedType.LPArray)] byte[] lpData, int nBufLen);
//Get image data in specified page of PDF handle.
[DllImport("pdfparsersdk2.dll")]
private static extern int VeryPDF_PDFParserSDK_GetTextInfoLength(int hPDFParserData, int nIndex);
//Get the text data length of specified page of PDF handle. The application will allocate memory by the length value.
[DllImport("pdfparsersdk2.dll")]
private static extern int VeryPDF_PDFParserSDK_GetTextInfoData(int hPDFParserData, int nIndex, [MarshalAs(UnmanagedType.LPArray)] byte[] lpData, int nBufLen);
//Return text data of specified page in PDF handle.
[DllImport("pdfparsersdk2.dll")]
private static extern int VeryPDF_PDFParserSDK_GetPageCount(string lpPDFFile);
//Get page count of the PDF file.
[DllImport("pdfparsersdk2.dll")]
private static extern int VeryPDF_PDFParserSDK_GetAllPagesCount(int hPDFParserData);
//Get the total page count of the PDF handle.
[DllImport("pdfparsersdk2.dll")]
private static extern int VeryPDF_PDFParserSDK_Free(int hPDFParserData);
//Release the PDF handle.
private void DiskFile_Click(object sender, EventArgs e)
{
string appPath = Path.GetDirectoryName(Application.ExecutablePath);
int nRet;
string strOptions;
string strInPDFFile;
string strOutFile;
string strLogMsg;
strInPDFFile = (appPath + "\\example1.pdf");
strOutFile = (appPath + "\\out.png");
strOptions = "-$ XXXXXXXXXXXXXXXXXXXX";
nRet = VeryPDF_PDFParserSDK(strInPDFFile, strOutFile, strOptions);
strLogMsg = (strInPDFFile + ("\r\n"
+ (strOutFile + ("\r\n"
+ (strOptions + ("\r\n" + ("nRet = " + (nRet.ToString()))))))));
MessageBox.Show(strLogMsg);
}
private void Memory_Click(object sender, EventArgs e)
{
int nRet;
string strOptions;
string strInPDFFile;
string strOutFile;
int nPDFSDKLib;
int nCount;
int nItem;
byte[] vecImgData = null;
byte[] vecTxtData = null;
int nImageDataLen;
int nTextInfoLen;
string strLogMsg;
string appPath = Path.GetDirectoryName(Application.ExecutablePath);
strInPDFFile = (appPath + "\\example1.pdf");
strOptions = "-$ XXXXXXXXXXXXXXXXXXXX";
nPDFSDKLib = VeryPDF_PDFParserSDK_GetHandle(strInPDFFile, strOptions);
nCount = VeryPDF_PDFParserSDK_GetCount(nPDFSDKLib);
for (nItem = 0; (nItem <= (nCount - 1)); nItem++)
{
nImageDataLen = VeryPDF_PDFParserSDK_GetImageLength(nPDFSDKLib, nItem);
nTextInfoLen = VeryPDF_PDFParserSDK_GetTextInfoLength(nPDFSDKLib, nItem);
vecImgData = new byte[nImageDataLen];
vecTxtData = new byte[nTextInfoLen];
VeryPDF_PDFParserSDK_GetImageData(nPDFSDKLib, nItem, vecImgData, nImageDataLen);
VeryPDF_PDFParserSDK_GetTextInfoData(nPDFSDKLib, nItem, vecTxtData, nTextInfoLen);
strLogMsg = System.Text.Encoding.ASCII.GetString(vecTxtData);
MessageBox.Show(strLogMsg.Substring(0, 300));
}
VeryPDF_PDFParserSDK_Free(nPDFSDKLib);
nPDFSDKLib = 0;
}
private void PageByPage_Click(object sender, EventArgs e)
{
int nRet;
string strOptions;
string strInPDFFile;
string strOutFile;
int nPDFSDKLib;
int nCount;
int nItem;
byte[] vecImgData = null;
byte[] vecTxtData = null;
int nImageDataLen;
int nTextInfoLen;
string strLogMsg;
int nPageCount;
int page;
string appPath = Path.GetDirectoryName(Application.ExecutablePath);
strInPDFFile = (appPath + "\\example2.pdf");
nPageCount = VeryPDF_PDFParserSDK_GetPageCount(strInPDFFile);
for (page = 1; (page <= nPageCount); page++)
{
strOptions = ("-$ XXXXXXXXXXXXXXXXXXXX -f "
+ ((page.ToString()) + (" -l " + (page.ToString()))));
nPDFSDKLib = VeryPDF_PDFParserSDK_GetHandle(strInPDFFile, strOptions);
nCount = VeryPDF_PDFParserSDK_GetCount(nPDFSDKLib);
for (nItem = 0; (nItem <= (nCount - 1)); nItem++)
{
nImageDataLen = VeryPDF_PDFParserSDK_GetImageLength(nPDFSDKLib, nItem);
nTextInfoLen = VeryPDF_PDFParserSDK_GetTextInfoLength(nPDFSDKLib, nItem);
vecImgData = new byte[nImageDataLen];
vecTxtData = new byte[nTextInfoLen];
VeryPDF_PDFParserSDK_GetImageData(nPDFSDKLib, nItem, vecImgData, nImageDataLen);
VeryPDF_PDFParserSDK_GetTextInfoData(nPDFSDKLib, nItem, vecTxtData, nTextInfoLen);
strLogMsg = System.Text.Encoding.ASCII.GetString(vecTxtData);
if ((nTextInfoLen == 0))
{
strLogMsg = "This page hasn\'t text contents.";
}
if ((strLogMsg.Length > 300))
{
strLogMsg = strLogMsg.Substring(0, 300);
}
MessageBox.Show(strLogMsg, ("Text contents of Page " + (page.ToString())));
}
VeryPDF_PDFParserSDK_Free(nPDFSDKLib);
nPDFSDKLib = 0;
}
}
"CSharp_WindowsFormsApplication1.exe". Run the executable file, and then you will see its interface as displayed in Fig. 2.
Fig. 2 Interface of compiled C# demonstration project
out_pg_0001.htm
out_pg_0001.png
666,78,84,14,MORGAN;
761,78,90,14,STANLEY;
862,78,57,14,FIXED;
930,78,76,14,INCOME;
1017,78,107,14,RESEARCH;
private void DiskFile_Click(object sender, EventArgs e)
{
string appPath = Path.GetDirectoryName(Application.ExecutablePath);
int nRet;
string strOptions;
string strInPDFFile;
string strOutFile;
string strLogMsg;
strInPDFFile = (appPath + "\\example1.pdf");
strOutFile = (appPath + "\\out.png");
strOptions = "-$ XXXXXXXXXXXXXXXXXXXX -html";
nRet = VeryPDF_PDFParserSDK(strInPDFFile, strOutFile, strOptions);
strLogMsg = (strInPDFFile + ("\r\n"
+ (strOutFile + ("\r\n"
+ (strOptions + ("\r\n" + ("nRet = " + (nRet.ToString()))))))));
MessageBox.Show(strLogMsg);
}
<div style="position: absolute;top:2;left:-1"><img width="1275" height="1650" src="out_pg_0001.png"></img></div>
<div style="position:absolute;left:666;top:78;width:84;height:14"><span style="font-style:normal;font-weight:700;font-size:13px;font-family:Arial;color:#000000;">MORGAN</span></div>
<div style="position:absolute;left:761;top:78;width:90;height:14"><span style="font-style:normal;font-weight:700;font-size:13px;font-family:Arial;color:#000000;">STANLEY</span></div>
<div style="position:absolute;left:862;top:78;width:57;height:14"><span style="font-style:normal;font-weight:700;font-size:13px;font-family:Arial;color:#000000;">FIXED</span></div>
<div style="position:absolute;left:930;top:78;width:76;height:14"><span style="font-style:normal;font-weight:700;font-size:13px;font-family:Arial;color:#000000;">INCOME</span></div>
<div style="position:absolute;left:1017;top:78;width:107;height:14"><span style="font-style:normal;font-weight:700;font-size:13px;font-family:Arial;color:#000000;">RESEARCH</span></div>
<div style="position:absolute;left:666;top:118;width:76;height:14"><span style="font-style:normal;font-weight:700;font-size:13px;font-family:Arial;color:#000000;">September</span></div>
<div style="position:absolute;left:746;top:118;width:20;height:14"><span style="font-style:normal;font-weight:700;font-size:13px;font-family:Arial;color:#000000;">20,</span></div>
Fig. 3 View the created HTML file in a web browser
Fig. 4 Modify a PDF with compiled demo application
Fig. 5 Replace text in PDF with demo application
Fig. 6 Result of text replacement
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)
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
If you have any questions in using VeryPDF PDF Parse & Modify Component for .NET, please contact us (http://support.verypdf.com/).