如何将pdf的每个页面拆分成C#中的多个pdf文件(使用iTextSharp)?

时间:2016-04-25 05:55:46

标签: c# winforms pdf itextsharp

我正在尝试将pdf的每个页面转换为单独的pdf文件。我给出了6个范围来创建6个单独的pdf文件。

using System;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.Windows.Forms;
using System.IO;

namespace Learning.SpitPdfApp {
    public partial class MainForm : Form {
        public MainForm() {
            InitializeComponent();
        }

        private void SplitPdfButton_Click(object sender, EventArgs e) {
            MainForm objMainForm = new MainForm();
            objMainForm.ExtractPage(SourceTextBox.Text, DestinationTextBox.Text);
        }

        public void ExtractPage(string sourcePath, string outputPath) {
            int startPage = 1;
            PdfReader objReader = new PdfReader(sourcePath+".pdf");
            int endPage = 6;
            Document objDocument = new Document(objReader.GetPageSizeWithRotation(startPage));

            objDocument.Open();

            for (int index = startPage; index <= endPage; index++) {
                PdfCopy pdfCopyProvider = new PdfCopy(objDocument, new FileStream(outputPath+""+index+".pdf", FileMode.Create));
                PdfImportedPage importedPage = pdfCopyProvider.GetImportedPage(objReader, index);
                pdfCopyProvider.AddPage(importedPage);
            }
            objDocument.Close();
            objReader.Close();
            MessageBox.Show(@"Splitting successful!");
        }
    }
}

但它抛出 null引用指针异常。我无法弄清楚我所造成的问题。

任何帮助将不胜感激。 提前谢谢。

2 个答案:

答案 0 :(得分:0)

这提取页面没有任何例外。

public void ExtractPage(string sourcePdfPath, string outputPdfPath, int pageNumber)
            {
                PdfReader reader = null;
                Document document = null;
                PdfCopy pdfCopyProvider = null;
                PdfImportedPage importedPage = null;

                try
                {
                    // Intialize a new PdfReader instance with the contents of the source Pdf file:
                    reader = new PdfReader(sourcePdfPath);

                    // Capture the correct size and orientation for the page:
                    document = new Document(reader.GetPageSizeWithRotation(pageNumber));

                    // Initialize an instance of the PdfCopyClass with the source
                    // document and an output file stream:
                    pdfCopyProvider = new PdfCopy(document,
                        new System.IO.FileStream(outputPdfPath, System.IO.FileMode.Create));

                    document.Open();

                    // Extract the desired page number:
                    importedPage = pdfCopyProvider.GetImportedPage(reader, pageNumber);
                    pdfCopyProvider.AddPage(importedPage);
                    document.Close();
                    reader.Close();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }

答案 1 :(得分:0)

为什么要创建相同表单的实例来调用ExtractPage方法?何时可以在不创建新实例的情况下调用它?

以下是对您的代码的修复:

private void SplitPdfButton_Click(object sender, EventArgs e)
        {
            try
            {
                ExtractPage(SourceTextBox.Text, DestinationTextBox.Text);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        public void ExtractPage(string sourcePath, string outputPath)
        {
            int startPage = 1;
            int endPage = 6;

                for (int index = startPage; index <= endPage; index++)
                {

                    PdfReader objReader = new PdfReader(sourcePath + ".pdf");
                    Document objDocument = new Document(objReader.GetPageSizeWithRotation(startPage));

                    string destination = Path.Combine(outputPath, index + ".pdf");
                    PdfCopy pdfCopyProvider = new PdfCopy(objDocument, new FileStream(destination, FileMode.Create));
                    objDocument.Open();

                    PdfImportedPage importedPage = pdfCopyProvider.GetImportedPage(objReader, index);
                    pdfCopyProvider.AddPage(importedPage);
                    objDocument.Close();
                    objReader.Close();
                }

            MessageBox.Show(@"Splitting successful!");
        }