2016-04-25 196 views
0

我想將pdf的每個頁面轉換爲單獨的pdf文件。我已經給出6的範圍來創建6個單獨的PDF文件。如何將PDF的每個頁面拆分爲C#中的多個pdf文件(使用iTextSharp)?

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!"); 
     } 
    } 
} 

但它拋出一個空引用指針異常。我無法弄清楚我造成的問題。

任何幫助,將不勝感激。 在此先感謝。

+0

那一行是拋出的異常? – petric

回答

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; 
       } 
      } 
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!"); 
     } 
相關問題