【问题标题】:How to split every pages of a pdf into multiple pdf files in C# (using iTextSharp)?如何在 C# 中将 pdf 的每一页拆分为多个 pdf 文件(使用 iTextSharp)?
【发布时间】:2016-04-25 05:55:45
【问题描述】:

我正在尝试将 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!");
        }
    }
}

但是它抛出了一个空引用指针异常。我无法弄清楚我造成的问题。

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

【问题讨论】:

  • 在哪一行抛出异常?

标签: c# winforms pdf itextsharp


【解决方案1】:

这会毫无例外地提取页面。

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;
                }
            }

【讨论】:

    【解决方案2】:

    为什么要创建相同表单的实例来调用 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!");
            }
    

    【讨论】:

      猜你喜欢
      • 2013-09-16
      • 2022-12-19
      • 2010-10-04
      • 1970-01-01
      • 2021-12-16
      • 2011-04-04
      • 1970-01-01
      • 2023-03-11
      • 2019-01-26
      相关资源
      最近更新 更多