Print several BitmapSources with different page orientations

370 views Asked by At

In C#, I have a collection of BitmapSource that are in the same document. These BitmapSources have independent page orientation each, and I want to print them, keeping the page orientation set for each page, in the same print job.

Today, I'm unable to do it, since I'm using PrintDialog class, and each BitmapSource is added to a FixedPage, inside a FixedDocument - with this approach, I can only set the page orientation to the whole document (one orientation per print ticket). What should I do to print several BitmapSources with different page orientations in a single print job (in the PrintDialog class, using only one print ticket)?

1

There are 1 answers

0
KCL On BEST ANSWER

I can't leave comments yet since I only have 11 rep, but I'll try to give your the information I have which might help.

A couple things: it may depend on how you're creating the FixedDocument and generating FixedPages. If you're first generating a FixedPage and then adding it to a FixedDocument, I can think of one thing you can try.

Also, it depends on how you're printing the pages, are printing each page or do you want to call print once, and print the entire document.

Anyways, one way to do it is to adjust the sizes of the fixed pages before you add them to the document. If you use the print dialog, take the print ticket properties and scale your controls or what have you, to the size they need to be.

This worked for me:

private void Button_Click_1(object sender, RoutedEventArgs e)
{
    var fp1 = new FixedPage() {Height=1056, Width =816};
    var g = new Grid();
    g.Children.Add(new Rectangle { Height = 1056, Width = 816, Fill = Brushes.Orange });
    fp1.Children.Add(g);

    var fp2 = new FixedPage() { Height = 816, Width = 1056 };
    var g1 = new Grid();
    g1.Children.Add(new Rectangle { Height = 816, Width = 1056, Fill = Brushes.Pink });
    fp2.Children.Add(g1);

    var fd = new FixedDocument();

    fd.Pages.Add(new PageContent{Child = fp1 });
    fd.Pages.Add(new PageContent{Child = fp2 });

    var d = new XpsDocument(@"C:\users\me\desktop\tmp.xps",
                            FileAccess.ReadWrite);

    var doc =
        XpsDocument.CreateXpsDocumentWriter(d);

    doc.Write(fd);
    d.Close();
}

And tmp.xps in Windows Explorer preview looks like:

tmp.xps

Let's say you don't want to print it to a file, or you want to send it to a printer as well.

var hardCopy = fd.DocumentPaginator;
var pd = new PrintDialog();
if(pd.ShowDialog() == true)
{
    pd.PrintQueue.AddJob("My Document",
                         @"C:\users\me\desktop\tmp.xps",
                         false);
}

Note: The print out will only have correct orientation if you print using a XPS device. See PrintQueue.IsXpsDevice

If this doesn't suit your needs, you will have to write your own DocumentPaginator which allows you to handle printing for each page. One option you have here is to transform your visuals/pages as they are called in DocumentPaginator.GetPage(int index). Remember to call Measure, Arrange, and UpdateLayout for each page if you change any UI properties.