public RtfImporter(Stream stream, IDocument document) { this.stream = stream; this.rtfParser = new Common.Text.RTF.Parser.RTF(stream); this.document = document; _style = new RtfSectionStyle(); }
// To allow us to keep track of the sections and revert formatting // as we go in and out of sections of the document. private void HandleGroup(Common.Text.RTF.Parser.RTF rtf) { //start group - save the current formatting on to a stack //end group - go back to the formatting at the current group if (_sectionStack == null) { _sectionStack = new Stack(); } if (rtf.Major == Major.BeginGroup) { _sectionStack.Push(_style.Clone()); //spec specifies resetting unicode ignore at begin group as an attempt at error //recovery. _skipCount = 0; } else if (rtf.Major == Major.EndGroup) { if (_sectionStack.Count > 0) { FlushText(rtf, false); _style = (RtfSectionStyle)_sectionStack.Pop(); } } }
private void HandleText(Common.Text.RTF.Parser.RTF rtf) { var str = rtf.EncodedText; //todo - simplistically skips characters, should skip bytes? if (_skipCount > 0 && str.Length > 0) { int iToRemove = Math.Min(_skipCount, str.Length); str = str.Substring(iToRemove); _skipCount -= iToRemove; } /* * if ((RTF.StandardCharCode)rtf.Minor != RTF.StandardCharCode.nothing) { * rtf_line.Append(rtf_text_map[(RTF.StandardCharCode)rtf.Minor]); * } else { * if ((int)rtf.Major > 31 && (int)rtf.Major < 128) { * rtf_line.Append((char)rtf.Major); * } else { * //rtf_line.Append((char)rtf.Major); * Console.Write("[Literal:0x{0:X2}]", (int)rtf.Major); * } * } */ if (_style.Visible) { _line.Append(str); } }
private void SpecialChar(Common.Text.RTF.Parser.RTF rtf) { switch (rtf.Minor) { case Minor.Page: case Minor.Sect: case Minor.Row: case Minor.Line: case Minor.Par: { FlushText(rtf, true); break; } case Minor.Cell: { Trace.Write(" "); break; } case Minor.NoBrkSpace: { Trace.Write(" "); break; } case Minor.Tab: { _line.Append("\t"); // FlushText (rtf, false); break; } case Minor.NoReqHyphen: case Minor.NoBrkHyphen: { _line.Append("-"); // FlushText (rtf, false); break; } case Minor.Bullet: { Trace.WriteLine("*"); break; } case Minor.WidowCtrl: break; case Minor.EmDash: { _line.Append("\u2014"); break; } case Minor.EnDash: { _line.Append("\u2013"); break; } /* * case RTF.Minor.LQuote: { * Console.Write("\u2018"); * break; * } * * case RTF.Minor.RQuote: { * Console.Write("\u2019"); * break; * } * * case RTF.Minor.LDblQuote: { * Console.Write("\u201C"); * break; * } * * case RTF.Minor.RDblQuote: { * Console.Write("\u201D"); * break; * } */ default: { // Console.WriteLine ("skipped special char: {0}", rtf.Minor); // rtf.SkipGroup(); break; } } }
private void HandleControl(Common.Text.RTF.Parser.RTF rtf) { switch (rtf.Major) { case Major.Unicode: { switch (rtf.Minor) { case Minor.UnicodeCharBytes: { _style.SkipWidth = rtf.Param; break; } case Minor.UnicodeChar: { FlushText(rtf, false); _skipCount += _style.SkipWidth; _line.Append((char)rtf.Param); break; } } break; } case Major.Destination: { // Console.Write("[Got Destination control {0}]", rtf.Minor); rtf.SkipGroup(); break; } case Major.PictAttr: if (rtf.Picture != null && rtf.Picture.IsValid()) { //var line = document.GetLine (_cursorY); //document.InsertPicture (line, 0, rtf.Picture); _cursorX++; FlushText(rtf, true); rtf.Picture = null; } break; case Major.CharAttr: { FlushText(rtf, false); switch (rtf.Minor) { case Minor.ForeColor: { var color = Color.GetColor(rtf, rtf.Param); if (color != null) { if (color.Red == -1 && color.Green == -1 && color.Blue == -1) { this._style.Color = ForeColor; } else { this._style.Color = XD.Color.FromBytes((byte)color.Red, (byte)color.Green, (byte)color.Blue); } } break; } case Minor.FontSize: { this._style.FontSize = rtf.Param / 2; break; } case Minor.FontNum: { var font = Font.GetFont(rtf, rtf.Param); if (font != null) { this._style.FontName = font.Name; } break; } case Minor.Plain: { _style.SectionAttribute = SectionAttribute.Regular; break; } case Minor.Bold: { if (rtf.Param == Common.Text.RTF.Parser.RTF.NoParam) { _style.SectionAttribute |= SectionAttribute.Bold; } else { _style.SectionAttribute &= ~SectionAttribute.Bold; } break; } case Minor.Italic: { if (rtf.Param == Common.Text.RTF.Parser.RTF.NoParam) { _style.SectionAttribute |= SectionAttribute.Italic; } else { _style.SectionAttribute &= ~SectionAttribute.Italic; } break; } case Minor.StrikeThru: { if (rtf.Param == Common.Text.RTF.Parser.RTF.NoParam) { _style.SectionAttribute |= SectionAttribute.Strikeout; } else { _style.SectionAttribute &= ~SectionAttribute.Strikeout; } break; } case Minor.Underline: { if (rtf.Param == Common.Text.RTF.Parser.RTF.NoParam) { _style.SectionAttribute |= SectionAttribute.Underline; } else { _style.SectionAttribute &= ~SectionAttribute.Underline; } break; } case Minor.SubScrShrink: { if (rtf.Param == Common.Text.RTF.Parser.RTF.NoParam) { _style.SectionAttribute |= SectionAttribute.Subscript; } else { _style.SectionAttribute &= ~SectionAttribute.Subscript; } break; } case Minor.SuperScrShrink: { if (rtf.Param == Common.Text.RTF.Parser.RTF.NoParam) { _style.SectionAttribute |= SectionAttribute.Superscript; } else { _style.SectionAttribute &= ~SectionAttribute.Superscript; } break; } case Minor.Invisible: { _style.Visible = false; break; } case Minor.NoUnderline: { _style.SectionAttribute &= ~SectionAttribute.Underline; break; } } break; } case Major.ParAttr: { switch (rtf.Minor) { case Minor.ParDef: FlushText(rtf, false); _style.ParLineLeftIndent = 0; _style.Align = Alignment.Start; break; case Minor.LeftIndent: _style.ParLineLeftIndent = (int)(((float)rtf.Param / 1440.0F) * DpiX + 0.5F); break; case Minor.QuadCenter: FlushText(rtf, false); _style.Align = Alignment.Center; break; case Minor.QuadJust: FlushText(rtf, false); _style.Align = Alignment.Center; break; case Minor.QuadLeft: FlushText(rtf, false); _style.Align = Alignment.Start; break; case Minor.QuadRight: FlushText(rtf, false); _style.Align = Alignment.End; break; } break; } case Major.SpecialChar: { //Console.Write("[Got SpecialChar control {0}]", rtf.Minor); SpecialChar(rtf); break; } } }