InteractiveFormsTest.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1.  using ComPDFKit.Import;
  2. using ComPDFKit.PDFAnnotation;
  3. using ComPDFKit.PDFAnnotation.Form;
  4. using ComPDFKit.PDFDocument;
  5. using ComPDFKit.PDFDocument.Action;
  6. using ComPDFKit.PDFPage;
  7. using ComPDFKit.PDFPage.Edit;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.IO;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. namespace InteractiveFormsTest
  15. {
  16. internal class InteractiveFormsTest
  17. {
  18. private static string outputPath =Path.Combine(Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory()))) ?? string.Empty, "Output", "CS");
  19. static void Main(string[] args)
  20. {
  21. Console.WriteLine("Running interactive forms test sample…\r\n");
  22. SDKLicenseHelper.LicenseVerify();
  23. if (!Directory.Exists(outputPath))
  24. {
  25. Directory.CreateDirectory(outputPath);
  26. }
  27. CPDFDocument document = CPDFDocument.InitWithFilePath("CommonFivePage.pdf");
  28. if (CreateForms(document))
  29. {
  30. Console.WriteLine("Create forms done.");
  31. }
  32. else
  33. {
  34. Console.WriteLine("Create forms failed.");
  35. }
  36. Console.WriteLine("--------------------");
  37. Console.WriteLine("Done");
  38. Console.WriteLine("--------------------");
  39. Console.ReadLine();
  40. }
  41. /// <summary>
  42. /// Create text field.
  43. /// Text field: used to input text
  44. /// </summary>
  45. static private void CreateTextField(CPDFDocument document)
  46. {
  47. CPDFPage page = document.PageAtIndex(0);
  48. CPDFTextWidget textField = page.CreateWidget(C_WIDGET_TYPE.WIDGET_TEXTFIELD) as CPDFTextWidget;
  49. textField.SetRect(new CRect( 28, 75, 235, 32));
  50. textField.SetWidgetBorderRGBColor(new byte[] { 0, 0, 0 });
  51. textField.SetWidgetBgRGBColor(new byte[] {240,255,240});
  52. }
  53. /// <summary>
  54. /// Create push button.
  55. /// Push button: Click to perform some actions, such as jumping to a page or website.
  56. /// </summary>
  57. static private void CreatePushButton(CPDFDocument document)
  58. {
  59. CPDFPage page = document.PageAtIndex(0);
  60. CPDFPushButtonWidget pushButton1 = page.CreateWidget(C_WIDGET_TYPE.WIDGET_PUSHBUTTON) as CPDFPushButtonWidget;
  61. pushButton1.SetRect(new CRect(28, 150, 150, 100));
  62. pushButton1.SetWidgetBorderRGBColor(new byte[] { 0, 0, 0 });
  63. pushButton1.SetWidgetBgRGBColor(new byte[] { 180, 180, 220 });
  64. pushButton1.SetButtonTitle("Go To Page 2");
  65. CTextAttribute attribute = new CTextAttribute();
  66. attribute.FontColor = new byte[] { 0, 0, 0 };
  67. attribute.FontSize = 14;
  68. attribute.FontName = "Helvetica";
  69. pushButton1.SetTextAttribute(attribute);
  70. CPDFGoToAction gotoAction = new CPDFGoToAction();
  71. CPDFDestination dest = new CPDFDestination();
  72. dest.PageIndex = 1;
  73. gotoAction.SetDestination(document, dest);
  74. pushButton1.SetButtonAction(gotoAction);
  75. pushButton1.UpdateFormAp();
  76. CPDFPushButtonWidget pushButton2 = page.CreateWidget(C_WIDGET_TYPE.WIDGET_PUSHBUTTON) as CPDFPushButtonWidget;
  77. pushButton2.SetRect(new CRect(168, 150, 290, 100));
  78. pushButton2.SetWidgetBorderRGBColor(new byte[] { 0, 0, 0 });
  79. pushButton2.SetWidgetBgRGBColor(new byte[] { 180, 180, 220 });
  80. pushButton2.SetButtonTitle("Go To ComPDFKit");
  81. CTextAttribute attribute2 = new CTextAttribute();
  82. attribute2.FontColor = new byte[] { 0, 0, 0 };
  83. attribute2.FontSize = 14;
  84. attribute2.FontName = "Helvetica";
  85. pushButton2.SetTextAttribute(attribute);
  86. CPDFUriAction uriAction = new CPDFUriAction();
  87. uriAction.SetUri("https://www.compdf.com/");
  88. pushButton2.SetButtonAction(uriAction);
  89. pushButton2.UpdateFormAp();
  90. }
  91. /// <summary>
  92. /// Create ListBox
  93. /// </summary>
  94. static private void CreateListBox(CPDFDocument document)
  95. {
  96. CPDFPage page = document.PageAtIndex(0);
  97. CPDFListBoxWidget listbox = page.CreateWidget(C_WIDGET_TYPE.WIDGET_LISTBOX) as CPDFListBoxWidget;
  98. listbox.SetRect(new CRect(28, 330, 150, 230));
  99. listbox.AddOptionItem(0, "1", "ComPDFKit1");
  100. listbox.AddOptionItem(1, "2", "ComPDFKit2");
  101. listbox.SetWidgetBorderRGBColor(new byte[] { 0, 0, 0 });
  102. listbox.SetWidgetBgRGBColor(new byte[] { 200, 180, 180 });
  103. }
  104. /// <summary>
  105. /// Create SignatureField
  106. /// Provide an area for electronic signatures.
  107. /// </summary>
  108. static private void CreateSignatureField(CPDFDocument document)
  109. {
  110. CPDFPage page = document.PageAtIndex(0);
  111. CPDFSignatureWidget signatureField = page.CreateWidget(C_WIDGET_TYPE.WIDGET_SIGNATUREFIELDS) as CPDFSignatureWidget;
  112. signatureField.SetRect(new CRect(28, 420, 150, 370));
  113. signatureField.SetWidgetBorderRGBColor(new byte[] { 0, 0, 0 });
  114. signatureField.SetWidgetBgRGBColor(new byte[] { 150, 180, 210 });
  115. }
  116. /// <summary>
  117. /// Create CheckBox
  118. /// </summary>
  119. static private void CreateCheckBox(CPDFDocument document)
  120. {
  121. CPDFPage page = document.PageAtIndex(0);
  122. CPDFCheckBoxWidget checkBox1 = page.CreateWidget(C_WIDGET_TYPE.WIDGET_CHECKBOX) as CPDFCheckBoxWidget;
  123. checkBox1.SetRect(new CRect(28, 470, 48, 450));
  124. checkBox1.SetWidgetBorderRGBColor(new byte[] { 0, 0, 0 });
  125. checkBox1.SetWidgetBgRGBColor(new byte[] { 150, 180, 210 });
  126. CPDFCheckBoxWidget checkBox2 = page.CreateWidget(C_WIDGET_TYPE.WIDGET_CHECKBOX) as CPDFCheckBoxWidget;
  127. checkBox2.SetRect(new CRect(58, 470, 78, 450));
  128. checkBox2.SetWidgetBorderRGBColor(new byte[] { 0, 0, 0 });
  129. checkBox2.SetWidgetBgRGBColor(new byte[] { 150, 180, 210 });
  130. CPDFCheckBoxWidget checkBox3 = page.CreateWidget(C_WIDGET_TYPE.WIDGET_CHECKBOX) as CPDFCheckBoxWidget;
  131. checkBox3.SetRect(new CRect(88, 470, 108, 450));
  132. checkBox3.SetWidgetBorderRGBColor(new byte[] { 0, 0, 0 });
  133. checkBox3.SetWidgetBgRGBColor(new byte[] { 150, 180, 210 });
  134. }
  135. /// <summary>
  136. /// Create RadioButton
  137. /// </summary>
  138. static private void CreateRadioButton(CPDFDocument document)
  139. {
  140. CPDFPage page = document.PageAtIndex(0);
  141. CPDFRadioButtonWidget radioButton1 = page.CreateWidget(C_WIDGET_TYPE.WIDGET_RADIOBUTTON) as CPDFRadioButtonWidget;
  142. radioButton1.SetRect(new CRect(28, 500, 48, 480));
  143. radioButton1.SetWidgetBorderRGBColor(new byte[] { 0, 0, 0 });
  144. radioButton1.SetWidgetBgRGBColor(new byte[] { 210, 180, 150 });
  145. radioButton1.SetWidgetCheckStyle(C_CHECK_STYLE.CK_CIRCLE);
  146. CPDFRadioButtonWidget radioButton2 = page.CreateWidget(C_WIDGET_TYPE.WIDGET_RADIOBUTTON) as CPDFRadioButtonWidget;
  147. radioButton2.SetRect(new CRect(58, 500, 78, 480));
  148. radioButton2.SetWidgetBorderRGBColor(new byte[] { 0, 0, 0 });
  149. radioButton2.SetWidgetBgRGBColor(new byte[] { 210, 180, 150 });
  150. radioButton2.SetWidgetCheckStyle(C_CHECK_STYLE.CK_CIRCLE);
  151. CPDFRadioButtonWidget radioButton3 = page.CreateWidget(C_WIDGET_TYPE.WIDGET_RADIOBUTTON) as CPDFRadioButtonWidget;
  152. radioButton3.SetRect(new CRect(88, 500, 108, 480));
  153. radioButton3.SetWidgetBorderRGBColor(new byte[] { 0, 0, 0 });
  154. radioButton3.SetWidgetBgRGBColor(new byte[] { 210, 180, 150 });
  155. radioButton3.SetWidgetCheckStyle(C_CHECK_STYLE.CK_CIRCLE);
  156. }
  157. /// <summary>
  158. /// Generate various signatures.
  159. /// </summary>
  160. static private bool CreateForms(CPDFDocument document)
  161. {
  162. CreateTextField(document);
  163. CreatePushButton(document);
  164. CreateListBox(document);
  165. CreateSignatureField(document);
  166. CreateCheckBox(document);
  167. CreateRadioButton(document);
  168. // Save to pointed path so you can observe the effect.
  169. string path = Path.Combine(outputPath, "CreateFormsTest.pdf");
  170. if (!document.WriteToFilePath(path))
  171. {
  172. return false;
  173. }
  174. Console.WriteLine("Browse the changed file in " + path);
  175. return true;
  176. }
  177. }
  178. }