Skip to main content

C#/VB.NET Merge two PDF Pages into a Single Page

  

Though there are lots of articles illustrate how to merge PDF files, I didn’t find a solution about merge PDF pages among them. In this article, I will share you with how to merge two PDF pages into a single page in C# and VB.NET.

The library used

Free Spire.PDF for .NET

You can get the dll of the Free Spire.PDF for .NET library by downloading from the official website or installing from NuGet.org.

Merge PDF Pages

In fact, there is no direct way to merge two or more PDF pages into a single page, you can't do this even in Adobe Acrobat. Free Spire.PDF provides a CreateTemplate() method and a DrawTemplate() method which allows you to create templates of PDF pages and draw the templates to a new page, using these two methods, you’re able to merge PDF pages.

The following example shows how to merge two PDF pages inside a PDF file into a single page.

C# Language

  1. using Spire.Pdf;  
  2. using Spire.Pdf.Graphics;  
  3. using System.Drawing;  
  4.   
  5.   
  6. namespace MergeAndSplitPdfPages  
  7. {  
  8.     class Program  
  9.     {  
  10.         static void Main(string[] args)  
  11.         {  
  12.             //Load a Pdf document  
  13.             PdfDocument doc = new PdfDocument("Sample.pdf");  
  14.               
  15.             //Get the first page  
  16.             PdfPageBase page1 = doc.Pages[0];  
  17.   
  18.             //Get the second page  
  19.             PdfPageBase page2 = doc.Pages[1];  
  20.   
  21.             //Create a template for page 2  
  22.             PdfTemplate template = page2.CreateTemplate();  
  23.   
  24.             //Set transparency  
  25.             page2.Canvas.SetTransparency(1f, 1f, PdfBlendMode.Overlay);  
  26.   
  27.             //Draw the template to specified location of the first page  
  28.             page1.Canvas.DrawTemplate(template, new PointF(0, 150));  
  29.   
  30.             //Remove the second page  
  31.             doc.Pages.RemoveAt(1);  
  32.   
  33.             //Save the document  
  34.             doc.SaveToFile("MergePdfPages.pdf");  
  35.         }  
  36.     }  
  37. }  

 VB.NET Language

  1. Imports Spire.Pdf  
  2. Imports Spire.Pdf.Graphics  
  3. Imports System.Drawing  
  4.   
  5. Namespace MergeAndSplitPdfPages  
  6.     Class Program  
  7.         Private Shared Sub Main(ByVal args As String())  
  8.             Dim doc As PdfDocument = New PdfDocument("Sample.pdf")  
  9.             Dim page1 As PdfPageBase = doc.Pages(0)  
  10.             Dim page2 As PdfPageBase = doc.Pages(1)  
  11.             Dim template As PdfTemplate = page2.CreateTemplate()  
  12.             page2.Canvas.SetTransparency(1F, 1F, PdfBlendMode.Overlay)  
  13.             page1.Canvas.DrawTemplate(template, New PointF(0, 150))  
  14.             doc.Pages.RemoveAt(1)  
  15.             doc.SaveToFile("MergePdfPages.pdf")  
  16.         End Sub  
  17.     End Class  
  18. End Namespace  

Output:


 

 


Comments

Popular posts from this blog

3 Ways to Generate Word Documents from Templates in Java

A template is a document with pre-applied formatting like styles, tabs, line spacing and so on. You can quickly generate a batch of documents with the same structure based on the template. In this article, I am going to show you the different ways to generate Word documents from templates programmatically in Java using Free Spire.Doc for Java library. Prerequisite First of all, you need to add needed dependencies for including Free Spire.Doc for Java into your Java project. There are two ways to do that. If you use maven, you need to add the following code to your project’s pom.xml file. <repositories>               <repository>                   <id>com.e-iceblue</id>                   <name>e-iceblue</name>                   <url>http: //repo.e-iceblue.com/nexus/content/groups/public/</url>                </repository>       </repositories>       <dependencies>           <dependency>               <g

Simple Java Code to Convert Excel to PDF in Java

This article demonstrates a simple solution to convert an Excel file to PDF in Java by using free Excel API – Free Spire.XLS for Java . The following examples illustrate two possibilities to convert Excel to PDF:      Convert the whole Excel file to PDF     Convert a particular Excel Worksheet to PDF Before start with coding, you need to Download Free Spire.XLS for Java package , unzip it and import Spire.Xls.jar file from the lib folder in your project as a denpendency. 1. Convert the whole Excel file to PDF Spire.XLS for Java provides saveToFile method in Workbook class that enables us to easily save a whole Excel file to PDF. import com.spire.xls.FileFormat; import com.spire.xls.Workbook; public class ExcelToPDF {     public static void main(String[] args){         //Create a Workbook         Workbook workbook = new Workbook();         workbook.loadFromFile( "Sample.xlsx" );         //Fit to page         workbook.getConverterSetting().setShee

Insert and Extract OLE objects in Word in Java

You can use OLE (Object Linking and Embedding) to include content from other programs, such as another Word document, an Excel or PowerPoint document to an existing Word document. This article demonstrates how to insert and extract embedded OLE objects in a Word document in Java by using Free Spire.Doc for Java API.   Add dependencies First of all, you need to add needed dependencies for including Free Spire.Doc for Java into your Java project. There are two ways to do that. If you use maven, you need to add the following code to your project’s pom.xml file.     <repositories>               <repository>                   <id>com.e-iceblue</id>                   <name>e-iceblue</name>                   <url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>               </repository>       </repositories>       <dependencies>           <dependency>               <groupId>