Spire.PDF for .NET (290)
Spire.PDF offers a method of PdfDocument.MergeFiles(); to enable developers to merge PDF files easily and conveniently. This article will show you how to insert a new page from the first PDF into the second PDF file at a specified index by using the method of Pages.Insert(); offered by Spire.PDF.
Note: Before Start, please download the latest version of Spire.PDF and add Spire.PDF.dll in the bin folder as the reference of Visual Studio.
Here comes to the steps of how to insert the page from the first PDF (sample.pdf) into the second PDF (test.pdf) at a specified index:
Step 1: Create the first PDF document and load file.
PdfDocument doc1 = new PdfDocument();
doc1.LoadFromFile("sample.pdf");
Step 2: Create the second PDF document and load file.
PdfDocument doc2 = new PdfDocument();
doc2.LoadFromFile("test.pdf");
Step 3: Get the first page and its size from the first PDF document.
PdfPageBase page = doc1.Pages[0]; SizeF size = page.Size;
Step 4: Inserts a new blank page with the specified size at the specified index into the second PDF.
PdfPageBase newPage = doc2.Pages.Insert(1, size);
Step 5: Copy the contents on the page into the second PDF.
newPage.Canvas.DrawTemplate(page.CreateTemplate(), new PointF(0, 0));
Step 6: Save the document to file.
doc2.SaveToFile("result.pdf");
Effective screenshot of insert a new PDF page to an existing PDF at a specified index:

Full codes:
using Spire.Pdf;
using System.Drawing;
namespace InsertNewPage
{
class Program
{
static void Main(string[] args)
{
PdfDocument doc1 = new PdfDocument();
doc1.LoadFromFile("sample.pdf");
PdfDocument doc2 = new PdfDocument();
doc2.LoadFromFile("test.pdf");
PdfPageBase page = doc1.Pages[0];
SizeF size = page.Size;
PdfPageBase newPage = doc2.Pages.Insert(1, size);
newPage.Canvas.DrawTemplate(page.CreateTemplate(), new PointF(0, 0));
doc2.SaveToFile("result.pdf");
}
}
}
For PDF documents with pages out of order, rearranging the pages can avoid confusing the reader and also make the document more organized. This article will demonstrate how to programmatically rearrange the pages in an existing PDF document 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 DLLs files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.PDF
Rearrange Pages in an Existing PDF Document
- Create a PdfDocument object.
- Load a sample PDF document using PdfDocument.LoadFromFile() method.
- Get the pages in the PDF document using PdfDocument.Pages property.
- Rearrange PDF pages using PdfPageCollection.ReArrange(int[] orderArray) method.
- Save the document to another file using PdfDocument.SaveToFile() method.
- C#
- VB.NET
using Spire.Pdf;
namespace RearrangePDF
{
class Program
{
static void Main(string[] args)
{
//Create a PdfDocument object
PdfDocument pdf = new PdfDocument();
//Load a sample PDF document
pdf.LoadFromFile("input.pdf");
//Rearrange pages by page index
pdf.Pages.ReArrange(new int[] { 1, 0, 2, 3 });
//Save the document
pdf.SaveToFile("ChangeOrder.pdf");
pdf.Close();
}
}
}

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.
Print Multiple PDF Pages per Sheet and Print Single PDF Page to Multiple Sheets in C#
2017-10-24 08:04:36 Written by KoohjiWhen it comes to printing, Spire.PDF offers users a wide range of options to fulfil their printing needs. Two of these options are “print multiple PDF pages per sheet” and “print single PDF page to multiple sheets”. This article elaborates how to achieve these two options using Spire.PDF and C#.
Print Multiple PDF Pages per Sheet
Uses can invoke the SelectMultiPageLayout(int rows, int columns) method of the PdfPrintSettings class to print multiple PDF pages per sheet of paper.
using Spire.Pdf;
namespace PrintPDF
{
class Program
{
static void Main(string[] args)
{
//Initialize a PdfDocument object
PdfDocument pdf = new PdfDocument();
//Load the pdf file
pdf.LoadFromFile("Input.pdf");
//Print two PDF pages per sheet
pdf.PrintSettings.SelectMultiPageLayout(1, 2);
pdf.Print();
}
}
}
Below screenshot shows the sample pdf document which contains two pages:

Screenshot after printing to XPS:

Print Single PDF Page to Multiple Sheets
The SelectSplitPageLayout() method of the PdfPrintSettings class can be used when printing a large PDF page to multiple sheets. This method splits the PDF page to multiple pages according to the standard A4 paper size: 595pt*842pt.
using Spire.Pdf;
namespace PrintPDF2
{
class Program
{
static void Main(string[] args)
{
//Initialize a PdfDocument object
PdfDocument pdf = new PdfDocument();
//Load the pdf file
pdf.LoadFromFile("Input1.pdf");
//Print the PDF page to multiple sheets
pdf.PrintSettings.SelectSplitPageLayout();
pdf.Print();
}
}
}
Below screenshot shows the sample pdf document which contains a single page with a size of 2100pt*750pt:

Screenshot after printing to XPS:

PDF documents have been a popular choice for sharing information due to their cross-platform compatibility and ability to preserve the original layout and formatting. However, as the web continues to evolve, there is an increasing demand for content that can be easily integrated into websites and other online platforms. In this context, converting PDF to HTML format has become highly valuable. By converting PDF files to more flexible and accessible HTML, users gain the ability to better utilize, share, and reuse PDF-based information within the web environment. In this article, we will demonstrate how to convert PDF files to HTML format in C# using Spire.PDF for .NET.
- Convert PDF to HTML in C#
- Set Conversion Options When Converting PDF to HTML in C#
- Convert PDF to HTML Stream in C#
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 HTML in C#
To convert a PDF document to HTML format, you can use the PdfDocument.SaveToFile(string fileName, FileFormat.HTML) method provided by Spire.PDF for .NET. The detailed steps are as follows.
- Create an instance of the PdfDocument class.
- Load a PDF document using the PdfDocument.LoadFromFile(string fileName) method.
- Save the PDF document to HTML format using the PdfDocument.SaveToFile(string fileName, FileFormat.HTML) method.
- C#
using Spire.Pdf;
namespace ConvertPdfToHtml
{
internal class Program
{
static void Main(string[] args)
{
// Create an instance of the PdfDocument class
PdfDocument doc = new PdfDocument();
// Load a PDF document
doc.LoadFromFile("Sample.pdf");
// Save the PDF document to HTML format
doc.SaveToFile("PdfToHtml.html", FileFormat.HTML);
doc.Close();
}
}
}

Set Conversion Options When Converting PDF to HTML in C#
The PdfConvertOptions.SetPdfToHtmlOptions() method allows you to customize the conversion options when transforming PDF files to HTML. This method takes several parameters that you can use to configure the conversion process, such as:
- useEmbeddedSvg (bool): Indicates whether to embed SVG in the resulting HTML file.
- useEmbeddedImg (bool): Indicates whether to embed images in the resulting HTML file. This option is applicable only when useEmbeddedSvg is set to false.
- maxPageOneFile (int): Specifies the maximum number of pages to be included per HTML file. This option is applicable only when useEmbeddedSvg is set to false.
- useHighQualityEmbeddedSvg (bool): Indicates whether to use high-quality embedded SVG in the resulting HTML file. This option is applicable when useEmbeddedSvg is set to true.
The following steps explain how to customize the conversion options when transforming a PDF to HTML using Spire.PDF for .NET.
- Create an instance of the PdfDocument class.
- Load a PDF document using the PdfDocument.LoadFromFile(string fileName) method.
- Get the PdfConvertOptions object using the PdfDocument.ConvertOptions property.
- Set the PDF to HTML conversion options using PdfConvertOptions.SetPdfToHtmlOptions(bool useEmbeddedSvg, bool useEmbeddedImg, int maxPageOneFile, bool useHighQualityEmbeddedSvg) method.
- Save the PDF document to HTML format using PdfDocument.SaveToFile(string fileName, FileFormat.HTML) method.
- C#
using Spire.Pdf;
namespace ConvertPdfToHtmlWithCustomOptions
{
internal class Program
{
static void Main(string[] args)
{
// Create an instance of the PdfDocument class
PdfDocument doc = new PdfDocument();
// Load a PDF document
doc.LoadFromFile("Sample.pdf");
// Set the conversion options to embed images in the resulting HTML and limit one page per HTML file
PdfConvertOptions pdfToHtmlOptions = doc.ConvertOptions;
pdfToHtmlOptions.SetPdfToHtmlOptions(false, true, 1);
// Save the PDF document to HTML format
doc.SaveToFile("PdfToHtmlWithCustomOptions.html", FileFormat.HTML);
doc.Close();
}
}
}
Convert PDF to HTML Stream in C#
Instead of saving a PDF document to an HTML file, you can save it to an HTML stream by using the PdfDocument.SaveToStream(Stream stream, FileFormat.HTML) method. The detailed steps are as follows.
- Create an instance of the PdfDocument class.
- Load a PDF document using the PdfDocument.LoadFromFile(string fileName) method.
- Create an instance of the MemoryStream class.
- Save the PDF document to an HTML stream using the PdfDocument.SaveToStream(Stream stream, FileFormat.HTML) method.
- C#
using Spire.Pdf;
using System.IO;
namespace ConvertPdfToHtmlStream
{
internal class Program
{
static void Main(string[] args)
{
// Create an instance of the PdfDocument class
PdfDocument doc = new PdfDocument();
// Load a PDF document
doc.LoadFromFile("Sample.pdf");
// Save the PDF document to HTML stream
using (var fileStream = new MemoryStream())
{
doc.SaveToStream(fileStream, FileFormat.HTML);
// You can now do something with the HTML stream, such as Write it to a file
using (var outputFile = new FileStream("PdfToHtmlStream.html", FileMode.Create))
{
fileStream.Seek(0, SeekOrigin.Begin);
fileStream.CopyTo(outputFile);
}
}
doc.Close();
}
}
}
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.
Spire.PDF allows getting and verifying a specific signature in a PDF file, now starts from version 3.8.82, Spire.PDF supports to get all certificates in a PDF signature. In this article, we will show you the steps of how to achieve this task by using Spire.PDF.
For demonstration, we used a template PDF file which contains two certificates:

Code snippets:
Step 1: Instantiate a PdfDocument object and load the PDF file.
PdfDocument doc = new PdfDocument();
doc.LoadFromFile("UPS.pdf");
Step 2: Create a List object.
List<PdfSignature> signatures = new List<PdfSignature>();
Step 3: Get all signatures from the PDF file and add them into the list object.
var form = (PdfFormWidget)doc.Form;
for (int i = 0; i < form.FieldsWidget.Count; ++i)
{
var field = form.FieldsWidget[i] as PdfSignatureFieldWidget;
if (field != null && field.Signature != null)
{
PdfSignature signature = field.Signature;
signatures.Add(signature);
}
}
Step 4: Get the first signature from the list, and then get all the certificates from the signature.
PdfSignature signatureOne = signatures[0]; X509Certificate2Collection collection = signatureOne.Certificates;
Effective screenshot:

Full code:
using System;
using System.Collections.Generic;
using System.Security.Cryptography.X509Certificates;
using Spire.Pdf;
using Spire.Pdf.Security;
using Spire.Pdf.Widget;
namespace Get_all_certificates_in_PDF_signature
{
class Program
{
static void Main(string[] args)
{
PdfDocument doc = new PdfDocument();
doc.LoadFromFile("UPS.pdf");
List<PdfSignature> signatures = new List<PdfSignature>();
var form = (PdfFormWidget)doc.Form;
for (int i = 0; i < form.FieldsWidget.Count; ++i)
{
var field = form.FieldsWidget[i] as PdfSignatureFieldWidget;
if (field != null && field.Signature != null)
{
PdfSignature signature = field.Signature;
signatures.Add(signature);
}
}
PdfSignature signatureOne = signatures[0];
X509Certificate2Collection collection = signatureOne.Certificates;
foreach (var certificate in collection)
{
Console.WriteLine(certificate.Subject);
}
Console.ReadKey();
}
}
}
More...