yu_ominae
yu_ominae

Reputation: 2935

PrintDocument using multiple page sizes

Working in .NET 3.5.

Summary:

Trying to replicate functionality of an existing third party component, which breaks in Windows 7. Until now the user could select a bunch of image files to print, specify a page size for each image and then send them off to print all in one go. I am in dire need of a conceptual explanation of how to go about printing switching the page size on the fly when printing each page.

Details

So far I have figured out how to print multiple images all with the same page size. I use a list of images and use a PrintDocument object, setting the HasMorePages property of the PrintPageEventArgs to true until I reach the end of the list.

Here's a class I quickly threw together to test this:

public partial class Form1 : Form
{
    private List<Image> images { get; set; }

    private PrintDocument printDocument { get; set; }

    public Form1()
    {
        InitializeComponent();

        this.images = new List<Image>();
        this.images.Add(Image.FromFile(@"C:\test60.bmp"));
        this.images.Add(Image.FromFile(@"C:\SuperLargeTest.jpg"));

        this.printDocument = new PrintDocument()
        {
            PrinterSettings = new PrinterSettings()
        };
        this.printDocument.PrintPage += printDocument_PrintPage;
    }

    private void printDocument_PrintPage(object sender, PrintPageEventArgs e)
    {
        Graphics g = e.Graphics;

        e.PageSettings.PaperSize = this.paperSizes[this.currentImageIndex];

        RectangleF marginBounds = e.MarginBounds;
        RectangleF printableArea = e.PageSettings.PrintableArea;

        int availableWidth = (int)Math.Floor(printDocument.OriginAtMargins ? marginBounds.Width : (e.PageSettings.Landscape ? printableArea.Height : printableArea.Width));
        int availableHeight = (int)Math.Floor(printDocument.OriginAtMargins ? marginBounds.Height : (e.PageSettings.Landscape ? printableArea.Width : printableArea.Height));
        g.DrawRectangle(Pens.Red, 0, 0, availableWidth - 1, availableHeight - 1);
        g.DrawImage(this.images[currentImageIndex], printableArea);

        e.HasMorePages = ++currentImageIndex < this.images.Count();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        this.printDocument.OriginAtMargins = false;
        this.printDocument.Print();
    }
}

The thing that I really can't figure out is how to go about changing the page size for, say, the second image. If I wanted the first image to print in A4 and then the second one to print on A3, how would I go about doing that?

I found this SO question here which seemed to suggest changing the PageSize in the PrintPageEventArgs, but had no joy there.

I also tried to use the QueryPageSettingsEventArgs event and set the PageSettings there, but that didn't seem to work either...

What I would like to achieve is print multiple pages of different size as a single document. Any suggestions, links, explanations, sample code would be very much appreciated.

Anything in C# or VB.NET is fine.

Upvotes: 1

Views: 2672

Answers (3)

Nick
Nick

Reputation: 1

If you want all the pages to appear as one job (in short avoid being interleaved with other jobs), you can set the page size for the next page inside the PrintPage event handler by changing the default page size of the PrintDocument object.

Upvotes: 0

Eve Huguet
Eve Huguet

Reputation: 91

That's work for me too.

Translated to C#:

    private bool SetPaperSize(PrintDocument pd, PaperKind nKind)
    {
        foreach(System.Drawing.Printing.PaperSize ps in pd.PrinterSettings.PaperSizes)
        {
            if (ps.Kind == nKind)
            {
                pd.DefaultPageSettings.PaperSize = ps;
                return true;
            }
        }

        return false;
    }

Upvotes: 2

matzone
matzone

Reputation: 5719

In VB.NET .. You can use this Sub ..

DocPrint is PrintDocument var

Sub SetPaperSize(ByVal nKind As PaperKind)
        Dim ps As PaperSize

        For ix As Integer = 0 To DocPrint.PrinterSettings.PaperSizes.Count - 1
            If DocPrint.PrinterSettings.PaperSizes(ix).Kind = nKind Then
                ps = DocPrint.PrinterSettings.PaperSizes(ix)
                DocPrint.DefaultPageSettings.PaperSize = ps
            End If
        Next
    End Sub

Hope this help ..

Upvotes: 1

Related Questions