/// <summary> /// 从RTF文档生成文本文档内容对象 /// </summary> /// <param name="document">文本文档对象</param> /// <returns>包含生成的内容对象的列表</returns> public void ReadContent(DomDocument document) { document.Clear(); if (this.EnableDocumentSetting) { XPageSettings ps = new XPageSettings(); ps.Landscape = _RTFDocument.Landscape; ps.LeftMargin = GraphicsUnitConvert.FromTwips( _RTFDocument.LeftMargin, GraphicsUnit.Document) / 3; ps.TopMargin = GraphicsUnitConvert.FromTwips( _RTFDocument.TopMargin, GraphicsUnit.Document) / 3; ps.RightMargin = GraphicsUnitConvert.FromTwips( _RTFDocument.RightMargin, GraphicsUnit.Document) / 3; ps.BottomMargin = GraphicsUnitConvert.FromTwips( _RTFDocument.BottomMargin, GraphicsUnit.Document) / 3; ps.PaperWidth = GraphicsUnitConvert.FromTwips( _RTFDocument.PaperWidth, GraphicsUnit.Document) / 3; ps.PaperHeight = GraphicsUnitConvert.FromTwips( _RTFDocument.PaperHeight, GraphicsUnit.Document) / 3; document.PageSettings = ps; document.Info.Title = _RTFDocument.Info.Title; document.Info.Author = _RTFDocument.Info.Author; document.Info.CreationTime = _RTFDocument.Info.Creatim; document.Info.Description = _RTFDocument.Info.Comment; document.Info.LastPrintTime = _RTFDocument.Info.Printim; document.Info.LastModifiedTime = _RTFDocument.Info.Revtim; document.Info.EditMinute = _RTFDocument.Info.edmins; } document.Elements.Clear(); document.Initializing = true; ReadContent( _RTFDocument, document, document.Elements, new DocumentFormatInfo()); document.Initializing = false; document.AfterLoad(FileFormat.RTF); }
private void btnOK_Click(object sender, EventArgs e) { this.DialogResult = System.Windows.Forms.DialogResult.OK; _CommandParameter.FirstLineIndent = (float )GraphicsUnitConvert.Convert((double)txtFirstLineIndent.Value * 10.0, GraphicsUnit.Millimeter, GraphicsUnit.Document); _CommandParameter.LeftIndent = (float )GraphicsUnitConvert.FromTwips((double)txtLeftIndent.Value * 210, GraphicsUnit.Document); _CommandParameter.SpacingBefore = (float)GraphicsUnitConvert.FromTwips((double)txtSpacingBefore.Value * 312, GraphicsUnit.Document); _CommandParameter.SpacingAfter = (float)GraphicsUnitConvert.FromTwips((double)txtSpacingAfter.Value * 312, GraphicsUnit.Document); _CommandParameter.LineSpacingStyle = (LineSpacingStyle)cboLineSpacingStyle.SelectedIndex; switch (_CommandParameter.LineSpacingStyle) { case LineSpacingStyle.SpaceSpecify: _CommandParameter.LineSpacing = (float)(GraphicsUnitConvert.FromTwips((double )txtLineSpacing.Value * 20, GraphicsUnit.Document)); break; case LineSpacingStyle.SpaceMultiple: _CommandParameter.LineSpacing = (float)txtLineSpacing.Value; break; default: _CommandParameter.LineSpacing = 0; break; } this.Close(); }
private void ReadContent( RTFDomElement parentNode, DomDocument doc, DomElementList result, DocumentFormatInfo format) { if (format == null) { format = new DocumentFormatInfo(); } //if ( parentNode == null || parentNode.Elements == null) //{ // System.Console.WriteLine(""); // return; //} foreach (RTFDomElement element in parentNode.Elements) { if (element is RTFDomHeader) { if (((RTFDomHeader)element).HasContentElement) { XTextDocumentHeaderElement header = new XTextDocumentHeaderElement(); doc.PageSettings.HeaderDistance = (int)(GraphicsUnitConvert.FromTwips( (double)this._RTFDocument.HeaderDistance, GraphicsUnit.Inch) * 100.0); result.Add(header); ReadContent(element, doc, header.Elements, format); } } else if (element is RTFDomFooter) { if (((RTFDomFooter)element).HasContentElement) { XTextDocumentFooterElement footer = new XTextDocumentFooterElement(); doc.PageSettings.FooterDistance = (int)(GraphicsUnitConvert.FromTwips( (double)this._RTFDocument.FooterDistance, GraphicsUnit.Inch) * 100.0); result.Add(footer); ReadContent(element, doc, footer.Elements, format); } } else if (element is RTFDomParagraph) { RTFDomParagraph domP = (RTFDomParagraph)element; //XTextParagraph p = new XTextParagraph(); //p.OwnerDocument = doc; DocumentContentStyle style = ToDocumentContentStyle( domP.Format, doc.DocumentGraphicsUnit); ReadContent(element, doc, result, domP.Format); if (domP.IsTemplateGenerated == false || this.ImportTemplateGenerateParagraph) { DomParagraphFlagElement eof = new DomParagraphFlagElement(); eof.StyleIndex = doc.ContentStyles.GetStyleIndex(style); result.Add(eof); } } else if (element is RTFDomText) { RTFDomText domText = (RTFDomText)element; if (domText.Format.Hidden == false && domText.Text != null && domText.Text.Length > 0) { DocumentContentStyle style = ToDocumentContentStyle( domText.Format, doc.DocumentGraphicsUnit); int si = doc.ContentStyles.GetStyleIndex(style); result.AddRange(doc.CreateChars(domText.Text, si)); } } else if (element is RTFDomImage) { // 插入图片 RTFDomImage domImg = (RTFDomImage)element; DomImageElement img = new DomImageElement(); img.OwnerDocument = doc; img.Image = new XImageValue(domImg.Data); DocumentContentStyle style = ToDocumentContentStyle( domImg.Format, doc.DocumentGraphicsUnit); img.StyleIndex = doc.ContentStyles.GetStyleIndex(style); img.Width = GraphicsUnitConvert.FromTwips(domImg.Width, doc.DocumentGraphicsUnit); img.Height = GraphicsUnitConvert.FromTwips(domImg.Height, doc.DocumentGraphicsUnit); result.Add(img); } else if (element is RTFDomShape) { } else if (element is RTFDomShapeGroup) { } else if (element is RTFDomLineBreak) { // 软回车 result.Add(new DomLineBreakElement());// doc.CreateLineBreak()); } else if (element.Elements != null && element.Elements.Count > 0) { ReadContent(element, doc, result, format.Clone()); } }//foreach }
public static DocumentContentStyle ToDocumentContentStyle( DocumentFormatInfo format, GraphicsUnit documentUnit) { if (format == null) { throw new ArgumentNullException("format"); } DocumentContentStyle result = new DocumentContentStyle(); switch (format.Align) { case RTFAlignment.Left: result.Align = DocumentContentAlignment.Left; break; case RTFAlignment.Center: result.Align = DocumentContentAlignment.Center; break; case RTFAlignment.Right: result.Align = DocumentContentAlignment.Right; break; case RTFAlignment.Justify: result.Align = DocumentContentAlignment.Justify; break; } result.BackgroundColor = format.BackColor; if (format.BackColor == Color.White) { result.BackgroundColor = Color.Transparent; } result.Bold = format.Bold; result.BorderColor = format.BorderColor; result.BorderStyle = format.BorderStyle; result.BorderLeft = format.LeftBorder; result.BorderTop = format.TopBorder; result.BorderBottom = format.BottomBorder; result.BorderRight = format.RightBorder; result.BorderWidth = format.BorderWidth; result.BorderSpacing = GraphicsUnitConvert.FromTwips( format.BorderSpacing, documentUnit); if (format.LeftBorder || format.TopBorder || format.RightBorder || format.BottomBorder) { if (format.BorderThickness) { result.BorderWidth = 2; } else { result.BorderWidth = 1; } } result.BulletedList = format.BulletedList; result.FontName = format.FontName; result.FontSize = format.FontSize; result.Italic = format.Italic; result.LeftIndent = GraphicsUnitConvert.FromTwips( format.LeftIndent, documentUnit); if (format.LineSpacing == 0) { // 单倍行距 result.LineSpacingStyle = LineSpacingStyle.SpaceSingle; } else if (format.LineSpacing < 0) { // 行间距是固定值 result.LineSpacingStyle = LineSpacingStyle.SpaceSpecify; result.LineSpacing = GraphicsUnitConvert.FromTwips(format.LineSpacing, documentUnit); } else { if (format.MultipleLineSpacing) { // 多倍行距 result.LineSpacingStyle = LineSpacingStyle.SpaceMultiple; result.LineSpacing = format.LineSpacing / 240.0f; } else { // 最小行距 result.LineSpacingStyle = LineSpacingStyle.SpaceExactly; } } result.Link = format.Link; result.NumberedList = format.NumberedList; result.FirstLineIndent = GraphicsUnitConvert.FromTwips( format.ParagraphFirstLineIndent, documentUnit); result.Spacing = GraphicsUnitConvert.FromTwips( format.Spacing, documentUnit); result.SpacingBeforeParagraph = GraphicsUnitConvert.FromTwips( format.SpacingBefore, documentUnit); result.SpacingAfterParagraph = GraphicsUnitConvert.FromTwips( format.SpacingAfter, documentUnit); result.Strikeout = format.Strikeout; result.Subscript = format.Subscript; result.Superscript = format.Superscript; result.Color = format.TextColor; result.Underline = format.Underline; return(result); }