C 列印word檔案

2021-06-21 00:48:00 字數 4422 閱讀 4378

#region 輔助定義

//印表機狀態

[flagsattribute]

internal enum printerstatus

;[structlayout(layoutkind.sequential, charset = charset.auto)]

internal struct structprinterdefaults

;//nested types

[structlayout(layoutkind.sequential)]

public class docinfoa

[structlayout(layoutkind.sequential)]

internal struct printer_info_2

#endregion

///

/// 印表機狀態

///

public enum eprinterstate

///

/// 列印的檔案型別

///

public enum eprintfiletype

///

/// 列印類

/// 使用方法:

/// getprinterlist 獲取所有可用印表機名稱列表

/// checkprintervalid 檢查某個印表機是否可用.

/// getprinterstatus 獲取印表機狀態

/// printfile 使用指定印表機列印檔案.

/// printfilebysystem 使用指定印表機列印檔案,呼叫作業系統預設程式列印

///

public class printerhelper

}return false;

}///

/// 獲取本機安裝的所有印表機

///

///

public static listgetprinterlist()

foreach (string printername in printersettings.installedprinters)

return ret;

}///

/// 檢查印表機是否可用

///

///

///

public static boolean checkprintervalid(string printername)

eprinterstate state = getprinterstatus(printername);

if (state == eprinterstate.ready || state == eprinterstate.busy)

return false;

}///

/// 獲取印表機狀態

///

///

///

public static eprinterstate getprinterstatus(string printername)

#endregion

#region 內部實現

///

/// 列印word

///

///

///

///

private static boolean printword(string printername, string filename)

#endregion

#region 系統api

// [methods]

[dllimport("winspool.drv", callingconvention = callingconvention.stdcall, setlasterror = true, exactspelling = true)]

internal static extern bool closeprinter(intptr hprinter);

[dllimport("winspool.drv", callingconvention = callingconvention.stdcall, setlasterror = true, exactspelling = true)]

internal static extern bool enddocprinter(intptr hprinter);

[dllimport("winspool.drv", callingconvention = callingconvention.stdcall, setlasterror = true, exactspelling = true)]

internal static extern bool endpageprinter(intptr hprinter);

[dllimport("winspool.drv", entrypoint = "openprintera", callingconvention = callingconvention.stdcall, charset = charset.ansi, setlasterror = true, exactspelling = true)]

internal static extern bool openprinter([marshalas(unmanagedtype.lpstr)] string szprinter, out intptr hprinter, intptr pd);

[dllimport("winspool.drv", entrypoint = "getprintera", callingconvention = callingconvention.stdcall, charset = charset.ansi, setlasterror = true, exactspelling = true)]

internal static extern bool getprinter(intptr hprinter, int dwlevel, intptr pprinter, int dwbuf, out int dwneeded);

[dllimport("winspool.drv", entrypoint = "startdocprintera", callingconvention = callingconvention.stdcall, charset = charset.ansi, setlasterror = true, exactspelling = true)]

internal static extern bool startdocprinter(intptr hprinter, int level, [in, marshalas(unmanagedtype.lpstruct)] docinfoa di);

[dllimport("winspool.drv", callingconvention = callingconvention.stdcall, setlasterror = true, exactspelling = true)]

internal static extern bool startpageprinter(intptr hprinter);

[dllimport("winspool.drv", callingconvention = callingconvention.stdcall, setlasterror = true, exactspelling = true)]

internal static extern bool writeprinter(intptr hprinter, intptr pbytes, int dwcount, out int dwwritten);

///

/// 獲取印表機狀態字

///

///

///

internal static int getprinterstatusint(string printername)

int cbneeded = 0;

bl = getprinter(hprinter, 2, intptr.zero, 0, out cbneeded);

//此處不能判斷bl.應該只判斷 cbneeded

if (cbneeded < 1)

intptr paddr = marshal.allochglobal((int)cbneeded);

bl = getprinter(hprinter, 2, paddr, cbneeded, out cbneeded);

if (!bl)

printer_info_2 info2 = new printer_info_2();

info2 = (printer_info_2)marshal.ptrtostructure(paddr, typeof(printer_info_2));

intret = system.convert.toint32(info2.status);

marshal.freehglobal(paddr);

closeprinter(hprinter);

return intret;

}#endregion

}

C 根據Word模版生成Word檔案

指定的word模版 2,生成word類 新增com microsoft word 11.0 object library 引用 usingsystem usingsystem.collections.generic usingsystem.data usingsystem.windows.forms...

C 完成word文件列印的方法

在日常工作中,我們可能常常需要列印各種檔案資料,比如word文件。對於程式設計員,應用程式中文件的列印是一項非常重要的功能,也一直是乙個非常複雜的工作。特別是提到web列印,這的確會很棘手。一般如果要想選擇非預設印表機或者說想顯示列印設定對話方塊時,我們也需要對 進行一定的設定。針對這樣的問題,今天...

Word 列印 PrintOut 方法

from,to,item,copies,pages,pagetype,printtofile,collate,filename,activeprintermacgx,manualduplexprint,printzoomcolumn,printzoomrow,printzoom width,prin...