打印XpsDocument缩放以适合页面

时间:2013-05-14 11:56:41

标签: .net wpf c#-4.0 .net-4.0 wpf-controls

我们在应用程序中使用Telerik的WPF图表控件。图表控件具有内置支持,可将图形导出到XPS文件。我想利用WPF的支持来打印XPS文档来打印图表。在大多数情况下,这很有效,但我无法将图表缩放到一个页面中。图表图形始终在右侧和底部切断。该图表对于页面来说太大了。有任何想法吗?

以下是我正在使用的代码:

const string packageUriString = "pack://inMemoryChart.xps";

using (var memoryStream = new MemoryStream())
{
    //utilize Telerik ChartView method to export to XPS... 
    _chart.ExportToXps(memoryStream);

    using (var package = Package.Open(memoryStream))
    {
        var packageUri = new Uri(packageUriString);

        PackageStore.AddPackage(packageUri, package);

        var xpsDocument = new XpsDocument(package, CompressionOption.Maximum, packageUriString);

        var printDialog = new PrintDialog();
        var result = printDialog.ShowDialog();

        if (!result.GetValueOrDefault())
            return;

        var fixedDocumentSequence = xpsDocument.GetFixedDocumentSequence();

        if (fixedDocumentSequence == null)
            return;

        var documentPaginator = fixedDocumentSequence.DocumentPaginator;

        if (documentPaginator == null)
            return;

        //attempt to adjust to fit page?
        AdjustFlowDocumentToPage(documentPaginator, printDialog.PrintQueue); 

        printDialog.PrintDocument(documentPaginator , "Chart");

        PackageStore.RemovePackage(packageUri);
    }
}

我在另一个site找到了AdjustFlowDocumentToPage:

public static void AdjustFlowDocumentToPage(DocumentPaginator documentPaginator, PrintQueue printQueue)
{
   const double inch = 96;

   var printTicket = printQueue.UserPrintTicket;

   // Get the media size.
   double width = printTicket.PageMediaSize.Width.Value;
   double height = printTicket.PageMediaSize.Height.Value;

   // Set the margins.
   const double leftmargin = 1.25 * inch;
   const double rightmargin = 1.25 * inch;
   const double topmargin = 1 * inch;
   const double bottommargin = 1 * inch;

   // Calculate the content size.
   var contentwidth = width - leftmargin - rightmargin;
   var contentheight = height - topmargin - bottommargin;

   documentPaginator.PageSize = new Size(contentwidth, contentheight);
}

1 个答案:

答案 0 :(得分:0)

经过进一步研究,我发现了以下实施,它看起来效果很好! http://www.telerik.com/community/forums/wpf/chart/behaviour-based-chart-print-solution.aspx