简体   繁体   中英

WPF Print XAML Control

I'm trying to add a Print function to a WPF Project. The user can add and delete text to textbox or other controls at runtime so the size of the xaml control is not fixed. If he clicks the print button i want to figure out the size of the control and print a (if neccessary multipage) document. this is my code so far

public void Print(FrameworkElement element)
    {

        System.Windows.Controls.PrintDialog printDlg = new System.Windows.Controls.PrintDialog();
        if (printDlg.ShowDialog() == true) {

            double height = element.ActualHeight;
            double width = element.ActualWidth;
            Size pageSize = new Size(printDlg.PrintableAreaWidth, printDlg.PrintableAreaHeight);
            //element.Measure(pageSize);
            //element.Arrange(new Rect(5, 5, pageSize.Width, pageSize.Height));

            printDlg.PrintVisual(element, "this is a test");
        }

    }

My idea is to check the actual height and width of the control. If one of these is larger than the pagesize, I know i have to print multiple pages. I'm not quite sure how to do that, but i think i will have to make use of XPSDocument class. Can someone please help me, I dont know how I can split my document into multiple pages and print them and can someone also tell me how to create a FlowDocument from my xaml code.

thanks in advance!

By default, you can't print control on multiple pages, but you can do it manually. Here's the GREAT article, which solves your problem: http://www.codeproject.com/Articles/164033/WPF-Visual-Print-Component

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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