private static XStringFormat CreateStringFormat(XStringAlignment alignment, XLineAlignment lineAlignment) { XStringFormat format = new XStringFormat(); format.Alignment = alignment; format.LineAlignment = lineAlignment; return(format); }
///<summary>The pdfSharp version of drawstring. g is used for measurement. scaleToPix scales xObjects to pixels.</summary> public static void DrawStringX(XGraphics xg,Graphics g,double scaleToPix,string str,XFont xfont,XBrush xbrush,XRect xbounds, XStringAlignment sa) { //There are two coordinate systems here: pixels (used by us) and points (used by PdfSharp). //MeasureString and ALL related measurement functions must use pixels. //DrawString is the ONLY function that uses points. //pixels: Rectangle bounds=new Rectangle((int)(scaleToPix*xbounds.Left), (int)(scaleToPix*xbounds.Top), (int)(scaleToPix*xbounds.Width), (int)(scaleToPix*xbounds.Height)); FontStyle fontstyle=FontStyle.Regular; if(xfont.Style==XFontStyle.Bold) { fontstyle=FontStyle.Bold; } //pixels: (except Size is em-size) Font font=new Font(xfont.Name,(float)xfont.Size,fontstyle); //pixels: SizeF fit=new SizeF((float)(bounds.Width-rightPad),(float)(font.Height)); StringFormat format=StringFormat.GenericTypographic; //pixels: float pixelsPerLine=LineSpacingForFont(font.Name) * (float)font.Height; float lineIdx=0; int chars; int lines; //points: RectangleF layoutRectangle; for(int ix=0;ix<str.Length;ix+=chars) { if(bounds.Y+topPad+pixelsPerLine*lineIdx>bounds.Bottom) { break; } //pixels: g.MeasureString(str.Substring(ix),font,fit,format,out chars,out lines); //PdfSharp isn't smart enough to cut off the lower half of a line. //if(bounds.Y+topPad+pixelsPerLine*lineIdx+font.Height > bounds.Bottom) { // layoutH=bounds.Bottom-(bounds.Y+topPad+pixelsPerLine*lineIdx); //} //else { // layoutH=font.Height+2; //} //use points here: float adjustTextDown=10f;//this value was arrived at by trial and error. layoutRectangle=new RectangleF( (float)xbounds.X, //(float)(xbounds.Y+(float)topPad/scaleToPix+(pixelsPerLine/scaleToPix)*lineIdx), (float)(xbounds.Y+adjustTextDown+(pixelsPerLine/scaleToPix)*lineIdx), (float)xbounds.Width+50,//any amount of extra padding here will not cause malfunction 0);//layoutH); XStringFormat sf=XStringFormats.Default; sf.Alignment=sa; //sf.LineAlignment= XLineAlignment.Near; //xg.DrawString(str.Substring(ix,chars),xfont,xbrush,layoutRectangle,sf); xg.DrawString(str.Substring(ix,chars),xfont,xbrush,(double)layoutRectangle.Left,(double)layoutRectangle.Top,sf); lineIdx+=1; } }
private static XStringFormat CreateStringFormat(XStringAlignment alignment, XLineAlignment lineAlignment) { XStringFormat format = new XStringFormat(); format.Alignment = alignment; format.LineAlignment = lineAlignment; return format; }