Advertisement
inoonan

Print Raw Pdf File

Dec 16th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.93 KB | None | 0 0
  1. using System;
  2. using System.Drawing.Printing;
  3. using System.IO;
  4. using System.Runtime.InteropServices;
  5. using System.Text;
  6.  
  7. namespace PrintPdfFile
  8. {
  9.     public class RawPrinterHelper
  10.     {
  11.         // Structure and API declarions:
  12.         [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
  13.         private class DOCINFOA
  14.         {
  15.             [MarshalAs(UnmanagedType.LPStr)]
  16.             public string pDocName;
  17.  
  18.             [MarshalAs(UnmanagedType.LPStr)]
  19.             public string pOutputFile;
  20.  
  21.             [MarshalAs(UnmanagedType.LPStr)]
  22.             public string pDataType;
  23.         }
  24.  
  25.         #region dll Wrappers
  26.  
  27.         [DllImport("winspool.Drv", EntryPoint = "OpenPrinterA", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
  28.         private static extern bool OpenPrinter([MarshalAs(UnmanagedType.LPStr)] string szPrinter, out IntPtr hPrinter, IntPtr pd);
  29.  
  30.         [DllImport("winspool.Drv", EntryPoint = "ClosePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
  31.         private static extern bool ClosePrinter(IntPtr hPrinter);
  32.  
  33.         [DllImport("winspool.Drv", EntryPoint = "StartDocPrinterA", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
  34.         private static extern bool StartDocPrinter(IntPtr hPrinter, Int32 level, [In, MarshalAs(UnmanagedType.LPStruct)] DOCINFOA di);
  35.  
  36.         [DllImport("winspool.Drv", EntryPoint = "EndDocPrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
  37.         private static extern bool EndDocPrinter(IntPtr hPrinter);
  38.  
  39.         [DllImport("winspool.Drv", EntryPoint = "StartPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
  40.         private static extern bool StartPagePrinter(IntPtr hPrinter);
  41.  
  42.         [DllImport("winspool.Drv", EntryPoint = "EndPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
  43.         private static extern bool EndPagePrinter(IntPtr hPrinter);
  44.  
  45.         [DllImport("winspool.Drv", EntryPoint = "WritePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
  46.         private static extern bool WritePrinter(IntPtr hPrinter, IntPtr pBytes, Int32 dwCount, out Int32 dwWritten);
  47.  
  48.         [DllImport("winspool.drv", CharSet = CharSet.Auto, SetLastError = true)]
  49.         public static extern bool GetDefaultPrinter(StringBuilder pszBuffer, ref int size);
  50.  
  51.         #endregion dll Wrappers
  52.  
  53.         #region Methods
  54.  
  55.         /// <summary>
  56.         /// This function gets the pdf file name.
  57.         /// This function opens the pdf file, gets all its bytes & send them to print.
  58.         /// </summary>
  59.         /// <param name="szPrinterName">Printer Name</param>
  60.         /// <param name="szFileName">Pdf File Name</param>
  61.         /// <returns>true on success, false on failure</returns>
  62.         public static bool SendFileToPrinter(string pdfFileName)
  63.         {
  64.             try
  65.             {
  66.                 #region Get Connected Printer Name
  67.  
  68.                 PrintDocument pd = new PrintDocument();
  69.                 StringBuilder dp = new StringBuilder(256);
  70.                 int size = dp.Capacity;
  71.                 if (GetDefaultPrinter(dp, ref size))
  72.                 {
  73.                     pd.PrinterSettings.PrinterName = dp.ToString().Trim();
  74.                 }
  75.  
  76.                 #endregion Get Connected Printer Name
  77.  
  78.                 // Open the PDF file.
  79.                 FileStream fs = new FileStream(pdfFileName, FileMode.Open);
  80.                 // Create a BinaryReader on the file.
  81.                 BinaryReader br = new BinaryReader(fs);
  82.                 Byte[] bytes = new Byte[fs.Length];
  83.                 bool success = false;
  84.                 // Unmanaged pointer.
  85.                 IntPtr ptrUnmanagedBytes = new IntPtr(0);
  86.                 int nLength = Convert.ToInt32(fs.Length);
  87.                 // Read contents of the file into the array.
  88.                 bytes = br.ReadBytes(nLength);
  89.                 // Allocate some unmanaged memory for those bytes.
  90.                 ptrUnmanagedBytes = Marshal.AllocCoTaskMem(nLength);
  91.                 // Copy the managed byte array into the unmanaged array.
  92.                 Marshal.Copy(bytes, 0, ptrUnmanagedBytes, nLength);
  93.                 // Send the unmanaged bytes to the printer.
  94.                 success = SendBytesToPrinter(pd.PrinterSettings.PrinterName, ptrUnmanagedBytes, nLength);
  95.                 // Free the unmanaged memory that you allocated earlier.
  96.                 Marshal.FreeCoTaskMem(ptrUnmanagedBytes);
  97.                 return success;
  98.             }
  99.             catch (Exception ex)
  100.             {
  101.                 throw new Exception(ex.Message);
  102.             }
  103.         }
  104.  
  105.         /// <summary>
  106.         /// This function gets the printer name and an unmanaged array of bytes, the function sends those bytes to the print queue.
  107.         /// </summary>
  108.         /// <param name="szPrinterName">Printer Name</param>
  109.         /// <param name="pBytes">No. of bytes in the pdf file</param>
  110.         /// <param name="dwCount">Word count</param>
  111.         /// <returns>True on success, false on failure</returns>
  112.         private static bool SendBytesToPrinter(string szPrinterName, IntPtr pBytes, Int32 dwCount)
  113.         {
  114.             try
  115.             {
  116.                 Int32 dwError = 0, dwWritten = 0;
  117.                 IntPtr hPrinter = new IntPtr(0);
  118.                 DOCINFOA di = new DOCINFOA();
  119.                 bool success = false; // Assume failure unless you specifically succeed.
  120.  
  121.                 di.pDocName = "PDF Document";
  122.                 di.pDataType = "RAW";
  123.  
  124.                 // Open the printer.
  125.                 if (OpenPrinter(szPrinterName.Normalize(), out hPrinter, IntPtr.Zero))
  126.                 {
  127.                     // Start a document.
  128.                     if (StartDocPrinter(hPrinter, 1, di))
  129.                     {
  130.                         // Start a page.
  131.                         if (StartPagePrinter(hPrinter))
  132.                         {
  133.                             // Write the bytes.
  134.                             success = WritePrinter(hPrinter, pBytes, dwCount, out dwWritten);
  135.                             EndPagePrinter(hPrinter);
  136.                         }
  137.                         EndDocPrinter(hPrinter);
  138.                     }
  139.                     ClosePrinter(hPrinter);
  140.                 }
  141.  
  142.                 // If print did not succeed, GetLastError may give more information about the failure.
  143.                 if (success == false)
  144.                 {
  145.                     dwError = Marshal.GetLastWin32Error();
  146.                 }
  147.                 return success;
  148.             }
  149.             catch (Exception ex)
  150.             {
  151.                 throw new Exception(ex.Message);
  152.             }
  153.         }
  154.  
  155.         #endregion Methods
  156.     }
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement