我有一个带有波纹管属性的wpf usercontrol:
我想在尺寸超过(宽度= 170毫米,高度= 85毫米)的纸张上打印
我的问题是:当我打印它时,一些物品打印出来的纸张,我认为纸张大小如果是默认的,如果它是正确的,我怎么能把它改成宽度和高度以上?
我的代码如下:
var p = new myUserControl();
var pDoc = new System.Windows.Controls.PrintDialog();
if (pDoc.ShowDialog().Value)
{
pDoc.PrintVisual(p, "MyPrint");
}
可能是这样的需求(这是 System.Windows.Forms.PrintDialog 的设置,但我使用 System.Windows.Controls.PrintDialog 它没有< strong> PrinterSettings 属性):
var printerSettings = new PrinterSettings();
var labelPaperSize = new PaperSize { RawKind = (int)PaperKind.A6, Height = 148, Width = 105 };
printerSettings.DefaultPageSettings.PaperSize = labelPaperSize;
var labelPaperSource = new PaperSource { RawKind = (int)PaperSourceKind.Manual };
printerSettings.DefaultPageSettings.PaperSource = labelPaperSource;
if (printerSettings.CanDuplex)
{
printerSettings.Duplex = Duplex.Default;
}
答案 0 :(得分:2)
在WPF 1单位= 1/96英寸,因此您可以使用此公式计算您的尺寸(英寸)
您可以将printDlg.PrintTicket.PageMediaSize设置为Paper的大小,然后将窗口转换为在该区域中打印,如下所示:
private void _print()
{
PrintDialog printDlg = new System.Windows.Controls.PrintDialog();
PrintTicket pt = printDlg.PrintTicket;
Double printableWidth = pt.PageMediaSize.Width.Value;
Double printableHeight = pt.PageMediaSize.Height.Value;
Double xScale = (printableWidth - xMargin * 2) / printableWidth;
Double yScale = (printableHeight - yMargin * 2) / printableHeight;
this.Transform = new MatrixTransform(xScale, 0, 0, yScale, xMargin, yMargin);
//now print the visual to printer to fit on the one page.
printDlg.PrintVisual(this, "Print Page");
}