SetFieldFormat.cs
  1. //
  2. // This code is part of Document Solutions for PDF demos.
  3. // Copyright (c) MESCIUS inc. All rights reserved.
  4. //
  5. using System;
  6. using System.IO;
  7. using System.Drawing;
  8. using GrapeCity.Documents.Pdf;
  9. using GrapeCity.Documents.Text;
  10. using GrapeCity.Documents.Pdf.AcroForms;
  11. using GrapeCity.Documents.Pdf.Annotations;
  12. using GrapeCity.Documents.Pdf.Actions;
  13.  
  14. namespace DsPdfWeb.Demos
  15. {
  16. // This example shows how to use the helper methods SetPercentValue, SetNumberValue,
  17. // SetDateValue, SetTimeValue, SetSpecialFormatValue on TextField, CombTextField and ComboBoxField
  18. // to set the fields' format/formatted values.
  19. // Note that setting any property affecting the appearance of a field causes any formatting
  20. // set using these methods to be lost, so these methods should be called after any adjustments
  21. // to the field's appearance are done.
  22. public class SetFieldFormat
  23. {
  24. public int CreatePDF(Stream stream)
  25. {
  26. var doc = new GcPdfDocument();
  27. var p = doc.NewPage();
  28. var g = p.Graphics;
  29.  
  30. g.DrawString("SetPercentFormat", StandardFonts.Helvetica, 14, Color.Black, new PointF(10, 10));
  31. AddPercentFormat(p, new RectangleF(10, 40, 60, 20), 0.2f, 2, TextField.NumberSeparatorStyle.Comma);
  32. AddPercentFormat(p, new RectangleF(80, 40, 60, 20), 1, 0, TextField.NumberSeparatorStyle.CommaDot);
  33. AddPercentFormat(p, new RectangleF(150, 40, 60, 20), -0.1f, 3, TextField.NumberSeparatorStyle.DotComma);
  34. AddPercentFormat(p, new RectangleF(220, 40, 60, 20), 2f, 2, TextField.NumberSeparatorStyle.Dot);
  35. AddPercentFormat(p, new RectangleF(290, 40, 60, 20), 0.5f, 2, TextField.NumberSeparatorStyle.ApostropheDot);
  36.  
  37. g.DrawString("SetNumberFormat", StandardFonts.Helvetica, 14, Color.Black, new PointF(10, 70));
  38. AddNumberFormat(p, new RectangleF(10, 100, 60, 20), 12345.67f, 2, TextField.NumberSeparatorStyle.Comma,
  39. TextField.NumberNegativeStyle.None, null, TextField.CurrencySymbolStyle.BeforeWithSpace);
  40. AddNumberFormat(p, new RectangleF(80, 100, 60, 20), -12.7f, 0, TextField.NumberSeparatorStyle.CommaDot,
  41. TextField.NumberNegativeStyle.UseRedText, "R", TextField.CurrencySymbolStyle.BeforeWithSpace);
  42. AddNumberFormat(p, new RectangleF(150, 100, 60, 20), 0.123f, 3, TextField.NumberSeparatorStyle.DotComma,
  43. TextField.NumberNegativeStyle.ShowParentheses, "\u20ac", TextField.CurrencySymbolStyle.AfterWithSpace);
  44. AddNumberFormat(p, new RectangleF(220, 100, 60, 20), 12345, 2, TextField.NumberSeparatorStyle.Dot,
  45. TextField.NumberNegativeStyle.ShowParentheses, "TL", TextField.CurrencySymbolStyle.AfterNoSpace);
  46. AddNumberFormat(p, new RectangleF(290, 100, 60, 20), -5674.344f, 2, TextField.NumberSeparatorStyle.ApostropheDot,
  47. TextField.NumberNegativeStyle.ShowParentheses | TextField.NumberNegativeStyle.UseRedText, "TL", TextField.CurrencySymbolStyle.AfterWithSpace);
  48.  
  49. g.DrawString("SetDateFormat", StandardFonts.Helvetica, 14, Color.Black, new PointF(10, 130));
  50. AddDateFormat(p, new RectangleF(10, 160, 60, 20), Common.Util.TimeNow(), "dd-mm-yyyy");
  51. AddDateFormat(p, new RectangleF(80, 160, 60, 20), Common.Util.TimeNow(), "d-m-yy");
  52. AddDateFormat(p, new RectangleF(150, 160, 60, 20), Common.Util.TimeNow(), "yyyy/mmmm/dd");
  53.  
  54. g.DrawString("SetTimeFormat", StandardFonts.Helvetica, 14, Color.Black, new PointF(10, 200));
  55. var dt = new DateTime(2000, 1, 1, 1, 2, 3);
  56. AddTimeFormat(p, new RectangleF(10, 230, 60, 20), dt, "HH:MM");
  57. AddTimeFormat(p, new RectangleF(80, 230, 60, 20), dt, "h:MM tt");
  58. AddTimeFormat(p, new RectangleF(150, 230, 60, 20), dt, "HH:MM:ss");
  59.  
  60. g.DrawString("Special Format", StandardFonts.Helvetica, 14, Color.Black, new PointF(10, 270));
  61. AddSpecialFormat(p, new RectangleF(10, 300, 60, 20), "35004", TextField.SpecialFormat.ZipCode);
  62. AddSpecialFormat(p, new RectangleF(80, 300, 60, 20), "84606-6580", TextField.SpecialFormat.ZipCode4);
  63. AddSpecialFormat(p, new RectangleF(150, 300, 60, 20), "(123) 456-7890", TextField.SpecialFormat.Phone);
  64. AddSpecialFormat(p, new RectangleF(220, 300, 60, 20), "123-45-6789", TextField.SpecialFormat.SSN);
  65.  
  66. // Done:
  67. doc.Save(stream);
  68. return doc.Pages.Count;
  69. }
  70.  
  71. private static TextField AddPercentFormat(Page p,
  72. RectangleF rect,
  73. float v,
  74. int decimalPlaces,
  75. TextField.NumberSeparatorStyle separatorStyle)
  76. {
  77. var result = new TextField();
  78. p.Doc.AcroForm.Fields.Add(result);
  79.  
  80. result.Widget.Page = p;
  81. result.Widget.Rect = rect;
  82. result.Widget.Border.Width = 1;
  83. result.SetPercentValue(v, decimalPlaces, separatorStyle);
  84. return result;
  85. }
  86.  
  87. private static TextField AddNumberFormat(Page p,
  88. RectangleF rect,
  89. float v,
  90. int decimalPlaces,
  91. TextField.NumberSeparatorStyle separatorStyle,
  92. TextField.NumberNegativeStyle negativeStyle,
  93. string currencySymbol,
  94. TextField.CurrencySymbolStyle currencySymbolStyle)
  95. {
  96. var result = new TextField();
  97. p.Doc.AcroForm.Fields.Add(result);
  98.  
  99. result.Widget.Page = p;
  100. result.Widget.Rect = rect;
  101. result.Widget.Border.Width = 1;
  102. result.Widget.Justification = VariableTextJustification.RightJustified;
  103. result.SetNumberValue(v, decimalPlaces, separatorStyle, negativeStyle, currencySymbol, currencySymbolStyle);
  104. return result;
  105. }
  106.  
  107. private static TextField AddDateFormat(Page p,
  108. RectangleF rect,
  109. DateTime v,
  110. string format)
  111. {
  112. var result = new TextField();
  113. p.Doc.AcroForm.Fields.Add(result);
  114.  
  115. result.Widget.Page = p;
  116. result.Widget.Rect = rect;
  117. result.Widget.Border.Width = 1;
  118. result.SetDateValue(v, format);
  119. return result;
  120. }
  121.  
  122. private static TextField AddTimeFormat(Page p,
  123. RectangleF rect,
  124. DateTime v,
  125. string format)
  126. {
  127. var result = new TextField();
  128. p.Doc.AcroForm.Fields.Add(result);
  129.  
  130. result.Widget.Page = p;
  131. result.Widget.Rect = rect;
  132. result.Widget.Border.Width = 1;
  133. result.SetTimeValue(v, format);
  134. return result;
  135. }
  136.  
  137. private static TextField AddSpecialFormat(Page p,
  138. RectangleF rect,
  139. string v,
  140. TextField.SpecialFormat format)
  141. {
  142. var result = new TextField();
  143. p.Doc.AcroForm.Fields.Add(result);
  144.  
  145. result.Widget.Page = p;
  146. result.Widget.Rect = rect;
  147. result.Widget.Border.Width = 1;
  148. result.SetSpecialFormatValue(v, format);
  149. return result;
  150. }
  151. }
  152. }
  153.