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.
- Instantiate a Document object
- Load the sample document using Document.LoadFromFile() method.
- Get the PrintDocument object using Document.PrintDocument property.
- Set the custom paper size using DefaultPageSettings.PaperSize Property.
- Print the document using PrintDocument.Print() method.
- 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.
