123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587 |
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Drawing;
- using System.IO;
- using FastReport.Utils;
- using FastReport.RichTextParser;
- using System.Globalization;
- namespace FastReport.Export.RichText
- {
- /// <summary>
- /// Specifies the image format in RTF export.
- /// </summary>
- public enum RTFImageFormat
- {
- /// <summary>
- /// Specifies the .png format.
- /// </summary>
- Png,
- /// <summary>
- /// Specifies the .jpg format.
- /// </summary>
- Jpeg,
- /// <summary>
- /// Specifies the .emf format.
- /// </summary>
- Metafile
- }
- /// <summary>
- /// Represents the RTF export filter.
- /// </summary>
- public partial class RTFExport : ExportBase
- {
- #region Constants
- const float Xdivider = 15.05F;
- const float Ydivider1 = 14.8F;
- const float Ydivider2 = 14.8F;
- const float Ydivider3 = 14.7F;
- const float MargDivider = 56.904405f; // 3.793627f * 15 | PxInMm * TwipsInPX
- const float FONT_DIVIDER = 15F;
- const float IMAGE_DIVIDER = 25.3F;
- const int PIC_BUFF_SIZE = 512;
- #endregion
- #region Private fields
- private List<string> colorTable;
- private bool pageBreaks;
- private List<string> fontTable;
- private ExportMatrix matrix;
- private bool wysiwyg;
- private bool printOptimized;
- private string creator;
- private bool autoSize;
- private MyRes res;
- private RTFImageFormat imageFormat;
- private bool pictures;
- private string tempFile;
- private int jpegQuality;
- private float dpiFactor;
- private float yDiv;
- private Color textColor;
- private bool keepRichText;
- private CultureInfo localization;
- private bool exportLocale;
- #endregion
- #region Properties
- /// <summary>
- /// Gets or sets the quality of Jpeg images in RTF file.
- /// </summary>
- /// <remarks>
- /// Default value is 90. This property will be used if you select Jpeg
- /// in the <see cref="ImageFormat"/> property.
- /// </remarks>
- public int JpegQuality
- {
- get { return jpegQuality; }
- set { jpegQuality = value; }
- }
- /// <summary>
- /// Gets or sets the image format that will be used to save pictures in RTF file.
- /// </summary>
- /// <remarks>
- /// Default value is <b>Metafile</b>. This format is better for exporting such objects as
- /// <b>MSChartObject</b> and <b>ShapeObject</b>.
- /// </remarks>
- public RTFImageFormat ImageFormat
- {
- get { return imageFormat; }
- set { imageFormat = value; }
- }
- /// <summary>
- /// Gets or sets a value indicating that pictures are enabled.
- /// </summary>
- public bool Pictures
- {
- get { return pictures; }
- set { pictures = value; }
- }
- /// <summary>
- /// Gets or sets a value indicating that page breaks are enabled.
- /// </summary>
- public bool PageBreaks
- {
- get { return pageBreaks; }
- set { pageBreaks = value; }
- }
- /// <summary>
- /// Get or set a locale for all document.
- /// </summary>
- public CultureInfo Locale
- {
- get { return localization; }
- set { localization = value; }
- }
- /// <summary>
- /// Gets or sets a value indicating that locale export are enabled.
- /// </summary>
- public bool ExportLocale
- {
- get { return exportLocale; }
- set { exportLocale = value; }
- }
- /// <summary>
- /// Gets or sets a value that determines whether the wysiwyg mode should be used
- /// for better results.
- /// </summary>
- /// <remarks>
- /// Default value is <b>true</b>. In wysiwyg mode, the resulting rtf file will look
- /// as close as possible to the prepared report. On the other side, it may have a lot
- /// of small rows/columns, which will make it less editable. If you set this property
- /// to <b>false</b>, the number of rows/columns in the resulting file will be decreased.
- /// You will get less wysiwyg, but more editable file.
- /// </remarks>
- public bool Wysiwyg
- {
- get { return wysiwyg; }
- set { wysiwyg = value; }
- }
- /// <summary>
- /// Gets or sets the PrintOptimized.
- /// </summary>
- public bool PrintOptimized
- {
- get { return printOptimized; }
- set { printOptimized = value; }
- }
- /// <summary>
- /// Gets or sets the creator of the document.
- /// </summary>
- public string Creator
- {
- get { return creator; }
- set { creator = value; }
- }
- /// <summary>
- /// Gets or sets a value that determines whether the rows in the resulting table
- /// should calculate its height automatically.
- /// </summary>
- /// <remarks>
- /// Default value for this property is <b>false</b>. In this mode, each row in the
- /// resulting table has fixed height to get maximum wysiwyg. If you set it to <b>true</b>,
- /// the height of resulting table will be calculated automatically by the Word processor.
- /// The document will be more editable, but less wysiwyg.
- /// </remarks>
- public bool AutoSize
- {
- get { return autoSize; }
- set { autoSize = value; }
- }
- /// <summary>
- /// Gets or sets a value that determines whether the repot's RichObject will be
- /// translated as picture or joined to generated RTF.
- /// </summary>
- /// <remarks>
- /// Default value for this property is <b>false</b>. In this mode, each RichObject
- /// will be embedded as a picture. This is default behavior. If you set it to <b>true</b>,
- /// the RichObject will be incorporated as a navive part of document. This is experimetal
- /// feature.
- /// </remarks>
- public bool EmbedRichObject
- {
- get { return keepRichText; }
- set { keepRichText = value; }
- }
- #endregion
- #region Private Methods
- private string GetRTFBorders(ExportIEMStyle Style)
- {
- //// +debug
- //Style.Border.Lines = BorderLines.All;
- //Style.Border.Width = 1;
- //// -debug
- StringBuilder result = new StringBuilder(256);
- // top
- if ((Style.Border.Lines & BorderLines.Top) > 0)
- result.Append("\\clbrdrt").
- Append(GetRTFLineStyle(Style.Border.TopLine.Style)).
- Append("\\brdrw").
- Append(((int)Math.Round(Style.Border.TopLine.Width * 20)).ToString()).
- Append("\\brdrcf").
- Append(GetRTFColorFromTable(GetRTFColor(Style.Border.TopLine.Color)));
- // left
- if ((Style.Border.Lines & BorderLines.Left) > 0)
- result.Append("\\clbrdrl").
- Append(GetRTFLineStyle(Style.Border.LeftLine.Style)).
- Append("\\brdrw").
- Append(((int)Math.Round(Style.Border.LeftLine.Width * 20)).ToString()).
- Append("\\brdrcf").
- Append(GetRTFColorFromTable(GetRTFColor(Style.Border.LeftLine.Color)));
- // bottom
- if ((Style.Border.Lines & BorderLines.Bottom) > 0)
- result.Append("\\clbrdrb").
- Append(GetRTFLineStyle(Style.Border.BottomLine.Style)).
- Append("\\brdrw").
- Append(((int)Math.Round(Style.Border.BottomLine.Width * 20)).ToString()).
- Append("\\brdrcf").
- Append(GetRTFColorFromTable(GetRTFColor(Style.Border.BottomLine.Color)));
- // right
- if ((Style.Border.Lines & BorderLines.Right) > 0)
- result.Append("\\clbrdrr").
- Append(GetRTFLineStyle(Style.Border.RightLine.Style)).
- Append("\\brdrw").
- Append(((int)Math.Round(Style.Border.RightLine.Width * 20)).ToString()).
- Append("\\brdrcf").
- Append(GetRTFColorFromTable(GetRTFColor(Style.Border.RightLine.Color)));
- return result.ToString();
- }
- private string GetRTFLineStyle(LineStyle lineStyle)
- {
- switch (lineStyle)
- {
- case LineStyle.Dash:
- return "\\brdrdash";
- case LineStyle.DashDot:
- return "\\brdrdashd";
- case LineStyle.DashDotDot:
- return "\\brdrdashdd";
- case LineStyle.Dot:
- return "\\brdrdot";
- case LineStyle.Double:
- return "\\brdrdb";
- default:
- return "\\brdrs";
- }
- }
- private string GetRTFColor(Color c)
- {
- StringBuilder result = new StringBuilder(64);
- result.Append("\\red").Append(Convert.ToString(c.R)).
- Append("\\green").Append(Convert.ToString(c.G)).
- Append("\\blue").Append(Convert.ToString(c.B)).Append(";");
- return result.ToString();
- }
- private string GetRTFFontStyle(FontStyle f)
- {
- StringBuilder result = new StringBuilder(8);
- if ((f & FontStyle.Italic) != 0)
- result.Append("\\i");
- if ((f & FontStyle.Bold) != 0)
- result.Append("\\b");
- if ((f & FontStyle.Underline) != 0)
- result.Append("\\ul");
- return result.ToString();
- }
- private string GetRTFColorFromTable(string f)
- {
- string Result;
- int i = colorTable.IndexOf(f);
- if (i != -1)
- Result = (i + 1).ToString();
- else
- {
- colorTable.Add(f);
- Result = colorTable.Count.ToString();
- }
- return Result;
- }
- private string GetRTFFontName(string f)
- {
- string Result;
- int i = fontTable.IndexOf(f);
- if (i != -1)
- Result = (i).ToString();
- else
- {
- fontTable.Add(f);
- Result = (fontTable.Count - 1).ToString();
- }
- return Result;
- }
- private string GetRTFHAlignment(HorzAlign HAlign)
- {
- switch (HAlign)
- {
- case HorzAlign.Right:
- return "\\qr";
- case HorzAlign.Center:
- return "\\qc";
- case HorzAlign.Justify:
- return "\\qj";
- default:
- return "\\ql";
- }
- }
- private string GetRTFVAlignment(VertAlign VAlign)
- {
- switch (VAlign)
- {
- case VertAlign.Top:
- return "\\clvertalt";
- case VertAlign.Center:
- return "\\clvertalc";
- default:
- return "\\clvertalb";
- }
- }
- private string StrToRTFSlash(string Value)
- {
- StringBuilder Result = new StringBuilder();
- for (int i = 0; i < Value.Length; i++)
- {
- if (Value[i] == '\\')
- Result.Append("\\\\");
- else if (Value[i] == '{')
- Result.Append("\\{");
- else if (Value[i] == '}')
- Result.Append("\\}");
- else if ((Value[i] == '\r') && (i < (Value.Length - 1)) && (Value[i + 1] == '\n'))
- {
- Result.Append("\\line\r\n");
- i++;
- }
- else
- Result.Append(Value[i]);
- }
- return Result.ToString();
- }
- private string ParseHtmlTags(string s)
- {
- int Index = 0;
- int Begin = 0;
- int End = 0;
- string Tag;
- string Text;
- string result;
- string TagClose = "";
- CurrentStyle current_style = new CurrentStyle();
- CurrentStyle previos_style;
- current_style.Size = 10;
- current_style.Bold = false;
- current_style.Italic = false;
- current_style.Underline = false;
- current_style.Colour = Color.FromName("Black");
- current_style.Strike = false;
- current_style.Sub = false;
- current_style.Sup = false;
- Stack<CurrentStyle> style_stack = new Stack<CurrentStyle>();
- Begin = s.IndexOfAny(new char[1] { '<' }, Index);
- if (Begin == -1) result = s;
- else
- {
- result = "";
- while (Begin != -1)
- {
- if (Begin != 0 && Index == 0)
- {
- if (Index == 0)
- {
- result += s.Substring(Index, Begin);
- }
- }
- End = s.IndexOfAny(new char[1] { '>' }, Begin + 1);
- if (End == -1) break;
- Tag = s.Substring(Begin + 1, End - Begin - 1);
- bool CloseTag = Tag.StartsWith("/");
- if (CloseTag) Tag = Tag.Remove(0, 1);
- string[] items = Tag.Split(' ');
- Tag = items[0].ToUpper();
- TagClose = "";
- bool PutOnStack = true;
- if (!CloseTag)
- {
- current_style.LastTag = Tag;
- switch (Tag)
- {
- case "B":
- current_style.Bold = true;
- TagClose = "\\b ";
- break;
- case "I":
- current_style.Italic = true;
- TagClose = "\\i ";
- break;
- case "U":
- current_style.Underline = true;
- TagClose = "\\ul ";
- break;
- case "STRIKE":
- current_style.Strike = true;
- TagClose = "\\strike ";
- break;
- case "SUB":
- current_style.Sub = true;
- TagClose = "\\sub ";
- break;
- case "SUP":
- current_style.Sup = true;
- TagClose = "\\super ";
- break;
- case "BR":
- current_style.Sup = true;
- TagClose = "\\line ";
- break;
- case "FONT":
- {
- if (items.Length > 1)
- {
- string[] attrs = items[1].Split('=');
- if (attrs[0] == "color")
- {
- TagClose = "\\cf" + GetRTFColorFromTable(GetRTFColor(System.Drawing.ColorTranslator.FromHtml(attrs[1].Replace("\"", "")))) + " ";
- }
- }
- }
- /*current_style.Font = items[1];*/
- // ParseFont(items[1], current_style, out current_style);
- break;
- default:
- TagClose = Tag;
- PutOnStack = false;
- break;
- }
- if (PutOnStack) style_stack.Push(current_style);
- }
- else
- {
- if (style_stack.Count > 0)
- {
- previos_style = style_stack.Pop();
- #if false
- if (previos_style.LastTag != Tag)
- {
- throw new Exception("Unaligned HTML TAGS");
- }
- #endif
- switch (Tag)
- {
- case "B": TagClose = "\\b0 "; break;
- case "I": TagClose = "\\i0 "; break;
- case "U": TagClose = "\\ul0 "; break;
- case "STRIKE": TagClose = "\\strike0 "; break;
- case "SUB": TagClose = "\\nosupersub "; break;
- case "SUP": TagClose = "\\nosupersub "; break;
- case "FONT":
- TagClose = "\\cf" + GetRTFColorFromTable(GetRTFColor((textColor))) + " ";
- /*current_style.Font = items[1];*/
- // ParseFont(items[1], current_style, out current_style);
- break;
- default:
- throw new Exception("Unsupported HTML TAG");
- }
- current_style = previos_style;
- }
- }
- Index = End + 1;
- Begin = s.IndexOfAny(new char[1] { '<' }, Index);
- if (Begin == -1)
- {
- Text = s.Substring(Index);
- }
- else
- {
- Text = s.Substring(Index, Begin - Index);
- }
- result += TagClose + Text;
- }
- }
- return result;
- }
- private string StrToRTFUnicodeEx(string Value, TextRenderType textRenderType)
- {
- Value = StrToRTFUnicode(StrToRTFSlash(Value));
- switch (textRenderType)
- {
- case TextRenderType.HtmlParagraph:
- //TODO DETRAV RTF
- break;
- case TextRenderType.HtmlTags:
- Value = ParseHtmlTags(Value);
- break;
- }
- return Value;
- }
- private string StrToRTFUnicode(string Value)
- {
- StringBuilder Result = new StringBuilder(128);
- foreach (UInt16 c in Value)
- {
- if (c > 127)
- Result.Append("\\u").Append(c.ToString()).Append("\\'3f");
- else
- Result.Append((char)c);
- }
- return Result.ToString();
- }
- private void Prepare()
- {
- int i;
- ExportIEMObject Obj;
- for (int y = 0; y < matrix.Height; y++)
- for (int x = 0; x < matrix.Width; x++)
- {
- i = matrix.Cell(x, y);
- if (i != -1)
- {
- Obj = matrix.ObjectById(i);
- if (Obj.Counter != -1)
- {
- Obj.Counter = -1;
- if (Obj.Style != null)
- {
- GetRTFColorFromTable(GetRTFColor(Obj.Style.FillColor));
- GetRTFColorFromTable(GetRTFColor(Obj.Style.Border.LeftLine.Color));
- GetRTFColorFromTable(GetRTFColor(Obj.Style.Border.RightLine.Color));
- GetRTFColorFromTable(GetRTFColor(Obj.Style.Border.TopLine.Color));
- GetRTFColorFromTable(GetRTFColor(Obj.Style.Border.BottomLine.Color));
- GetRTFColorFromTable(GetRTFColor(Obj.Style.TextColor));
- GetRTFFontName(Obj.Style.Font.Name);
- }
- }
- }
- }
- }
- private string SetPageProp(int Page)
- {
- StringBuilder result = new StringBuilder(64);
- result.Append("\\pgwsxn").
- Append(((int)Math.Round(matrix.PageWidth(Page) * MargDivider)).ToString()).
- Append("\\pghsxn").
- Append(((int)Math.Round(matrix.PageHeight(Page) * MargDivider)).ToString()).
- Append("\\marglsxn").
- Append(((int)Math.Round(matrix.PageLMargin(Page) * MargDivider)).ToString()).
- Append("\\margrsxn").
- Append(((int)Math.Round(matrix.PageRMargin(Page) * MargDivider)).ToString()).
- Append("\\margtsxn").
- Append(((int)Math.Round(matrix.PageTMargin(Page) * MargDivider)).ToString()).
- Append("\\margbsxn").
- Append(((int)Math.Round(matrix.PageBMargin(Page) * MargDivider)).ToString()).
- Append(matrix.Landscape(Page) ? "\\lndscpsxn" : String.Empty);
- return result.ToString();
- }
- private void Write(Stream stream, string str)
- {
- byte[] buff = Converter.StringToByteArray(str);
- stream.Write(buff, 0, buff.Length);
- }
- private void WriteLine(Stream stream, string str)
- {
- Write(stream, str);
- Write(stream, "\r\n");
- }
- private Stream GetTempFileStream()
- {
- tempFile = Path.Combine(Config.GetTempFolder(), Path.GetRandomFileName());
- return new FileStream(tempFile, FileMode.Create);
- }
- private void DeleteTempFile()
- {
- if (File.Exists(tempFile))
- File.Delete(tempFile);
- }
- private string GetRTFString(ExportIEMObject obj, float drow)
- {
- ExportIEMStyle style = obj.Style;
- string text = obj.Text;
- TextRenderType textRenderType = obj.TextRenderType;
- StringBuilder CellsStream = new StringBuilder();
- CellsStream.Append(GetRTFHAlignment(style.HAlign));
- if (!String.IsNullOrEmpty(obj.URL))
- {
- CellsStream.Append("{\\field{\\*\\fldinst HYPERLINK \"" + obj.URL + "\"}{\\fldrslt");
- }
- else
- {
- CellsStream.Append("{");
- }
- CellsStream.Append("\\f").Append(GetRTFFontName(style.Font.Name));
- string s = StrToRTFUnicodeEx(ExportUtils.TruncReturns(text), textRenderType);
- double fh = style.Font.Height * dpiFactor * yDiv * 0.98;
- double lh = style.LineHeight * 8.0;
- if (s.Length > 0)
- {
- CellsStream.Append("\\fs").Append((Math.Round(style.Font.Size * 2)).ToString());
- CellsStream.Append(GetRTFFontStyle(style.Font.Style)).Append("\\cf");
- CellsStream.Append(GetRTFColorFromTable(GetRTFColor((style.TextColor))));
- CellsStream.Append(style.RTL ? "\\rtlch" : String.Empty);
- CellsStream.Append("\\sb").
- Append(((int)Math.Round(style.Padding.Top * yDiv)).ToString()).
- Append("\\sa").
- Append(((int)Math.Round(style.Padding.Bottom * yDiv)).ToString()).
- Append("\\li").
- Append(((int)Math.Round((style.Padding.Left) * Xdivider)).ToString()).
- Append("\\ri").
- Append(((int)Math.Round((style.Padding.Right) * Xdivider)).ToString()).
- Append("\\sl-").
- Append(((int)Math.Round((fh + lh))).ToString()).
- Append("\\slmult0 ");
- if (s.StartsWith("\t") && style.FirstTabOffset != 0)
- {
- // replace first tab symbol with \\fi
- s = "\\fi" + ((int)Math.Round((style.FirstTabOffset) * Xdivider)).ToString() + " " + s.Remove(0, 1);
- // convert multiline text to rtf paragraphs. \\fi can be applied to a paragraph only
- s = s.Replace("\\line\r\n\t", "\\par ");
- }
- CellsStream.Append(s);
- }
- else
- {
- int j = (int)(drow / FONT_DIVIDER);
- j = j > 20 ? 20 : j;
- CellsStream.Append("\\fs").Append(j.ToString());
- }
- if (!String.IsNullOrEmpty(obj.URL))
- {
- CellsStream.Append("}");
- }
- CellsStream.Append("\\cell}");
- return CellsStream.ToString();
- }
- private string GetRTFMetafile(ExportIEMObject Obj)
- {
- byte[] picbuff = new Byte[PIC_BUFF_SIZE];
- string scale = ((int)(100 / dpiFactor)).ToString();
- StringBuilder CellsStream = new StringBuilder(256);
- if (!String.IsNullOrEmpty(Obj.URL))
- {
- CellsStream.Append("{\\field{\\*\\fldinst HYPERLINK \"" + Obj.URL + "\"}{\\fldrslt");
- }
- else
- {
- CellsStream.Append("{");
- }
- Obj.PictureStream.Position = 0;
- if (pictures && Config.FullTrust && (imageFormat == RTFImageFormat.Metafile))
- {
- scale = (int.Parse(scale) * dpiFactor).ToString();
- CellsStream.Append("\\sb0\\li0\\sl0\\slmult0\\qc\\clvertalc {");
- float dx = Obj.Width * 15;
- float dy = Obj.Height * 15;
- CellsStream.Append("\\pict\\picw").Append(dx.ToString());
- CellsStream.Append("\\pich").Append(dy.ToString());
- CellsStream.Append("\\picscalex").Append(scale);
- CellsStream.Append("\\picscaley").Append(scale);
- CellsStream.Append("\\picwGoal").Append(Convert.ToString(dx));
- CellsStream.Append("\\pichGoal").Append(Convert.ToString(dy));
- CellsStream.Append("\\emfblip\r\n");
- int n;
- do
- {
- n = Obj.PictureStream.Read(picbuff, 0, PIC_BUFF_SIZE);
- for (int z = 0; z < n; z++)
- {
- CellsStream.Append(ExportUtils.XCONV[picbuff[z] >> 4]);
- CellsStream.Append(ExportUtils.XCONV[picbuff[z] & 0xF]);
- }
- CellsStream.Append("\r\n");
- }
- while (n == PIC_BUFF_SIZE);
- CellsStream.Append("}");
- }
- else if (pictures && (imageFormat != RTFImageFormat.Metafile))
- {
- CellsStream.Append("\\sb0\\li0\\sl0\\slmult0\\qc\\clvertalc {");
- float dx = (int)Obj.Width;
- float dy = (int)Obj.Height;
- CellsStream.Append("\\pict\\picw").Append(dx.ToString());
- CellsStream.Append("\\pich").Append(dy.ToString());
- CellsStream.Append("\\picscalex").Append(scale);
- CellsStream.Append("\\picscaley").Append(scale);
- CellsStream.Append("\\");
- CellsStream.Append(imageFormat == RTFImageFormat.Jpeg ? "jpegblip\r\n" : "pngblip\r\n");
- int n;
- do
- {
- n = Obj.PictureStream.Read(picbuff, 0, PIC_BUFF_SIZE);
- for (int z = 0; z < n; z++)
- {
- CellsStream.Append(ExportUtils.XCONV[picbuff[z] >> 4]);
- CellsStream.Append(ExportUtils.XCONV[picbuff[z] & 0xF]);
- }
- CellsStream.Append("\r\n");
- }
- while (n == PIC_BUFF_SIZE);
- CellsStream.Append("}");
- }
- if (!String.IsNullOrEmpty(Obj.URL))
- {
- CellsStream.Append("}");
- }
- CellsStream.Append("\\cell}\r\n");
- return CellsStream.ToString();
- }
- private string GetRTFHeader()
- {
- StringBuilder sb = new StringBuilder();
- sb.Append("{\\rtf1\\ansi");
- sb.Append("{\\fonttbl");
- for (int i = 0; i < fontTable.Count; i++)
- sb.Append("{\\f" + Convert.ToString(i) + " " + fontTable[i] + "}");
- sb.Append("}");
- sb.Append("{\\colortbl;");
- for (int i = 0; i < colorTable.Count; i++)
- sb.Append(colorTable[i]);
- sb.Append("}");
- return sb.ToString();
- }
- private string GetRTFMetaInfo()
- {
- StringBuilder buf = new StringBuilder(256);
- buf.Append("{\\info{\\title ").Append(StrToRTFUnicodeEx(Report.ReportInfo.Name, TextRenderType.Default)).
- Append("}{\\author ").Append(StrToRTFUnicodeEx(creator, TextRenderType.Default)).
- Append("}{\\creatim\\yr").Append(String.Format("{0:yyyy}", SystemFake.DateTime.Now)).
- Append("\\mo").Append(String.Format("{0:MM}", SystemFake.DateTime.Now)).
- Append("\\dy").Append(String.Format("{0:dd}", SystemFake.DateTime.Now)).
- Append("\\hr").Append(String.Format("{0:HH}", SystemFake.DateTime.Now)).
- Append("\\min").Append(String.Format("{0:mm}", SystemFake.DateTime.Now)).AppendLine("}}");
- return buf.ToString();
- }
- struct CurrentStyle
- {
- public int Size;
- public bool Bold;
- public bool Italic;
- public bool Underline;
- public bool Strike;
- public bool Sub;
- public bool Sup;
- public Color Colour;
- public string LastTag;
- };
- #region Export RichObject to RTF
- private RichDocument rtf;
- private RunFormat current_format;
- private RichTextParser.ParagraphFormat.HorizontalAlign prev_align = RichTextParser.ParagraphFormat.HorizontalAlign.Left;
- private string HorizontalAlignCode(RichTextParser.ParagraphFormat.HorizontalAlign align)
- {
- switch (align)
- {
- case RichTextParser.ParagraphFormat.HorizontalAlign.Centered: return "\\qc";
- case RichTextParser.ParagraphFormat.HorizontalAlign.Left: return "\\ql";
- case RichTextParser.ParagraphFormat.HorizontalAlign.Right: return "\\qr";
- case RichTextParser.ParagraphFormat.HorizontalAlign.Justified: return "\\qj";
- case RichTextParser.ParagraphFormat.HorizontalAlign.Distributed: return "\\qd";
- case RichTextParser.ParagraphFormat.HorizontalAlign.Kashida: return "\\qk";
- case RichTextParser.ParagraphFormat.HorizontalAlign.Thai: return "\\qt";
- default: return "";
- }
- }
- private string VerticalAlignCode(Column.VertAlign valign)
- {
- switch (valign)
- {
- case Column.VertAlign.Top: return "\\clvertalt";
- case Column.VertAlign.Center: return "\\clvertalc";
- case Column.VertAlign.Bottom: return "\\clvertalb";
- default: return "";
- }
- }
- private void SavePargraph(StringBuilder s, RichTextParser.Paragraph par, bool InTable, float Left)
- {
- char lineBreakSumb = ' ';
- bool skipSpace = false;
- if (par.runs.Count == 0)
- {
- if (!InTable)
- s.Append("\\pard\\par\n");
- else
- s.Append("\\pard\\intbl\\cell\n");
- }
- else
- {
- if (InTable)
- s.Append("\\intbl\n");
- if (par.format.align != prev_align)
- {
- s.AppendFormat("{0} ", HorizontalAlignCode(par.format.align));
- prev_align = par.format.align;
- }
- // add tab stops with taking into consideration left padding of object
- if (par.format.tab_positions != null && par.format.tab_positions.Count > 0)
- {
- foreach (float tab in par.format.tab_positions)
- s.Append($"\\tx{Math.Round(tab + Left * Xdivider)}");
- }
- foreach (Run r in par.runs)
- {
- RunFormat fmt = r.format;
- if (current_format.bold != fmt.bold)
- {
- s.Append(fmt.bold ? "\\b" : "\\b0");
- current_format.bold = fmt.bold;
- }
- if (current_format.strike != fmt.strike)
- {
- s.Append(fmt.strike ? "\\strike" : "\\strike0");
- current_format.strike = fmt.strike;
- }
- if (current_format.underline != fmt.underline)
- {
- s.Append(fmt.underline ? "\\ul" : "\\ulnone");
- current_format.underline = fmt.underline;
- }
- if (current_format.italic != fmt.italic)
- {
- s.Append(fmt.italic ? "\\i" : "\\i0");
- current_format.italic = fmt.italic;
- }
- if (current_format.color.ToArgb() != fmt.color.ToArgb())
- {
- string color_def = GetRTFColor(fmt.color);
- int i = colorTable.IndexOf(color_def);
- if (i != -1)
- {
- s.AppendFormat("\\cf{0}", i + 1);
- }
- else
- {
- colorTable.Add(color_def);
- s.AppendFormat("\\cf{0}", colorTable.Count);
- }
- current_format.color = fmt.color;
- }
- if (current_format.font_size != fmt.font_size)
- {
- s.AppendFormat("\\fs{0}", fmt.font_size);
- current_format.font_size = fmt.font_size;
- }
- if (current_format.font_idx != fmt.font_idx)
- {
- string font_name = rtf.font_list[(int)fmt.font_idx].FontName;
- int i = fontTable.IndexOf(font_name);
- if (i != -1)
- {
- s.AppendFormat("\\f{0}", i);
- }
- else
- {
- fontTable.Add(font_name);
- s.AppendFormat("\\f{0}", (fontTable.Count - 1));
- }
- current_format.font_idx = fmt.font_idx;
- }
- // avoid line break second symbol \r\n
- if ((r.text == "\r" || r.text == "\n") && (lineBreakSumb == r.text[0] || lineBreakSumb == ' '))
- {
- s.Append("\\par ");
- lineBreakSumb = r.text[0];
- skipSpace = true;
- }
- else if (r.text == "\t")
- {
- s.Append("\\tab ");
- skipSpace = true;
- }
- else
- {
- if (!skipSpace)
- s.Append(" ");
- s.Append(StrToRTFUnicode(r.text));
- skipSpace = false;
- }
- }
- if (!InTable)
- s.Append("\\par\n");
- else
- s.Append("\\cell\n");
- }
- }
- private void SaveColumn(StringBuilder s, Column col)
- {
- // "clcbpat" - background color
- s.AppendFormat("{0}\\cellx{1}", VerticalAlignCode(col.valign), col.Width);
- }
- private void SaveRow(StringBuilder s, TableRow row, bool InTable)
- {
- s.AppendFormat("\\trgaph{0}\\trrh{1}\\trpaddl{2}\\trpaddr{3}",
- row.trgaph, row.height, row.default_pad_left, row.default_pad_right);
- foreach (RichObjectSequence seq in row.cells)
- {
- SaveSequence(s, seq, true, 0f);
- if (!InTable)
- s.AppendLine("\\cell");
- else
- s.AppendLine("\\nestcell");
- }
- if (!InTable)
- s.Append("\\row");
- else
- s.Append("\\nestrow");
- }
- private void SaveTable(StringBuilder s, RichTextParser.Table tbl, bool InTable)
- {
- s.Append("\\trowd");
- foreach (Column col in tbl.columns)
- SaveColumn(s, col);
- foreach (TableRow row in tbl.rows)
- SaveRow(s, row, InTable);
- }
- private void SavePicture(StringBuilder CellsStream, Picture pic, bool InTable)
- {
- CellsStream.Append("\\sb0\\li0\\sl0\\slmult0\\qc\\clvertalc {");
- float dx = (int)pic.width;
- float dy = (int)pic.height;
- CellsStream.Append("\\pict\\picw").Append(dx.ToString());
- CellsStream.Append("\\pich").Append(dy.ToString());
- CellsStream.Append("\\picscalex").Append(pic.scalex);
- CellsStream.Append("\\picscaley").Append(pic.scaley);
- System.Drawing.Imaging.ImageFormat format;
- if (imageFormat == RTFImageFormat.Jpeg)
- {
- CellsStream.Append("\\jpegblip\r\n");
- format = System.Drawing.Imaging.ImageFormat.Jpeg;
- }
- else
- {
- CellsStream.Append("\\pngblip\r\n");
- format = System.Drawing.Imaging.ImageFormat.Png;
- }
- int n;
- byte[] picbuff = new Byte[PIC_BUFF_SIZE];
- MemoryStream pic_stream = new MemoryStream();
- pic.image.Save(pic_stream, format);
- pic_stream.Seek(0, SeekOrigin.Begin);
- do
- {
- n = pic_stream.Read(picbuff, 0, PIC_BUFF_SIZE);
- for (int z = 0; z < n; z++)
- {
- CellsStream.Append(ExportUtils.XCONV[picbuff[z] >> 4]);
- CellsStream.Append(ExportUtils.XCONV[picbuff[z] & 0xF]);
- }
- CellsStream.Append("\r\n");
- }
- while (n == PIC_BUFF_SIZE);
- CellsStream.Append("}");
- }
- private void SaveSequence(StringBuilder s, RichObjectSequence seq, bool InTable, float Left)
- {
- for (int i = 0; i < seq.objects.Count; i++)
- {
- if (!InTable && i == seq.objects.Count - 1)
- InTable = true;
- switch (seq.objects[i].type)
- {
- case RichTextParser.RichObject.Type.Paragraph:
- SavePargraph(s, seq.objects[i].paragraph, InTable, Left);
- break;
- case RichTextParser.RichObject.Type.Picture:
- SavePicture(s, seq.objects[i].picture, InTable);
- break;
- case RichTextParser.RichObject.Type.Table:
- SaveTable(s, seq.objects[i].table, InTable);
- break;
- }
- }
- }
- private void TranslateEmbeddedRTF(ExportIEMObject Obj, StringBuilder CellsStream)
- {
- current_format.color = Color.FromArgb(0, 0, 0, 0);
- current_format.BColor = Color.FromArgb(0, 0, 0, 0);
- current_format.FillColor = Color.FromArgb(0, 0, 0, 0);
- current_format.bold = false;
- current_format.strike = false;
- current_format.italic = false;
- current_format.underline = false;
- current_format.font_size = 0;
- current_format.font_idx = 65536;
- current_format.script_type = RunFormat.ScriptType.PlainText;
- using (RTF_DocumentParser parser = new RTF_DocumentParser())
- {
- parser.Load(Obj.Text);
- rtf = parser.Document;
- }
- foreach (Page page in rtf.pages)
- {
- CellsStream.AppendFormat(@"\margl{0}\margr{1}\margt{2}\margb{3}",
- 200, // page.margin_left,
- page.margin_right,
- 600, // page.margin_top,
- page.margin_bottom);
- CellsStream.Append($"\\li{Math.Round(Obj.Left * Xdivider)}");
- SaveSequence(CellsStream, page.sequence, false, Obj.Left);
- }
- }
- private void AddWatermark(Stream stream)
- {
- Watermark watermark = Report.PreparedPages.GetPage(0).Watermark;
- if (watermark.Text != "")
- {
- int OLEColor = watermark.TextObject.TextColor.R + (watermark.TextObject.TextColor.G * 256) + (watermark.TextObject.TextColor.B * 256 * 256);
- double height = watermark.Font.SizeInPoints * 19;
- double width = 0;
- int rotation = 0;
- bool needSwapHeigtAndWidth = false;
- int countUpperLetter = 0;
- foreach (var c in watermark.Text)
- {
- if (char.IsUpper(c))
- countUpperLetter++;
- }
- width = (watermark.Text.Length - countUpperLetter) * (height / 2.5) + countUpperLetter * (height / 2 * 1.28);
- switch (watermark.TextRotation)
- {
- case WatermarkTextRotation.Horizontal:
- {
- rotation = 0;
- break;
- }
- case WatermarkTextRotation.Vertical:
- {
- rotation = -5900000;
- needSwapHeigtAndWidth = true;
- break;
- }
- case WatermarkTextRotation.ForwardDiagonal:
- {
- if (matrix.Landscape(1))
- rotation = -2400000;
- else
- {
- rotation = -3600000;
- needSwapHeigtAndWidth = true;
- }
- break;
- }
- case WatermarkTextRotation.BackwardDiagonal:
- {
- if (!matrix.Landscape(1))
- {
- rotation = 3600000;
- needSwapHeigtAndWidth = true;
- }
- else
- rotation = 2250000;
- break;
- }
- }
- if (needSwapHeigtAndWidth)
- {
- double temp = height;
- height = width;
- width = temp;
- }
- Write(stream,
- @"\headery0{\header\pard\plain\itap0\s0\aspalpha\aspnum\adjustright\ltrpar\li0\lin0\ri0\rin0\ql\faauto\rtlch\afs24\ltrch\fs24{\rtlch\afs24\ltrch\fs2" + "\n" +
- @"{\shp{\*\shpinst\shplid2051\shpleft0\shpright" + width + @"\shptop0\shpbottom" + height + @"\shpfhdr1\shpz2\shpfblwtxt0" + "\n" +
- @"\shpbxmargin\shpbxignore\shpbymargin\shpbyignore\shpwr3\shpwrk0{\sp{\sn fLayoutInCell}" + "\n" +
- @"{\sv 1}" + "\n" +
- @"}" + "\n" +
- @"{\sp{\sn rotation}" + "\n" +
- @"{\sv " + rotation + @"}" + "\n" +
- @"}" + "\n" +
- @"{\sp{\sn fFlipH}" + "\n" +
- @"{\sv 0}" + "\n" +
- @"}" + "\n" +
- @"{\sp{\sn fFlipV}" + "\n" +
- @"{\sv 0}" + "\n" +
- @"}" + "\n" +
- @"{\sp{\sn gtextUNICODE}" + "\n" +
- @"{\sv " + StrToRTFUnicodeEx(ExportUtils.TruncReturns(watermark.Text), TextRenderType.Default) + @"}" + "\n" +
- @"}" + "\n" +
- @"{\sp{\sn gtextSize}" + "\n" +
- @"{\sv 123932160}" + "\n" +
- @"}" + "\n" +
- @"{\sp{\sn gtextFont}" + "\n" +
- @"{\sv " + watermark.Font.Name + @"}" + "\n" +
- @"}" + "\n" +
- @"{\sp{\sn fGtext}" + "\n" +
- @"{\sv 1}" + "\n" +
- @"}" + "\n" +
- @"{\sp{\sn fillColor}" + "\n" +
- @"{\sv " + OLEColor + @"}" + "\n" +
- @"}" + "\n" +
- @"{\sp{\sn fillOpacity}" + "\n" +
- @"{\sv 32768}" + "\n" +
- @"}" + "\n" +
- @"{\sp{\sn fFilled}" + "\n" +
- @"{\sv 1}" + "\n" +
- @"}" + "\n" +
- @"{\sp{\sn fLine}" + "\n" +
- @"{\sv 0}" + "\n" +
- @"}" + "\n" +
- @"{\sp{\sn wzName}" + "\n" +
- @"{\sv PowerPlusWaterMarkObject100001}" + "\n" +
- @"}" + "\n" +
- @"{\sp{\sn posh}" + "\n" +
- @"{\sv 2}" + "\n" +
- @"}" + "\n" +
- @"{\sp{\sn posrelh}" + "\n" +
- @"{\sv 0}" + "\n" +
- @"}" + "\n" +
- @"{\sp{\sn posv}" + "\n" +
- @"{\sv 2}" + "\n" +
- @"}" + "\n" +
- @"{\sp{\sn posrelv}" + "\n" +
- @"{\sv 0}" + "\n" +
- @"}" + "\n" +
- @"{\sp{\sn shapeType}" + "\n" +
- @"{\sv 136}" + "\n" +
- @"}" + "\n" +
- @"}" + "\n" +
- @"{\shprslt\par\pard" + "\n" +
- @"\par}" + "\n" +
- @"}" + "\n" +
- @"}" + "\n" +
- @"{\rtlch\afs24\ltrch\fs24\par}" + "\n" +
- @"}"
- );
- }
- else if (watermark.Image != null)
- {
- byte[] buffer;
- int pictureContrast = 60000;
- int pictureBrightness = -20000;
- using (var memStream = new MemoryStream())
- {
- watermark.Image.Save(memStream, watermark.PictureObject.ImageFormat);
- buffer = memStream.ToArray();
- }
- string hex = BitConverter.ToString(buffer, 0).Replace("-", string.Empty);
- string picture = @"{\sv{\pict\picscalex382\picscaley382\piccropl0\piccropr0\piccropt0\piccropb0";
- float widthPic = (float)watermark.Image.Width / watermark.Image.HorizontalResolution;
- float heightPic = (float)watermark.Image.Height / watermark.Image.VerticalResolution;
- picture += @"\picw" + (int)(widthPic * 2540);
- picture += @"\pich" + (int)(heightPic * 2540);
- picture += @"\picwgoal" + (int)(widthPic * 1440);
- picture += @"\pichgoal" + (int)(heightPic * 1440);
- if (watermark.PictureObject.ImageFormat == System.Drawing.Imaging.ImageFormat.Jpeg)
- {
- picture += @"\jpegblip";
- }
- else if (watermark.PictureObject.ImageFormat == System.Drawing.Imaging.ImageFormat.Png)
- picture += @"\pngblip";
- int width;
- int height;
- switch (watermark.ImageSize)
- {
- case WatermarkImageSize.Zoom:
- float scaleX = matrix.PageWidth(0) / (watermark.Image.Width / 3.8f);
- float scaleY = matrix.PageHeight(0) / (watermark.Image.Height / 3.8f);
- width = (int)(watermark.Image.Width / 3.8f * Math.Min(scaleY, scaleX) * MargDivider);
- height = (int)(watermark.Image.Height / 3.8f * Math.Min(scaleY, scaleX) * MargDivider);
- break;
- case WatermarkImageSize.Stretch:
- width = (int)((matrix.PageWidth(0) - matrix.PageLMargin(0) - matrix.PageRMargin(0)) * MargDivider);
- height = (int)((matrix.PageHeight(0) - matrix.PageTMargin(0) - matrix.PageBMargin(0)) * MargDivider);
- break;
- default:
- width = (int)(watermark.Image.Width / 3.8f * MargDivider);
- height = (int)(watermark.Image.Height / 3.8f * MargDivider);
- break;
- }
- pictureContrast -= (int)(watermark.ImageTransparency * 100 * 600);
- pictureBrightness += (int)(watermark.ImageTransparency * 100 * 600);
- WriteLine(stream,
- @"\headery0{\header\pard\plain\itap0\s0\aspalpha\aspnum\adjustright\ltrpar\li0\lin0\ri0\rin0\ql\faauto\rtlch\afs24\ltrch\fs24{\rtlch\afs24\ltrch\fs2" + "\n" +
- @"{\shp{\*\shpinst\shpleft-0\shptop-0\shpright" + width + @"\shpbottom" + height + @"\shpfhdr1\shpbxcolumn\shpbxignore\shpbypara\shpbyignore\shpwr3\shpwrk0\shpfblwtxt1\shpz2\shplid2054{\sp{\sn shapeType}" + "\n" +
- @"{\sv 75}" + "\n" +
- @"}" + "\n" +
- @"{\sp{\sn fFlipH}" + "\n" +
- @"{\sv 0}" + "\n" +
- @"}" + "\n" +
- @"{\sp{\sn fFlipV}" + "\n" +
- @"{\sv 0}" + "\n" +
- @"}" + "\n" +
- @"{\sp{\sn pib}" + "\n"
- );
- WriteLine(stream, picture + @"\bliptag-1263442625{\*\blipuid b4b1653f1c65f1c28791719870001760}");
- WriteLine(stream, hex + "}");
- WriteLine(stream,
- @"}" + "\n" +
- @"}" + "\n" +
- @"{\sp{\sn pibFlags}" + "\n" +
- @"\sv 2}" + "\n" +
- @"}" + "\n" +
- @"{\sp{\sn pictureContrast}" + "\n" +
- @"{\sv " + pictureContrast + "}" + "\n" +
- @"}" + "\n" +
- @"{\sp{\sn pictureBrightness}" + "\n" +
- @"{\sv " + pictureBrightness + "}" + "\n" +
- @"}" + "\n" +
- @"{\sp{\sn fLine}" + "\n" +
- @"{\sv 0}" + "\n" +
- @"}" + "\n" +
- @"{\sp{\sn wzName}" + "\n" +
- @"{\sv WordPictureWatermark85221080}" + "\n" +
- @"}" + "\n" +
- @"{\sp{\sn posh}" + "\n" +
- @"{\sv 2}" + "\n" +
- @"}" + "\n" +
- @"{\sp{\sn posrelh}" + "\n" +
- @"{\sv 0}" + "\n" +
- @"}" + "\n" +
- @"{\sp{\sn posv}" + "\n" +
- @"{\sv 2}" + "\n" +
- @"}" + "\n" +
- @"{\sp{\sn posrelv}" + "\n" +
- @"{\sv 0}" + "\n" +
- @"}" + "\n" +
- @"{\sp{\sn dhgt}" + "\n" +
- @"{\sv 251660288}" + "\n" +
- @"}" + "\n" +
- @"{\sp{\sn fLayoutInCell}" + "\n" +
- @"{\sv 0}" + "\n" +
- @"}" + "\n" +
- @"{\sp{\sn fBehindDocument}" + "\n" +
- @"{\sv 1}" + "\n" +
- @"}" + "\n" +
- @"{\sp{\sn fLayoutInCell}" + "\n" +
- @"{\sv 0}" + "\n" +
- @"}" + "\n" +
- @"}" + "\n" +
- @"{\shprslt" + "\n" +
- @"}" + "\n" +
- @"}" + "\n" +
- @"{\rtlch\afs24\ltrch\fs24\par}" + "\n" +
- @"}"
- );
- }
- }
- #endregion
- private void ExportRTF(Stream stream)
- {
- int i, j, x, fx, fy, dx, dy, pbk;
- int dcol, drow, xoffs;
- ExportIEMObject Obj;
- Prepare();
- //Write a header is below now
- pbk = 0;
- Write(stream, SetPageProp(pbk));
- if (Report.PreparedPages.GetPage(0).Watermark.Enabled)
- AddWatermark(stream);
- if (ShowProgress)
- Config.ReportSettings.OnProgress(Report, res.Get("SavePage") + " " + (pbk + 1).ToString());
- for (int y = 0; y < matrix.Height - 1; y++)
- {
- if (pageBreaks)
- if (pbk < matrix.PagesCount)
- if (matrix.PageBreak(pbk) <= matrix.YPosById(y))
- {
- // WriteLine(stream, "\\pagebb\\sect");
- WriteLine(stream, "\\pard\\sect");
- pbk++;
- if (pbk < matrix.PagesCount)
- Write(stream, SetPageProp(pbk));
- if (ShowProgress)
- Config.ReportSettings.OnProgress(Report,
- res.Get("SavePage") + " " + (pbk + 1).ToString());
- }
- if (pbk == matrix.PagesCount - 1)
- yDiv = Ydivider3;
- else if (pbk > 0)
- yDiv = Ydivider1;
- else
- yDiv = Ydivider2;
- drow = (int)Math.Round((matrix.YPosById(y + 1) - matrix.YPosById(y)) * 14.90);
- StringBuilder buff = new StringBuilder(512);
- buff.Append(autoSize ? "\\trrh" : "\\trrh-" + (drow).ToString() + "\\trgaph15");
- if (exportLocale)
- {
- if (localization != null)
- buff.Append($"\\lang{localization.LCID}");
- else
- buff.Append($"\\lang{CultureInfo.GetCultureInfoByIetfLanguageTag(Res.LocaleName.Substring(0, 2)).LCID}");
- }
- xoffs = (int)Math.Round(matrix.XPosById(0));
- StringBuilder CellsStream = new StringBuilder();
- for (x = 0; x <= matrix.Width - 2; x++)
- {
- i = matrix.Cell(x, y);
- if (i != -1)
- {
- Obj = matrix.ObjectById(i);
- matrix.ObjectPos(i, out fx, out fy, out dx, out dy);
- if (Obj.Counter == -1)
- {
- if (dy > 1)
- buff.Append("\\clvmgf");
- if (Obj.Style != null)
- {
- if (Obj.Style.FillColor != Color.Transparent)
- buff.Append("\\clcbpat").Append(GetRTFColorFromTable(GetRTFColor(Obj.Style.FillColor)));
- buff.Append(GetRTFVAlignment(Obj.Style.VAlign)).Append(GetRTFBorders(Obj.Style))
- .Append("\\cltxlrtb");
- if (Obj.Style.Angle == 90)
- buff.Append(" \\cltxtbrl ");
- else if (Obj.Style.Angle == 270)
- buff.Append(" \\cltxbtlr ");
- }
- dcol = (int)Math.Round((Obj.Left + Obj.Width - xoffs) * Xdivider);
- buff.Append("\\cellx").Append(dcol.ToString());
- if (Obj.IsText)
- {
- if (Obj.IsRichText)
- {
- // Write a rich text
- StringBuilder EmneddedRichStream = new StringBuilder();
- TranslateEmbeddedRTF(Obj, EmneddedRichStream);
- string debug = EmneddedRichStream.ToString();
- CellsStream.Append(EmneddedRichStream);
- }
- else
- {
- // Write a text
- textColor = Obj.Style.TextColor;
- CellsStream.AppendLine(GetRTFString(Obj, drow));
- }
- }
- else
- {
- // Write a picture
- CellsStream.Append(GetRTFMetafile(Obj));
- }
- Obj.Counter = y + 1;
- }
- else
- {
- if ((dy > 1) && (Obj.Counter != y + 1))
- {
- buff.Append("\\clvmrg").
- Append((Obj.Style != null) ? GetRTFBorders(Obj.Style) : String.Empty).
- Append("\\cltxlrtb");
- dcol = (int)Math.Round((Obj.Left + Obj.Width - xoffs) * Xdivider);
- buff.Append("\\cellx").Append(dcol.ToString());
- j = (int)(drow / FONT_DIVIDER);
- j = j > 20 ? 20 : j;
- CellsStream.Append("{\\fs").Append(j.ToString());
- CellsStream.AppendLine("\\cell}");
- Obj.Counter = y + 1;
- }
- }
- }
- }
- if (CellsStream.Length > 0)
- {
- WriteLine(stream, "\\trowd");
- WriteLine(stream, buff.ToString());
- WriteLine(stream, "\\pard\\intbl");
- WriteLine(stream, CellsStream.ToString());
- WriteLine(stream, "\\pard\\intbl{\\trowd");
- WriteLine(stream, buff.ToString());
- WriteLine(stream, "\\row}");
- }
- }
- WriteLine(stream, "\\pard\\fs2\\par}"); //insert empty text with minimum size for avoiding creating a new page
- WriteLine(stream, "}");
- #region Write a header
- byte[] b = new byte[stream.Length];
- stream.Position = 0;
- stream.Read(b, 0, (int)stream.Length);
- stream.SetLength(0);
- WriteLine(stream, GetRTFHeader());
- WriteLine(stream, GetRTFMetaInfo());
- stream.Write(b, 0, b.Length);
- #endregion
- stream.Flush();
- }
- #endregion
- #region Protected Methods
- /// <inheritdoc/>
- protected override string GetFileFilter()
- {
- return new MyRes("FileFilters").Get("RtfFile");
- }
- /// <inheritdoc/>
- protected override void Start()
- {
- base.Start();
- colorTable = new List<string>();
- fontTable = new List<string>();
- matrix = new ExportMatrix();
- if (wysiwyg)
- matrix.Inaccuracy = 0.5f;
- else
- matrix.Inaccuracy = 10;
- matrix.RotatedAsImage = false;
- matrix.PlainRich = false;
- matrix.AreaFill = true;
- matrix.CropAreaFill = false;
- matrix.ShowProgress = ShowProgress;
- matrix.Report = Report;
- if (printOptimized)
- matrix.PrintOptimized = printOptimized;
- if (imageFormat != RTFImageFormat.Metafile)
- {
- matrix.FullTrust = false;
- matrix.ImageFormat = imageFormat == RTFImageFormat.Jpeg ?
- System.Drawing.Imaging.ImageFormat.Jpeg : System.Drawing.Imaging.ImageFormat.Png;
- matrix.JpegQuality = jpegQuality;
- }
- matrix.KeepRichText = keepRichText;
- }
- /// <inheritdoc/>
- protected override void ExportPageBegin(ReportPage page)
- {
- base.ExportPageBegin(page);
- matrix.AddPageBegin(page);
- }
- /// <inheritdoc/>
- protected override void ExportBand(BandBase band)
- {
- base.ExportBand(band);
- matrix.AddBand(band, this);
- }
- /// <inheritdoc/>
- protected override void ExportPageEnd(ReportPage page)
- {
- matrix.AddPageEnd(page);
- }
- /// <inheritdoc/>
- protected override void Finish()
- {
- matrix.Prepare();
- ExportRTF(Stream);
- }
- #endregion
- /// <inheritdoc/>
- public override void Serialize(FRWriter writer)
- {
- base.Serialize(writer);
- writer.WriteBool("Wysiwyg", Wysiwyg);
- writer.WriteBool("PageBreaks", PageBreaks);
- writer.WriteBool("Pictures", Pictures);
- writer.WriteValue("ImageFormat", ImageFormat);
- writer.WriteValue("KeeoRich", EmbedRichObject);
- writer.WriteValue("Locale", Locale);
- writer.WriteBool("ExportLocale", ExportLocale);
- }
- /// <summary>
- /// Initializes a new instance of the <see cref="RTFExport"/> class.
- /// </summary>
- public RTFExport()
- {
- dpiFactor = 96f / DrawUtils.ScreenDpi;
- pageBreaks = true;
- wysiwyg = true;
- printOptimized = false;
- autoSize = false;
- pictures = true;
- imageFormat = RTFImageFormat.Metafile;
- jpegQuality = 90;
- ExportLocale = false;
- creator = "FastReport";
- res = new MyRes("Export,Misc");
- }
- }
- }
|