当前位置:网站首页>C#/VB. Net merge PDF document

C#/VB. Net merge PDF document

2022-07-01 15:57:00 InfoQ

In our daily work, we often encounter many PDF The merging of documents . Most people will choose to first PDF Convert to Word file , Then copy and paste directly , Finally, it turns to PDF file . This method is not only troublesome , And sometimes it may cause typographical disorder . The method introduced to you today is through C#/VB.NET Code to achieve this function , You can merge quickly and easily PDF file , The most important thing is not to cause formatting errors and typographical confusion , Achieve better merger effect . This article will introduce you to two kinds of mergers PDF Method of documentation . Here are my thoughts and detailed steps , And attach C#/VB.NET Code for your reference , I hope it can help you .
Will be multiple PDF Merge into one PDF file
Will be different  PDF The specified pages of are merged into one  PDF file
Program environment :
In this test , Introduce... Into the program  Spire.PDF.dll  file .
 
Method  1:
take FreeSpire.PDF for .NET  Download to local , decompression , find  BIN  Under folder  Spire.PDF.dll. And then in  Visual Studio  Open in “ Solution explorer ”, Right click “ quote ”,“ Add reference ”, The local path  BIN  Under folder  dll  Add a reference to the program .
 
Method  2:
adopt  
NuGet
  install . This can be done by  2  Methods of installation :
 
  1.  Can be in  Visual Studio  Open in “ Solution explorer ”, Right click “ quote ”,“ management NuGet  package ”, And then the search “Free Spire.PDF”, Click on “ install ”. Wait for the program installation to complete .
 
  2.  Copy the following to  PM  Console installation .
 
Install-Package FreeSpire.PDF-Version 8.6.0

Will be multiple PDF Merge into one PDF file

Specific steps :
  • Get the paths of the documents to be merged and store them in a string array .
  • call PdfDocument.MergeFiles() Method to merge documents .
  • use PdfDocumentBase.Save() Method is saved as a PDF file
Complete code :
C#】
using System;
using Spire.Pdf;

namespace MergePDFs
 {
 class Program
 {
 static void Main(string[] args)
 {
 // Get the path of the document to be merged
 String[] files = new String[] {
 "Sample.pdf",
 "Sample1.pdf",
 "Samplex.pdf"
 // Merge documents
 PdfDocumentBase doc = PdfDocument.MergeFiles(files);

 // Save the results to  PDF file
 doc.Save("MergeDocuments.pdf", FileFormat.PDF);
 }
 }
}
VB.NET
Imports System
Imports Spire.Pdf

Namespace MergePDFs
 
 Class Program
 
 Private Shared Sub Main(ByVal args() As String)
 ' Get the path of the document to be merged
 Dim files() As String = New String() {" Sample.pdf", "Sample-1.pdf", "Samplex.pdf"}
 ' Merge documents
 Dim doc As PdfDocumentBase = PdfDocument.MergeFiles(files)
 ' Save the results to  PDF file
 doc.Save("output.pdf", FileFormat.PDF)
 End Sub
 End Class
End Namespace
  design sketch :

Will be different  PDF The specified pages of are merged into one  PDF file

Specific steps :
  • Get the path of the source document and store them in the string array .
  • Create a  PdfDocument  Array , And load each source document into a separate  PdfDocument  object .
  • Use PdfDocument.InsertPage()  Methods and  PdfDocument.InsertPageRange()  Method to insert the specified page of the source document into the new document .
  • use PdfDocument.SaveToFile() Method to save the new document as PDF.
Complete code :
【C#】
using System;
using Spire.Pdf;

namespace MergeSelectedPages
{
 class Program
 {
 static void Main(string[] args)
 {
 // Get the path of the document to be merged
 String[] files = new String[] {
 "Sample1.pdf",
 "Sample.pdf",
 "Sample2.pdf"};

 // Create a  PdfDocument  Array
 PdfDocument[] docs = new PdfDocument[files.Length];

 // Cycle through documents
 for (int i = 0; i < files.Length; i++)
 {
 // Load the specified document
 docs[i] = new PdfDocument(files[i]);
 }

 // Create a  PdfDocument  Object to generate a new  PDF  file
 PdfDocument doc = new PdfDocument();

 // Insert the specified pages in different documents into the new document
 doc.InsertPage(docs[0], 0);
 doc.InsertPageRange(docs[1], 0, 2);
 doc.InsertPage(docs[2], 1);

 // Save the document as PDF file
 doc.SaveToFile(&quot;output.pdf&quot;);
 }
 }
}
【VB.NET】
Imports System
Imports Spire.Pdf

Namespace MergeSelectedPages
 
 Class Program
 
 Private Shared Sub Main(ByVal args() As String)
 ' Get the path of the document to be merged
 Dim files() As String = New String() {&quot;Sample1.pdf&quot;, &quot;Sample.pdf&quot;, &quot;Sample2.pdf&quot;}
 ' Create a  PdfDocument  Array
 Dim docs() As PdfDocument = New PdfDocument((files.Length) - 1) {}
 ' Cycle through documents
 Dim i As Integer = 0
 Do While (i < files.Length)
 ' Load the specified document
 docs(i) = New PdfDocument(files(i))
 i = (i + 1)
 Loop
 
 ' Create a  PdfDocument  Object to generate a new  PDF  file
 Dim doc As PdfDocument = New PdfDocument
 ' Insert the specified pages in different documents into the new document
 doc.InsertPage(docs(0), 0)
 doc.InsertPageRange(docs(1), 0, 2)
 doc.InsertPage(docs(2), 1)
 ' Save the document as PDF file
 doc.SaveToFile(&quot;output.pdf&quot;)
 End Sub
 End Class
End Namespace
design sketch :

原网站

版权声明
本文为[InfoQ]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/182/202207011532468644.html