Knowledgebase (2330)
Children categories
The XML Paper Specification (XPS) format is an electronic representation of digital documents based on XML. It is a paginated, fixed-layout format that enables the content and design details of a document to be maintained intact across computers. Sometimes you may need to convert a PowerPoint document to XPS for better printing or sharing, and this article will demonstrate how to accomplish this task programmatically using Spire.Presentation for .NET.
Install Spire.Presentation for .NET
To begin with, you need to add the DLL files included in the Spire.Presentation 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.Presentation
Convert PowerPoint to XPS
The detailed steps are as follows:
- Create a Presentation instance.
- Load a sample PowerPoint document using Presentation.LoadFromFile() method.
- Save the PowerPoint document to XPS using Presentation.SaveToFile(String, FileFormat) method.
- C#
- VB.NET
using Spire.Presentation;
namespace PowerPointtoXPS
{
class Program
{
static void Main(string[] args)
{
//Create a Presentation instance
Presentation presentation = new Presentation();
//Load a sample PowerPoint document
presentation.LoadFromFile("test.pptx");
//Save to XPS file
presentation.SaveToFile("toXPS.xps", FileFormat.XPS);
}
}
}

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 encounter a situation where you need to replace images in a Word document. For example, if you are creating a resume from a template, you may need to replace the profile picture in the template with your own photo. In this article, we will show you how to replace images in a Word document 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
Replace Image with New Image in Word in C# and VB.NET
To replace an image in a Word document with another image, you need to loop through the elements of the document, find the images and add them to a list, then get the image that you want to replace from the list and call the DocPicture.LoadImage() method to replace it with another image.
The following are the detailed steps:
- Initialize an instance of the Document class.
- Load a Word document using Document.LoadFromFile() method.
- Initialize an instance of the List class.
- Iterate through all sections in the document.
- Iterate through all paragraphs in each section.
- Iterate through all child objects in each paragraph.
- Find the images and add them to the list.
- Get a specific image from the list and replace it with another image using DocPicture.LoadImage() method.
- Save the result document using Document.SaveToFile() method.
- C#
- VB.NET
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Collections.Generic;
using System.Drawing;
namespace ReplaceImageWithImage
{
internal class Program
{
static void Main(string[] args)
{
//Initialize an instance of the Document class
Document doc = new Document();
//Load a Word document
doc.LoadFromFile("Sample.docx");
//Initialize an instance of the List class
List pictures = new List();
//Iterate through all sections in the document
foreach (Section sec in doc.Sections)
{
//Iterate through all paragraphs in each section
foreach (Paragraph para in sec.Paragraphs)
{
//Iterate through all child objects in each paragraph
foreach (DocumentObject docObj in para.ChildObjects)
{
//Find the images and add them to the list
if (docObj.DocumentObjectType == DocumentObjectType.Picture)
{
pictures.Add(docObj);
}
}
}
}
//Replace the first picture in the list with another image
DocPicture picture = pictures[0] as DocPicture;
picture.LoadImage(Image.FromFile(@"doc.png"));
//Save the result document
doc.SaveToFile("ReplaceWithNewImage.docx", FileFormat.Docx2013);
}
}
}

Replace Image with Text in Word in C# and VB.NET
Spire.Doc doesn’t provide a direct method to replace image with text, but you can achieve this task by inserting the text at the image location and then removing the image from the document.
The following steps demonstrate how to replace all images in a Word document with text:
- Initialize an instance of the Document class.
- Load a Word document using Document.LoadFromFile() method.
- Iterate through all sections in the document.
- Iterate through all paragraphs in each section.
- Initialize an instance of the List class.
- Iterate through all child objects in each paragraph.
- Find the images and add them to the list.
- Iterate through the images in the list.
- Get the index of the image in the paragraph using Paragraph.ChildObjects.Indexof() method.
- Initialize an instance of TextRange class and set text for the text range through TextRange.Text property.
- Insert the text range at the image location using Paragraph.ChildObjects.Insert() method.
- Remove the image from the paragraph using Paragraph.ChildObjects.Remove() method.
- Save the result document using Document.SaveToFile() method.
- C#
- VB.NET
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Collections.Generic;
namespace ReplaceImageWithText
{
internal class Program
{
static void Main(string[] args)
{
//Initialize an instance of the Document class
Document doc = new Document();
//Load a Word document
doc.LoadFromFile("Sample.docx");
int j = 1;
//Iterate through all sections in the document
foreach (Section sec in doc.Sections)
{
//Iterate through all paragraphs in each section
foreach (Paragraph para in sec.Paragraphs)
{
//Initialize an instance of the List class
List pictures = new List();
//Find the images and add them to the list
foreach (DocumentObject docObj in para.ChildObjects)
{
if (docObj.DocumentObjectType == DocumentObjectType.Picture)
{
pictures.Add(docObj);
}
}
//Iterate through all images in the list and replace them with text "Here is image {image index}"
foreach (DocumentObject pic in pictures)
{
int index = para.ChildObjects.IndexOf(pic);
TextRange range = new TextRange(doc);
range.Text = string.Format("Here is image-{0}", j);
para.ChildObjects.Insert(index, range);
para.ChildObjects.Remove(pic);
j++;
}
}
}
//Save the result document
doc.SaveToFile("ReplaceWithText.docx", FileFormat.Docx);
}
}
}

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.

Printing PDF documents in C# can be achieved without Adobe Acrobat. Using Spire.PDF, developers can easily incorporate powerful printing functionalities into their applications. This library provides a smooth, programmatic method to manage everything from simple printing tasks to advanced features such as duplex printing and silent printing. Whether you need to print a single page or an entire multi-page document, Spire.PDF ensures precision and efficiency.
In this article, we’ll explore how to leverage Spire.PDF for .NET to print PDFs directly from your C# applications , customize print settings, and resolve common issues.
Best C# .NET Library for Printing PDF
When it comes to PDF printing in C#, several libraries are available, but Spire.PDF stands out as one of the most robust and developer-friendly options. Spire.PDF offers:
- Comprehensive PDF manipulation capabilities
- Simple yet powerful printing functionality
- No dependency on Adobe Acrobat
- Support for both Windows Forms and Console applications
- Extensive customization options for print settings
The library handles all the low-level complexities of PDF rendering and printer communication, allowing developers to focus on implementing business logic rather than wrestling with printer APIs.
To begin, install the Spire.PDF for .NET library via NuGet Package Manager :
Install-Package Spire.PDF
Alternatively, you can download Spire.PDF directly from our official website and reference the DLLs in your project.
Basic PDF Printing in C#: Directly Print PDF to Default Printer
Now, let's start with the simplest scenario: printing a PDF document to the system's default printer. Spire.PDF makes this incredibly straightforward:
using Spire.Pdf;
namespace PrintWithDefaultPrinter
{
class Program
{
static void Main(string[] args)
{
// Create a PdfDocument object
PdfDocument doc = new PdfDocument();
// Load a PDF file
doc.LoadFromFile("C:/Users/Administrator/Desktop/Input.pdf");
// Print to default printer
doc.Print();
// Clean up resources
doc.Dispose();
}
}
}
This basic example demonstrates the core workflow:
- Create a PdfDocument instance.
- Load an existing PDF file.
- Call the Print() method to send the document to the default printer.
- Clean up resources.
The simplicity of this approach makes it ideal for scenarios where you just need to quickly print a document without any special requirements.
Advanced Print Settings in Spire.PDF
While the basic printing method works for simple cases, most real-world applications require more control over the printing process. Spire.PDF provides extensive print customization through its PrintSettings property.
1. Specify Printer Name
Instead of using the default printer, you can target a specific printer by name:
doc.PrintSettings.PrinterName = "Your Printer Name";
2. Set Print Page Range
For large documents, you might want to print only specific pages. Use SelectPageRange() to define a start and end page:
doc.PrintSettings.SelectPageRange(1, 5); // Pages 1 to 5
For non-sequential pages, use SelectSomePages() :
doc.PrintSettings.SelectSomePages(newint[] { 1, 3, 5, 7 });
3. Adjust Number of Copies
Need multiple copies? Set the Copies property to the desired number:
doc.PrintSettings.Copies = 2;
4. Enable Duplex (Double-Sided) Printing
Duplex printing can save paper and is commonly required for professional documents. Check if the printer supports it first:
if (doc.PrintSettings.CanDuplex)
{
doc.PrintSettings.Duplex = Duplex.Default;
}
5. Black and White (Grayscale) Printing
For documents where color isn't necessary, you can force grayscale printing:
doc.PrintSettings.Color = false;
6. Silent Printing (Hide Dialog Box & Process)
For automated workflows, you often want to print without any user interaction:
doc.PrintSettings.PrintController = new StandardPrintController();
This suppresses all print dialogs, making the process completely silent. Use this carefully, as it removes the user's ability to confirm or adjust settings.
7. Print Different Pages to Different Trays
Advanced printers with multiple paper trays can handle complex document assembly automatically:
// Register event handler for paper settings
doc.PrintSettings.PaperSettings += delegate (object sender, PdfPaperSettingsEventArgs e)
{
// Use tray 1 for pages 1-10
if (1 <= e.CurrentPaper && e.CurrentPaper <= 10)
{
e.CurrentPaperSource = e.PaperSources[0];
}
// Use tray 2 for pages beyond 10
else
{
e.CurrentPaperSource = e.PaperSources[1];
}
};
This feature allows for professional document production where cover pages, inserts, or chapter dividers can automatically print on different paper stock.
8. Print Multiple Pages Per Sheet
Optimize paper usage by printing multiple PDF pages on a single sheet. The SelectMultiPageLayout() method lets you specify the grid layout (rows × columns) for page arrangement:
doc.PrintSettings.SelectMultiPageLayout(2, 2); // Prints 4 pages per sheet (2 rows × 2 columns)
This setting is ideal for printing booklets, handouts, or draft documents while conserving paper. The pages are automatically scaled to fit the specified layout.
Conclusion
Printing PDFs programmatically in C# doesn't require Adobe Acrobat or complex printer APIs . With Spire.PDF, you can implement everything from simple printing to advanced, professional-grade output with just a few lines of code. The library abstracts away the complexities while providing fine-grained control when needed.
Whether you're building a document management system, a reporting tool, or any application that needs PDF printing capabilities, Spire.PDF offers a comprehensive solution that balances ease of use with powerful features. Start exploring its capabilities today and transform your PDF printing workflows!
FAQs
Q1: How do I print a PDF in C# without Adobe?
Use Spire.PDF’s Print() method to send the document directly to the printer.
Q2: Can I print PDFs to a network printer?
Yes, as long as the printer is properly installed on your system, you can specify it by name just like a local printer.
Q3: How to print a PDF in WPF or WinFroms?
The code snippets provided in this guide work seamlessly in both WPF and WinForms applications. In WPF, consider adding printer selection dialogs for an enhanced user experience.
Q4: How can I improve print quality when printing PDFs programmatically in C#?
You can control print quality by setting the printer resolution using the PrinterResolutionKind property. For high-quality output, use:
doc.PrintSettings.PrinterResolutionKind = PdfPrinterResolutionKind.High;
Spire.PDF supports these resolution options:
- Low (Draft quality)
- Medium (Standard quality)
- High (Best quality)
- Custom (Requires additional DPI settings)
Note: Actual output depends on your printer's capabilities. For photo-quality prints, ensure your printer supports high DPI (e.g., 1200x1200) and use high-quality paper. Combine this with doc.PrintSettings.Color = true for color-critical documents.
Get a Free License
To fully experience the capabilities of Spire.PDF for .NET without any evaluation limitations, you can request a free 30-day trial license.