public int ToPixel(float documentLength) { return((int)GraphicsUnitConvert.Convert( documentLength, this.MainDocument.DocumentGraphicsUnit, GraphicsUnit.Pixel)); }
private void cboLineSpacingStyle_SelectedIndexChanged(object sender, EventArgs e) { LineSpacingStyle style = (LineSpacingStyle)cboLineSpacingStyle.SelectedIndex; switch (style) { case LineSpacingStyle.SpaceSingle: case LineSpacingStyle.Space1pt5: case LineSpacingStyle.SpaceDouble: case LineSpacingStyle.SpaceExactly: txtLineSpacing.Value = 0; txtLineSpacing.Enabled = false; txtLineSpacing.Increment = 1m; lblBang.Visible = false; break; case LineSpacingStyle.SpaceSpecify: txtLineSpacing.Enabled = true; txtLineSpacing.Value = (decimal)(GraphicsUnitConvert.ToTwips(_CommandParameter.LineSpacing, GraphicsUnit.Document) / 20.0); txtLineSpacing.Increment = 1m; lblBang.Visible = true; break; case LineSpacingStyle.SpaceMultiple: txtLineSpacing.Value = 3; txtLineSpacing.Increment = 0.25m; lblBang.Visible = false; txtLineSpacing.Enabled = true; break; } }
public override bool RefreshView() { if (myOwnerDocument.Info.Printing) { return(true); } RefreshSize(); int y = this.RealTop + (this.Height / 2); int x = 0; if (WeiWenProcess.weiwen) { //修正因为字号过大引起的回车在行首光标位置错位的问题 add by Ukey zhang 2017-11-11 if (this.RealLeft + this.Width > this.OwnerLine.ContentWidth) { this.Left = this.OwnerLine.ContentWidth - this.Width; } x = this.RealLeft + this.Width; } else { x = this.RealLeft; } if (myOwnerDocument.Info.ShowParagraphFlag) { myOwnerDocument.View.DrawParagraphFlag( x, y, GraphicsUnitConvert.GetRate(myOwnerDocument.DocumentGraphicsUnit, System.Drawing.GraphicsUnit.Pixel)); } return(true); }
/// <summary> /// 移动光标到新的光标区域 /// </summary> /// <param name="vLeft">在视图中光标区域的左端位置</param> /// <param name="vTop">在视图中光标区域的顶端位置</param> /// <param name="vWidth">光标区域宽度</param> /// <param name="vHeight">光标区域高度</param> /// <returns>移动光标是否造成滚动</returns> public bool MoveCaretTo( int vLeft, int vTop, int vWidth, int vHeight) { //return false; if (this.IsUpdating) { return(false); } if (this.ForceShowCaret == false && this.Focused == false) { if (this.CaretCreated) { this.Caret.Hide(); } //this.ScrollToView( vLeft , vTop , vWidth , vHeight ); return(false); } int height = GraphicsUnitConvert.Convert( vHeight, this.GraphicsUnit, System.Drawing.GraphicsUnit.Pixel); if (vWidth > 0 && vHeight > 0) { bolCaretCreated = this.Caret.Create(0, vWidth, height); if (this.CaretCreated) { if (bolMoveCaretWithScroll) { this.ScrollToView(vLeft, vTop, vWidth, vHeight); } System.Drawing.Point p = this.ViewPointToClient(vLeft, vTop); myCaret.SetPos(p.X, p.Y); myCaret.Show(); Win32Imm imm = new Win32Imm(this); if (imm.IsImmOpen()) { imm.SetImmPos(p); } myCaretBounds = new System.Drawing.Rectangle( p.X, p.Y, vWidth, height); if (this.MoveCaretWithScroll) { return(true); } } } return(false); }
/// <summary> /// 创建指定页的BMP图片对象 /// </summary> /// <param name="page">页面对象</param> /// <param name="DrawMargin">是否绘制页边距线</param> /// <returns>创建的BMP图片对象</returns> public Metafile GetPageBmp2(PrintPage page, bool DrawMargin) { double rate = GraphicsUnitConvert.GetRate( myDocument.DocumentGraphicsUnit, System.Drawing.GraphicsUnit.Pixel); int width = ( int )Math.Ceiling(myPages.PaperWidth / rate); int height = ( int )Math.Ceiling(myPages.PaperHeight / rate); System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(width, height); Graphics g1 = Graphics.FromImage(bmp); Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height); string folder = CreateFolder(); string m_FilePath1 = folder + Guid.NewGuid().ToString() + ".wmf"; Metafile mf1 = new Metafile(m_FilePath1, g1.GetHdc(), rect, MetafileFrameUnit.Pixel); using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(mf1)) { //Add by wwj 2013-01-24 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; if (intBackColor.A != 0) { g.Clear(this.intBackColor); } g.PageUnit = myDocument.DocumentGraphicsUnit; //Metafile mf = new Metafile(bmp,g.GetHdc(),); PageFrameDrawer drawer = new PageFrameDrawer(); //Graphics gg = Graphics.FromImage(mf); drawer.DrawMargin = DrawMargin; drawer.BackColor = System.Drawing.Color.Transparent; drawer.BorderColor = this.intBorderColor; drawer.BorderWidth = 1; drawer.LeftMargin = myPages.LeftMargin; drawer.TopMargin = myPages.TopMargin; drawer.RightMargin = myPages.RightMargin; drawer.BottomMargin = myPages.BottomMargin; drawer.Bounds = new System.Drawing.Rectangle( 0, 0, myPages.PaperWidth, myPages.PaperHeight); drawer.DrawPageFrame(g, System.Drawing.Rectangle.Empty); DrawPage(page, g, page.Bounds, true); } return(mf1); }
/// <summary> /// 输出嵌入在RTF文档中的文档元素对象 /// </summary> /// <param name="element">文档元素对象</param> /// <param name="attributes">属性列表</param> /// <param name="resultImage">表示元素内容的图片对象</param> public void WriteEmbObject( DomElement element, Image resultImage) { bool disposeImage = false; if (resultImage == null) { resultImage = element.CreateContentImage(); disposeImage = true; } Size size = new Size( GraphicsUnitConvert.ToTwips(resultImage.Width, GraphicsUnit.Pixel), GraphicsUnitConvert.ToTwips(resultImage.Height, GraphicsUnit.Pixel)); this.WriteStartGroup(); this.WriteKeyword("object"); this.WriteKeyword("objemb"); this.WriteCustomAttribute("objclass", XWriterObjectPrefix + element.GetType().FullName, true); this.WriteKeyword("objw" + size.Width); this.WriteKeyword("objh" + size.Height); StringWriter writer = new StringWriter(); XmlSerializer ser = Xml.MyXmlSerializeHelper.GetElementXmlSerializer(element.GetType()); ser.Serialize(writer, element); writer.Close(); string xmlText = writer.ToString(); byte[] bs = Encoding.UTF8.GetBytes(xmlText); this.WriteStartGroup(); this.WriteKeyword("objdata", true); this.WriteBytes(bs); this.WriteEndGroup(); this.WriteStartGroup(); this.WriteKeyword("result"); this.WriteImage( resultImage, resultImage.Width, resultImage.Height, null, element.RuntimeStyle); this.WriteEndGroup(); this.WriteEndGroup(); if (disposeImage) { resultImage.Dispose(); } }
private void dlgParagraphFormatcs_Load(object sender, EventArgs e) { if (_CommandParameter == null) { _CommandParameter = new ParagraphFormatCommandParameter(); } this.txtLeftIndent.Value = ( decimal )(GraphicsUnitConvert.ToTwips(_CommandParameter.LeftIndent, GraphicsUnit.Document) / 210.0); txtFirstLineIndent.Value = (decimal)(GraphicsUnitConvert.Convert(_CommandParameter.FirstLineIndent, GraphicsUnit.Document, GraphicsUnit.Millimeter) / 10.0); txtSpacingBefore.Value = (decimal)(GraphicsUnitConvert.ToTwips(_CommandParameter.SpacingBefore, GraphicsUnit.Document) / 312.0); txtSpacingAfter.Value = (decimal)(GraphicsUnitConvert.ToTwips(_CommandParameter.SpacingAfter, GraphicsUnit.Document) / 312.0); cboLineSpacingStyle.SelectedIndex = (int)_CommandParameter.LineSpacingStyle; cboLineSpacingStyle_SelectedIndexChanged(null, null); }
/// <summary> /// 输出对象到RTF文档中 /// </summary> /// <param name="writer">RTF文档书写器</param> public override void WriteRTF(DCSoft.CSharpWriter.RTF.RTFContentWriter writer) { System.Drawing.SizeF size = new System.Drawing.SizeF(this.Width, this.Height); size = GraphicsUnitConvert.Convert( size, this.OwnerDocument.DocumentGraphicsUnit, GraphicsUnit.Pixel); writer.WriteImage( this.Image.Value, (int)size.Width, (int)size.Height, this.Image.ImageData, this.RuntimeStyle); }
public override bool RefreshView() { if (myOwnerDocument.Info.Printing) { return(true); } if (myOwnerDocument.Info.ShowParagraphFlag) { myOwnerDocument.View.DrawLineFlag( this.RealLeft, this.RealTop + this.Height, GraphicsUnitConvert.GetRate(myOwnerDocument.DocumentGraphicsUnit, System.Drawing.GraphicsUnit.Pixel)); } return(true); }
//private System.Drawing.Printing.Margins myClientMargins = new System.Drawing.Printing.Margins(); //private System.Drawing.Size myClientPageSize = System.Drawing.Size.Empty ; /// <summary> /// 刷新坐标转换信息 /// </summary> protected override void RefreshScaleTransform() { MultiPageTransform trans = ( MultiPageTransform )this.myTransform; intGraphicsUnit = _Pages.GraphicsUnit; trans.Rate = GraphicsUnitConvert.GetRate( intGraphicsUnit, System.Drawing.GraphicsUnit.Pixel); //float rate = ( float )( 1.0 / this.ClientToViewXRate ); //trans.Pages = myPages ; //trans.Refresh( rate , this.intPageSpacing ); System.Drawing.Point sp = this.AutoScrollPosition; trans.ClearSourceOffset(); trans.OffsetSource(sp.X, sp.Y, true); }
/// <summary> /// 创建预览用的图片 /// </summary> /// <returns>创建的图片对象</returns> public override Image CreateContentImage() { if (this.Image.HasContent) { System.Drawing.SizeF size = new System.Drawing.SizeF(this.Width, this.Height); size = GraphicsUnitConvert.Convert( size, this.OwnerDocument.DocumentGraphicsUnit, GraphicsUnit.Pixel); return(this.Image.GetThumbnailImage((int)size.Width, (int)size.Height).Value); } else { return(base.CreateContentImage()); } }
/// <summary> /// 根据图片内容更新元素的大小 /// </summary> public void UpdateSize() { if (this.Image.HasContent) { System.Drawing.Size size = myImage.Size; //base.WidthHeightRate = ( double )size.Width / ( double ) size.Height ; size = GraphicsUnitConvert.Convert( myImage.Value.Size, System.Drawing.GraphicsUnit.Pixel, this.OwnerDocument.DocumentGraphicsUnit); UpdateWidthHeightRate(); this.EditorSize = new SizeF(size.Width, size.Height); } else { this.EditorSize = new SizeF(100, 100); } }
private float[] ParseLengths(string text) { List <float> result = new List <float>(); string[] items = SplitItems(text); if (items != null) { foreach (string item in items) { double v = GraphicsUnitConvert.ParseCSSLength(item, this.DomDocument.DocumentGraphicsUnit, double.NaN); if (double.IsNaN(v) == false) { result.Add((float)v); } } } return(result.ToArray()); }
private bool TryParseLength(string text, out float length) { length = 0; if (string.IsNullOrEmpty(text)) { return(false); } text = text.Trim(); if ("0123456789.-".IndexOf(text[0]) >= 0) { double v = GraphicsUnitConvert.ParseCSSLength(text, this.DomDocument.DocumentGraphicsUnit, double.NaN); if (double.IsNaN(v) == false) { length = (float)v; return(true); } } return(false); }
/// <summary> /// 创建指定页的BMP图片对象 /// </summary> /// <param name="page">页面对象</param> /// <param name="DrawMargin">是否绘制页边距线</param> /// <returns>创建的BMP图片对象</returns> public System.Drawing.Bitmap GetPageBmp(PrintPage page, bool DrawMargin) { XPageSettings pageSettings = page.PageSettings; double rate = GraphicsUnitConvert.GetRate( myDocument.DocumentGraphicsUnit, System.Drawing.GraphicsUnit.Pixel); int width = (int)Math.Ceiling(pageSettings.ViewPaperWidth / rate); int height = (int)Math.Ceiling(pageSettings.ViewPaperHeight / rate); System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(width, height); using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bmp)) { if (intBackColor.A != 0) { g.Clear(this.intBackColor); } g.PageUnit = myDocument.DocumentGraphicsUnit; PageFrameDrawer drawer = new PageFrameDrawer(); drawer.DrawMargin = DrawMargin; drawer.BackColor = System.Drawing.Color.Transparent; drawer.BorderColor = this.intBorderColor; drawer.BorderWidth = 1; drawer.LeftMargin = (int)page.ViewLeftMargin; drawer.TopMargin = (int)page.ViewTopMargin; drawer.RightMargin = (int)page.ViewRightMargin; drawer.BottomMargin = (int)page.ViewBottomMargin; drawer.Bounds = new System.Drawing.Rectangle( 0, 0, (int)page.ViewPaperWidth, (int)page.ViewPaperHeight); drawer.BackgroundImage = this.PageBackgroundImage; drawer.DrawPageFrame(g, System.Drawing.Rectangle.Empty); DrawPage(page, g, page.Bounds, true); } return(bmp); }
/// <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); }
/// <summary> /// 创建包含元素内容的图片对象 /// </summary> /// <returns>图片对象</returns> public virtual System.Drawing.Image CreateContentImage() { if (this.Width <= 0 || this.Height <= 0) { return(null); } System.Drawing.SizeF size = new System.Drawing.SizeF(this.Width, this.Height); size = GraphicsUnitConvert.Convert( size, this.OwnerDocument.DocumentGraphicsUnit, System.Drawing.GraphicsUnit.Pixel); System.Drawing.Bitmap bmp = new System.Drawing.Bitmap( (int)Math.Ceiling(size.Width), (int)Math.Ceiling(size.Height)); using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bmp)) { g.Clear(Color.White); g.PageUnit = this.OwnerDocument.DocumentGraphicsUnit; RectangleF bounds = this.AbsBounds; g.TranslateTransform(-bounds.Left, -bounds.Top); DocumentPaintEventArgs args = new DocumentPaintEventArgs(g, Rectangle.Empty); args.Document = this.OwnerDocument; args.PageClipRectangle = Rectangle.Ceiling(bounds); args.Render = this.OwnerDocument.Render; args.RenderStyle = DocumentRenderStyle.Bitmap; args.Style = this.RuntimeStyle; args.Type = this.DocumentContentElement.ContentPartyStyle; args.ViewBounds = Rectangle.Ceiling(bounds); args.ActiveMode = true; args.Bounds = bounds; args.Element = this; args.ForCreateImage = true; this.Draw(args); }//using return(bmp); }
/// <summary> /// 输出图片类型的元素 /// </summary> /// <param name="element">文档元素对象</param> public void WriteImageElement(DomElement element) { DocumentContentStyle style = element.RuntimeStyle; Image img = element.CreateContentImage(); if (img != null) { HtmlAttachFile file = this.AddImage(img, null); WriteStartElement("img"); WriteStartStyle(); WriteDocumentContentStyle(element.RuntimeStyle, element); WriteEndStyle(); WriteAttributeString("src", file.ReferenceCode); WriteAttributeString( "width", GraphicsUnitConvert.ToCSSLength( element.Width, element.OwnerDocument.DocumentGraphicsUnit, CssLengthUnit.Pixels)); WriteAttributeString( "width", GraphicsUnitConvert.ToCSSLength( element.Height, element.OwnerDocument.DocumentGraphicsUnit, CssLengthUnit.Pixels)); if (element is DomImageElement) { string title = ((DomImageElement)element).Title; if (title != null && title.Length > 0) { WriteAttributeString("title", title); } } WriteEndElement(); } }
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(); }
//public override void WriteStartDocument() //{ // base.WriteStartDocument(); // this.WriteStartElement("div"); //} //public override void WriteEndDocument() //{ // this.WriteEndElement(); // base.WriteEndDocument(); //} /// <summary> /// 输出文档样式 /// </summary> /// <param name="style">文档样式信息对象</param> /// <param name="element">相关的文档元素对象</param> public void WriteDocumentContentStyle(DocumentContentStyle style, DomElement element) { if (style == null) { throw new ArgumentNullException("style"); } GraphicsUnit documentUnit = GraphicsUnit.Document; if (this.MainDocument != null) { documentUnit = this.MainDocument.DocumentGraphicsUnit; } // 输出边框线 base.WriteBorderStyle( style.BorderLeft, style.BorderTop, style.BorderRight, style.BorderBottom, style.BorderColor, (int)style.BorderWidth, style.BorderStyle); // 输出字体 base.WriteFontStyle(style.Font.Value); // 输出内边距 base.WritePaddingStyle( ToPixel(style.PaddingLeft), ToPixel(style.PaddingTop), ToPixel(style.PaddingRight), ToPixel(style.PaddingBottom)); // 输出外边距 base.WriteMarginStyle( ToPixel(style.MarginLeft), ToPixel(style.MarginTop), ToPixel(style.MarginRight), ToPixel(style.MarginBottom)); if (element is DomParagraphFlagElement || element is DomParagraphElement) { // 文本水平对齐方式 switch (style.Align) { case DocumentContentAlignment.Left: base.WriteStyleItem("text-align", "left"); break; case DocumentContentAlignment.Center: base.WriteStyleItem("text-align", "center"); break; case DocumentContentAlignment.Right: base.WriteStyleItem("text-align", "right"); break; case DocumentContentAlignment.Justify: base.WriteStyleItem("text-align", "justify"); break; default: base.WriteStyleItem("text-align", "left"); break; } } if (XDependencyObject.HasPropertyValue(style, ContentStyle.PropertyName_BackgroundColor)) { // 输出背景色 WriteStyleItem("background-color", ColorToString(style.BackgroundColor)); } if (style.BackgroundImage != null && style.BackgroundImage.HasContent) { // 输出背景图片 HtmlAttachFile file = base.AddImage(style.BackgroundImage.Value, null); WriteStyleItem("background-image", "url(" + file.ReferenceCode + ")"); } if (XDependencyObject.HasPropertyValue(style, ContentStyle.PropertyName_BackgroundPosition)) { // 背景位置 string strItem = null; switch (style.BackgroundPosition) { case ContentAlignment.TopLeft: strItem = "top left"; break; case ContentAlignment.TopCenter: strItem = "top center"; break; case ContentAlignment.TopRight: strItem = "top right"; break; case ContentAlignment.MiddleLeft: strItem = "center left"; break; case ContentAlignment.MiddleCenter: strItem = "center center"; break; case ContentAlignment.MiddleRight: strItem = "center right"; break; case ContentAlignment.BottomLeft: strItem = "bottom left"; break; case ContentAlignment.BottomCenter: strItem = "bottom center"; break; case ContentAlignment.BottomRight: strItem = "bottom right"; break; } WriteStyleItem("background-position", strItem); } if (style.BackgroundPositionX != 0) { WriteStyleItem("background-position-x", GraphicsUnitConvert.ToCSSLength( ToPixel(style.BackgroundPositionX), documentUnit, CssLengthUnit.Pixels)); } if (style.BackgroundPositionY != 0) { WriteStyleItem("background-position-y", GraphicsUnitConvert.ToCSSLength( ToPixel(style.BackgroundPositionY), documentUnit, CssLengthUnit.Pixels)); } if (style.BackgroundRepeat) { WriteStyleItem("background-repeat", "repeat"); } if (XDependencyObject.HasPropertyValue(style, ContentStyle.PropertyName_Color)) { WriteStyleItem("color", ColorToString(style.Color)); } // 首行缩进 if (Math.Abs(style.FirstLineIndent) > 0.05) { WriteStyleItem("text-indent", GraphicsUnitConvert.ToCSSLength( style.FirstLineIndent, documentUnit, CssLengthUnit.Pixels)); } //case ContentStyle.PropertyName_LineSpacing: // { // // 行间距 // float ls = style.LineSpacing; // if (Math.Abs(ls) > 0.05) // { // } // } // break; // 后置强制分页 if (style.PageBreakAfter) { WriteStyleItem("page-break-after", "always"); } // 前置强制分页 if (style.PageBreakBefore) { WriteStyleItem("page-break-before", "always"); } // 从右到左排版 if (style.RightToLeft) { WriteStyleItem("direction", "rtl"); } // 字符间距 if (Math.Abs(style.Spacing) > 0.05) { WriteStyleItem("letter-spacing", GraphicsUnitConvert.ToCSSLength( style.Spacing, documentUnit, CssLengthUnit.Pixels)); } // 垂直显示文本 if (style.VertialText) { } // 可见性 if (style.Visible == false) { WriteStyleItem("visibility", "hidden"); } if (XDependencyObject.HasPropertyValue(style, ContentStyle.PropertyName_Zoom)) { // 缩放比率 WriteStyleItem("zoom", style.Zoom.ToString()); } }
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 }
/// <summary> /// 打印指定页面 /// </summary> /// <param name="myPage">页面对象</param> /// <param name="g">绘图操作对象</param> /// <param name="MainClipRect">主剪切矩形</param> /// <param name="UseMargin">是否启用页边距</param> public override void DrawPage( PrintPage myPage, System.Drawing.Graphics g, System.Drawing.Rectangle MainClipRect, bool UseMargin) { //XPageSettings pageSettings = myPage.PageSettings; int LeftMargin = 0; int TopMargin = 0; int RightMargin = 0; int BottomMargin = 0; if (UseMargin) { LeftMargin = (int)myPage.ViewLeftMargin; TopMargin = (int)myPage.ViewTopMargin; RightMargin = (int)myPage.ViewRightMargin; BottomMargin = (int)myPage.ViewBottomMargin; } this.OnBeforeDrawPage(myPage, g); IntPtr hdc = g.GetHdc(); DeviceCapsClass dcc = new DeviceCapsClass(hdc); g.ReleaseHdc(); DomDocument document = (DomDocument)this.Document; XPageSettings ps = myPage.PageSettings; if (ps == null) { ps = document.PageSettings; } g.PageUnit = document.DocumentGraphicsUnit; System.Drawing.Rectangle ClipRect = System.Drawing.Rectangle.Empty; if (this.PageHeadText != null) { // 绘制标题文本 g.DrawString( this.PageHeadText, System.Windows.Forms.Control.DefaultFont, System.Drawing.Brushes.Red, 20, 20, System.Drawing.StringFormat.GenericDefault); } float printableAreaOffsetX = (float)GraphicsUnitConvert.Convert( this.PrintableAreaOffset.X / 100.0, System.Drawing.GraphicsUnit.Inch, document.DocumentGraphicsUnit); float printableAreaOffsetY = (float)GraphicsUnitConvert.Convert( this.PrintableAreaOffset.Y / 100.0, System.Drawing.GraphicsUnit.Inch, document.DocumentGraphicsUnit); float headerHeight = Math.Max(ps.ViewHeaderHeight, document.Header.Height); int headerHeightFix = 0; if (document.Header.Height > ps.ViewHeaderHeight - 10) { headerHeightFix = ( int )(document.Header.Height - (ps.ViewHeaderHeight - 10)); } if (this.DrawHead) { // 绘制页眉 g.ResetTransform(); g.ResetClip(); ClipRect = new System.Drawing.Rectangle( 0, 0, myPage.Width, (int)headerHeight); g.ScaleTransform(this.XZoomRate, this.YZoomRate); g.TranslateTransform( LeftMargin - printableAreaOffsetX, ps.ViewHeaderDistance - printableAreaOffsetY); g.SetClip(new System.Drawing.Rectangle( ClipRect.Left, ClipRect.Top, ClipRect.Width + 1, ClipRect.Height + 1)); PageDocumentPaintEventArgs args = new PageDocumentPaintEventArgs( g, ClipRect, document, myPage, PageContentPartyStyle.Header); args.RenderMode = ContentRenderMode.Print; args.ContentBounds = ClipRect; document.DrawContent(args); //DesignPaintEventArgs e = new DesignPaintEventArgs( g , ClipRect ); //myDocument.RefreshView( e ); g.ResetClip(); g.ResetTransform(); } // 绘制页面正文 ClipRect = new System.Drawing.Rectangle( 0, myPage.Top, myPage.Width, myPage.Height); if (!MainClipRect.IsEmpty) { ClipRect = System.Drawing.Rectangle.Intersect(ClipRect, MainClipRect); } if (!ClipRect.IsEmpty) { g.ScaleTransform(this.XZoomRate, this.YZoomRate); g.TranslateTransform( LeftMargin - printableAreaOffsetX, TopMargin - myPage.Top + headerHeightFix - printableAreaOffsetY); //System.Drawing.Drawing2D.GraphicsPath clipPath = new System.Drawing.Drawing2D.GraphicsPath(); //clipPath.AddRectangle( ClipRect ); //g.SetClip( clipPath ); //g.TranslateTransform( myPages.LeftMargin , myPages.TopMargin - myPage.Top + myPages.HeadHeight ); System.Drawing.RectangleF rect = DrawerUtil.FixClipBounds( g, ClipRect.Left, ClipRect.Top, ClipRect.Width, ClipRect.Height); rect.Offset(-4, -4); rect.Width = rect.Width + 8; rect.Height = rect.Height + 8; g.SetClip(rect); // System.Drawing.RectangleF rect2 = g.ClipBounds ; // if( rect.Top < rect2.Top ) // { // float dy = rect2.Top - rect.Top ; // rect.Y = rect.Y - dy * 2 ; // rect.Height = rect.Height + dy * 4 ; // } // g.SetClip( rect ); PageDocumentPaintEventArgs args = new PageDocumentPaintEventArgs( g, ClipRect, document, myPage, PageContentPartyStyle.Body); args.RenderMode = ContentRenderMode.Print; args.ContentBounds = ClipRect; document.DrawContent(args); //myDocument.DrawDocument( g , ClipRect ); //DesignPaintEventArgs e = new DesignPaintEventArgs( g , ClipRect ); //myDocument.RefreshView( e ); } if (this.DrawFooter) { // 绘制页脚 g.ResetClip(); g.ResetTransform(); int documentHeight = myPage.DocumentHeight; float footerHeight = Math.Max(document.Footer.Height, ps.ViewFooterHeight); ClipRect = new System.Drawing.Rectangle( 0, 0, myPage.Width, (int)footerHeight); int dy = 0; dy = (int)(myPage.ViewPaperHeight - ps.ViewFooterDistance - document.Footer.Height); g.ScaleTransform(this.XZoomRate, this.YZoomRate); g.TranslateTransform( LeftMargin - printableAreaOffsetX, dy - printableAreaOffsetY); g.SetClip(new System.Drawing.Rectangle( ClipRect.Left, ClipRect.Top, ClipRect.Width + 1, ClipRect.Height + 1)); PageDocumentPaintEventArgs args = new PageDocumentPaintEventArgs( g, ClipRect, document, myPage, PageContentPartyStyle.Footer); args.RenderMode = ContentRenderMode.Print; args.ContentBounds = ClipRect; document.DrawContent(args); //DesignPaintEventArgs e = new DesignPaintEventArgs( g , ClipRect ); //myDocument.RefreshView( e ); } //if( this.bolDrawFooter ) } //public void DrawPage()
private void ParseStyleItem(DocumentContentStyle cstyle, string styleName, string styleValue) { switch (styleName) { case "width": { float v = 0; if (TryParseLength(styleValue, out v)) { cstyle.WidthValue = v; } } break; case "height": { float v = 0; if (TryParseLength(styleValue, out v)) { cstyle.HeightValue = v; } } break; case "background": { // 解析背景设置 string[] items = SplitItems(styleValue); if (items != null) { foreach (string item in items) { Color c = Color.Empty; string url = GetAttributeUrl(item); if (url != null) { url = this.HtmlDocument.GetAbsoluteURL(url); XImageValue img = new XImageValue(); if (img.Load(url) <= 0) { cstyle.BackgroundImage = img; } } else if (TryParseColor(item, out c)) { cstyle.BackgroundColor = c; } else if (string.Equals(item, "repeat", StringComparison.CurrentCultureIgnoreCase)) { cstyle.BackgroundRepeat = true; } else if (string.Equals(item, "no-repeat", StringComparison.CurrentCultureIgnoreCase)) { cstyle.BackgroundRepeat = false; } } //foreach } } break; case "background-color": { Color c = Color.Empty; if (TryParseColor(styleValue, out c)) { cstyle.BackgroundColor = c; } } break; case "background-repeat": { string text = Trim(styleValue); if (string.Equals(text, "repeat", StringComparison.CurrentCultureIgnoreCase)) { cstyle.BackgroundRepeat = true; } else if (string.Equals(text, "no-repeat", StringComparison.CurrentCultureIgnoreCase)) { cstyle.BackgroundRepeat = false; } else { cstyle.BackgroundRepeat = true; } } break; case "border": { // 解析边框设置 string[] items = SplitItems(styleValue); if (items != null) { foreach (string item in items) { DashStyle ds = DashStyle.Custom; Color c = Color.Empty; float w = 0; if (TryParseBorderStyle(item, out ds)) { cstyle.BorderStyle = ds; if (ds == DashStyle.Custom) { cstyle.BorderStyle = DashStyle.Solid; cstyle.BorderWidth = 0; break; } } else if (TryParseColor(item, out c)) { cstyle.BorderColor = c; } else if (TryParseBorderWidth(item, ref w)) { cstyle.BorderWidth = w; } } //foreach } } break; case "border-color": { Color c = Color.Empty; if (TryParseColor(styleValue, out c)) { cstyle.BorderColor = c; } } break; case "border-width": { float w = 0; if (TryParseBorderWidth(styleValue, ref w)) { cstyle.BorderWidth = w; } } break; case "border-style": { DashStyle bs = DashStyle.Solid; if (TryParseBorderStyle(styleValue, out bs)) { cstyle.BorderStyle = bs; if (bs == DashStyle.Custom) { cstyle.BorderStyle = DashStyle.Solid; cstyle.BorderWidth = 0; } } } break; case "color": { Color c = Color.Empty; if (TryParseColor(styleValue, out c)) { cstyle.Color = c; } } break; case "font": { string[] items = SplitItems(styleValue); if (items != null) { for (int iCount = 0; iCount < items.Length; iCount++) { string item = items[iCount]; float size = 0; if (string.Equals(item, "italic", StringComparison.CurrentCultureIgnoreCase)) { cstyle.Italic = true; } else if (string.Equals(item, "oblique", StringComparison.CurrentCultureIgnoreCase)) { cstyle.Italic = true; } else if (string.Equals(item, "bold", StringComparison.CurrentCultureIgnoreCase)) { cstyle.Bold = true; } else if (string.Equals(item, "bolder", StringComparison.CurrentCultureIgnoreCase)) { cstyle.Bold = true; } else if (TryParseLength(item, out size)) { cstyle.FontSize = GraphicsUnitConvert.Convert(size, this.DomDocument.DocumentGraphicsUnit, GraphicsUnit.Point); } else { string name2 = GetFontName(item); if (name2 != null) { cstyle.FontName = name2; } } } } } break; case "font-family": { string fn = GetFontName(styleValue); if (fn != null) { cstyle.FontName = fn; } } break; case "font-size": { float size = 0; if (TryParseLength(styleValue, out size)) { cstyle.FontSize = GraphicsUnitConvert.Convert( size, this.DomDocument.DocumentGraphicsUnit, GraphicsUnit.Point); } } break; case "font-style": { if (Contains(styleValue, "italic") || Contains(styleValue, "oblique")) { cstyle.Italic = true; } } break; case "font-weight": if (Contains(styleValue, "bold")) { cstyle.Bold = true; } else if (Contains(styleValue, "700")) { cstyle.Bold = true; } break; case "line-height": break; case "text-align": if (Contains(styleValue, "left")) { cstyle.Align = DocumentContentAlignment.Left; } else if (Contains(styleValue, "right")) { cstyle.Align = DocumentContentAlignment.Right; } else if (Contains(styleValue, "center")) { cstyle.Align = DocumentContentAlignment.Center; } else if (Contains(styleValue, "justify")) { cstyle.Align = DocumentContentAlignment.Justify; } break; case "text-indent": { float v = 0; if (TryParseLength(styleValue, out v)) { cstyle.FirstLineIndent = v; } } break; case "padding": { // 读取内边距 float[] values = ParseLengths(styleValue); if (values != null) { if (values.Length >= 4) { cstyle.PaddingTop = values[0]; cstyle.PaddingRight = values[1]; cstyle.PaddingBottom = values[2]; cstyle.PaddingLeft = values[3]; } else if (values.Length == 1) { cstyle.PaddingLeft = values[0]; cstyle.PaddingTop = values[0]; cstyle.PaddingRight = values[0]; cstyle.PaddingBottom = values[0]; } else if (values.Length == 2) { cstyle.PaddingTop = values[0]; cstyle.PaddingBottom = values[0]; cstyle.PaddingLeft = values[1]; cstyle.PaddingRight = values[1]; } else if (values.Length == 3) { cstyle.PaddingTop = values[0]; cstyle.PaddingRight = values[1]; cstyle.PaddingLeft = values[1]; cstyle.PaddingBottom = values[2]; } } } break; case "padding-left": { float v = 0; if (TryParseLength(styleValue, out v)) { cstyle.PaddingLeft = v; } } break; case "padding-top": { float v = 0; if (TryParseLength(styleValue, out v)) { cstyle.PaddingTop = v; } } break; case "padding-right": { float v = 0; if (TryParseLength(styleValue, out v)) { cstyle.PaddingRight = v; } } break; case "padding-bottom": { float v = 0; if (TryParseLength(styleValue, out v)) { cstyle.PaddingBottom = v; } } break; }//switch }
public double GetHeight(System.Drawing.GraphicsUnit unit) { return((int)GraphicsUnitConvert.Convert(intHeight * 3, System.Drawing.GraphicsUnit.Document, unit)); }
public void SetHeight(double vHeight, System.Drawing.GraphicsUnit unit) { intHeight = (int)(GraphicsUnitConvert.Convert(vHeight, unit, System.Drawing.GraphicsUnit.Document) / 3.0); }
public void SetWidth(double vWidth, System.Drawing.GraphicsUnit unit) { intWidth = (int)(GraphicsUnitConvert.Convert(vWidth, unit, System.Drawing.GraphicsUnit.Document) / 3.0); }
public double GetWidth(System.Drawing.GraphicsUnit unit) { return(GraphicsUnitConvert.Convert(intWidth * 3, System.Drawing.GraphicsUnit.Document, unit)); }
public static double GetRate(System.Drawing.GraphicsUnit unit) { return(GraphicsUnitConvert.GetRate(unit, System.Drawing.GraphicsUnit.Document) / 3.0); }
public int ToTwips(float Value) { return(GraphicsUnitConvert.ToTwips(Value, this.MainDocument.DocumentGraphicsUnit)); }
/// <summary> /// 重新计算对象大小 /// </summary> /// <param name="g">图形绘制对象</param> protected virtual void RefreshSize(DomCharElement chr, System.Drawing.Graphics g) { DocumentViewOptions opt = this.Document.Options.ViewOptions; if (myMeasureFormat == null) { myMeasureFormat = new System.Drawing.StringFormat( System.Drawing.StringFormat.GenericTypographic); myMeasureFormat.FormatFlags = System.Drawing.StringFormatFlags.FitBlackBox | System.Drawing.StringFormatFlags.MeasureTrailingSpaces | StringFormatFlags.NoClip; } DomDocument document = chr.OwnerDocument; if (_EnableCharSizeBuffer) { if (chr.CharValue != '\t') { if (charSizes != null) { if (charSizes.ContainsKey(chr.StyleIndex)) { Dictionary <char, SizeF> sizes = charSizes[chr.StyleIndex]; if (sizes.ContainsKey(chr.CharValue)) { // 从缓存区中获得字母大小 SizeF bsize = sizes[chr.CharValue]; chr.Width = bsize.Width; chr.Height = bsize.Height; chr._FontHeight = chr.RuntimeStyle.Font.GetHeight(g); return; } } } } //if } //if DocumentContentStyle rs = chr.RuntimeStyle; System.Drawing.SizeF size = System.Drawing.SizeF.Empty; g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixel; //this.UseGDIFontSize = true; if (opt.RichTextBoxCompatibility) { // 使用GDI模式计算字符大小 string key = rs.FontName + "|" + rs.FontSize + "|" + rs.FontStyle; GDIFont font = null; if (gdiFonts.ContainsKey(key)) { font = gdiFonts[key]; } else { font = new GDIFont( rs.FontName, //(int)( rs.FontSize * 1.3837 * 3 ), (int)(GraphicsUnitConvert.Convert( rs.FontSize, GraphicsUnit.Point, GraphicsUnit.Document)), rs.Bold, rs.Italic, rs.Underline, rs.Strikeout); gdiFonts[key] = font; } Size[] sizes = font.MeasureCharactersSize(g, chr.CharValue.ToString()); size.Width = sizes[0].Width; size.Height = sizes[0].Height; if (chr.CharValue == ' ') { if (rs.FontName == "Times New Roman") { size.Width = size.Width * 1.28f; } } } else { if (chr.CharValue == ' ') { size = g.MeasureString( " ", rs.Font.Value, 10000, System.Drawing.StringFormat.GenericDefault); size.Width = size.Width * 0.57f; } else if (chr.CharValue == '\t') { } else { size = g.MeasureString( chr.CharValue.ToString(), rs.Font.Value, 10000, myMeasureFormat); size.Width = size.Width; } } chr.Width = size.Width; chr.Height = size.Height + 1; chr._FontHeight = rs.Font.GetHeight(g); chr.Height = chr._FontHeight;// size.Height; if (rs.Superscript) { chr.Height = chr.Height * 1.2f; } if (rs.Superscript || rs.Subscript) { chr.Width = chr.Width * 0.6f; } if (_EnableCharSizeBuffer) { if (charSizes != null) { Dictionary <char, SizeF> sizes2 = null; if (charSizes.ContainsKey(chr.StyleIndex)) { sizes2 = charSizes[chr.StyleIndex]; } else { sizes2 = new Dictionary <char, SizeF>(); charSizes[chr.StyleIndex] = sizes2; } sizes2[chr.CharValue] = new SizeF(chr.Width, chr.Height); } } }