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!");
}
}
}
但它拋出一個空引用指針異常。我無法弄清楚我造成的問題。
任何幫助,將不勝感激。 在此先感謝。
那一行是拋出的異常? – petric