itsho
itsho

Reputation: 4800

how do I set Paper Type while using PrinterDialog?

I'm trying to silently print a picture file and i need to print it on special paper type ("Glossy Photo Paper"), and on certain size (10cm on 15cm).

On normal windows 7 print dialog i can choose:

Paper Size,

Paper Quality (for example - "Auto", "High", "Standard", "Custom")

Paper Type ("Plain paper", "Glossy photo paper", "Photo Paper Plus Glossy", "Photo Paper Pro Platinum", "Hagaki", etc...)

But, through c# code, I've managed to set only the PaperSize (which is 4'' on 6'' == 10cm on 15cm).

My problem is how do i get the option to set the Paper Type, and not the PaperSource ("Tray 1", "Tray 2", etc)....

I know that every printer has its own Paper Types which it supports, so i probably need to iterate through it all, but I just couldn't figure it how.

this is my current code:

string strPrinterName = "Canon iP4850";

PrintDocument printDoc = new PrintDocument();

// We set the paper size
printDoc.DefaultPageSettings.PaperSize = new PaperSize("PhotoPaper", 400, 600);

// Inside the event i actually draw the image all over the paper by using e.Graphics.DrawImage(...)
printDoc.PrintPage += PrintDocPrintPage;

// Creating the print dialog
PrintDialog dlgPrint = new PrintDialog
{
    Document = printDoc
};

// We choose the printer
dlgPrint.PrinterSettings.PrinterName = strPrinterName;

// just to be sure - give the new size of our paper
dlgPrint.PrinterSettings.DefaultPageSettings.PaperSize = new PaperSize("PhotoPaper", 400, 600);

// If the printer is invalid
if (!dlgPrint.PrinterSettings.IsValid)
{
    throw new Exception(@"Printer is invalid" + Environment.NewLine + strPrinterName);
}

// Print without showing the dialog
printDoc.Print();

thank you all in advance.

Upvotes: 2

Views: 4794

Answers (2)

user957902
user957902

Reputation: 3060

I'm not saying that it is impossible, but its not going to be pretty. Theoretically you can get the DEVMODE structure of the device( that structure will have extensions that are specific to the printer driver) set the correct values, and then write it back. There are some helper functions the the PrinterSettings object to do this. There is an example of doing that here

Upvotes: 2

P.Brian.Mackey
P.Brian.Mackey

Reputation: 44285

This can actually be done without DEVMODE. Set the paper type via PrintTicket.PageMediaType property. For example:

    // ---------------------- GetPrintTicketFromPrinter ----------------------- 
    /// <summary> 
    ///   Returns a PrintTicket based on the current default printer.</summary> 
    /// <returns> 
    ///   A PrintTicket for the current local default printer.</returns> 
    public PrintTicket GetPrintTicketFromPrinter()
    {
        PrintQueue printQueue = null;

        var localPrintServer = new LocalPrintServer();

        // Retrieving collection of local printer on user machine
        PrintQueueCollection localPrinterCollection = localPrintServer.GetPrintQueues();

        System.Collections.IEnumerator localPrinterEnumerator =
            localPrinterCollection.GetEnumerator();

        if (localPrinterEnumerator.MoveNext())
        {
            // Get PrintQueue from first available printer
            printQueue = (PrintQueue)localPrinterEnumerator.Current;
        }
        else
        {
            // No printer exist, return null PrintTicket 
            return null;
        }

        // Get default PrintTicket from printer
        PrintTicket printTicket = printQueue.DefaultPrintTicket;

        PrintCapabilities printCapabilites = printQueue.GetPrintCapabilities();

        // Modify PrintTicket 
        if (printCapabilites.PageMediaTypeCapability.Contains(PageMediaType.CardStock))
        {
            printTicket.PageMediaType = PageMediaType.CardStock;
        }

        return printTicket;
    }

Upvotes: 3

Related Questions