TextExportForm.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. using System;
  2. using System.Drawing;
  3. using System.Text;
  4. using System.Windows.Forms;
  5. using FastReport.Export;
  6. using FastReport.Export.Text;
  7. using FastReport.Utils;
  8. using System.Globalization;
  9. using System.IO;
  10. namespace FastReport.Forms
  11. {
  12. /// <summary>
  13. /// Form for <see cref="TextExport"/>.
  14. /// For internal use only.
  15. /// </summary>
  16. public partial class TextExportForm : BaseExportForm
  17. {
  18. private TextExport previewExport;
  19. private Report report;
  20. private int prevPage;
  21. /// <inheritdoc/>
  22. public override void Init(ExportBase export)
  23. {
  24. base.Init(export);
  25. TextExport textExport = Export as TextExport;
  26. report = textExport.Report;
  27. ProfessionalColorTable vs2005ColorTable = new ProfessionalColorTable();
  28. vs2005ColorTable.UseSystemColors = true;
  29. toolStrip.Renderer = new ToolStripProfessionalRenderer(vs2005ColorTable);
  30. cbPageBreaks.Checked = textExport.PageBreaks;
  31. cbEmptyLines.Checked = textExport.EmptyLines;
  32. if (textExport.Frames && textExport.TextFrames)
  33. cbbFrames.SelectedIndex = 1;
  34. else if (textExport.Frames && !textExport.TextFrames)
  35. cbbFrames.SelectedIndex = 2;
  36. else
  37. cbbFrames.SelectedIndex = 0;
  38. cbDataOnly.Checked = textExport.DataOnly;
  39. if (textExport.Encoding == Encoding.Default)
  40. cbbCodepage.SelectedIndex = 0;
  41. else if (textExport.Encoding == Encoding.UTF8)
  42. cbbCodepage.SelectedIndex = 1;
  43. else if (textExport.Encoding == Encoding.GetEncoding(CultureInfo.CurrentCulture.TextInfo.OEMCodePage))
  44. cbbCodepage.SelectedIndex = 2;
  45. udX.Value = (decimal)textExport.ScaleX;
  46. udY.Value = (decimal)textExport.ScaleY;
  47. udX.ValueChanged += new EventHandler(udX_ValueChanged);
  48. udY.ValueChanged += new EventHandler(udX_ValueChanged);
  49. cbbFrames.SelectedIndexChanged += new EventHandler(cbbFrames_SelectedIndexChanged);
  50. tbPage.Text = "1";
  51. prevPage = 1;
  52. lblTotalPages.Text = String.Format(Res.Get("Misc,ofM"), report.PreparedPages.Count);
  53. cbFontSize.SelectedIndex = 4;
  54. previewExport = new TextExport();
  55. previewExport.PreviewMode = true;
  56. CalcScale();
  57. }
  58. /// <inheritdoc/>
  59. public override void Localize()
  60. {
  61. base.Localize();
  62. MyRes res = new MyRes("Export,Text");
  63. Text = res.Get("");
  64. lblFrames.Text = res.Get("Frames");
  65. cbEmptyLines.Text = res.Get("EmptyLines");
  66. cbDataOnly.Text = res.Get("DataOnly");
  67. cbbFrames.Items[0] = res.Get("FramesNone");
  68. cbbFrames.Items[1] = res.Get("FramesText");
  69. cbbFrames.Items[2] = res.Get("FramesGraphic");
  70. lblCodepage.Text = res.Get("Codepage");
  71. cbbCodepage.Items[0] = res.Get("Default");
  72. cbbCodepage.Items[1] = res.Get("Unicode");
  73. cbbCodepage.Items[2] = res.Get("OEM");
  74. lblX.Text = res.Get("ScaleX");
  75. lblY.Text = res.Get("ScaleY");
  76. btnCalculate.Text = res.Get("AutoScale");
  77. lblLoss.Text = res.Get("DataLoss");
  78. lblPageWidth.Text = res.Get("PageWidth");
  79. lblPageHeight.Text = res.Get("PageHeight");
  80. gbScale.Text = res.Get("Scale");
  81. res = new MyRes("Export,Misc");
  82. gbOptions.Text = res.Get("Options");
  83. cbPageBreaks.Text = res.Get("PageBreaks");
  84. res = new MyRes("Preview");
  85. btnPrint.Text = res.Get("PrintText");
  86. btnPrint.ToolTipText = res.Get("Print");
  87. btnSave.ToolTipText = res.Get("Save");
  88. btnSave.Text = res.Get("SaveText");
  89. btnZoomOut.ToolTipText = Res.Get("Designer,Toolbar,Zoom,ZoomOut");
  90. btnZoomIn.ToolTipText = Res.Get("Designer,Toolbar,Zoom,ZoomIn");
  91. btnFirst.ToolTipText = res.Get("First");
  92. btnPrior.ToolTipText = res.Get("Prior");
  93. lblTotalPages.Text = String.Format(Res.Get("Misc,ofM"), 1);
  94. btnNext.ToolTipText = res.Get("Next");
  95. btnLast.ToolTipText = res.Get("Last");
  96. btnClose.Text = Res.Get("Buttons,Close");
  97. }
  98. /// <inheritdoc/>
  99. public override void UpdateDpiDependencies()
  100. {
  101. base.UpdateDpiDependencies();
  102. toolStrip.Font = Font;
  103. Status.Font = Font;
  104. cbFontSize.Size = this.LogicalToDevice(new Size(40, 21));
  105. tbPage.Size = this.LogicalToDevice(new Size(40, 21));
  106. btnPrint.Image = GetImage(195);
  107. btnSave.Image = GetImage(2);
  108. btnZoomOut.Image = GetImage(193);
  109. btnZoomIn.Image = GetImage(192);
  110. btnFirst.Image = GetImage(185);
  111. btnPrior.Image = GetImage(186);
  112. btnNext.Image = GetImage(187);
  113. btnLast.Image = GetImage(188);
  114. picPerforation.BackgroundImage = this.GetImage("perforation.png");
  115. }
  116. private void CalcScale()
  117. {
  118. TextExport textExport = Export as TextExport;
  119. using (ProgressForm progressForm = new ProgressForm(null))
  120. {
  121. progressForm.Show();
  122. MyRes res = new MyRes("Export,Text");
  123. progressForm.ShowProgressMessage(res.Get("ScaleMessage"));
  124. textExport.EmptyLines = cbEmptyLines.Checked;
  125. textExport.Frames = cbbFrames.SelectedIndex != 0;
  126. textExport.TextFrames = cbbFrames.SelectedIndex == 1;
  127. textExport.DataOnly = cbDataOnly.Checked;
  128. textExport.ScaleX = (float)udX.Value;
  129. textExport.ScaleY = (float)udY.Value;
  130. textExport.CalculateScale(progressForm);
  131. udX.Value = (decimal)Math.Round(textExport.ScaleX, 2);
  132. udY.Value = (decimal)Math.Round(textExport.ScaleY, 2);
  133. }
  134. UpdatePreview();
  135. }
  136. private void UpdatePreview()
  137. {
  138. if (previewExport != null)
  139. {
  140. TextExport textExport = Export as TextExport;
  141. int pageNo = int.Parse(tbPage.Text) - 1;
  142. using (ReportPage page = textExport.Report.PreparedPages.GetPage(pageNo))
  143. {
  144. previewExport.PageBreaks = cbPageBreaks.Checked;
  145. previewExport.DataOnly = cbDataOnly.Checked;
  146. previewExport.Frames = cbbFrames.SelectedIndex != 0;
  147. previewExport.TextFrames = cbbFrames.SelectedIndex == 1;
  148. previewExport.EmptyLines = cbEmptyLines.Checked;
  149. previewExport.Encoding = GetEncoding();
  150. previewExport.ScaleX = (float)udX.Value;
  151. previewExport.ScaleY = (float)udY.Value;
  152. previewExport.SetReport(textExport.Report);
  153. tbPreview.Text = previewExport.ExportPage(pageNo);
  154. lblPageWidthValue.Text = previewExport.PageWidth.ToString();
  155. lblPageHeightValue.Text = previewExport.PageHeight.ToString();
  156. lblLoss.Visible = !previewExport.DataSaved;
  157. }
  158. }
  159. }
  160. private Encoding GetEncoding()
  161. {
  162. Encoding encoding;
  163. if (cbbCodepage.SelectedIndex == 0)
  164. encoding = Encoding.Default;
  165. else if (cbbCodepage.SelectedIndex == 1)
  166. encoding = Encoding.UTF8;
  167. else if (cbbCodepage.SelectedIndex == 2)
  168. encoding = Encoding.GetEncoding(CultureInfo.CurrentCulture.TextInfo.OEMCodePage);
  169. else
  170. throw new Exception("Unknown encoding");
  171. return encoding;
  172. }
  173. private void cbPageBreaks_Click(object sender, EventArgs e)
  174. {
  175. UpdatePreview();
  176. }
  177. private void cbbCodepage_SelectedValueChanged(object sender, EventArgs e)
  178. {
  179. UpdatePreview();
  180. }
  181. private void btnCalculate_Click(object sender, EventArgs e)
  182. {
  183. CalcScale();
  184. }
  185. private void tbPage_TextChanged(object sender, EventArgs e)
  186. {
  187. bool validate = false;
  188. try
  189. {
  190. int i = int.Parse(tbPage.Text);
  191. validate = (i > 0 && i <= report.PreparedPages.Count);
  192. UpdatePreview();
  193. btnFirst.Enabled = i > 1;
  194. btnPrior.Enabled = btnFirst.Enabled;
  195. btnNext.Enabled = i < report.PreparedPages.Count;
  196. btnLast.Enabled = btnNext.Enabled;
  197. }
  198. catch
  199. {
  200. }
  201. if (!validate)
  202. tbPage.Text = prevPage.ToString();
  203. else
  204. prevPage = int.Parse(tbPage.Text);
  205. }
  206. private void btnClose_Click(object sender, EventArgs e)
  207. {
  208. DialogResult = DialogResult.Cancel;
  209. }
  210. private void btnFirst_Click(object sender, EventArgs e)
  211. {
  212. tbPage.Text = "1";
  213. }
  214. private void btnPrior_Click(object sender, EventArgs e)
  215. {
  216. int i = int.Parse(tbPage.Text);
  217. if (i > 1)
  218. tbPage.Text = (i - 1).ToString();
  219. }
  220. private void btnNext_Click(object sender, EventArgs e)
  221. {
  222. int i = int.Parse(tbPage.Text);
  223. if (i < report.PreparedPages.Count)
  224. tbPage.Text = (i + 1).ToString();
  225. }
  226. private void btnLast_Click(object sender, EventArgs e)
  227. {
  228. tbPage.Text = report.PreparedPages.Count.ToString();
  229. }
  230. private void cbFontSize_SelectedIndexChanged(object sender, EventArgs e)
  231. {
  232. tbPreview.Font = new Font(tbPreview.Font.FontFamily, int.Parse(cbFontSize.Text));
  233. }
  234. private void btnZoomOut_Click(object sender, EventArgs e)
  235. {
  236. if (cbFontSize.SelectedIndex > 0)
  237. cbFontSize.SelectedIndex--;
  238. }
  239. private void btnZoomIn_Click(object sender, EventArgs e)
  240. {
  241. if (cbFontSize.SelectedIndex < cbFontSize.Items.Count - 1)
  242. cbFontSize.SelectedIndex++;
  243. }
  244. private void btnSave_Click(object sender, EventArgs e)
  245. {
  246. TextExport textExport = new TextExport();
  247. textExport.PageBreaks = cbPageBreaks.Checked;
  248. textExport.EmptyLines = cbEmptyLines.Checked;
  249. textExport.Frames = cbbFrames.SelectedIndex != 0;
  250. textExport.TextFrames = cbbFrames.SelectedIndex == 1;
  251. textExport.DataOnly = cbDataOnly.Checked;
  252. textExport.Encoding = GetEncoding();
  253. textExport.ScaleX = (float)udX.Value;
  254. textExport.ScaleY = (float)udY.Value;
  255. textExport.OpenAfterExport = false;
  256. textExport.AvoidDataLoss = false;
  257. using (SaveFileDialog dialog = new SaveFileDialog())
  258. {
  259. dialog.FileName = Path.GetFileNameWithoutExtension(Path.GetFileName(report.FileName));
  260. dialog.Filter = textExport.FileFilter;
  261. string defaultExt = dialog.Filter.Split('|')[1];
  262. dialog.DefaultExt = Path.GetExtension(defaultExt);
  263. if (dialog.ShowDialog() == DialogResult.OK)
  264. {
  265. Config.DoEvent();
  266. textExport.Export(report, dialog.FileName);
  267. }
  268. }
  269. }
  270. private void udX_ValueChanged(object sender, EventArgs e)
  271. {
  272. UpdatePreview();
  273. }
  274. private void udY_ValueChanged(object sender, EventArgs e)
  275. {
  276. UpdatePreview();
  277. }
  278. private void cbbFrames_SelectedIndexChanged(object sender, EventArgs e)
  279. {
  280. CalcScale();
  281. }
  282. private void btnCalculate_Click_1(object sender, EventArgs e)
  283. {
  284. CalcScale();
  285. }
  286. private void btnPrint_Click(object sender, EventArgs e)
  287. {
  288. TextExport parentExport = Export as TextExport;
  289. TextExport textExport = new TextExport();
  290. using (TextExportPrintForm printDialog = new TextExportPrintForm(textExport))
  291. {
  292. textExport.PrinterTypes = parentExport.PrinterTypes;
  293. textExport.Copies = parentExport.Copies;
  294. printDialog.CurrentPage = tbPage.Text;
  295. if (printDialog.ShowDialog() == DialogResult.OK)
  296. {
  297. if (cbbCodepage.SelectedIndex == 1)
  298. cbbCodepage.SelectedIndex = 2;
  299. textExport.PageBreaks = cbPageBreaks.Checked;
  300. textExport.EmptyLines = cbEmptyLines.Checked;
  301. textExport.Frames = cbbFrames.SelectedIndex != 0;
  302. textExport.TextFrames = cbbFrames.SelectedIndex == 1;
  303. textExport.DataOnly = cbDataOnly.Checked;
  304. textExport.Encoding = GetEncoding();
  305. textExport.ScaleX = (float)udX.Value;
  306. textExport.ScaleY = (float)udY.Value;
  307. textExport.PrintAfterExport = true;
  308. textExport.OpenAfterExport = false;
  309. textExport.AvoidDataLoss = false;
  310. using (MemoryStream memStream = new MemoryStream())
  311. textExport.Export(report, memStream);
  312. }
  313. }
  314. }
  315. private void TextExportForm_KeyDown(object sender, KeyEventArgs e)
  316. {
  317. if (e.KeyCode == Keys.Escape)
  318. DialogResult = DialogResult.Cancel;
  319. else if (e.KeyCode == Keys.PageDown)
  320. btnNext_Click(null, null);
  321. else if (e.KeyCode == Keys.PageUp)
  322. btnPrior_Click(null, null);
  323. }
  324. private void TextExportForm_Shown(object sender, EventArgs e)
  325. {
  326. // do not set this property in the .Designer.cs file:
  327. // modal dialog form with Maximized state will be displayed on wrong monitor and have scaling issues
  328. WindowState = FormWindowState.Maximized;
  329. }
  330. private void TextExportForm_FormClosing(object sender, FormClosingEventArgs e)
  331. {
  332. TextExport textExport = Export as TextExport;
  333. textExport.PageBreaks = cbPageBreaks.Checked;
  334. textExport.EmptyLines = cbEmptyLines.Checked;
  335. textExport.Frames = cbbFrames.SelectedIndex != 0;
  336. textExport.TextFrames = cbbFrames.SelectedIndex == 1;
  337. textExport.DataOnly = cbDataOnly.Checked;
  338. textExport.Encoding = GetEncoding();
  339. textExport.ScaleX = (float)udX.Value;
  340. textExport.ScaleY = (float)udY.Value;
  341. }
  342. /// <summary>
  343. /// Initializes a new instance of the <see cref="TextExportForm"/> class.
  344. /// </summary>
  345. public TextExportForm()
  346. {
  347. InitializeComponent();
  348. }
  349. }
  350. }