C#/VB.NET: Print Word Documents

2023-06-30 01:36:16 Written by  support iceblue
Rate this item
(0 votes)

Printing Word documents is a fundamental skill that allows you to convert your digital text into physical copies. Whether you need to create hard copies of reports, resumes, essays, or any other written material, understanding how to print Word documents efficiently can save time and ensure professional-looking results. In this article, you will learn how to print a Word document with the specified print settings in C# and VB.NET using Spire.Doc for .NET.

Install Spire.Doc for .NET

To begin with, you need to add the DLL files included in the Spire.Doc for .NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.Doc

Print Word Documents in C#, VB.NET

With the help of the PrintDocument class, programmers can send a Word document to a specific printer and specify the print settings such as page range, number of copies, duplex printing, and paper size. The detailed steps to print a Word document using Spire.Doc for .NET are as follows.

  • Create a Document object.
  • Load a Word document using Document.LoadFromFile() method.
  • Get the PrintDocument object through Document.PrintDocument property.
  • Specify the printer name through PrintDocument.PrinterSettings.PrinterName property.
  • Specify the range of pages to print through PrintDocument.PrinterSettings.PrinterName property.
  • Set the number of copies to print through PrintDocument.PrinterSettings.Copies property.
  • Print the document using PrintDocument.Print() method.
  • C#
  • VB.NET
using Spire.Doc;
using System.Drawing.Printing;

namespace PrintWordDocument
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Document object
            Document doc = new Document();

            //Load a Word document
            doc.LoadFromFile(@"C:\Users\Administrator\Desktop\input.docx");

            //Get the PrintDocument object
            PrintDocument printDoc = doc.PrintDocument;

            //Specify the printer name
            printDoc.PrinterSettings.PrinterName = "NPI7FE2DF (HP Color LaserJet MFP M281fdw)"; 

            //Specify the range of pages to print
            printDoc.PrinterSettings.FromPage = 1;
            printDoc.PrinterSettings.ToPage = 10;

            //Set the number of copies to print
            printDoc.PrinterSettings.Copies = 1;

            //Print the document
            printDoc.Print();
        }
    }
}

Silently Print Word Documents in C#, VB.NET

Silent printing is a printing method that does not show any printing process or status. To enable silent printing, set the print controller to StandardPrintController. The following are the detailed steps.

  • Create a Document object.
  • Load a Word document using Document.LoadFromFile() method.
  • Get the PrintDocument object through Document.PrintDocument property.
  • Specify the printer name through PrintDocument.PrinterSettings.PrinterName property.
  • Set the print controller to StandardPrintController through PrintDocument.PrintController property.
  • Print the document using PrintDocument.Print() method.
  • C#
  • VB.NET
using Spire.Doc;
using System.Drawing.Printing;

namespace SilentlyPrintWord
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Document object
            Document doc = new Document();

            //Load a Word document
            doc.LoadFromFile(@"C:\Users\Administrator\Desktop\input.docx");

            //Get the PrintDocument object
            PrintDocument printDoc = doc.PrintDocument;

            //Specify the printer name
            printDoc.PrinterSettings.PrinterName = "NPI7FE2DF (HP Color LaserJet MFP M281fdw)";

            //Specify the print controller to StandardPrintController
            printDoc.PrintController = new StandardPrintController();

            //Print the document
            printDoc.Print();
        }
    }
}

Print Word to PDF in C#, VB.NET

In addition to printing Word documents with a physical printer, you can also print documents with virtual printers, such as Microsoft Print to PDF and Microsoft XPS Document Writer. The following are the steps to print Word to PDF using Spire.Doc for .NET.

  • Create a Document object.
  • Load a Word document using Document.LoadFromFile() method.
  • Get the PrintDocument object through Document.PrintDocument property.
  • Specify the printer name as “Microsoft Print to PDF” through PrintDocument.PrinterSettings.PrinterName property.
  • Specify the output file path and name through PrintDocument.PrinterSettings.PrintFileName property.
  • Print the document using PrintDocument.Print() method.
  • C#
  • VB.NET
using Spire.Doc;
using System.Drawing.Printing;

namespace PrintWordToPdf
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Document object
            Document doc = new Document();

            //Load a Word document
            doc.LoadFromFile(@"C:\Users\Administrator\Desktop\input.docx");

            //Get the PrintDocument object
            PrintDocument printDoc = doc.PrintDocument;

            //Print the document to file
            printDoc.PrinterSettings.PrintToFile = true;

            //Specify the printer name
            printDoc.PrinterSettings.PrinterName = "Microsoft Print to PDF";

            //Specify the output file path and name
            printDoc.PrinterSettings.PrintFileName = @"C:\Users\Administrator\Desktop\ToPDF.pdf";

            //Print the document
            printDoc.Print();
        }
    }
}

Print Word on a Custom Sized Paper in C#, VB.NET

Setting the paper size is necessary when you need to ensure the printed output meets specific size requirements or adapts to a particular purpose. The following are the steps to print Word on a custom size pager using Spire.Doc for .NET.

  • Create a Document object.
  • Load a Word document using Document.LoadFromFile() method.
  • Get the PrintDocument object through Document.PrintDocument property.
  • Specify the printer name through PrintDocument.PrinterSettings.PrinterName property.
  • Specify the paper size through PrintDocument.DefaultPageSettings.PaperSize property.
  • Print the document using PrintDocument.Print() method.
  • C#
  • VB.NET
using Spire.Doc;
using System.Drawing.Printing;

namespace PrintOnCustomSizedPaper
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Document object
            Document doc = new Document();

            //Load a Word document
            doc.LoadFromFile(@"C:\Users\Administrator\Desktop\input.docx");

            //Get the PrintDocument object
            PrintDocument printDoc = doc.PrintDocument;

            //Specify the printer name
            printDoc.PrinterSettings.PrinterName = "NPI7FE2DF(HP Color LaserJet MFP M281fdw)";

            //Specify the paper size
            printDoc.DefaultPageSettings.PaperSize = new PaperSize("custom", 500, 800);

            //Print the document
            printDoc.Print();
        }
    }
}

Print Multiple Pages on One Sheet in C#, VB.NET

Printing multiple pages on a single sheet of paper can help save paper and create compact handbooks or booklets. The steps to print multiple pages on one sheet are as follows.

  • Create a Document object.
  • Load a Word document using Document.LoadFromFile() method.
  • Get the PrintDocument object through Document.PrintDocument property.
  • Specify the printer name through PrintDocument.PrinterSettings.PrinterName property.
  • Specify the number of pages to be printed on one page and print the document using Doucment.PrintMultipageToOneSheet() method.

Note: This feature is NOT applicable to .NET Framework 5.0 or above.

  • C#
  • VB.NET
using Spire.Doc;
using Spire.Doc.Printing;
using System.Drawing.Printing;

namespace PrintMultiplePagesOnOneSheet
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //Instantiate an instance of the Document class
            Document doc = new Document();

            //Load a Word document
            doc.LoadFromFile(@"C:\\Users\\Administrator\\Desktop\\input.docx");

            //Get the PrintDocument object
            PrintDocument printDoc = doc.PrintDocument;

            //Enable single-sided printing
            printDoc.PrinterSettings.Duplex = Duplex.Simplex;

            //Specify the number of pages to be printed on one page and print the document
            doc.PrintMultipageToOneSheet(PagesPreSheet.TwoPages, false);
        }
    }
}

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Additional Info

  • tutorial_title:
Last modified on Friday, 06 September 2024 02:48