缩放 WPF 窗口以进行打印



晚上好,

我正在 VS2017 中开发一个使用 c# 的小型 WPF 应用程序。它本质上只是为我在现场的工人进行计算。我已经构建和测试了所有内容,但是,在打印结果时遇到了问题。我已经搜索了互联网的角落,并且已经让打印对话框打开甚至打印为PDF。我遇到的问题是,当它打印时,它是全尺寸的。我只需要能够将应用程序窗口缩放到其大小的 80-90%,然后打印。我将添加代码,看看我是否只是忽略了某些东西。

    private void InvokePrint(object sender, RoutedEventArgs e)
    {
        PrintDialog printDlg = new System.Windows.Controls.PrintDialog();
        if (printDlg.ShowDialog() == true)
        {
            //get selected printer capabilities
            System.Printing.PrintCapabilities capabilities = printDlg.PrintQueue.GetPrintCapabilities(printDlg.PrintTicket);

            //get scale of the print wrt to screen of WPF visual
            double scale = Math.Min((ActualWidth*.9),ActualHeight);

            //get the size of the printer page
            Size sz = new Size((Width*.9), ActualHeight);

            //update the layout of the visual to the printer page size.
            this.Measure(sz);
            this.Arrange(new Rect(new Point(0,0), sz));

            //now print the visual to printer to fit on the one page.
            printDlg.PrintVisual(this, "Offset Calculations");

它是什么打印

我要打印的内容

添加对 ReachFramework 的引用.dll 4.0

        PrintDialog dlg = new PrintDialog();
        if ((bool)dlg.ShowDialog().GetValueOrDefault()) {
            //switch landscape
            dlg.PrintTicket.PageOrientation = System.Printing.PageOrientation.Landscape;
            //get selected printer capabilities
            System.Printing.PrintCapabilities capabilities = dlg.PrintQueue.GetPrintCapabilities(dlg.PrintTicket);
            //get scale of the print wrt to screen of WPF visual
            double scale = Math.Min(capabilities.PageImageableArea.ExtentWidth / this.ActualWidth, capabilities.PageImageableArea.ExtentHeight /  this.ActualHeight);
            //Transform the Visual to scale
            this.LayoutTransform = new ScaleTransform(scale, scale);
            //get the size of the printer page
            Size sz = new Size(capabilities.PageImageableArea.ExtentWidth, capabilities.PageImageableArea.ExtentHeight);
            //update the layout of the visual to the printer page size.
            this.Measure(sz);
            this.Arrange(new Rect(new System.Windows.Point((int)capabilities.PageImageableArea.OriginWidth, (int)capabilities.PageImageableArea.OriginHeight), sz));
            //show the print dialog
            dlg.PrintVisual(this, "MyDoc_" + DateTime.Now.ToString("yyyyMMdd_HHmmss"));
        }

最新更新