Spire.PDF for .NET (290)
In order to distinguish nested bookmarks more clearly, you can set different font style and font color for parent and child bookmarks respectively. In this article, I’ll introduce how to modify bookmarks in the terms of font style, font color and text using Spire.PDF in C#, VB.NET.
Main Steps:
Step 1: Initialize a new object of PdfDocument and load the existing PDF file.
PdfDocument doc = new PdfDocument("Bookmark.Pdf");
Step 2: Get all bookmarks from the test file.
PdfBookmarkCollection bookmarks = doc.Bookmarks;
Step 3: Traverse very first-level bookmark in the bookmark tree. Change the properties of selected or all bookmarks, including title, color and display style.
foreach (PdfBookmark parentBookmark in bookmarks)
{
//change "Word Bookmarks" to "M BookMark"
bookmarks[0].Title = "Modified BookMarks";
//set the color of the bookmark
parentBookmark.Color = Color.Black;
parentBookmark.DisplayStyle = PdfTextStyle.Bold;
//edit child bookmark of parent bookmark
EditChildBookmark(parentBookmark);
}
Step 4: In step 3, there is a nested method predefined as below. By invoking this method, we are able to modify child bookmarks of the upper level bookmark. In this example, we have two nested level bookmarks under the first-level bookmark.
static void EditChildBookmark(PdfBookmark parentBookmark)
{
foreach (PdfBookmark childBookmark in parentBookmark)
{
childBookmark.Color = Color.Brown;
childBookmark.DisplayStyle = PdfTextStyle.Regular;
EditChild2Bookmark(childBookmark);
}
}
static void EditChild2Bookmark(PdfBookmark childBookmark)
{
foreach (PdfBookmark child2Bookmark in childBookmark)
{
child2Bookmark.Color = Color.LightSalmon;
child2Bookmark.DisplayStyle = PdfTextStyle.Italic;
}
}
Step 5: Save and launch the file.
doc.SaveToFile("Result.Pdf");
System.Diagnostics.Process.Start("Result.Pdf");
Screenshot of Effect:

Entire Code:
using Spire.Pdf;
using Spire.Pdf.Bookmarks;
using System.Drawing;
namespace ModifyBookmarks
{
class Program
{
static void Main(string[] args)
{
PdfDocument doc = new PdfDocument("Bookmark.Pdf");
PdfBookmarkCollection bookmarks = doc.Bookmarks;
foreach (PdfBookmark parentBookmark in bookmarks)
{
//change "Word Bookmarks" to "M BookMark"
bookmarks[0].Title = "Modified BookMarks";
//set the color of the bookmark
parentBookmark.Color = Color.Black;
parentBookmark.DisplayStyle = PdfTextStyle.Bold;
//edit child bookmark of parent bookmark
EditChildBookmark(parentBookmark);
}
doc.SaveToFile("Result.Pdf");
System.Diagnostics.Process.Start("Result.Pdf");
}
static void EditChildBookmark(PdfBookmark parentBookmark)
{
foreach (PdfBookmark childBookmark in parentBookmark)
{
childBookmark.Color = Color.Brown;
childBookmark.DisplayStyle = PdfTextStyle.Regular;
EditChild2Bookmark(childBookmark);
}
}
static void EditChild2Bookmark(PdfBookmark childBookmark)
{
foreach (PdfBookmark child2Bookmark in childBookmark)
{
child2Bookmark.Color = Color.LightSalmon;
child2Bookmark.DisplayStyle = PdfTextStyle.Italic;
}
}
}
}
Imports Spire.Pdf
Imports Spire.Pdf.Bookmarks
Imports System.Drawing
Namespace ModifyBookmarks
Class Program
Private Shared Sub Main(args As String())
Dim doc As New PdfDocument("Bookmark.Pdf")
Dim bookmarks As PdfBookmarkCollection = doc.Bookmarks
For Each parentBookmark As PdfBookmark In bookmarks
'change "Word Bookmarks" to "M BookMark"
bookmarks(0).Title = "Modified BookMarks"
'set the color of the bookmark
parentBookmark.Color = Color.Black
parentBookmark.DisplayStyle = PdfTextStyle.Bold
'edit child bookmark of parent bookmark
EditChildBookmark(parentBookmark)
Next
doc.SaveToFile("Result.Pdf")
System.Diagnostics.Process.Start("Result.Pdf")
End Sub
Private Shared Sub EditChildBookmark(parentBookmark As PdfBookmark)
For Each childBookmark As PdfBookmark In parentBookmark
childBookmark.Color = Color.Brown
childBookmark.DisplayStyle = PdfTextStyle.Regular
EditChild2Bookmark(childBookmark)
Next
End Sub
Private Shared Sub EditChild2Bookmark(childBookmark As PdfBookmark)
For Each child2Bookmark As PdfBookmark In childBookmark
child2Bookmark.Color = Color.LightSalmon
child2Bookmark.DisplayStyle = PdfTextStyle.Italic
Next
End Sub
End Class
End Namespace
Converting PDF files to images is a common requirement for applications that involve document previews, thumbnails generation, or PDF content archiving. For .NET developers, the Spire.PDF library offers a robust and efficient way to achieve this PDF to image conversion with high fidelity. Its key advantages include:
- High-Fidelity Rendering: Preserve original layouts and graphics.
- Multi-Format Support: Export to PNG, JPEG, BMP, TIFF, and more.
- Cross-Platform: Works with .NET Framework, .NET Core, and .NET 6+.

In this guide, you’ll learn how to use the Spire.PDF for .NET library to convert PDF to JPG or PNG images in C#, with installation guide, practical code snippets, and conversion optimization tips.
- Install the PDF to Image Converter Library
- Convert PDF to JPG Images in C#: Basic Example
- Advanced PDF to Image Conversion Options
- Performance Optimization Tips
- FAQs (PDF to TIFF, PDF to SVG)
Install the PDF to Image Converter Library
Before you can start using C# to convert PDF to PNG or PDF to JPG, it’s necessary to install the Spire.PDF for .NET library first. You can do this via NuGet Package Manager:
- Open your project in Visual Studio
- Go to “Tools > NuGet Package Manager > Manage NuGet Packages for Solution”
- Search for "Spire.PDF"
- Select and install the package
Alternatively, use the Package Manager Console:
PM> Install-Package Spire.PDF
Once installed, you're ready to start writing code to handle PDF to image conversions.
Convert PDF to JPG Images in C#: Basic Example
The SaveAsImage(int pageIndex, PdfImageType type) method of the PdfDocument class can be used to convert a specified PDF page to an image. The two parameters are:
- pageIndex (Int32)
- Description: The index of the PDF page to be converted (page index starts from 0).
- Example:
- 0 = First page
- 1 = Second page
- type (PdfImageType)
- Description: Specifies the type of the PdfImage.
- Enum Values:
- Bitmap
- Metafile
The following code demonstrates how to convert the first page of a PDF to a JPEG image using Spire.PDF:
using Spire.Pdf;
using Spire.Pdf.Graphics;
using System.Drawing;
using System.Drawing.Imaging;
namespace PDFtoJPG
{
class Program
{
static void Main(string[] args)
{
// Create a PdfDocument instance
PdfDocument pdf = new PdfDocument();
// Load a sample PDF document
pdf.LoadFromFile("input.pdf");
// Convert the first page to a bitmap image
Image image = pdf.SaveAsImage(0, PdfImageType.Bitmap);
// Save the image as a JPG file
image.Save("PdfToJPG.jpg", ImageFormat.Jpeg);
// Disposes PDF resources
pdf.Dispose();
}
}
}

Advanced PDF to Image Conversion Options
1. Set Image Resolution
Spire.PDF offers another overload of the SaveAsImage() method that takes four parameters: the page index, image type, horizontal (dpiX) and vertical (dpiY) resolution. This allows you to control the quality and size of the output image by customizing the DPI settings.
The following code example converts the first page of a PDF to a high-resolution PNG image (300 DPI):
using Spire.Pdf;
using Spire.Pdf.Graphics;
using System.Drawing;
using System.Drawing.Imaging;
namespace PDFtoImage
{
class Program
{
static void Main(string[] args)
{
// Create a PdfDocument instance
PdfDocument pdf = new PdfDocument();
// Load a sample PDF document
pdf.LoadFromFile("input.pdf");
// Convert the first page to an image and with specified image resolution
Image image = pdf.SaveAsImage(0, PdfImageType.Bitmap, 300, 300);
// Save the image as a PNG file
image.Save("PdfToPNG.png", ImageFormat.Png);
// Disposes PDF resources
pdf.Dispose();
}
}
}

Note: Higher DPI improves image quality but slow down conversion.
2. Convert an Entire PDF File to Images
Loop through all pages in the PDF and then convert each one to a separate image.
// Loop through all pages in the PDF
for (int i = 0; i < pdf.Pages.Count; i++)
{
// Convert each page to an image and set the image Dpi
Image image = pdf.SaveAsImage(i, PdfImageType.Bitmap, 300, 300);
// Save the image as PNG format to a specified folder
string file = string.Format("Image\\ToImage-{0}.png", i);
image.Save(file, ImageFormat.Png);
}
3. Convert to Different Image Formats
By changing the ImageFormat parameter of the Image.Save() method, you can convert PDF to JPG, PNG, BMP, or Gif formats:
// Save to JPEG format
image.Save("PDFtoJPG.jpg", ImageFormat.Jpeg);
// Save to PNG format
image.Save("PDFtoPNG.png", ImageFormat.Png);
// Save to BMP format
image.Save("PDFtoBMP.bmp", ImageFormat.Bmp);
// Save to EMF format
image.Save("PDFtoEMF.emf", ImageFormat.Emf);
// Save to GIF format
image.Save("PDFtoGIF.gif", ImageFormat.Gif);
4. Using MemoryStream
For scenarios requiring stream handling, use the following code:
// Create a MemoryStream object
MemoryStream ms = new MemoryStream();
// Convert the first page to an image with specified dpi
Image image = pdf.SaveAsImage(0, PdfImageType.Bitmap, 300, 300);
// Save the image to stream in PNG format
image.Save(ms, ImageFormat.Png);
// Write the Stream object to a byte array for further processing
byte[] imageBytes = ms.ToArray();
Performance Optimization Tips
- Memory Management: Convert pages one by one to avoid memory overload for large PDFs.
- DPI Optimization: High DPI settings improve image quality, but also increase file size and processing time.
- Dispose Resources: Call the Dispose() method of PdfDocument class to free resources.
Conclusion
Spire.PDF provides a straightforward and efficient way to convert PDF files to images in C#. With its rich feature set, high-quality rendering, and support for multiple image formats, it’s a reliable choice for both simple and complex PDF-to-image conversion tasks. By following the examples in this article, you can quickly implement PDF converter functionality in your .NET applications.
FAQs
Q1: How to remove the watermarks on the output images?
A: You can request a free 30-day trial license here to remove the red watermark and fully evaluate the Spire.PDF library. Or you can use its Free version (with certain page limitations).
Q2: Can I convert PDFs to multi-page TIFF files with Spire.PDF?
A: Yes! A detailed guide for this can be found at: Convert PDF to TIFF in C#
Q3: Can I convert PDF to SVG using Spire.PDF?
A: Yes! Spire.PDF for .NET offers the SaveToFile(String, FileFormat.SVG) method to convert a PDF file to a SVG file. You can check out the article for a comprehensive guide.
Q4. Where can I find additional documentation or support?
Spire.PDF has a function of adding, removing the blank pages in C#. We have already shown you how to remove the blank page in a PDF file. This article will show you how to insert an empty page in a PDF file in C#. By using the Spire.PDF, we can add the blank page to any place in the PDF file you want, such as at the first, the middle of the PDF file or at the end of the PDF file. It is very easy and you only need three lines of code to accomplish this task.
Make sure Spire.PDF for .NET has been installed correctly and then add Spire.Pdf.dll as reference in the downloaded Bin folder though the below path: "..\Spire.Pdf\Bin\NET4.0\Spire.Pdf.dll".
The following code snippet shows you how to insert an empty page in a PDF file. We will show you how to add the empty page at the end of the file and as the second page of the file.
//create a PDF document and load file
PdfDocument doc = new PdfDocument();
doc.LoadFromFile("sample.pdf");
//insert blank page at the end of the PDF file
doc.Pages.Add();
//insert blank page as the second page
doc.Pages.Insert(1);
//Save the document to file
doc.SaveToFile("result.pdf");
Check the effective screenshots as below:
Add the blank page at the end of the PDF file:

Add the blank page as the second page of the PDF file:

Full codes:
using Spire.Pdf;
using System;
namespace InsertPage
{
class Program
{
static void Main(string[] args)
{
//create PdfDocument instance and load file
PdfDocument doc = new PdfDocument();
doc.LoadFromFile("sample.pdf");
//insert blank page as last page
doc.Pages.Add();
doc.SaveToFile("result.pdf");
doc.Close();
System.Diagnostics.Process.Start("result.pdf");
//create PdfDocument instance and load file
PdfDocument doc2 = new PdfDocument();
doc2.LoadFromFile("sample.pdf");
//insert blank page as second page
doc2.Pages.Insert(1);
doc2.SaveToFile("result2.pdf");
doc2.Close();
System.Diagnostics.Process.Start("result2.pdf");
}
}
}
By using Acrobat, we can edit the PDF bookmark actions and set the zoom level to "Inherit Zoom". Then no matter which bookmark you click, the PDF page will stay the same size as the previous page you are viewing and it won't be changed. Spire.PDF also enables developers to set the Bookmark actions to inherit zoom by setting PdfDestination.Zoom as 0. This article will show you how to update bookmarks in a PDF document in C#.
Firstly, view the original screenshot of the PDF bookmark property:

Here comes to the step of how to use Spire.PDF to set the PDF bookmark actions.
Step 1: Create a new PDF document and load the document from file.
PdfDocument pdfdoc = new PdfDocument();
pdfdoc.LoadFromFile("TheGreatGatsby.pdf");
Step 2: Get bookmarks collections of the PDF file.
PdfBookmarkCollection bookmarks = pdfdoc.Bookmarks;
Step 3: Set Zoom level as 0, which the value is inherit zoom.
foreach (PdfBookmark bookMark in bookmarks)
{
//value 1 is the actual size, other value is the customized size.
bookMark.Destination.Zoom =0;
}
Step 4: Save the document to file.
pdfdoc.SaveToFile("result.pdf");
Effective screenshot after setting the zoom level to Inherit zoom.

Full codes:
using Spire.Pdf;
using Spire.Pdf.Bookmarks;
namespace SetInheritZoomProperty
{
class Program
{
static void Main(string[] args)
{
PdfDocument pdfdoc = new PdfDocument();
pdfdoc.LoadFromFile("TheGreatGatsby.pdf");
PdfBookmarkCollection bookmarks = pdfdoc.Bookmarks;
foreach (PdfBookmark bookMark in bookmarks)
{
bookMark.Destination.Zoom = 0;
}
pdfdoc.SaveToFile("result.pdf");
}
}
}
XPS is a format similar to PDF but uses XML in layout, appearance and printing information of a file. XPS format was developed by Microsoft and it is natively supported by the Windows operating systems. If you want to work with your PDF files on a Windows computer without installing other software, you can convert it to XPS format. Likewise, if you need to share a XPS file with a Mac user or use it on various devices, it is more recommended to convert it to PDF. This article will demonstrate how to programmatically convert PDF to XPS or XPS to PDF using Spire.PDF for .NET.
Install Spire.PDF for .NET
To begin with, you need to add the DLL files included in the Spire.PDF 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.PDF
Convert PDF to XPS in C# and VB.NET
Spire.PDF for .NET supports converting PDF to various file formats, and to achieve the PDF to XPS conversion, you just need three lines of core code. The following are the detailed steps.
- Create a PdfDocument instance.
- Load a sample PDF document using PdfDocument.LoadFromFile() method.
- Convert the PDF document to an XPS file using PdfDocument.SaveToFile (string filename, FileFormat.XPS) method.
- C#
- VB.NET
using Spire.Pdf;
namespace ConvertPdfToXps
{
class Program
{
static void Main(string[] args)
{
//Create a PdfDocument instance
PdfDocument pdf = new PdfDocument();
//Load sample PDF document
pdf.LoadFromFile("sample.pdf");
//Save it to XPS format
pdf.SaveToFile("ToXPS.xps", FileFormat.XPS);
pdf.Close();
}
}
}

Convert XPS to PDF in C# and VB.NET
Conversion from XPS to PDF can also be achieved with Spire.PDF for .NET. While converting, you can set to keep high quality image on the generated PDF file by using the PdfDocument.ConvertOptions.SetXpsToPdfOptions() method. The following are the detailed steps.
- Create a PdfDocument instance.
- Load an XPS file using PdfDocument.LoadFromFile(string filename, FileFormat.XPS) method or PdfDocument.LoadFromXPS() method.
- While conversion, set the XPS to PDF convert options to keep high quality images using PdfDocument.ConvertOptions.SetXpsToPdfOptions() method.
- Save the XPS file to a PDF file using PdfDocument.SaveToFile(string filename, FileFormat.PDF) method.
- C#
- VB.NET
using Spire.Pdf;
namespace ConvertXPStoPDF
{
class Program
{
static void Main(string[] args)
{
//Create a PdfDocument instance
PdfDocument pdf = new PdfDocument();
//Load a sample XPS file
pdf.LoadFromFile("Sample.xps", FileFormat.XPS);
//pdf.LoadFromXPS("Sample.xps");
//Keep high quality images when converting XPS to PDF
pdf.ConvertOptions.SetXpsToPdfOptions(true);
//Save the XPS file to PDF
pdf.SaveToFile("XPStoPDF.pdf", FileFormat.PDF);
}
}
}

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.
More...