PrintDialog/XPSドキュメントライターの紙のサイズを無視しました
-
29-10-2019 - |
質問
WPFで印刷しようとしています PrintDialog
class(namespace system.windows.controls in PresentationFramework.dll、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ドキュメントライター」に選択した紙のサイズに関係なく、生成されたXPSは常に、幅と高さを持っています」手紙" 紙の種類:
これは、内部XPSパッケージを見つけることができるXAMLコードです。
<FixedPage ... Width="816" Height="1056">
解決
プリントダイアログのペーパーサイズを変更すると、固定ページのコンテンツではなく、印刷物にのみ影響します。 PrintVisualメソッドは文字サイズのページを作成するため、異なるページサイズを作成するには、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");
}
}
所属していません StackOverflow