how do I set Paper Type while using PrinterDialog?

安稳与你 提交于 2019-12-04 09:48:12

问题


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.


回答1:


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




回答2:


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;
    }


来源:https://stackoverflow.com/questions/7668873/how-do-i-set-paper-type-while-using-printerdialog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!