使用WPF动态创建xps报表的最佳方法是什么? [英] what is the best approch to create a xps report dynamically using WPF
问题描述
我正在使用wpf动态创建xps报告.我的方法是使用
绘图上下文类提供的用于绘制文本,矩形,线条等的方法,
但是"alignment
"不是很正确.我所有的"alignment
"都是使用一些复杂的方法完成的
计算.我知道WPF是自由流动的,任何人都可以提出更好的方法,
这样我就可以使用WPF的全部功能,并且可以避免计算.
我将发送一些示例代码:
visual = 新 DrawingVisual();
drawingContext = visual.RenderOpen();
私有 无效 PrintHeader(){
x = Sizes.Gutter;
y = Sizes.Margin;
Rect rect = 新 Rect(Sizes.Gutter,Sizes.Margin,Sizes.Width,Sizes.Header);
drawingContext.DrawRectangle(color,Pens.Blueline,rect);
位图位图= Resources.Logo;
BitmapSource imgSource = ConvertToBitmapSource(位图);
drawingContext.DrawImage(imgSource,新 Rect(Sizes.Gutter + 10 ,Sizes.Margin + 10 , 150 , 50 )));
DrawCentered(GetMetaText(" ).ToUpper(),Font.Header,Brushes.White,rect ,FontSizes.Header);
rect.Offset( 0 , 20 );
DrawCentered(GetMetaText(" ),Font.Body,Brushes.White,rect,FontSizes.Body );
rect.Offset( 0 , 10 );
DrawCentered(GetMetaText(" ),Font.Body,Brushes.White,rect,FontSizes.Body );
Rect lineRect = 新 Rect(rect.Left, 0 ,rect.Width,Sizes.Leading);
lineRect.Y =矩形底部-(((( float ) value -bounds.MinY)*矩形.高度/(bounds.Height));
}
内部 无效 DrawLeft(字符串文本,字体字体,画笔,矩形, int 大小){
rect.Inflate(-Sizes.Padding,-Sizes.Padding);
drawingContext.DrawText(新 FormattedText(text,CultureInfo.GetCultureInfo(" ),
FlowDirection.LeftToRight,字体,
12 ,画笔),新 Point(rect.X,rect.Y));
}
内部 无效 DrawCentered(字符串文本,字体字体,画笔,矩形,浮动大小){
drawingContext.DrawText(新 FormattedText(text,CultureInfo.GetCultureInfo(" ),
FlowDirection.LeftToRight,字体,
大小,画笔),新 Point(rect.X,rect.Y));
}
内部 无效 DrawRight(字符串文本,字体字体,画笔,矩形,浮动大小){
rect.Inflate(-Sizes.Padding,-Sizes.Padding);
drawingContext.DrawText(新 FormattedText(text,CultureInfo.GetCultureInfo(" ),
FlowDirection.LeftToRight,字体,
大小,画笔),新点(正Y- 20 ,正Y));
}
我会使用FlowDocument.这样,您可以在XAML中生成报告.
<FlowDocumentReader> <FlowDocument Name="flowDocument" ColumnWidth="400" FontSize="14" FontFamily="Georgia"> <Paragraph> <Grid> <Rectangle Fill="Red" Width="200" Height="100"/> <TextBlock Text="Hello World!" HorizontalAlignment="Center" VerticalAlignment="Center"/> </Grid> </Paragraph> </FlowDocument> </FlowDocumentReader>
使用一些C#代码将其另存为XPS:
公共 静态 void SaveAsXps(字符串路径,FlowDocument文档) { 使用( var package = Package.Open(路径,FileMode.Create)) { 使用( var xpsDocument = 新 XpsDocument(包,System.IO.Packaging.CompressionOption.Maximum)) { var xpsSerializationManager = 新 XpsSerializationManager(新 XpsPackagingPolicy (xpsDocument), false ); var documentPaginator =(((IDocumentPaginatorSource)document).DocumentPaginator; xpsSerializationManager.SaveAsXaml(documentPaginator); } } }
最后是对该函数的调用:
SaveAsXps(" ,flowDocument);
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));
}
I would use the FlowDocument. That way, you can produce your report in XAML.
<FlowDocumentReader> <FlowDocument Name="flowDocument" ColumnWidth="400" FontSize="14" FontFamily="Georgia"> <Paragraph> <Grid> <Rectangle Fill="Red" Width="200" Height="100"/> <TextBlock Text="Hello World!" HorizontalAlignment="Center" VerticalAlignment="Center"/> </Grid> </Paragraph> </FlowDocument> </FlowDocumentReader>
With some C# code to save it as XPS:
public static void SaveAsXps(string path, FlowDocument document) { using (var package = Package.Open(path, FileMode.Create)) { using (var xpsDocument = new XpsDocument(package, System.IO.Packaging.CompressionOption.Maximum)) { var xpsSerializationManager = new XpsSerializationManager(new XpsPackagingPolicy(xpsDocument), false); var documentPaginator = ((IDocumentPaginatorSource)document).DocumentPaginator; xpsSerializationManager.SaveAsXaml(documentPaginator); } } }
And finally the call to this function:
SaveAsXps("temp.xps", flowDocument);
这篇关于使用WPF动态创建xps报表的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!