我们在我们的应用程序中使用 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);
}
}
我在另一个网站上找到了 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);
}