pdf - Print always take documents folder file path instead of custom -
i using this print existing pdf documents taking c drive documents folder create file not path given me. either don't know process or may issue in code can me understand this.
code:
namespace printpdffile { public class rawprinterhelper { // structure , api declarions: [structlayout(layoutkind.sequential, charset = charset.ansi)] private class docinfoa { [marshalas(unmanagedtype.lpstr)] public string pdocname; [marshalas(unmanagedtype.lpstr)] public string poutputfile; [marshalas(unmanagedtype.lpstr)] public string pdatatype; } #region dll wrappers [dllimport("winspool.drv", entrypoint = "openprintera", setlasterror = true, charset = charset.ansi, exactspelling = true, callingconvention = callingconvention.stdcall)] private static extern bool openprinter([marshalas(unmanagedtype.lpstr)] string szprinter, out intptr hprinter, intptr pd); [dllimport("winspool.drv", entrypoint = "closeprinter", setlasterror = true, exactspelling = true, callingconvention = callingconvention.stdcall)] private static extern bool closeprinter(intptr hprinter); [dllimport("winspool.drv", entrypoint = "startdocprintera", setlasterror = true, charset = charset.ansi, exactspelling = true, callingconvention = callingconvention.stdcall)] private static extern bool startdocprinter(intptr hprinter, int32 level, [in, marshalas(unmanagedtype.lpstruct)] docinfoa di); [dllimport("winspool.drv", entrypoint = "enddocprinter", setlasterror = true, exactspelling = true, callingconvention = callingconvention.stdcall)] private static extern bool enddocprinter(intptr hprinter); [dllimport("winspool.drv", entrypoint = "startpageprinter", setlasterror = true, exactspelling = true, callingconvention = callingconvention.stdcall)] private static extern bool startpageprinter(intptr hprinter); [dllimport("winspool.drv", entrypoint = "endpageprinter", setlasterror = true, exactspelling = true, callingconvention = callingconvention.stdcall)] private static extern bool endpageprinter(intptr hprinter); [dllimport("winspool.drv", entrypoint = "writeprinter", setlasterror = true, exactspelling = true, callingconvention = callingconvention.stdcall)] private static extern bool writeprinter(intptr hprinter, intptr pbytes, int32 dwcount, out int32 dwwritten); [dllimport("winspool.drv", charset = charset.auto, setlasterror = true)] public static extern bool getdefaultprinter(stringbuilder pszbuffer, ref int size); #endregion dll wrappers #region methods /// <summary> /// function gets pdf file name. /// function opens pdf file, gets bytes & send them print. /// </summary> /// <param name="szprintername">printer name</param> /// <param name="szfilename">pdf file name</param> /// <returns>true on success, false on failure</returns> public static bool sendfiletoprinter(string pdffilename) { try { #region connected printer name printdocument pd = new printdocument(); stringbuilder dp = new stringbuilder(256); int size = dp.capacity; if (getdefaultprinter(dp, ref size)) { pd.printersettings.printername = dp.tostring().trim(); } #endregion connected printer name // open pdf file. filestream fs = new filestream(pdffilename, filemode.open); // create binaryreader on file. binaryreader br = new binaryreader(fs); byte[] bytes = new byte[fs.length]; bool success = false; // unmanaged pointer. intptr ptrunmanagedbytes = new intptr(0); int nlength = convert.toint32(fs.length); // read contents of file array. bytes = br.readbytes(nlength); // allocate unmanaged memory bytes. ptrunmanagedbytes = marshal.alloccotaskmem(nlength); // copy managed byte array unmanaged array. marshal.copy(bytes, 0, ptrunmanagedbytes, nlength); // send unmanaged bytes printer. success = sendbytestoprinter(pd.printersettings.printername, ptrunmanagedbytes, nlength); // free unmanaged memory allocated earlier. marshal.freecotaskmem(ptrunmanagedbytes); return success; } catch (exception ex) { throw new exception(ex.message); } } /// <summary> /// function gets printer name , unmanaged array of bytes, function sends bytes print queue. /// </summary> /// <param name="szprintername">printer name</param> /// <param name="pbytes">no. of bytes in pdf file</param> /// <param name="dwcount">word count</param> /// <returns>true on success, false on failure</returns> private static bool sendbytestoprinter(string szprintername, intptr pbytes, int32 dwcount) { try { int32 dwerror = 0, dwwritten = 0; intptr hprinter = new intptr(0); docinfoa di = new docinfoa(); bool success = false; // assume failure unless succeed. di.pdocname = "pdf document"; di.pdatatype = "raw"; // open printer. if (openprinter(szprintername.normalize(), out hprinter, intptr.zero)) { // start document. if (startdocprinter(hprinter, 1, di)) { // start page. if (startpageprinter(hprinter)) { // write bytes. success = writeprinter(hprinter, pbytes, dwcount, out dwwritten); endpageprinter(hprinter); } enddocprinter(hprinter); } closeprinter(hprinter); } // if print did not succeed, getlasterror may give more information failure. if (success == false) { dwerror = marshal.getlastwin32error(); } return success; } catch (exception ex) { throw new exception(ex.message); } } #endregion methods } } //testing above code class program { static void main(string[] args) { try { rawprinterhelper.sendfiletoprinter(@"e:\xyzdocs\xyz.pdf"); } catch (exception ex) { console.writeline(ex.message); } } }
i have used above code print existing pdf file sending path send in queue pdf document not loaded correctly , not print done.
pdf document when code run , send file in queue c:/documents:
Comments
Post a Comment