简体   繁体   中英

How to print flowdocument in landscape (WPF,C#)?

I'd like to print my programmatically created flowdocument in landscape mode and I tried all versions what I've found but none of them works. Here's my code below:

try
            {
                // Create a PrintDialog 
                PrintDialog printDlg = new PrintDialog();
                printDlg.PrintTicket.PageOrientation = System.Printing.PageOrientation.Landscape;


                // Create a FlowDocument dynamically. 
                FlowDocument doc = CreateFlowDocumentSum();
                doc.Name = "FlowDoc";
                doc.ColumnWidth = printDlg.PrintableAreaWidth;

                // Create IDocumentPaginatorSource from FlowDocument 
                IDocumentPaginatorSource idpSource = doc;



                // Call PrintDocument method to send document to printer 

                printDlg.PrintDocument(idpSource.DocumentPaginator, "sum");
                doc.Blocks.Clear();
                sumTable.Clear();

            }
            catch
            { }

I did it finally. Just modified the code in the print button event:

PrintDialog printDlg = new PrintDialog();
                LocalPrintServer ps = new LocalPrintServer();
                PrintQueue pq = ps.DefaultPrintQueue;

                PrintTicket pt = pq.UserPrintTicket;

                    pt.PageOrientation = PageOrientation.Landscape;

                FlowDocument doc = CreateFlowDocumentSum();


                doc.PageHeight = 768;
                doc.PageWidth = 1104;

                PageMediaSize pageMediaSize = new PageMediaSize(doc.PageWidth, doc.PageHeight);
                    pt.PageMediaSize = pageMediaSize;
                    IDocumentPaginatorSource source = doc as IDocumentPaginatorSource;


                printDlg.PrintDocument(source.DocumentPaginator, "sum");

Then in my FlowDocument I set the width and height:

FlowDocument docSum = new FlowDocument();
        docSum.PageHeight = 768;
        docSum.PageWidth = 1104;
        docSum.ColumnWidth = 1104;

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