[Office | Excel | C#] Excel浮水印系列(3)-如何取得Excel 版面配置(Pagesetup)的紙張大小(PaperSize)與寬高(Width/Height)

  • 3711
  • 0
  • C#
  • 2015-02-18

為了可以產生滿版Excel浮水印圖片,並在列印時符合紙張類型 ( 紙張大小寬高) 與版面,所以在頁首頁尾模式插入圖片時,需要取得目前的紙張類型,不過這件事其實不難,因為Excel有提供對應的列舉可以取得,但是卻無法取得此紙張大小的寬與高( 真的找了很久,不像Word很方便有提供,但如果有朋友有找到歡迎告知我 ) ,所以這邊提供了一種方式可以取得,各位可以參考參考。

前言

 


 

為了可以產生滿版Excel浮水印圖片,並在列印時符合紙張類型 ( 紙張大小寬高) 與版面,所以在頁首頁尾模式插入圖片時,需要取得目前的紙張類型,不過這件事其實不難,因為Excel有提供對應的列舉可以取得,但是卻無法取得此紙張大小的寬與高( 真的找了很久,不像Word很方便有提供,但如果有朋友有找到歡迎告知我 ) ,所以這邊提供了一種方式可以取得,各位可以參考參考。

 

取得Excel目前版面配置( Pagesetup )的紙張大小(PaperSize)

 


 

在Excel有一個列舉參數叫做XlPaperSize,底下提供了所有紙張大小的

image

可以參考MSDN的文件,有詳列所有的紙張類型

然而取得紙張大小後,卻無法進一步取得此紙張大小的寬高,只能從Summary中的文件提示看到。

所以這邊的一個處理方式,便是透過Excel連線的印表機取得對應的紙張寬高

 

透過預設 "Microsoft XPS Document Writer" 印表機取得紙張實際大小寬高

 


 

後來發現,可以透過System.Drawing.Printing下的PrinterSettings類別中的PaperSize類別有提供紙張大小的寬高,所以發現可以從這裡下手,但是取得的對應紙張總是錯誤? 明明要拿的是A4,卻總是拿到別的紙張?

測試後了解,原來Excel會根據目前連線到的印表機,在版面配置時會列出此印表機所支援的紙張類型,而我的預設印表機是OneNote所以提供的紙張類型沒有跟Excel.XlPaperSize一樣多,所以取得的會有錯。

 

後來發現,原來預設 "Microsoft XPS Document Writer"有提供所有紙張類型,而且與Excel.XlPaperSize所提供的一樣,對應的列舉順序也一致!

所以就可以取得了!

程式如下:


//取得目前Sheet的紙張類型
Excel.XlPaperSize excelPaperSize = currentSheetPageSetup.PaperSize;

//Excel取得不到紙張大小的寬高,但是Excel紙張大小的類型列舉編號與Windows內建的"Microsoft XPS Document Writer"一樣,所以透過印表機類別取得對應的紙張大小資料
PrinterSettings currentPrinters = new System.Drawing.Printing.PrinterSettings();
string existedPrinterName = currentPrinters.PrinterName;

PrinterSettings settingForGetPaper = new System.Drawing.Printing.PrinterSettings();
settingForGetPaper.PrinterName = "Microsoft XPS Document Writer";

//PaperSize對應的紙張類型索引在RawKind下
paperType = settingForGetPaper.PaperSizes[(int)excelPaperSize - 1];

 

RawKind的索引順序請參考MSDN文件

再來便是從paperType物件提取Width與Height,不過這裡要提一下,PaperSize的寬與高是

是百分之一英吋所以換算時要注意

 

恭喜,可以Excel目前版面設定的紙張寬高!

 

第二種方式-預先紀錄XPS Document Writer的所有紙張大小

 


 

為何這邊還需要第二種方式呢? 因為並非每一台電腦都有XPS Document Writer (如果想要安裝,可以參考這篇文章),並且有些業務需求,可能就是希望不要安裝,或是考慮到安裝後,是否會對原有電腦的其他軟體造成影響,因此,才會有第二種方式,也就是把所有的XPS紙張都記錄在程式碼中。

程式如下:


//自定義的類別,紀錄紙張資訊
public class PageSize
  {

            public string PaperName { get; set; }
            public int RawKind { get; set; }
            public int Width { get; set; }
            public int Height { get; set; }
 }

 


 private List<PageSize> XPSDefaultPrinterPaperSizes = new List<PageSize>(){
            new PageSize(){ PaperName = "Letter",RawKind = 1,Width = 612,Height = 792},
            new PageSize(){ PaperName = "Letter Small",RawKind = 2,Width = 612,Height = 792},
            new PageSize(){ PaperName = "Tabloid",RawKind = 3,Width = 792,Height = 1224},
            new PageSize(){ PaperName = "Ledger",RawKind = 4,Width = 1224,Height = 792},
            new PageSize(){ PaperName = "Legal",RawKind = 5,Width = 612,Height = 1008},
            new PageSize(){ PaperName = "Statement",RawKind = 6,Width = 396,Height = 612},
            new PageSize(){ PaperName = "Executive",RawKind = 7,Width = 522,Height = 756},
            new PageSize(){ PaperName = "A3",RawKind = 8,Width = 842,Height = 1191},
            new PageSize(){ PaperName = "A4",RawKind = 9,Width = 595,Height = 842},
            new PageSize(){ PaperName = "A4 Small",RawKind = 10,Width = 595,Height = 842},
            new PageSize(){ PaperName = "A5",RawKind = 11,Width = 420,Height = 595},
            new PageSize(){ PaperName = "B4 (JIS)",RawKind = 12,Width = 729,Height = 1032},
            new PageSize(){ PaperName = "B5 (JIS)",RawKind = 13,Width = 516,Height = 729},
            new PageSize(){ PaperName = "Folio",RawKind = 14,Width = 612,Height = 936},
            new PageSize(){ PaperName = "Quarto",RawKind = 15,Width = 609,Height = 780},
            new PageSize(){ PaperName = "10×14",RawKind = 16,Width = 720,Height = 1008},
            new PageSize(){ PaperName = "11×17",RawKind = 17,Width = 792,Height = 1224},
            new PageSize(){ PaperName = "Note",RawKind = 18,Width = 612,Height = 792},
            new PageSize(){ PaperName = "Envelope #9",RawKind = 19,Width = 279,Height = 639},
            new PageSize(){ PaperName = "Envelope #10",RawKind = 20,Width = 297,Height = 684},
            new PageSize(){ PaperName = "Envelope #11",RawKind = 21,Width = 324,Height = 747},
            new PageSize(){ PaperName = "Envelope #12",RawKind = 22,Width = 342,Height = 792},
            new PageSize(){ PaperName = "Envelope #14",RawKind = 23,Width = 360,Height = 828},
            new PageSize(){ PaperName = "C size sheet",RawKind = 24,Width = 1224,Height = 1584},
            new PageSize(){ PaperName = "D size sheet",RawKind = 25,Width = 1584,Height = 2448},
            new PageSize(){ PaperName = "E size sheet",RawKind = 26,Width = 2448,Height = 3168},
            new PageSize(){ PaperName = "Envelope DL",RawKind = 27,Width = 312,Height = 624},
            new PageSize(){ PaperName = "Envelope C5",RawKind = 28,Width = 459,Height = 649},
            new PageSize(){ PaperName = "Envelope C3",RawKind = 29,Width = 919,Height = 1298},
            new PageSize(){ PaperName = "Envelope C4",RawKind = 30,Width = 649,Height = 919},
            new PageSize(){ PaperName = "Envelope C6",RawKind = 31,Width = 323,Height = 459},
            new PageSize(){ PaperName = "Envelope C65",RawKind = 32,Width = 323,Height = 649},
            new PageSize(){ PaperName = "Envelope B4",RawKind = 33,Width = 708,Height = 1001},
            new PageSize(){ PaperName = "Envelope B5",RawKind = 34,Width = 499,Height = 708},
            new PageSize(){ PaperName = "Envelope B6",RawKind = 35,Width = 499,Height = 354},
            new PageSize(){ PaperName = "Envelope",RawKind = 36,Width = 312,Height = 652},
            new PageSize(){ PaperName = "Envelope Monarch",RawKind = 37,Width = 279,Height = 540},
            new PageSize(){ PaperName = "6 3/4 Envelope",RawKind = 38,Width = 261,Height = 468},
            new PageSize(){ PaperName = "US Std Fanfold",RawKind = 39,Width = 1071,Height = 792},
            new PageSize(){ PaperName = "German Std Fanfold",RawKind = 40,Width = 612,Height = 864},
            new PageSize(){ PaperName = "German Legal Fanfold",RawKind = 41,Width = 612,Height = 936},
            new PageSize(){ PaperName = "B4 (ISO)",RawKind = 42,Width = 708,Height = 1001},
            new PageSize(){ PaperName = "Japanese Postcard",RawKind = 43,Width = 284,Height = 420},
            new PageSize(){ PaperName = "9×11",RawKind = 44,Width = 648,Height = 792},
            new PageSize(){ PaperName = "10×11",RawKind = 45,Width = 720,Height = 792},
            new PageSize(){ PaperName = "15×11",RawKind = 46,Width = 1080,Height = 792},
            new PageSize(){ PaperName = "Envelope Invite",RawKind = 47,Width = 624,Height = 624},
            new PageSize(){ PaperName = "Letter Extra",RawKind = 50,Width = 684,Height = 864},
            new PageSize(){ PaperName = "Legal Extra",RawKind = 51,Width = 684,Height = 1080},
            new PageSize(){ PaperName = "A4 Extra",RawKind = 53,Width = 667,Height = 914},
            new PageSize(){ PaperName = "Letter Transverse",RawKind = 54,Width = 612,Height = 792},
            new PageSize(){ PaperName = "A4 Transverse",RawKind = 55,Width = 595,Height = 842},
            new PageSize(){ PaperName = "Letter Extra Transverse",RawKind = 56,Width = 684,Height = 864},
            new PageSize(){ PaperName = "Super A",RawKind = 57,Width = 644,Height = 1009},
            new PageSize(){ PaperName = "Super B",RawKind = 58,Width = 865,Height = 1380},
            new PageSize(){ PaperName = "Letter Plus",RawKind = 59,Width = 612,Height = 914},
            new PageSize(){ PaperName = "A4 Plus",RawKind = 60,Width = 595,Height = 935},
            new PageSize(){ PaperName = "A5 Transverse",RawKind = 61,Width = 420,Height = 595},
            new PageSize(){ PaperName = "B5 (JIS) Transverse",RawKind = 62,Width = 516,Height = 729},
            new PageSize(){ PaperName = "A3 Extra",RawKind = 63,Width = 913,Height = 1261},
            new PageSize(){ PaperName = "A5 Extra",RawKind = 64,Width = 493,Height = 666},
            new PageSize(){ PaperName = "B5 (ISO) Extra",RawKind = 65,Width = 570,Height = 783},
            new PageSize(){ PaperName = "A2",RawKind = 66,Width = 1191,Height = 1684},
            new PageSize(){ PaperName = "A3 Transverse",RawKind = 67,Width = 842,Height = 1191},
            new PageSize(){ PaperName = "A3 Extra Transverse",RawKind = 68,Width = 913,Height = 1261},
            new PageSize(){ PaperName = "Japanese Double Postcard",RawKind = 69,Width = 567,Height = 420},
            new PageSize(){ PaperName = "A6",RawKind = 70,Width = 297,Height = 420},
            new PageSize(){ PaperName = "Japanese Envelope Kaku #2",RawKind = 71,Width = 680,Height = 941},
            new PageSize(){ PaperName = "Japanese Envelope Kaku #3",RawKind = 72,Width = 612,Height = 786},
            new PageSize(){ PaperName = "Japanese Envelope Chou #3",RawKind = 73,Width = 340,Height = 666},
            new PageSize(){ PaperName = "Japanese Envelope Chou #4",RawKind = 74,Width = 255,Height = 581},
            new PageSize(){ PaperName = "Letter Rotated",RawKind = 75,Width = 792,Height = 612},
            new PageSize(){ PaperName = "A3 Rotated",RawKind = 76,Width = 1191,Height = 842},
            new PageSize(){ PaperName = "A4 Rotated",RawKind = 77,Width = 842,Height = 595},
            new PageSize(){ PaperName = "A5 Rotated",RawKind = 78,Width = 595,Height = 420},
            new PageSize(){ PaperName = "B4 (JIS) Rotated",RawKind = 79,Width = 1032,Height = 729},
            new PageSize(){ PaperName = "B5 (JIS) Rotated",RawKind = 80,Width = 729,Height = 516},
            new PageSize(){ PaperName = "Japanese Postcard Rotated",RawKind = 81,Width = 420,Height = 284},
            new PageSize(){ PaperName = "Double Japan Postcard Rotated",RawKind = 82,Width = 420,Height = 567},
            new PageSize(){ PaperName = "A6 Rotated",RawKind = 83,Width = 420,Height = 297},
            new PageSize(){ PaperName = "Japan Envelope Kaku #2 Rotated",RawKind = 84,Width = 941,Height = 680},
            new PageSize(){ PaperName = "Japan Envelope Kaku #3 Rotated",RawKind = 85,Width = 786,Height = 612},
            new PageSize(){ PaperName = "Japan Envelope Chou #3 Rotated",RawKind = 86,Width = 666,Height = 340},
            new PageSize(){ PaperName = "Japan Envelope Chou #4 Rotated",RawKind = 87,Width = 581,Height = 255},
            new PageSize(){ PaperName = "B6 (JIS)",RawKind = 88,Width = 363,Height = 516},
            new PageSize(){ PaperName = "B6 (JIS) Rotated",RawKind = 89,Width = 516,Height = 363},
            new PageSize(){ PaperName = "12×11",RawKind = 90,Width = 864,Height = 792},
            new PageSize(){ PaperName = "Japan Envelope You #4",RawKind = 91,Width = 297,Height = 666},
            new PageSize(){ PaperName = "Japan Envelope You #4 Rotated",RawKind = 92,Width = 666,Height = 297},
            new PageSize(){ PaperName = "PRC Envelope #1",RawKind = 96,Width = 289,Height = 468},
            new PageSize(){ PaperName = "PRC Envelope #3",RawKind = 98,Width = 354,Height = 499},
            new PageSize(){ PaperName = "PRC Envelope #4",RawKind = 99,Width = 312,Height = 590},
            new PageSize(){ PaperName = "PRC Envelope #5",RawKind = 100,Width = 312,Height = 624},
            new PageSize(){ PaperName = "PRC Envelope #6",RawKind = 101,Width = 340,Height = 652},
            new PageSize(){ PaperName = "PRC Envelope #7",RawKind = 102,Width = 454,Height = 652},
            new PageSize(){ PaperName = "PRC Envelope #8",RawKind = 103,Width = 340,Height = 876},
            new PageSize(){ PaperName = "PRC Envelope #9",RawKind = 104,Width = 649,Height = 919},
            new PageSize(){ PaperName = "PRC Envelope #10",RawKind = 105,Width = 919,Height = 1298},
            new PageSize(){ PaperName = "PRC Envelope #1 Rotated",RawKind = 109,Width = 468,Height = 289},
            new PageSize(){ PaperName = "PRC Envelope #3 Rotated",RawKind = 111,Width = 499,Height = 354},
            new PageSize(){ PaperName = "PRC Envelope #4 Rotated",RawKind = 112,Width = 590,Height = 312},
            new PageSize(){ PaperName = "PRC Envelope #5 Rotated",RawKind = 113,Width = 624,Height = 312},
            new PageSize(){ PaperName = "PRC Envelope #6 Rotated",RawKind = 114,Width = 652,Height = 340},
            new PageSize(){ PaperName = "PRC Envelope #7 Rotated",RawKind = 115,Width = 652,Height = 454},
            new PageSize(){ PaperName = "PRC Envelope #8 Rotated",RawKind = 116,Width = 876,Height = 340},
            new PageSize(){ PaperName = "PRC Envelope #9 Rotated",RawKind = 117,Width = 919,Height = 649},
            new PageSize(){ PaperName = "使用者自訂大小",RawKind = 256,Width = 595,Height = 842}

        };

 

尋找對應的紙張:


Excel.XlPaperSize excelPaperSize = currentSheetPageSetup.PaperSize;
 foreach (PageSize paperType in XPSDefaultPrinterPaperSizes)
 {
                if (paperType.RawKind == (int)excelPaperSize)
                    return paperType;
   }

 

 

補充-注意事項

 


 

若是今天此程式有Web需要會佈署在IIS,請確認目前的印表機與切換XPS印表機皆具備IIS權限(或如果使用Asp.Net模擬則模擬之帳號須具備存取權限),如下圖,要有管理文件與管理印表機的權限,才不會彈出錯誤

 

 

參考資料

 


 

PaperKind 列舉類型

PaperSize.RawKind Property

What's the best way to get the default printer in .NET

 


 

文章中的敘述如有觀念不正確錯誤的部分,歡迎告知指正 謝謝 =)

另外要轉載請附上出處 感謝