123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- Imports System.IO
- Imports ComPDFKit.PDFDocument
- Module BatesTest
- Private parentPath = Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory())))
- Private outputPath As String = Path.Combine(parentPath, "Output", "VB")
- Private IntToLocationDic As New Dictionary(Of Integer, String)() From
- {
- {0, "Top Left"},
- {1, "Top Middle"},
- {2, "Top Right"},
- {3, "Bottom Left"},
- {4, "Bottom Middle"},
- {5, "Bottom Right"}
- }
- Sub Main()
- #Region "Preparation work"
- Console.WriteLine("Running bates test sample..." & vbCrLf)
- SDKLicenseHelper.LicenseVerify()
- If Not Directory.Exists(outputPath) Then
- Directory.CreateDirectory(outputPath)
- End If
- #End Region
- #Region "Sample 1: Add bates"
- Dim document As CPDFDocument = CPDFDocument.InitWithFilePath("CommonFivePage.pdf")
- If AddBates(document) Then
- Console.WriteLine("Add bates done.")
- End If
- document.Release()
- Console.WriteLine("--------------------")
- #End Region
- #Region "Samples 2: Edit bates"
- document = CPDFDocument.InitWithFilePath("Bates.pdf")
- If EditBates(document) Then
- Console.WriteLine("Edit bates done.")
- End If
- document.Release()
- Console.WriteLine("--------------------")
- #End Region
- #Region "Sample 3: Clear bates"
- document = CPDFDocument.InitWithFilePath("CommonFivePage.pdf")
- If ClearBates(document) Then
- Console.WriteLine("Clear bates done.")
- End If
- document.Release()
- Console.WriteLine("--------------------")
- #End Region
- Console.WriteLine("Done!")
- Console.WriteLine("--------------------")
- Console.ReadLine()
- End Sub
- Private Function AddBates(document As CPDFDocument) As Boolean
- Dim addBatesPath As String = outputPath & "\AddBates.pdf"
- Dim bates As CPDFBates = document.GetBates()
- Dim color As Byte() = {255, 0, 0}
- bates.SetPages("0-" & (document.PageCount - 1)) ' Page numbering from 0
- For i As Integer = 0 To 5
- bates.SetText(i, "<<#3#5#Prefix-#-Suffix>>") ' 3 digits, starting from 5
- bates.SetTextColor(i, color)
- bates.SetFontSize(i, 14)
- Console.WriteLine("Text: {0}", bates.GetText(i))
- Console.WriteLine("Location: {0}" & vbCrLf, IntToLocationDic(i))
- Next
- bates.Update()
- If Not document.WriteToFilePath(addBatesPath) Then
- Return False
- End If
- Console.WriteLine("Browse the changed file in " & addBatesPath)
- Return True
- End Function
- Private Function EditBates(document As CPDFDocument) As Boolean
- Dim bates As CPDFBates = document.GetBates()
- ' Get the old Bates text from index 0
- If Not String.IsNullOrEmpty(bates.GetText(0)) Then
- Console.WriteLine("Get old Bates 0 done, text is {0}", bates.GetText(0))
- Else
- Console.WriteLine("Get Bates 0 failed, or it does not exist")
- Return False
- End If
- ' Edit the Bates text at index 0
- bates.SetText(0, "<<#3#1#ComPDFKit-#-ComPDFKit>>")
- ' Update the Bates text
- bates.Update()
- Console.WriteLine("Change Bates 0 done, new text is {0}", bates.GetText(0))
- Dim editBatesPath As String = outputPath & "\EditBatesTest.pdf"
- If document.WriteToFilePath(editBatesPath) Then
- Console.WriteLine("Browse the changed file in " & editBatesPath)
- Return True
- Else
- Return False
- End If
- End Function
- ''' <summary>
- ''' Clear bates.
- ''' </summary>
- ''' <param name="document">Document with bates</param>
- Private Function ClearBates(document As CPDFDocument) As Boolean
- Dim bates As CPDFBates = document.GetBates()
- bates.Clear()
- Dim clearBatesPath As String = outputPath & "\ClearBatesTest.pdf"
- If document.WriteToFilePath(clearBatesPath) Then
- Console.WriteLine("Browse the changed file in " & clearBatesPath)
- Return True
- Else
- Return False
- End If
- End Function
- End Module
|