InteractiveFormsTest.vb 7.4 KB

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