InteractiveFormsTest.vb 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. Imports ComPDFKit.Import
  2. Imports ComPDFKit.PDFAnnotation
  3. Imports ComPDFKit.PDFAnnotation.Form
  4. Imports ComPDFKit.PDFDocument
  5. Imports ComPDFKit.PDFDocument.Action
  6. Imports ComPDFKit.PDFPage
  7. Imports ComPDFKit.PDFPage.Edit
  8. Imports System
  9. Imports System.Collections.Generic
  10. Imports System.IO
  11. Imports System.Text
  12. Module InteractiveFormsTest
  13. Private outputPath As String = Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory()))) & "\Output\VB"
  14. Sub Main()
  15. Console.WriteLine("Running interactive forms test sample…" & vbCrLf)
  16. SDKLicenseHelper.LicenseVerify()
  17. If Not Directory.Exists(outputPath) Then
  18. Directory.CreateDirectory(outputPath)
  19. End If
  20. Dim document As CPDFDocument = CPDFDocument.InitWithFilePath("CommonFivePage.pdf")
  21. If CreateForms(document) Then
  22. Console.WriteLine("Create forms done.")
  23. Else
  24. Console.WriteLine("Create forms failed.")
  25. End If
  26. Console.WriteLine("--------------------")
  27. Console.WriteLine("Done")
  28. Console.WriteLine("--------------------")
  29. Console.ReadLine()
  30. End Sub
  31. ' Create text field.
  32. ' Text field: used to input text
  33. Private Sub CreateTextField(document As CPDFDocument)
  34. Dim page As CPDFPage = document.PageAtIndex(0)
  35. Dim textField As CPDFTextWidget = TryCast(page.CreateWidget(C_WIDGET_TYPE.WIDGET_TEXTFIELD), CPDFTextWidget)
  36. textField.SetRect(New CRect(28, 32, 235, 75))
  37. textField.SetWidgetBorderRGBColor(New Byte() {0, 0, 0})
  38. textField.SetWidgetBgRGBColor(New Byte() {240, 255, 240})
  39. End Sub
  40. ' Create push button.
  41. ' Push button: Click to perform some actions, such as jumping to a page or website.
  42. Private Sub CreatePushButton(document As CPDFDocument)
  43. Dim page As CPDFPage = document.PageAtIndex(0)
  44. Dim pushButton1 As CPDFPushButtonWidget = TryCast(page.CreateWidget(C_WIDGET_TYPE.WIDGET_PUSHBUTTON), CPDFPushButtonWidget)
  45. pushButton1.SetRect(New CRect(28, 150, 150, 100))
  46. pushButton1.SetWidgetBorderRGBColor(New Byte() {0, 0, 0})
  47. pushButton1.SetWidgetBgRGBColor(New Byte() {180, 180, 220})
  48. pushButton1.SetButtonTitle("Go To Page 2")
  49. Dim attribute As New CTextAttribute()
  50. attribute.FontColor = New Byte() {0, 0, 0}
  51. attribute.FontSize = 14
  52. attribute.FontName = "Helvetica"
  53. pushButton1.SetTextAttribute(attribute)
  54. Dim gotoAction As New CPDFGoToAction()
  55. Dim dest As New CPDFDestination()
  56. dest.PageIndex = 1
  57. gotoAction.SetDestination(document, dest)
  58. pushButton1.SetButtonAction(gotoAction)
  59. pushButton1.UpdateFormAp()
  60. Dim pushButton2 As CPDFPushButtonWidget = TryCast(page.CreateWidget(C_WIDGET_TYPE.WIDGET_PUSHBUTTON), CPDFPushButtonWidget)
  61. pushButton2.SetRect(New CRect(168, 150, 290, 100))
  62. pushButton2.SetWidgetBorderRGBColor(New Byte() {0, 0, 0})
  63. pushButton2.SetWidgetBgRGBColor(New Byte() {180, 180, 220})
  64. pushButton2.SetButtonTitle("Go To ComPDFKit")
  65. Dim attribute2 As New CTextAttribute()
  66. attribute2.FontColor = New Byte() {0, 0, 0}
  67. attribute2.FontSize = 14
  68. attribute2.FontName = "Helvetica"
  69. pushButton2.SetTextAttribute(attribute)
  70. Dim uriAction As New CPDFUriAction()
  71. uriAction.SetUri("https://www.compdf.com/")
  72. pushButton2.SetButtonAction(uriAction)
  73. pushButton2.UpdateFormAp()
  74. End Sub
  75. ' Create ListBox
  76. Private Sub CreateListBox(document As CPDFDocument)
  77. Dim page As CPDFPage = document.PageAtIndex(0)
  78. Dim listbox As CPDFListBoxWidget = TryCast(page.CreateWidget(C_WIDGET_TYPE.WIDGET_LISTBOX), CPDFListBoxWidget)
  79. listbox.SetRect(New CRect(28, 330, 150, 230))
  80. listbox.AddOptionItem(0, "1", "ComPDFKit1")
  81. listbox.AddOptionItem(1, "2", "ComPDFKit2")
  82. listbox.SetWidgetBorderRGBColor(New Byte() {0, 0, 0})
  83. listbox.SetWidgetBgRGBColor(New Byte() {200, 180, 180})
  84. End Sub
  85. ' Create SignatureField
  86. ' Provide an area for electronic signatures.
  87. Private Sub CreateSignatureField(document As CPDFDocument)
  88. Dim page As CPDFPage = document.PageAtIndex(0)
  89. Dim signatureField As CPDFSignatureWidget = TryCast(page.CreateWidget(C_WIDGET_TYPE.WIDGET_SIGNATUREFIELDS), CPDFSignatureWidget)
  90. signatureField.SetRect(New CRect(28, 420, 150, 370))
  91. signatureField.SetWidgetBorderRGBColor(New Byte() {0, 0, 0})
  92. signatureField.SetWidgetBgRGBColor(New Byte() {150, 180, 210})
  93. End Sub
  94. ' Create CheckBox
  95. Private Sub CreateCheckBox(document As CPDFDocument)
  96. Dim page As CPDFPage = document.PageAtIndex(0)
  97. Dim checkBox1 As CPDFCheckBoxWidget = TryCast(page.CreateWidget(C_WIDGET_TYPE.WIDGET_CHECKBOX), CPDFCheckBoxWidget)
  98. checkBox1.SetRect(New CRect(28, 470, 48, 450))
  99. checkBox1.SetWidgetBorderRGBColor(New Byte() {0, 0, 0})
  100. checkBox1.SetWidgetBgRGBColor(New Byte() {150, 180, 210})
  101. Dim checkBox2 As CPDFCheckBoxWidget = TryCast(page.CreateWidget(C_WIDGET_TYPE.WIDGET_CHECKBOX), CPDFCheckBoxWidget)
  102. checkBox2.SetRect(New CRect(58, 470, 78, 450))
  103. checkBox2.SetWidgetBorderRGBColor(New Byte() {0, 0, 0})
  104. checkBox2.SetWidgetBgRGBColor(New Byte() {150, 180, 210})
  105. Dim checkBox3 As CPDFCheckBoxWidget = TryCast(page.CreateWidget(C_WIDGET_TYPE.WIDGET_CHECKBOX), CPDFCheckBoxWidget)
  106. checkBox3.SetRect(New CRect(88, 470, 108, 450))
  107. checkBox3.SetWidgetBorderRGBColor(New Byte() {0, 0, 0})
  108. checkBox3.SetWidgetBgRGBColor(New Byte() {150, 180, 210})
  109. End Sub
  110. ' Create RadioButton
  111. Private Sub CreateRadioButton(document As CPDFDocument)
  112. Dim page As CPDFPage = document.PageAtIndex(0)
  113. Dim radioButton1 As CPDFRadioButtonWidget = TryCast(page.CreateWidget(C_WIDGET_TYPE.WIDGET_RADIOBUTTON), CPDFRadioButtonWidget)
  114. radioButton1.SetRect(New CRect(28, 500, 48, 480))
  115. radioButton1.SetWidgetBorderRGBColor(New Byte() {0, 0, 0})
  116. radioButton1.SetWidgetBgRGBColor(New Byte() {210, 180, 150})
  117. radioButton1.SetWidgetCheckStyle(C_CHECK_STYLE.CK_CIRCLE)
  118. Dim radioButton2 As CPDFRadioButtonWidget = TryCast(page.CreateWidget(C_WIDGET_TYPE.WIDGET_RADIOBUTTON), CPDFRadioButtonWidget)
  119. radioButton2.SetRect(New CRect(58, 500, 78, 480))
  120. radioButton2.SetWidgetBorderRGBColor(New Byte() {0, 0, 0})
  121. radioButton2.SetWidgetBgRGBColor(New Byte() {210, 180, 150})
  122. radioButton2.SetWidgetCheckStyle(C_CHECK_STYLE.CK_CIRCLE)
  123. Dim radioButton3 As CPDFRadioButtonWidget = TryCast(page.CreateWidget(C_WIDGET_TYPE.WIDGET_RADIOBUTTON), CPDFRadioButtonWidget)
  124. radioButton3.SetRect(New CRect(88, 500, 108, 480))
  125. radioButton3.SetWidgetBorderRGBColor(New Byte() {0, 0, 0})
  126. radioButton3.SetWidgetBgRGBColor(New Byte() {210, 180, 150})
  127. radioButton3.SetWidgetCheckStyle(C_CHECK_STYLE.CK_CIRCLE)
  128. End Sub
  129. ' Generate various signatures.
  130. Private Function CreateForms(document As CPDFDocument) As Boolean
  131. CreateTextField(document)
  132. CreatePushButton(document)
  133. CreateListBox(document)
  134. CreateSignatureField(document)
  135. CreateCheckBox(document)
  136. CreateRadioButton(document)
  137. ' Save to the specified path so you can observe the effect.
  138. Dim path As String = outputPath & "\CreateFormsTest.pdf"
  139. If Not document.WriteToFilePath(path) Then
  140. Return False
  141. End If
  142. Console.WriteLine("Browse the changed file in " & path)
  143. Return True
  144. End Function
  145. End Module