Setting PageOrientation for the Wpf DocumentViewer PrintDialog
Asked Answered
A

2

18

Using the Wpf DocumentViewer control I can't figure out how to set the PageOrientation on the PrintDialog that the DocumentViewer displays when the user clicks the print button. Is there a way to hook into this?

Australia answered 16/6, 2009 at 19:48 Comment(0)
P
16

Mike's answer works. The way I chose to implement it was to instead create my own doc viewer derived from DocumentViewer. Also, casting the Document property to FixedDocument wasn't working for me - casting to FixedDocumentSequence was.

GetDesiredPageOrientation is whatever you need it to be. In my case, I'm inspecting the first page's dimensions, because I generate documents that are uniform size and orientation for all pages in the document, and with only one doc in the sequence.

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Controls;
using System.Windows.Xps;
using System.Printing;
using System.Windows.Documents;

public class MyDocumentViewer : DocumentViewer
{
    protected override void OnPrintCommand()
    {
        // get a print dialog, defaulted to default printer and default printer's preferences.
        PrintDialog printDialog = new PrintDialog();
        printDialog.PrintQueue = LocalPrintServer.GetDefaultPrintQueue();
        printDialog.PrintTicket = printDialog.PrintQueue.DefaultPrintTicket;

        // get a reference to the FixedDocumentSequence for the viewer.
        FixedDocumentSequence docSeq = this.Document as FixedDocumentSequence;

        // set the default page orientation based on the desired output.
        printDialog.PrintTicket.PageOrientation = GetDesiredPageOrientation(docSeq);

        if (printDialog.ShowDialog() == true)
        {
            // set the print ticket for the document sequence and write it to the printer.
            docSeq.PrintTicket = printDialog.PrintTicket;

            XpsDocumentWriter writer = PrintQueue.CreateXpsDocumentWriter(printDialog.PrintQueue);
            writer.WriteAsync(docSeq, printDialog.PrintTicket);
        }
    }
}
Procto answered 14/1, 2010 at 18:29 Comment(4)
I had already overridden the default DocumentViewer because it contains text that is non-l18n-friendly so this turned out to be a great solution. Thanks!Barnebas
Should it be printDialog.PrintTicket = printDialog.PrintQueue.DefaultPrintTicket;? Doesn't compile otherwiseLinctus
Would also be nice to see your implementation of GetDesiredPageOrientation!Linctus
Noted, and fixed. Thanks. Also: gist.github.com/mcw0933/5543526 Just call GetPageOrientationOfFirstPageOfFixedDocSeq.Procto
A
10

The workaround I used to set the orientation on my DocumentViewer's print dialog was to hide the print button on the DocumentViewer control by omitting the button from the template. I then provided my own print button and tied it to the following code:

public bool Print()
    {
        PrintDialog dialog = new PrintDialog();
        dialog.PrintQueue = LocalPrintServer.GetDefaultPrintQueue();
        dialog.PrintTicket = dialog.PrintQueue.DefaultPrintTicket;
        dialog.PrintTicket.PageOrientation = PageOrientation.Landscape;

        if (dialog.ShowDialog() == true)
        {
            XpsDocumentWriter writer = PrintQueue.CreateXpsDocumentWriter(dialog.PrintQueue);
            writer.WriteAsync(_DocumentViewer.Document as FixedDocument, dialog.PrintTicket);
            return true;
        }

        return false;
    }
Australia answered 1/7, 2009 at 11:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.