C# 合并、拆分PDF文檔

來源:互聯網
上載者:User

標籤:users   分享   rom   模板   ext   關於   oss   c#語言   代碼   

在整理檔案時,將多個同類型文檔合并是實現文檔歸類的有效方法,也便於文件管理或者文檔傳輸。當然,也可以對一些比較大的檔案進行拆分來擷取自己想要的部分文檔。可以任意地對文檔進行合并、拆分無疑為我們了提供極大的便利。那麼在C#語言環境中怎麼來實現PDF文檔的和被拆分呢?下面將介紹具體的代碼操作方法。
所需工具:Free Spire.PDF for .NET、Visual Studio 2013

一、合并多個PDF文檔
using System;using Spire.Pdf;namespace MergePDF{    class Program    {        static void Main(string[] args)        {            //建立一組數組執行個體,數組元素為需要合并的多個PDF文檔的路徑            String[] files = new String[] { "test1.pdf", "test2.pdf", "test3.pdf" };            //調用方法MergeFiles()合并文檔            PdfDocumentBase doc = PdfDocument.MergeFiles(files);            //儲存文檔            doc.Save("合并.pdf", FileFormat.PDF);        }    }}


注意: 這裡合并的PDF文檔是以新的一頁來合并的文檔,目的不是將多個文檔合并為具有一定邏輯的文檔,而是出於方便文件管理以及其他動作的目的來合并。

二、拆分PDF文檔 (一)按每頁來拆分
using System;using Spire.Pdf;namespace SplitPDF1{    class Program    {        static void Main(string[] args)        {            //初始化一個PdfDocument類執行個體,並從檔案中載入需要被拆分的PDF文檔            PdfDocument doc = new PdfDocument(@"C:\Users\Administrator\Desktop\test.pdf");            //調用方法Split()方法將PDF文檔按頁面分割儲存            String pattern = "拆分{0}.pdf";            doc.Split(pattern);        }    }}

拆分結果:

拆分的文檔個數與原文檔頁數相同。

(二)按指定頁數拆分
using Spire.Pdf;using Spire.Pdf.Graphics;using System.Drawing;namespace SplitPDF2{    class Program    {        static void Main(string[] args)        {            //建立一個PdfDocument類對象,並載入一個現有的PDF文檔            PdfDocument pdf = new PdfDocument();            pdf.LoadFromFile(@"C:\Users\Administrator\Desktop\測試.pdf");            //建立1個PDF文檔            PdfDocument pdf1 = new PdfDocument();            PdfPageBase page;            //將現有的PDF文檔的第1-5頁面分割為一個文檔            for (int i = 0; i < 4; i++)            {                //向建立文檔添加與現有文檔頁面大小一致的頁面                page = pdf1.Pages.Add(pdf.Pages[i].Size, new PdfMargins(0));                //為現有文檔的頁面建立模板並將模板畫到建立文檔的頁面上                pdf.Pages[i].CreateTemplate().Draw(page, new PointF(0, 0));            }            //儲存文檔            pdf1.SaveToFile("1-5.pdf");            //建立第2個PDF文檔            PdfDocument pdf2 = new PdfDocument();            //將現有PDF文檔的第5-10頁面分割為另一個文檔            for (int i = 4; i < 9; i++)            {                //向建立文檔添加與現有文檔頁面大小一致的頁面                page = pdf2.Pages.Add(pdf.Pages[i].Size, new PdfMargins(0));                //為現有文檔的頁面建立模板並將模板畫到建立文檔的頁面上                pdf.Pages[i].CreateTemplate().Draw(page, new PointF(0, 0));            }            //儲存文檔            pdf2.SaveToFile("6-10.pdf");            //建立第3個PDF文檔            PdfDocument pdf3 = new PdfDocument();            //將現有PDF文檔的第10-15頁面分割為另一個文檔            for (int i = 9; i < 14; i++)            {                //向建立文檔添加與現有文檔頁面大小一致的頁面                page = pdf3.Pages.Add(pdf.Pages[i].Size, new PdfMargins(0));                //為現有文檔的頁面建立模板並將模板畫到建立文檔的頁面上                pdf.Pages[i].CreateTemplate().Draw(page, new PointF(0, 0));            }            //儲存文檔            pdf2.SaveToFile("11-15.pdf");        }    }}

以上內容為本篇文章關於合并、拆分PDF文檔的方法講述。如果喜歡,歡迎轉載(轉載請註明出處)
感謝瀏覽。

C# 合并、拆分PDF文檔

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.