简体   繁体   中英

How to get predefined paper size by PaperKind

I need to get paper size by System.Drawing.Printing.PaperKind. Are there any predefined values? I don't want to hardcode or calculate paper sizes, I just want to get it programmatically. Thanks.

A subset of predefined values can be had by iterating over a PrinterSettings.PaperSizes collection.

Our application has the user select a printer, providing us with a PrinterSettings object. Contained within PrinterSettings is a list of PaperSize 's supported by the printer - not everything (note that the XPS Document Driver (win7) supports all sizes).

In our case this subset of supported sizes is all we need. A user specified PaperKind is passed to our printing code, and it goes through our PrinterSettings object until it either finds the user's selection or gives up and uses a default.

In the example below you can see that the PaperSize objects are correctly filled.

PrinterSettings settings = new PrinterSettings();
foreach (PaperSize size in settings.PaperSizes)
    Debug.WriteLine(size);

It's only a subset, but maybe that's also enough for you. the printing APIs in .NET are really unclear and msdn isn't really much help... Hopefully it puts you on the right track!

A LINQ way to achieve your goal is something like this:

PrinterSettings printerSettings = new PrinterSettings();
IQueryable<PaperSize> paperSizes = printerSettings.PaperSizes.Cast<PaperSize>().AsQueryable();
PaperSize a4rotated = paperSizes.Where(paperSize => paperSize.Kind == PaperKind.A4Rotated).FirstOrDefault();

Good luck!

public static PaperSize GetPaperSize(string Name)
    {
        PaperSize size1 = null;
        Name=Name.ToUpper();
        PrinterSettings settings = new PrinterSettings();
        foreach (PaperSize size in settings.PaperSizes)
            if (size.Kind.ToString().ToUpper() == Name)
            {
                size1 = size;
                break;
            }
        return size1;            
    }

You might try the System.Drawing.Printing.PaperSize class. There's a RawKind property which can be set to a System.Drawing.Printing.PaperKind.

Something like:

PaperSize size = new PaperSize();
size.RawKind = (int) PaperKind.A3;
PaperSize pkSize;
PrintDocument printDoc = new PrintDocument();
for (int i = 0; i < printDoc.PrinterSettings.PaperSizes.Count; i++)
{
    pkSize = printDoc.PrinterSettings.PaperSizes[i];
    cmbPaperSize.Items.Add(pkSize);
}

Taking the comments and research by @hackerhasid , I reworked the deleted dotnet code for my own purposes returning a PaperSize class instead of a Point structure as done by the original code.

I needed this in restricted security environments where printer access might be limited. I found that the restricted access prevented me from iterating over printer defined page sizes.

A null is returned if the PaperKind is undefined or no longer supported.

NOTE: The PaperSize class stores the numbers as hundredths of an inch.

    private static PaperSize GetPaperSize(PaperKind kind)
    {
        // NOTE: This code is a resurrected and refactored version of code originally found at:
        // http://www.dotnetframework.org/default.aspx/DotNET/DotNET/8@0/untmp/whidbey/REDBITS/ndp/fx/src/CommonUI/System/Drawing/Printing/PaperSize@cs/1/PaperSize@cs

        var fnInches = new Func<double, double, PaperSize>((width, height) =>
        {
            Debug.Assert(width < 50 && height < 50, "You said inches, but you probably meant millimeters (" + width + ", " + height + ")");
            var ps = new PaperSize(kind.ToString(), (int)(width * 100), (int)(height * 100));
            ps.RawKind = (int)kind;
            return ps;
        });

        var fnMillimeters = new Func<double, double, PaperSize>((width, height) =>
        {
            Debug.Assert(width > 50 && height > 50, "You said millimeters, but you probably meant inches (" + width + ", " + height + ")");
            var ps = new PaperSize(kind.ToString(), (int)(width / 25.4 * 100), (int)(height / 25.4 * 100));
            ps.RawKind = (int)kind;
            return ps;
        });

        switch (kind)
        {
            case PaperKind.Letter: return fnInches(8.5, 11);
            case PaperKind.Legal: return fnInches(8.5, 14);
            case PaperKind.A4: return fnMillimeters(210, 297);
            case PaperKind.CSheet: return fnInches(17, 22);
            case PaperKind.DSheet: return fnInches(22, 34);
            case PaperKind.ESheet: return fnInches(34, 44);
            case PaperKind.LetterSmall: return fnInches(8.5, 11);
            case PaperKind.Tabloid: return fnInches(11, 17);
            case PaperKind.Ledger: return fnInches(17, 11);
            case PaperKind.Statement: return fnInches(5.5, 8.5);
            case PaperKind.Executive: return fnInches(7.25, 10.5);
            case PaperKind.A3: return fnMillimeters(297, 420);
            case PaperKind.A4Small: return fnMillimeters(210, 297);
            case PaperKind.A5: return fnMillimeters(148, 210);
            case PaperKind.B4: return fnMillimeters(250, 354);
            case PaperKind.B5: return fnMillimeters(182, 257);
            case PaperKind.Folio: return fnInches(8.5, 13);
            case PaperKind.Quarto: return fnMillimeters(215, 275);
            case PaperKind.Standard10x14: return fnInches(10, 14);
            case PaperKind.Standard11x17: return fnInches(11, 17);
            case PaperKind.Note: return fnInches(8.5, 11);
            case PaperKind.Number9Envelope: return fnInches(3.875, 8.875);
            case PaperKind.Number10Envelope: return fnInches(4.125, 9.5);
            case PaperKind.Number11Envelope: return fnInches(4.5, 10.375);
            case PaperKind.Number12Envelope: return fnInches(4.75, 11);
            case PaperKind.Number14Envelope: return fnInches(5, 11.5);
            case PaperKind.DLEnvelope: return fnMillimeters(110, 220);
            case PaperKind.C5Envelope: return fnMillimeters(162, 229);
            case PaperKind.C3Envelope: return fnMillimeters(324, 458);
            case PaperKind.C4Envelope: return fnMillimeters(229, 324);
            case PaperKind.C6Envelope: return fnMillimeters(114, 162);
            case PaperKind.C65Envelope: return fnMillimeters(114, 229);
            case PaperKind.B4Envelope: return fnMillimeters(250, 353);
            case PaperKind.B5Envelope: return fnMillimeters(176, 250);
            case PaperKind.B6Envelope: return fnMillimeters(176, 125);
            case PaperKind.ItalyEnvelope: return fnMillimeters(110, 230);
            case PaperKind.MonarchEnvelope: return fnInches(3.875, 7.5);
            case PaperKind.PersonalEnvelope: return fnInches(3.625, 6.5);
            case PaperKind.USStandardFanfold: return fnInches(14.875, 11);
            case PaperKind.GermanStandardFanfold: return fnInches(8.5, 12);
            case PaperKind.GermanLegalFanfold: return fnInches(8.5, 13);

            case PaperKind.JapanesePostcard: return fnMillimeters(100, 148);
            case PaperKind.Standard9x11: return fnInches(9, 11);
            case PaperKind.Standard10x11: return fnInches(10, 11);
            case PaperKind.Standard15x11: return fnInches(15, 11);
            case PaperKind.InviteEnvelope: return fnMillimeters(220, 220);
            //= SafeNativeMethods.DMPAPER_RESERVED_48, 
            //= SafeNativeMethods.DMPAPER_RESERVED_49,
            case PaperKind.LetterExtra: return fnInches(9.275, 12);
            case PaperKind.LegalExtra: return fnInches(9.275, 15);
            case PaperKind.TabloidExtra: return fnInches(11.69, 18);
            case PaperKind.A4Extra: return fnInches(9.27, 12.69);
            case PaperKind.LetterTransverse: return fnInches(8.275, 11);
            case PaperKind.A4Transverse: return fnMillimeters(210, 297);
            case PaperKind.LetterExtraTransverse: return fnInches(9.275, 12);
            case PaperKind.APlus: return fnMillimeters(227, 356);
            case PaperKind.BPlus: return fnMillimeters(305, 487);
            case PaperKind.LetterPlus: return fnInches(8.5, 12.69);
            case PaperKind.A4Plus: return fnMillimeters(210, 330);
            case PaperKind.A5Transverse: return fnMillimeters(148, 210);
            case PaperKind.B5Transverse: return fnMillimeters(182, 257);
            case PaperKind.A3Extra: return fnMillimeters(322, 445);
            case PaperKind.A5Extra: return fnMillimeters(174, 235);
            case PaperKind.B5Extra: return fnMillimeters(201, 276);
            case PaperKind.A2: return fnMillimeters(420, 594);
            case PaperKind.A3Transverse: return fnMillimeters(297, 420);
            case PaperKind.A3ExtraTransverse: return fnMillimeters(322, 445);

            case PaperKind.JapaneseDoublePostcard: return fnMillimeters(200, 148);
            case PaperKind.A6: return fnMillimeters(105, 148);
            case PaperKind.JapaneseEnvelopeKakuNumber2: return fnMillimeters(240, 332);
            case PaperKind.JapaneseEnvelopeKakuNumber3: return fnMillimeters(216, 277);
            case PaperKind.JapaneseEnvelopeChouNumber3: return fnMillimeters(120, 235);
            case PaperKind.JapaneseEnvelopeChouNumber4: return fnMillimeters(90, 205);
            case PaperKind.LetterRotated: return fnInches(11, 8.5);
            case PaperKind.A3Rotated: return fnMillimeters(420, 297);
            case PaperKind.A4Rotated: return fnMillimeters(297, 210);
            case PaperKind.A5Rotated: return fnMillimeters(210, 148);
            case PaperKind.JapanesePostcardRotated: return fnMillimeters(148, 100);
            case PaperKind.JapaneseDoublePostcardRotated: return fnMillimeters(148, 200);
            case PaperKind.A6Rotated: return fnMillimeters(148, 105);
            case PaperKind.JapaneseEnvelopeKakuNumber2Rotated: return fnMillimeters(332, 240);
            case PaperKind.JapaneseEnvelopeKakuNumber3Rotated: return fnMillimeters(277, 216);
            case PaperKind.JapaneseEnvelopeChouNumber3Rotated: return fnMillimeters(235, 120);
            case PaperKind.JapaneseEnvelopeChouNumber4Rotated: return fnMillimeters(205, 90);
            case PaperKind.Standard12x11: return fnInches(12, 11);
            case PaperKind.JapaneseEnvelopeYouNumber4: return fnMillimeters(105, 235);
            case PaperKind.JapaneseEnvelopeYouNumber4Rotated: return fnMillimeters(235, 105);

            default:
                return null;
        }
    }  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM