Click here to Skip to main content
16,020,622 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to print pdf document with page setting and printer setting. For this I have used following code:-
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Spire.Pdf;
using System.Drawing.Printing;
   
   public void PrintMethod(string path)
   {
     PrinterSettings oPrinterSettings = new PrinterSettings();
     PdfDocument pdfdocument = new PdfDocument();
     pdfdocument.LoadFromFile(path);
     pdfdocument.PrinterName = oPrinterSettings.PrinterName;
     pdfdocument.PrintDocument.PrinterSettings.Copies = 1;
     pdfdocument.PrintDocument.PrinterSettings.ToPage = 1;
     pdfdocument.PrintDocument.PrinterSettings.FromPage = 1;
     //pdfdocument.PrintDocument.PrinterSettings.DefaultPageSettings.Margins.Top = 50;
     //pdfdocument.PrintDocument.PrinterSettings.DefaultPageSettings.Margins = new System.Drawing.Printing.Margins(50, 50, 150, 150);
     //pdfdocument.PageSettings.Margins.Bottom = 150;
     pdfdocument.PageSettings.SetMargins(100, 50, 100, 150);
     pdfdocument.PrintFromPage = 1;
     pdfdocument.PrintToPage = 1;
     pdfdocument.PrintDocument.Print();
     pdfdocument.Dispose();
   }

But The page margins and tray is not getting set. Pl provide solution, Thanks in advance.

What I have tried:

I have tried following Code:-
C#
class PrinterTest
{
  // Structure and 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>
  /// This function gets the pdf file name.
  /// This function opens the pdf file, gets all its bytes & send them to 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 Get 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 Get Connected Printer Name

      // Open the PDF file.
      FileStream fs = new FileStream(pdfFileName, FileMode.Open);
      // Create a BinaryReader on the 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 the file into the array.
      bytes = br.ReadBytes(nLength);
      // Allocate some unmanaged memory for those bytes.
      ptrUnmanagedBytes = Marshal.AllocCoTaskMem(nLength);
      // Copy the managed byte array into the unmanaged array.
      Marshal.Copy(bytes, 0, ptrUnmanagedBytes, nLength);
      // Send the unmanaged bytes to the printer.
      success = SendBytesToPrinter(pd.PrinterSettings.PrinterName, ptrUnmanagedBytes, nLength);
      // Free the unmanaged memory that you allocated earlier.
      Marshal.FreeCoTaskMem(ptrUnmanagedBytes);
      return success;
    }
    catch (Exception ex)
    {
      throw new Exception(ex.Message);
    }
  }

  /// <summary>
  /// This function gets the printer name and an unmanaged array of bytes, the function sends those bytes to the print queue.
  /// </summary>
  /// <param name="szPrinterName">Printer Name</param>
  /// <param name="pBytes">No. of bytes in the 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 you specifically succeed.

      di.pDocName = "PDF Document";
      di.pDataType = "RAW";

      // Open the printer.
      if (OpenPrinter(szPrinterName.Normalize(), out hPrinter, IntPtr.Zero))
      {
        // Start a document.
        if (StartDocPrinter(hPrinter, 1, di))
        {
          // Start a page.
          if (StartPagePrinter(hPrinter))
          {
            // Write the bytes.
            success = WritePrinter(hPrinter, pBytes, dwCount, out dwWritten);
            EndPagePrinter(hPrinter);
          }
          EndDocPrinter(hPrinter);
        }
        ClosePrinter(hPrinter);
      }

      // If print did not succeed, GetLastError may give more information about the failure.
      if (success == false)
      {
        dwError = Marshal.GetLastWin32Error();
      }
      return success;
    }
    catch (Exception ex)
    {
      throw new Exception(ex.Message);
    }
  }
  #endregion Methods
}
Posted
Updated 29-Mar-16 20:31pm
v2
Comments
Sinisa Hajnal 30-Mar-16 3:19am    
Some printers keep minimum margins regardless of what you put into the settings. Are you sure your margins are not applied? Or they are just distorted somewhat?

I don't see you setting the tray anywhere...

I suggest you wrap your printing code in printmethod in try..finally and dispose of your document in finally block.
Imran Ahmad Rizvi 24-Apr-17 6:59am    
How to set paper size
Member 343598 19-Dec-22 10:08am    
I tried Second Part but it gave no output on printer..

though the Function returned success without error..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900