[英]Find a printer's default duplexing option
對於給定打印文檔的PrintSettings
, Duplex
值可能(並且很可能)設置為Duplex.Default
。
我怎樣才能知道這是否意味着所選打印機將或不會雙面打印?
如何找到已安裝打印機支持的行為的默認值?
我不確定您能否獲得給定打印機的默認值。 不過,如果您有創意,您可以獲得實際的當前值。 不過,如果您想確保擁有正確的信息,則必須獲取 DEVMODE 結構。 這不是一個簡單的操作,需要一些花哨的 Win32 功能。 這是改編自幾個來源,但適用於我的(公認的參差不齊的)測試。
[DllImport("kernel32.dll")]
static extern bool GlobalFree(IntPtr hMem);
[DllImport("kernel32.dll")]
public static extern IntPtr GlobalLock(IntPtr handle);
[DllImport("kernel32.dll")]
public static extern IntPtr GlobalUnlock(IntPtr handle);
private static short IsPrinterDuplex(string PrinterName)
{
IntPtr hDevMode; // handle to the DEVMODE
IntPtr pDevMode; // pointer to the DEVMODE
DEVMODE devMode; // the actual DEVMODE structure
PrintDocument pd = new PrintDocument();
StandardPrintController controller = new StandardPrintController();
pd.PrintController = controller;
pd.PrinterSettings.PrinterName = PrinterName;
// Get a handle to a DEVMODE for the default printer settings
hDevMode = pd.PrinterSettings.GetHdevmode();
// Obtain a lock on the handle and get an actual pointer so Windows won't
// move it around while we're futzing with it
pDevMode = GlobalLock(hDevMode);
// Marshal the memory at that pointer into our P/Invoke version of DEVMODE
devMode = (DEVMODE)Marshal.PtrToStructure(pDevMode, typeof(DEVMODE));
short duplex = devMode.dmDuplex;
// Unlock the handle, we're done futzing around with memory
GlobalUnlock(hDevMode);
// And to boot, we don't need that DEVMODE anymore, either
GlobalFree(hDevMode);
return duplex;
}
我使用了來自 pinvoke.net 的DEVMODE 結構定義。 請注意,pinvoke.net 上定義的字符集可能需要根據 B0bi 對原始鏈接的評論進行一些調整(即,在 DEVMODE 的 StructLayoutAttriute 中設置 CharSet = CharSet.Unicode)。 您還需要DM 枚舉。 並且不要忘記添加 using System.Runtime.InteropServices;
您應該能夠從這里縮小打印機設置的變化范圍。
簡短的回答? 你不知道。 不管各種設置怎么說,實際的打印機都可以設置為始終雙面打印作業。
我不完全確定您計划如何將文檔合並在一起,但聽起來您可以簡單地計算頁數並有選擇地在文檔之間插入空白頁以確保每個新文檔都從奇數頁開始。
這是一個更大的變化,但如果您願意遷移到 XPS 工作流,則有一個名為PageForceFrontSide的頁面級票證項目可以保證文檔不會錯誤地粘在一起。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.