我正在尝试使用 WPF 的 PrintDialog
进行打印类(PresentationFramework.dll 中的命名空间 System.Windows.Controls,v4.0.30319)。这是我使用的代码:
private void PrintMe()
{
var dlg = new PrintDialog();
if (dlg.ShowDialog() == true)
{
dlg.PrintVisual(new System.Windows.Shapes.Rectangle
{
Width = 100,
Height = 100,
Fill = System.Windows.Media.Brushes.Red
}, "test");
}
}
问题是无论我为“Microsoft XPS Document Writer”选择什么纸张尺寸,生成的 XPS 始终具有“Letter”纸张类型的宽度和高度:
这是我可以在 XPS 包中找到的 XAML 代码:
<FixedPage ... Width="816" Height="1056">
最佳答案
在打印对话框中更改纸张大小只会影响 PrintTicket,不会影响 FixedPage 内容。 PrintVisual 方法生成 Letter 大小的页面,因此为了获得不同的页面大小,您需要使用 PrintDocument 方法,如下所示:
private void PrintMe()
{
var dlg = new PrintDialog();
FixedPage fp = new FixedPage();
fp.Height = 100;
fp.Width = 100;
fp.Children.Add(new System.Windows.Shapes.Rectangle
{
Width = 100,
Height = 100,
Fill = System.Windows.Media.Brushes.Red
});
PageContent pc = new PageContent();
pc.Child = fp;
FixedDocument fd = new FixedDocument();
fd.Pages.Add(pc);
DocumentReference dr = new DocumentReference();
dr.SetDocument(fd);
FixedDocumentSequence fds = new FixedDocumentSequence();
fds.References.Add(dr);
if (dlg.ShowDialog() == true)
{
dlg.PrintDocument(fds.DocumentPaginator, "test");
}
}
关于c# - PrintDialog/XPS Document Writer 中忽略的纸张大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7390818/