Hi,
I am creating xps report dynamically using wpf. My approach was to use the
method provided by drawing context class to draw text, rectangle, line and etc,
but the "
alignment
" is not so correct. All my "
alignment
" is done using some complex
calculation. I Know that WPF is freeflowing can any one suggest the better approach,
so that I can use WPF full functionality and I can aviod the calculation.
I will send the some sample code:
visual = new DrawingVisual();
drawingContext = visual.RenderOpen();
private void PrintHeader() {
x = Sizes.Gutter;
y = Sizes.Margin;
Rect rect = new Rect(Sizes.Gutter,Sizes.Margin,Sizes.Width,Sizes.Header);
drawingContext.DrawRectangle(color,Pens.Blueline,rect);
Bitmap bitmap = Resources.Logo;
BitmapSource imgSource = ConvertToBitmapSource(bitmap);
drawingContext.DrawImage(imgSource, new Rect(Sizes.Gutter + 10, Sizes.Margin + 10, 150, 50));
DrawCentered(GetMetaText("Title").ToUpper(), Font.Header, Brushes.White, rect, FontSizes.Header);
rect.Offset(0,20);
DrawCentered(GetMetaText("Department"), Font.Body, Brushes.White, rect, FontSizes.Body);
rect.Offset(0,10);
DrawCentered(GetMetaText("Phone"), Font.Body, Brushes.White, rect, FontSizes.Body);
Rect lineRect = new Rect(rect.Left,0,rect.Width,Sizes.Leading);
lineRect.Y = rect.Bottom - (((float)value - bounds.MinY) * rect.Height / (bounds.Height));
}
internal void DrawLeft(string text,Typeface font,Brush brush,Rect rect,int size) {
rect.Inflate(-Sizes.Padding,-Sizes.Padding);
drawingContext.DrawText(new FormattedText(text,CultureInfo.GetCultureInfo("en-us"),
FlowDirection.LeftToRight,font,
12,brush),new Point(rect.X,rect.Y));
}
internal void DrawCentered(string text,Typeface font,Brush brush,Rect rect,float size) {
drawingContext.DrawText(new FormattedText(text,CultureInfo.GetCultureInfo("en-us"),
FlowDirection.LeftToRight,font,
size,brush),new Point(rect.X,rect.Y));
}
internal void DrawRight(string text,Typeface font,Brush brush,Rect rect,float size) {
rect.Inflate(-Sizes.Padding,-Sizes.Padding);
drawingContext.DrawText(new FormattedText(text,CultureInfo.GetCultureInfo("en-us"),
FlowDirection.LeftToRight,font,
size,brush),new Point(rect.Y - 20,rect.Y));
}