TalkingCode
TalkingCode

Reputation: 13557

PrintDialog with Landscape and Portrait pages

I have a System.Window.Controls.PrintDialog and I want to print pages with landscape mode and portrait mode in with single PrintTicket but it seems I only can set the orientation once.

As long as I want to print on paper this may not be a big deal but I want to print/create a PDF document from my Printjob. There I need pages with Landscape and Portrait mode in onc document.

So far I managed to rotate the landscape pages 90 degree and this works fine but in the PrintPreview it looks very strange because of the rotated content.

Upvotes: 8

Views: 2062

Answers (2)

amaca
amaca

Reputation: 1480

A long time later ...

You can do this by providing an EventHandler that gets called, asking for a custom PrintTicket, before each page is printed. The PageViewModel here effectively comprises

PageViewModel{
    Page Page {get;set;}
    PageOrientation? PageOrientation {get;set}
}

if (PrintDialog.ShowDialog() == true)
  {
    XpsDocumentWriter xpsDocumentWriter = PrintQueue.CreateXpsDocumentWriter(PrintDialog.PrintQueue);
    xpsDocumentWriter.WritingPrintTicketRequired += (s, e) =>
    {
      PageViewModel pVM = publicationVM.Pages[e.Sequence - 1];
      if (pVM.Orientation != null && pVM.Orientation != publicationVM.Orientation)
      {
        e.CurrentPrintTicket = new PrintTicket();
        e.CurrentPrintTicket.PageOrientation = PageOrientation.Portrait;
      }
    };
    VisualsToXpsDocument visualsToXpsDocument = (VisualsToXpsDocument) xpsDocumentWriter.CreateVisualsCollator(PrintDialog.PrintTicket, PrintDialog.PrintTicket);
    visualsToXpsDocument.BeginBatchWrite();
    Page page;
    foreach (PageViewModel pVM in publicationVM.Pages)
    {
      page = pVM.Page;
      visualsToXpsDocument.Write(page);
    }
    visualsToXpsDocument.EndBatchWrite();
  }

and bingo! mixed Portrait and Landscape. Duplexing this is fine too.

Upvotes: 3

crlanglois
crlanglois

Reputation: 3609

Not as clean as you would like and more of a workaround but I think it might accomplish your goal. You can use the iTextSharp library to concatenate PDFs into one following multiple print jobs. Here is some sample code.

Hopefully someone comes up with a more straight forward solution.

Upvotes: 1

Related Questions