文檔半島外圍網上直營>>E-iceblue中文文檔>>將多個 PDF 文件中的選定頁面合并為一個文件
                將多個 PDF 文件中的選定頁面合并為一個文件
Spire.PDF for .NET 是一款專門對 Word 文檔進行操作的 .NET 類庫。致力于在于幫助開發人員輕松快捷高效地創建、編輯、轉換和打印 Microsoft Word 文檔,而無需安裝 Microsoft Word。
行號用于在每行文本旁邊顯示 Word 自動計算的行數。當我們需要參考合同或法律文件等文檔中的特定行時,它非常有用。word中的行號功能允許我們設置起始值、編號間隔、與文本的距離以及行號的編號方式。使用 Spire.Doc,我們可以實現上述所有功能。本文將介紹如何將 HTML 轉換為 PDF。
歡迎加入spire技術交流群:767755948
使用 Spire.PDF,您不僅可以將多個 PDF 文件合并為一個文件,還可以從源文件中選擇特定頁面并將其合并為一個 PDF 文檔。以下代碼片段演示了相同的功能。
步驟1:獲取 PDF 文件路徑并存儲在字符串數組中。
string[] files = { "Sample1.pdf", "Sample2.pdf", "Sample3.pdf" };
 步驟 2:將每個 PDF 文檔加載到 PdfDocument 對象中,并將所有這些對象存儲到 PdfDocument 數組中。 
 PdfDocument[] docs = new PdfDocument[files.Length];
for (int i = 0; i < files.Length; i++)
{
    docs[i] = new PdfDocument(files[i]);
}
 步驟 3:創建 PdfDocument 類的實例。 
PdfDocument doc = new PdfDocument();步驟 4:調用 InsertPage(PdfDocument doc, int pageIndex) 方法和 InertPageRange(PdfDocument doc, int startIndex, int endIndex) 方法,將選定的頁面插入到新的 PDF 文檔中。
doc.InsertPage(docs[0], 0); doc.InsertPage(docs[1], 1); doc.InsertPageRange(docs[2], 2, 5);步驟 5:保存并啟動文件。
doc.SaveToFile("Result.pdf");
Process.Start("Result.pdf");
 效果截圖: 
 
完整代碼:
[C#]
using Spire.Pdf;
using System.Diagnostics;
namespace MergeSelectedPages
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] files = { "Sample1.pdf", "Sample2.pdf", "Sample3.pdf" };
            PdfDocument[] docs = new PdfDocument[files.Length];
            //open pdf documents
            for (int i = 0; i < files.Length; i++)
            {
                docs[i] = new PdfDocument(files[i]);
            }
            //create a new pdf document and insert selected pages
            PdfDocument doc = new PdfDocument();
            doc.InsertPage(docs[0], 0);
            doc.InsertPage(docs[1], 1);
            doc.InsertPageRange(docs[2], 2, 5);
            doc.SaveToFile("Result.pdf");
            Process.Start("Result.pdf");
        }
    }
}
[VB.NET] 
Imports Spire.Pdf
Imports System.Diagnostics
Namespace MergeSelectedPages
	Class Program
		Private Shared Sub Main(args As String())
			Dim files As String() = {"Sample1.pdf", "Sample2.pdf", "Sample3.pdf"}
Dim docs As PdfDocument() = New PdfDocument(files.Length - 1) {}
'open pdf documents
For i As Integer = 0 To files.Length - 1
	docs(i) = New PdfDocument(files(i))
Next
'create a new pdf document and insert selected pages
Dim doc As New PdfDocument()
doc.InsertPage(docs(0), 0)
doc.InsertPage(docs(1), 1)
doc.InsertPageRange(docs(2), 2, 5)
doc.SaveToFile("Result.pdf")
Process.Start("Result.pdf")
		End Sub
	End Class
End Namespace                
            
 QQ交談
QQ交談 在線咨詢
在線咨詢 
                 
                
 渝公網安備
            50010702500608號
渝公網安備
            50010702500608號
             
            
 客服熱線
客服熱線