ImportRtf.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776
  1. using FastReport;
  2. using System.IO;
  3. using FastReport.RichTextParser;
  4. using System.Drawing;
  5. using System.Collections.Generic;
  6. using System;
  7. using System.Text;
  8. using FastReport.Table;
  9. using System.Windows.Forms;
  10. namespace FastReport.Design.ImportPlugins.RTF
  11. {
  12. internal partial class ImportRtf
  13. {
  14. #region Transformation options
  15. public bool debugPageClient = false;
  16. public bool debugBandBorder = false;
  17. public bool debugMemoBorder = false;
  18. public bool convertPadding = true;
  19. public bool enableWYSIWYG = false;
  20. public bool allowExpressions = false;
  21. public bool exportHeader = true;
  22. public bool exportFooter = true;
  23. #endregion
  24. static int DpiX = 96;
  25. const float Divisor = 56.6929131f * 1.25f;
  26. Report report = null;
  27. string fileName = string.Empty;
  28. RichDocument rtf;
  29. int paragraph_counter;
  30. int tables_counter;
  31. int pictures_counter;
  32. int rows_counter;
  33. int columns_counter;
  34. int cell_counter;
  35. Font default_font = new Font(FontFamily.GenericSerif, 12, FontStyle.Regular);
  36. RunFormat current_format;
  37. internal Report Report { get { return report; } }
  38. private static float Twips2Millimeters(long twips)
  39. {
  40. return (float)(((double)twips) * 0.01763889);
  41. }
  42. private static float Twips2Pixels(long twips)
  43. {
  44. return (Twips2Millimeters(twips) * DpiX / 25.4f);
  45. }
  46. private Font GetFontFromRichStyle(RichDocument rtf, RunFormat format)
  47. {
  48. int font_idx = (int)format.font_idx;
  49. if (font_idx < rtf.font_list.Count)
  50. {
  51. RFont rf = rtf.font_list[font_idx];
  52. string Name = rf.FontName;
  53. FontStyle style = format.bold ? FontStyle.Bold : FontStyle.Regular;
  54. return new Font(rf.FontName, format.font_size / 2, style);
  55. }
  56. else
  57. return default_font;
  58. }
  59. private FastReport.BorderLine TranslateBorderLine(FastReport.RichTextParser.BorderLine rtf_border_line)
  60. {
  61. FastReport.BorderLine border_line = new FastReport.BorderLine();
  62. switch (rtf_border_line.style)
  63. {
  64. case FastReport.RichTextParser.BorderLine.Style.Thin:
  65. border_line.Style = LineStyle.Solid;
  66. break;
  67. case FastReport.RichTextParser.BorderLine.Style.Thick:
  68. border_line.Style = LineStyle.Solid;
  69. break;
  70. case FastReport.RichTextParser.BorderLine.Style.Double:
  71. border_line.Style = LineStyle.Double;
  72. break;
  73. case FastReport.RichTextParser.BorderLine.Style.Dotted:
  74. border_line.Style = LineStyle.Dot;
  75. break;
  76. default:
  77. border_line.Style = LineStyle.Solid;
  78. break;
  79. }
  80. border_line.Color = rtf_border_line.color;
  81. border_line.Width = Twips2Pixels((int)rtf_border_line.width);
  82. return border_line;
  83. }
  84. private FastReport.Border TranslateBorders(Column rtf_column)
  85. {
  86. FastReport.Border border = new Border();
  87. border.Lines = BorderLines.None;
  88. if (rtf_column.border_top.width > 0)
  89. {
  90. border.TopLine = TranslateBorderLine(rtf_column.border_top);
  91. border.Lines |= BorderLines.Top;
  92. }
  93. if (rtf_column.border_right.width > 0)
  94. {
  95. border.RightLine = TranslateBorderLine(rtf_column.border_right);
  96. border.Lines |= BorderLines.Right;
  97. }
  98. if (rtf_column.border_left.width > 0)
  99. {
  100. border.LeftLine = TranslateBorderLine(rtf_column.border_left);
  101. border.Lines |= BorderLines.Left;
  102. }
  103. if (rtf_column.border_bottom.width > 0)
  104. {
  105. border.BottomLine = TranslateBorderLine(rtf_column.border_bottom);
  106. border.Lines |= BorderLines.Bottom;
  107. }
  108. return border;
  109. }
  110. private TextObject CreateParagraph(Paragraph rtf_paragraph, float Width)
  111. {
  112. TextObject paragraph = new TextObject();
  113. paragraph.Width = Width;
  114. paragraph.SetReport(this.report);
  115. paragraph.TextRenderType = TextRenderType.HtmlParagraph;
  116. paragraph.CanGrow = true;
  117. paragraph.CanBreak = true;
  118. paragraph.CanShrink = false;
  119. paragraph.GrowToBottom = true;
  120. paragraph.Wysiwyg = enableWYSIWYG;
  121. paragraph.GrowToBottom = false;
  122. paragraph.AllowExpressions = allowExpressions;
  123. if (rtf_paragraph.format.first_line_indent > 0)
  124. paragraph.FirstTabOffset = rtf_paragraph.format.first_line_indent;
  125. ++paragraph_counter;
  126. paragraph.Name = "p" + paragraph_counter.ToString();
  127. if (rtf_paragraph.format.first_line_indent < 0)
  128. // paragraph.FirstTabOffset = -rtf_paragraph.format.first_line_indent;
  129. paragraph.FirstTabOffset = 0; // Check it
  130. //throw new Exception("This exception should be never happen due to negative first line indent handled early");
  131. else
  132. paragraph.FirstTabOffset = rtf_paragraph.format.first_line_indent;
  133. if (rtf_paragraph.runs.Count > 0)
  134. {
  135. if (rtf_paragraph.format.list_id != null && rtf_paragraph.format.list_id.Count > 0)
  136. {
  137. paragraph.Text = string.Empty;
  138. foreach (Run r in rtf_paragraph.format.list_id)
  139. {
  140. if (r.text == "\t")
  141. paragraph.Text += " ";
  142. else
  143. paragraph.Text += r.text;
  144. }
  145. paragraph.Text += GenerateHTMLText(paragraph, rtf_paragraph);
  146. }
  147. else
  148. paragraph.Text = GenerateHTMLText(paragraph, rtf_paragraph);
  149. }
  150. else
  151. paragraph.Font = default_font;
  152. switch (rtf_paragraph.format.align)
  153. {
  154. case FastReport.RichTextParser.ParagraphFormat.HorizontalAlign.Right:
  155. paragraph.HorzAlign = HorzAlign.Right;
  156. break;
  157. case FastReport.RichTextParser.ParagraphFormat.HorizontalAlign.Centered:
  158. paragraph.HorzAlign = HorzAlign.Center;
  159. break;
  160. case FastReport.RichTextParser.ParagraphFormat.HorizontalAlign.Justified:
  161. paragraph.HorzAlign = HorzAlign.Justify;
  162. break;
  163. default:
  164. paragraph.HorzAlign = HorzAlign.Left;
  165. break;
  166. }
  167. paragraph.Border.Lines = BorderLines.None;
  168. int lineheight = rtf_paragraph.format.line_spacing;
  169. if (lineheight == 0)
  170. paragraph.LineHeight = paragraph.Font.Height; // * 1.2f;
  171. else
  172. {
  173. switch (rtf_paragraph.format.lnspcmult)
  174. {
  175. case FastReport.RichTextParser.ParagraphFormat.LnSpcMult.Exactly:
  176. lineheight = (int)(lineheight / 240f);
  177. break;
  178. case FastReport.RichTextParser.ParagraphFormat.LnSpcMult.Multiply:
  179. lineheight = (int)Twips2Millimeters(lineheight);
  180. break;
  181. }
  182. paragraph.LineHeight = lineheight < 0 ? -lineheight : lineheight >= paragraph.Font.Height ? lineheight : paragraph.Font.Height;
  183. }
  184. if (convertPadding)
  185. {
  186. Padding p = paragraph.Padding;
  187. p.Top = (int)Twips2Pixels(rtf_paragraph.format.space_before);
  188. p.Bottom = (int)Twips2Pixels(rtf_paragraph.format.space_after);
  189. p.Left = (int)Twips2Pixels(rtf_paragraph.format.left_indent);
  190. p.Right = (int)Twips2Pixels(rtf_paragraph.format.right_indent);
  191. paragraph.Padding = p;
  192. }
  193. if (debugMemoBorder && paragraph.Border.Lines == BorderLines.None)
  194. {
  195. paragraph.Border.Color = Color.Red;
  196. paragraph.Border.Width = 1;
  197. paragraph.Border.Lines = BorderLines.All;
  198. }
  199. paragraph.Height = paragraph.CalcHeight() + paragraph.Padding.Vertical;
  200. return paragraph;
  201. }
  202. private TableObject CreateTable(FastReport.RichTextParser.Table rtf_table)
  203. {
  204. TableObject table = new TableObject();
  205. ++tables_counter;
  206. table.Name = "tbl_" + tables_counter.ToString();
  207. int idx = 0;
  208. uint prev_width = 0;
  209. IList<TranslationPropeties> row_properties = new List<TranslationPropeties>();
  210. foreach (Column rtf_column in rtf_table.columns)
  211. {
  212. TableColumn column = new TableColumn();
  213. column.Name = "cm_" + ++columns_counter;
  214. column.Width = Twips2Pixels((int)(rtf_column.Width - prev_width));
  215. prev_width = rtf_column.Width;
  216. column.SetIndex(idx);
  217. TranslationPropeties prop = new TranslationPropeties(
  218. TranslateBorders(rtf_column),
  219. rtf_column.back_color);
  220. row_properties.Add(prop);
  221. table.Columns.Add(column);
  222. idx++;
  223. }
  224. foreach (FastReport.RichTextParser.TableRow rtf_row in rtf_table.rows)
  225. {
  226. int height = rtf_row.height;
  227. if (height < 0)
  228. height = -height;
  229. FastReport.Table.TableRow row = new FastReport.Table.TableRow();
  230. row.Name = "rw_" + ++rows_counter;
  231. row.Height += height / 25;
  232. int cell_idx = 0;
  233. float x_pos = 0;
  234. float max_cell_height = 0;
  235. int colnum = 0;
  236. foreach (RichObjectSequence sequence in rtf_row.cells)
  237. {
  238. float y_pos = 0;
  239. Column column = rtf_table.columns[colnum++];
  240. TableColumn rtf_column = table.Columns[cell_idx];
  241. TableCell cell = new TableCell();
  242. ++cell_counter;
  243. cell.Name = "cl_" + cell_counter;
  244. TranslationPropeties prop = row_properties[cell_idx];
  245. cell.Border = prop.border;
  246. cell.FillColor = prop.background_color;
  247. foreach (FastReport.RichTextParser.RichObject obj in sequence.objects)
  248. {
  249. switch (obj.type)
  250. {
  251. case FastReport.RichTextParser.RichObject.Type.Paragraph:
  252. if (obj.paragraph.runs.Count == 0)
  253. {
  254. y_pos += 13.8f; // This is not correct value. Need to be calculated
  255. continue;
  256. }
  257. TextObject text_paragraph = CreateParagraph(obj.paragraph, column.Width);
  258. switch (column.valign)
  259. {
  260. case Column.VertAlign.Top:
  261. text_paragraph.VertAlign = VertAlign.Top;
  262. break;
  263. case Column.VertAlign.Center:
  264. text_paragraph.VertAlign = VertAlign.Center;
  265. break;
  266. case Column.VertAlign.Bottom:
  267. text_paragraph.VertAlign = VertAlign.Bottom;
  268. break;
  269. }
  270. //text_paragraph.VertAlign
  271. text_paragraph.Width = rtf_column.Width;
  272. text_paragraph.Height = text_paragraph.CalcHeight();
  273. text_paragraph.Top = y_pos;
  274. y_pos += text_paragraph.Height;
  275. Padding p = text_paragraph.Padding;
  276. p.Top = (int)Twips2Millimeters(obj.paragraph.format.space_before);
  277. p.Bottom = (int)Twips2Millimeters(obj.paragraph.format.space_after);
  278. p.Left = (int)Twips2Millimeters(obj.paragraph.format.left_indent) + 3;
  279. p.Right = (int)Twips2Millimeters(obj.paragraph.format.right_indent) + 3;
  280. text_paragraph.Padding = p;
  281. text_paragraph.Parent = cell;
  282. cell.Objects.Add(text_paragraph); // = GetRawText(obj.pargraph);
  283. break;
  284. case FastReport.RichTextParser.RichObject.Type.Picture:
  285. PictureObject picture = CreatePicture(obj.picture);
  286. //cell.Objects.Add(picture);
  287. picture.Parent = cell;
  288. break;
  289. case FastReport.RichTextParser.RichObject.Type.Table:
  290. TableObject subtable = CreateTable(obj.table);
  291. cell.Objects.Add(table);
  292. break;
  293. }
  294. }
  295. cell.Parent = row;
  296. cell.Left = x_pos;
  297. if (max_cell_height < y_pos)
  298. max_cell_height = y_pos;
  299. cell.Height = max_cell_height;
  300. row.Height = (row.Height > max_cell_height) ? row.Height : max_cell_height;
  301. if (cell.Objects != null && cell.Objects.Count == 1)
  302. {
  303. ReportComponentBase obj = cell.Objects[0] as ReportComponentBase;
  304. if (obj is TextObject)
  305. {
  306. TextObject text_cell = obj as TextObject;
  307. switch (column.valign)
  308. {
  309. case Column.VertAlign.Top:
  310. text_cell.VertAlign = VertAlign.Top;
  311. break;
  312. case Column.VertAlign.Center:
  313. text_cell.VertAlign = VertAlign.Center;
  314. break;
  315. case Column.VertAlign.Bottom:
  316. text_cell.VertAlign = VertAlign.Bottom;
  317. break;
  318. }
  319. }
  320. obj.Height = row.Height;
  321. }
  322. x_pos += rtf_column.Width;
  323. cell_idx++;
  324. }
  325. row.Parent = table;
  326. table.Rows.Add(row);
  327. table.Height += row.Height;
  328. }
  329. return table;
  330. }
  331. private PictureObject CreatePicture(FastReport.RichTextParser.Picture rtf_picture)
  332. {
  333. PictureObject pic = new PictureObject();
  334. pic.Image = rtf_picture.image;
  335. pic.SetReport(this.report);
  336. pic.Name = "pic" + ++pictures_counter;
  337. if (rtf_picture.desired_width != 0)
  338. {
  339. if (rtf_picture.scalex == 0)
  340. pic.Width = Twips2Pixels(rtf_picture.desired_width);
  341. else
  342. pic.Width = Twips2Pixels(rtf_picture.desired_width * rtf_picture.scalex / 100);
  343. }
  344. else
  345. pic.Width = Twips2Pixels(rtf_picture.width);
  346. if (rtf_picture.desired_height != 0)
  347. {
  348. if (rtf_picture.scaley != 0)
  349. pic.Height = Twips2Pixels(rtf_picture.desired_height);
  350. else
  351. pic.Height = Twips2Pixels(rtf_picture.desired_height * rtf_picture.scaley / 100);
  352. }
  353. else
  354. pic.Height = Twips2Pixels(rtf_picture.height);
  355. return pic;
  356. }
  357. #region Create RTF objects on Band
  358. private float CreateNegativeFirstLineIndent(BandBase band, Paragraph rtf_paragraph)
  359. {
  360. List<Run> left_runs = new List<Run>();
  361. List<Run> right_runs = new List<Run>();
  362. Font left_font = null;
  363. for (int i = 0; i < rtf_paragraph.runs.Count; ++i)
  364. {
  365. switch (i)
  366. {
  367. case 0:
  368. left_runs.Add(rtf_paragraph.runs[i]);
  369. left_font = GetFontFromRichStyle(this.rtf, rtf_paragraph.runs[i].format);
  370. if (left_font.Style != FontStyle.Regular)
  371. left_font = new Font(left_font, FontStyle.Regular);
  372. break;
  373. case 1:
  374. if (rtf_paragraph.runs[i].text != "\t")
  375. throw new Exception("State machine error");
  376. break;
  377. default:
  378. right_runs.Add(rtf_paragraph.runs[i]);
  379. break;
  380. }
  381. }
  382. TableObject table = new TableObject();
  383. table.Top = band.Height;
  384. table.Width = band.Width;
  385. table.SetReport(this.report);
  386. TableColumn left_column = new TableColumn();
  387. left_column.Width = Twips2Pixels(-rtf_paragraph.format.first_line_indent);
  388. left_column.SetIndex(0);
  389. //TranslationPropeties prop = new TranslationPropeties();
  390. //prop.border = TranslateBorders(column.);
  391. //prop.background_color = column.back_color;
  392. //row_properties.Add(prop);
  393. table.Columns.Add(left_column);
  394. TableColumn right_column = new TableColumn();
  395. right_column.Width = band.Width - Twips2Pixels(-rtf_paragraph.format.first_line_indent);
  396. right_column.SetIndex(1);
  397. table.Columns.Add(right_column);
  398. TableCellData left_cell_data = new TableCellData();
  399. left_cell_data.Objects = new ReportComponentCollection();
  400. TableCellData right_cell_data = new TableCellData();
  401. right_cell_data.Objects = new ReportComponentCollection();
  402. TextObject left_text = new TextObject();
  403. ++paragraph_counter;
  404. left_text.Width = left_column.Width;
  405. left_text.SetReport(this.report);
  406. left_text.TextRenderType = TextRenderType.HtmlParagraph;
  407. left_text.CanGrow = true;
  408. left_text.CanBreak = false;
  409. left_text.CanShrink = false;
  410. left_text.GrowToBottom = true;
  411. left_text.Wysiwyg = enableWYSIWYG;
  412. left_text.AllowExpressions = allowExpressions;
  413. left_text.Name = "p" + paragraph_counter.ToString();
  414. rtf_paragraph.runs = left_runs;
  415. left_text.Font = left_font;
  416. left_text.Text = GenerateHTMLText(left_text, rtf_paragraph);
  417. left_text.Font = left_font;
  418. TableCell left_cell = new TableCell();
  419. left_cell_data.Objects.Add(left_text);
  420. left_cell.CellData = left_cell_data;
  421. left_cell.Left = 0;
  422. left_text.SetParent(left_cell);
  423. TextObject right_text = new TextObject();
  424. ++paragraph_counter;
  425. right_text.Width = right_column.Width;
  426. right_text.SetReport(this.report);
  427. right_text.TextRenderType = TextRenderType.HtmlParagraph;
  428. right_text.CanGrow = true;
  429. right_text.CanBreak = true;
  430. right_text.CanShrink = false;
  431. right_text.GrowToBottom = true;
  432. right_text.Wysiwyg = enableWYSIWYG;
  433. right_text.AllowExpressions = allowExpressions;
  434. right_text.Name = "p" + paragraph_counter.ToString();
  435. rtf_paragraph.runs = right_runs;
  436. right_text.Font = left_font;
  437. right_text.Text = GenerateHTMLText(right_text, rtf_paragraph);
  438. right_text.Font = left_font;
  439. float height = right_text.CalcHeight();
  440. left_text.Height = height;
  441. right_text.Height = height;
  442. TableCell right_cell = new TableCell();
  443. right_cell_data.Objects.Add(right_text);
  444. right_cell.CellData = right_cell_data;
  445. right_text.SetParent(right_cell);
  446. FastReport.Table.TableRow row = new FastReport.Table.TableRow();
  447. row.Top = band.Height;
  448. row.Height = height;
  449. row.AddChild(left_cell);
  450. row.AddChild(right_cell);
  451. table.Rows.Add(row);
  452. table.Height = height + 150;
  453. band.Objects.Add(table);
  454. band.Height += table.Height;
  455. return height;
  456. }
  457. private float TransformTabstopsToTables(BandBase band, Paragraph rtf_paragraph)
  458. {
  459. TableObject table = new TableObject();
  460. table.Name = "t" + ++tables_counter;
  461. FastReport.Table.TableRow row = new FastReport.Table.TableRow();
  462. row.Name = "r" + ++rows_counter;
  463. int tabstsopcount = rtf_paragraph.format.tab_positions.Count;
  464. int tabcount = 0;
  465. int prev_pos = 0;
  466. int idx = 0;
  467. int left = 0;
  468. float cellHeight = 0;
  469. table.Top = band.Bottom;
  470. foreach (Run r in rtf_paragraph.runs)
  471. {
  472. if (r.text == "\t")
  473. ++tabcount;
  474. else
  475. {
  476. TableColumn column = new TableColumn();
  477. TableCell cell = new TableCell();
  478. if (idx < rtf_paragraph.format.tab_positions.Count)
  479. {
  480. left = rtf_paragraph.format.tab_positions[idx];
  481. column.Width = Twips2Pixels(left - prev_pos);
  482. }
  483. else
  484. {
  485. column.Width = (band.Width - Twips2Pixels(prev_pos));
  486. }
  487. column.Name = "cm_" + ++columns_counter;
  488. prev_pos = left;
  489. column.SetIndex(idx);
  490. cell.SetReport(report);
  491. ++cell_counter;
  492. cell.Name = "cl_" + cell_counter;
  493. cell.Parent = row;
  494. cell.Text = r.text;
  495. cell.Font = GetFontFromRichStyle(rtf, r.format);
  496. float chg = cell.CalcHeight();
  497. if (chg > cellHeight)
  498. cellHeight = chg;
  499. table.Columns.Add(column);
  500. idx++;
  501. }
  502. }
  503. row.Height = cellHeight;
  504. table.Height = row.Height;
  505. band.Height += row.Height;
  506. table.Rows.Add(row);
  507. row.Parent = table;
  508. band.Objects.Add(table);
  509. return 20;
  510. }
  511. private float CreateParagraph(BandBase band, Paragraph rtf_paragraph)
  512. {
  513. if (rtf_paragraph.format.tab_positions != null && rtf_paragraph.format.tab_positions.Count > 0)
  514. {
  515. return TransformTabstopsToTables(band, rtf_paragraph);
  516. }
  517. if (rtf_paragraph.format.first_line_indent < 0)
  518. {
  519. if (rtf_paragraph.runs.Count > 2)
  520. if (rtf_paragraph.runs[1].text == "\t")
  521. return CreateNegativeFirstLineIndent(band, rtf_paragraph);
  522. }
  523. if (rtf_paragraph.runs.Count != 0)
  524. {
  525. TextObject paragraph = CreateParagraph(rtf_paragraph, band.Width);
  526. paragraph.Top = band.Height;
  527. // paragraph.TabWidth = Twips2Pixels(916);
  528. band.Objects.Add(paragraph);
  529. band.Height += paragraph.Height;
  530. return paragraph.Height;
  531. }
  532. TextObject empty_paragraph = new TextObject();
  533. empty_paragraph.Width = band.Width;
  534. empty_paragraph.Text = "W";
  535. empty_paragraph.CanGrow = true;
  536. empty_paragraph.SetReport(report);
  537. float height = empty_paragraph.CalcHeight();
  538. empty_paragraph = null;
  539. band.Height += height;
  540. return height;
  541. }
  542. private float CreateTable(BandBase band, FastReport.RichTextParser.Table rtf_table)
  543. {
  544. TableObject table = CreateTable(rtf_table);
  545. table.Parent = band;
  546. table.Top = band.Height;
  547. table.Height = table.CalcHeight();
  548. band.Objects.Add(table);
  549. band.Height += table.Height;
  550. return table.Height;
  551. }
  552. private float CreatePicture(BandBase band, FastReport.RichTextParser.Picture rtf_picture)
  553. {
  554. PictureObject pic = CreatePicture(rtf_picture);
  555. pic.Height = pic.CalcHeight();
  556. pic.Top = band.Height;
  557. band.Objects.Add(pic);
  558. band.Height += pic.Height;
  559. return pic.Height;
  560. }
  561. #endregion
  562. private float FillBand(BandBase band, string name, RichObjectSequence objects_on_band)
  563. {
  564. float height = 0;
  565. band.Name = name;
  566. try
  567. {
  568. foreach (FastReport.RichTextParser.RichObject rtf_obj in objects_on_band.objects)
  569. {
  570. switch (rtf_obj.type)
  571. {
  572. case FastReport.RichTextParser.RichObject.Type.Paragraph:
  573. height += CreateParagraph(band, rtf_obj.paragraph);
  574. break;
  575. case FastReport.RichTextParser.RichObject.Type.Table:
  576. if (rtf_obj.table.rows.Count != 0)
  577. height += CreateTable(band, rtf_obj.table);
  578. break;
  579. case FastReport.RichTextParser.RichObject.Type.Picture:
  580. height += CreatePicture(band, rtf_obj.picture);
  581. break;
  582. }
  583. }
  584. if (debugBandBorder && band.Border.Lines == BorderLines.None)
  585. {
  586. band.Border.Color = Color.Green;
  587. band.Border.Width = 1;
  588. band.Border.Lines = BorderLines.All;
  589. }
  590. }
  591. catch (Exception excp)
  592. {
  593. // Don't use Windows.Forms API (MessageBox etc.) in shared files
  594. throw new Exception(excp.Message);
  595. }
  596. return height;
  597. }
  598. private void ImportRtfPage(int page_num, Page rtf_page)
  599. {
  600. float vertical_position_counter = 0;
  601. // float horizontal_position_counter = 0;
  602. ReportPage page = new ReportPage();
  603. if (debugPageClient)
  604. {
  605. page.Border.Width = 1;
  606. page.Border.Color = Color.Blue;
  607. page.Border.Lines = BorderLines.All;
  608. }
  609. //float hc = rtf.global_margin_top / 25.4f;
  610. //float vc = rtf.global_margin_right / 31.75f;
  611. page.Width = Twips2Pixels(rtf_page.page_width != 0 ? rtf_page.page_width : rtf.paper_width);
  612. page.Height = Twips2Pixels(rtf_page.page_heigh != 0 ? rtf_page.page_heigh : rtf.paper_height);
  613. page.LeftMargin = (rtf_page.margin_left != 0 ? rtf_page.margin_left : rtf.global_margin_left) / Divisor;
  614. page.RightMargin = (rtf_page.margin_right != 0 ? rtf_page.margin_right : rtf.global_margin_right) / Divisor;
  615. page.TopMargin = (rtf_page.margin_top != 0 ? rtf_page.margin_top : rtf.global_margin_top) / Divisor;
  616. page.BottomMargin = (rtf_page.margin_bottom != 0 ? rtf_page.margin_bottom : rtf.global_margin_bottom) / Divisor;
  617. float BandWidth = page.Width - (page.LeftMargin + page.RightMargin) * DpiX / 25.4f;
  618. page.Name = "page_" + page_num;
  619. page.PaperWidth = 260;
  620. if (exportHeader & (rtf_page.header.objects != null) && (rtf_page.header.objects.Count > 0))
  621. {
  622. page.PageHeader = new PageHeaderBand();
  623. page.PageHeader.Width = BandWidth;
  624. vertical_position_counter += FillBand(page.PageHeader, "PageHeader", rtf_page.header);
  625. }
  626. if ((rtf_page.sequence.objects != null) && (rtf_page.sequence.objects.Count > 0))
  627. {
  628. DataBand dataBand = new DataBand();
  629. dataBand.CanGrow = true;
  630. dataBand.Width = BandWidth;
  631. dataBand.Top = vertical_position_counter;
  632. vertical_position_counter += FillBand(dataBand, "rtf_page", rtf_page.sequence);
  633. page.Bands.Add(dataBand);
  634. }
  635. if (exportFooter & (rtf_page.footer.objects != null) && (rtf_page.footer.objects.Count > 0))
  636. {
  637. page.PageFooter = new PageFooterBand();
  638. page.PageFooter.Width = BandWidth;
  639. vertical_position_counter += FillBand(page.PageFooter, "PageFooter", rtf_page.footer);
  640. }
  641. report.Pages.Add(page);
  642. }
  643. internal Report CreateReport()
  644. {
  645. int page_num = 0;
  646. try
  647. {
  648. report = new Report();
  649. foreach (Page rtf_page in rtf.pages)
  650. ImportRtfPage(++page_num, rtf_page);
  651. report.ReportInfo.CreatorVersion = ("rtf2frx converter ver 0.5");
  652. report.ReportInfo.Created = SystemFake.DateTime.Now;
  653. }
  654. catch (Exception excpt)
  655. {
  656. report = null;
  657. fileName = excpt.Message;
  658. }
  659. return report;
  660. }
  661. internal void ResetProperties()
  662. {
  663. paragraph_counter = 0;
  664. tables_counter = 0;
  665. pictures_counter = 0;
  666. rows_counter = 0;
  667. }
  668. internal ImportRtf(string filename)
  669. {
  670. using (Stream rich_stream = File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.None))
  671. using (RTF_DocumentParser parser = new RTF_DocumentParser())
  672. {
  673. parser.Load(rich_stream);
  674. rtf = parser.Document;
  675. this.fileName = filename;
  676. }
  677. }
  678. internal ImportRtf(Stream file)
  679. {
  680. using (RTF_DocumentParser parser = new RTF_DocumentParser())
  681. {
  682. parser.Load(file);
  683. rtf = parser.Document;
  684. }
  685. }
  686. }
  687. }