Print

Print (2)

C#/VB.NET: Print Word Documents

2023-06-30 01:36:16 Written by support iceblue

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.

Sometimes you may want to print Word documents in accordance with your own preferences, for instance, print your files on custom paper sizes to make them more personalized. In this article, you will learn how to achieve this function 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 DLLs files can be either downloaded from this link or installed via NuGet.

  • Package Manager
PM> Install-Package Spire.Doc

Print Word on a Custom Paper Size

The table below shows a list of core classes, methods and properties utilized in this scenario.

Name Description
Document Class Represents a document model for Word.
PaperSize Class Specifies the size of a piece of paper.
PrintDocument Class Defines a reusable object that sends output to a printer, when printing from a Windows Forms application.
PrintDocument.DefaultPageSettings Property Gets or sets page settings that are used as defaults for all pages to be printed.
Document.PrintDocument Property Gets the PrintDocument object.
DefaultPageSettings.PaperSize Property Sets the custom paper size.
Document.LoadFromFile() Method Loads the sample document.
PrintDocument.Print() Method Prints the document.

The following are the steps to print Word on a custom paper size.

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

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

            //Load the document
            doc.LoadFromFile(@"Sample.docx");

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

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

            //Print the document
            printDoc.Print();

        }
    }
}
Imports Spire.Doc
Imports System.Drawing.Printing

Namespace PrintWord
	Class Program
		Private Shared Sub Main(args As String())
			'Instantiate a Document object.
			Dim doc As New Document()

			'Load the document
			doc.LoadFromFile("Sample.docx")

			'Get the PrintDocument object
			Dim printDoc As PrintDocument = doc.PrintDocument

			'Customize the paper size
			printDoc.DefaultPageSettings.PaperSize = New PaperSize("custom", 900, 800)

			'Print the document
			printDoc.Print()

		End Sub
	End Class
End Namespace

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.

page