简体   繁体   English

缩放WPF窗口进行打印

[英]Scaling WPF Window for Printing

Good Evening, 晚上好,

I am working on a small WPF app in VS2017 which uses c#. 我正在使用c#的VS2017中开发一个小型WPF应用程序。 Its essentially just to make calculations for my workers in the field. 从本质上来说,这只是为我在该领域的工人进行计算。 I have everything built and tested, however, I am running into an issue when it comes to printing the results. 我已经构建并测试了所有内容,但是在打印结果时遇到了一个问题。 I have scoured the corners of the internet and have gotten as far as getting the print dialog box to open and even print to PDF. 我搜寻了互联网的各个角落,甚至可以打开打印对话框,甚至可以打印为PDF。 The issue i am having is that when it prints, it is in full scale. 我遇到的问题是,当它打印时,它是完整的。 I just need to be able to scale the app window to 80-90% of its size and then print. 我只需要能够将应用程序窗口缩放到其大小的80-90%,然后打印即可。 i will add the code and see if i am just overlooking something. 我将添加代码,看看我是否只是在忽略某些东西。

    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");

What it is printing 它在打印什么

What I want to print 我想打印什么

add reference to ReachFramework.dll 4.0 添加对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"));
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM