HeaderFooterTest.vb 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. Imports ComPDFKit.PDFDocument
  2. Imports System
  3. Imports System.IO
  4. Imports System.Collections.Generic
  5. Module HeaderFooterTest
  6. Private outputPath As String = Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory()))) & "\Output\VB"
  7. Private IntToLocationDic As New Dictionary(Of Integer, String) From {
  8. {0, "Top Left"},
  9. {1, "Top Middle"},
  10. {2, "Top Right"},
  11. {3, "Bottom Left"},
  12. {4, "Bottom Middle"},
  13. {5, "Bottom Right"}
  14. }
  15. Sub Main(args As String())
  16. Console.WriteLine("Running header and footer test sample..." & vbCrLf)
  17. SDKLicenseHelper.LicenseVerify()
  18. Dim document As CPDFDocument = CPDFDocument.InitWithFilePath("CommonFivePage.pdf")
  19. If Not Directory.Exists(outputPath) Then
  20. Directory.CreateDirectory(outputPath)
  21. End If
  22. ' Add common header and footer
  23. If AddCommonHeaderFooter(document) Then
  24. Console.WriteLine("Add common header and footer done.")
  25. Else
  26. Console.WriteLine("Add common header and footer failed.")
  27. End If
  28. Console.WriteLine("--------------------")
  29. ' Add page header and footer
  30. If AddPageHeaderFooter(document) Then
  31. Console.WriteLine("Add page header and footer done.")
  32. Else
  33. Console.WriteLine("Add page header and footer failed.")
  34. End If
  35. Console.WriteLine("--------------------")
  36. ' Edit header and footer
  37. If EditHeaderFooter(document) Then
  38. Console.WriteLine("Edit header and footer done.")
  39. Else
  40. Console.WriteLine("Edit header and footer failed.")
  41. End If
  42. Console.WriteLine("--------------------")
  43. ' Delete header and footer
  44. If ClearHeaderFooter(document) Then
  45. Console.WriteLine("Delete header and footer done." & vbCrLf)
  46. Else
  47. Console.WriteLine("Delete header and footer failed" & vbCrLf)
  48. End If
  49. Console.WriteLine("--------------------")
  50. Console.WriteLine("Done")
  51. Console.WriteLine("--------------------")
  52. Console.ReadLine()
  53. End Sub
  54. ' Add common header and footer
  55. Private Function AddCommonHeaderFooter(document As CPDFDocument) As Boolean
  56. Dim headerFooter As CPDFHeaderFooter = document.GetHeaderFooter()
  57. Dim color As Byte() = {255, 0, 0}
  58. headerFooter.SetPages("0-" & (document.PageCount - 1))
  59. For i As Integer = 0 To 2
  60. headerFooter.SetText(i, "ComPDFKit")
  61. headerFooter.SetTextColor(i, color)
  62. headerFooter.SetFontSize(i, 14)
  63. Console.WriteLine("Text: {0}", headerFooter.GetText(i))
  64. Console.WriteLine("Location: {0}" & vbCrLf, IntToLocationDic(i))
  65. Next
  66. headerFooter.Update()
  67. Dim addHeaderFooterPath As String = outputPath & "\AddCommonHeaderFooterTest.pdf"
  68. If Not document.WriteToFilePath(addHeaderFooterPath) Then
  69. Return False
  70. End If
  71. Console.WriteLine("Browse the changed file in " & addHeaderFooterPath)
  72. Return True
  73. End Function
  74. ' Add page header and footer
  75. Private Function AddPageHeaderFooter(document As CPDFDocument) As Boolean
  76. Dim headerFooter As CPDFHeaderFooter = document.GetHeaderFooter()
  77. Dim color As Byte() = {255, 0, 0}
  78. headerFooter.SetPages("0-" & (document.PageCount - 1))
  79. For i As Integer = 3 To 5
  80. headerFooter.SetText(i, "<<1,2>>")
  81. headerFooter.SetTextColor(i, color)
  82. headerFooter.SetFontSize(i, 14)
  83. Console.WriteLine("Text: {0}", headerFooter.GetText(i))
  84. Console.WriteLine("Location: {0}" & vbCrLf, IntToLocationDic(i))
  85. Next
  86. headerFooter.Update()
  87. Dim addHeaderFooterPath As String = outputPath & "\AddPageHeaderFooterTest.pdf"
  88. If document.WriteToFilePath(addHeaderFooterPath) Then
  89. Console.WriteLine("Browse the changed file in " & addHeaderFooterPath)
  90. Return True
  91. Else
  92. Return False
  93. End If
  94. End Function
  95. ' Edit header and footer
  96. Private Function EditHeaderFooter(document As CPDFDocument) As Boolean
  97. Dim headerFooter As CPDFHeaderFooter = document.GetHeaderFooter()
  98. If headerFooter.GetText(0) <> String.Empty Then
  99. Console.WriteLine("Get old head and footer 0 succeeded, text is {0}", headerFooter.GetText(0))
  100. Else
  101. Console.WriteLine("Get head and footer 0 failed, or it does not exist")
  102. Return False
  103. End If
  104. headerFooter.SetText(0, "ComPDFKit Samples")
  105. headerFooter.Update()
  106. Console.WriteLine("Change head and footer 0 succeeded, new text is {0}", headerFooter.GetText(0))
  107. Dim editHeaderFooterPath As String = outputPath & "\EditHeaderFooterTest.pdf"
  108. If document.WriteToFilePath(editHeaderFooterPath) Then
  109. Console.WriteLine("Browse the changed file in " & editHeaderFooterPath)
  110. Return True
  111. Else
  112. Return False
  113. End If
  114. End Function
  115. ' Delete header and footer
  116. Private Function ClearHeaderFooter(document As CPDFDocument) As Boolean
  117. Dim headerFooter As CPDFHeaderFooter = document.GetHeaderFooter()
  118. headerFooter.Clear()
  119. Dim clearHeaderFooterPath As String = outputPath & "\ClearHeaderFooterTest.pdf"
  120. If document.WriteToFilePath(clearHeaderFooterPath) Then
  121. Console.WriteLine("Browse the changed file in " & clearHeaderFooterPath)
  122. Return True
  123. Else
  124. Return False
  125. End If
  126. End Function
  127. End Module