getting error System.NullReferenceException: 'Object reference not set to an instance of an object.' on this line
using (PdfCopy copy = new PdfCopy(docu, new FileStream(outputFilePath, FileMode.Create)))
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Apitron.PDF.Rasterizer;
using Apitron.PDF.Rasterizer.Configuration;
using iTextSharp.text;
using iTextSharp.text.pdf;
namespace SampleWinform
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.WindowState = FormWindowState.Maximized;
this.AutoSize = true;
}
private List<int> selectedPageNumbers = new List<int>();//list to store the selected page numbers.
OpenFileDialog Opd = new OpenFileDialog();
FileStream fs;
private void PdfUpload_Click(object sender, EventArgs e)
{
Opd.Filter = "(*.pdf)|*.pdf";
Opd.Title = "Select PDF File";
if (Opd.ShowDialog() == DialogResult.OK)
{
try
{
fs = new FileStream(Opd.FileName, FileMode.Open);
pdfViewer1.Document = new Apitron.PDF.Rasterizer.Document(fs);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
private void Break_Click(object sender, EventArgs e)
{
string currentPage = pdfViewer1.Document.Navigator.CurrentPage.Label;// Get the current page number
selectedPageNumbers.Add(int.Parse(currentPage)); // Add the page number to the list
UpdateTextBox();
}
private void UpdateTextBox()
{
// Update the textbox with the selected page numbers
if (txtInput.Text == "")
{
txtInput.Text = string.Join(",", selectedPageNumbers);
}
else
{
txtInput.Text = txtInput.Text + "-" + (int.Parse(pdfViewer1.Document.Navigator.CurrentPage.Label) - 1).ToString() + "," + pdfViewer1.Document.Navigator.CurrentPage.Label;
//string.Join(",", selectedPageNumbers);
}
}
private void SplitButton_Click(object sender, EventArgs e)
{
//if (int.Parse(pdfViewer1.Document.Navigator.ToString()) == 0)
//{
// MessageBox.Show("Please load a PDF file first.");
// return;
//}
string pageRangesText = txtInput.Text;
string[] range = pageRangesText.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
List<Tuple<int, int>> pageRanges = new List<Tuple<int, int>>();
foreach (string rangePair in range)
{
string outputDirectory = "C://SplitFiles";// Specify the path where you want to save the split PDFs
fs.Position = 0;
using (PdfReader reader = new PdfReader(fs))
{
int pageCount = reader.NumberOfPages;
using (iTextSharp.text.Document docu = new iTextSharp.text.Document(PageSize.A4))
{
foreach (var ranges in range)
{
string[] rangePairs = rangePair.Trim().Split(new char[] { '-' }, StringSplitOptions.RemoveEmptyEntries);
if (rangePairs.Length == 2)
{
int startPage = int.Parse(rangePairs[0]);
int endPage = int.Parse(rangePairs[1]);
if (startPage <= endPage)
{
string outputFileName = $"PdfPages{startPage}-{endPage}.pdf";
string outputFilePath = Path.Combine(outputDirectory, outputFileName);
using (PdfCopy copy = new PdfCopy(docu, new FileStream(outputFilePath, FileMode.Create)))
{
docu.Open();
for (int page = startPage; page <= endPage; page++)
{
PdfImportedPage importedPage = copy.GetImportedPage(reader, page);
copy.AddPage(importedPage);
}
}
}
}
else if (rangePairs.Length == 1)
{
int startPage = int.Parse(rangePairs[0]);
int endPage = pageCount;
if (startPage <= endPage)
{
string outputFileName = $"PdfPages{startPage}-{endPage}.pdf";
string outputFilePath = Path.Combine(outputDirectory, outputFileName);
using (PdfCopy copy = new PdfCopy(docu, new FileStream(outputFilePath, FileMode.Create)))
{
docu.Open();
for (int page = startPage; page <= endPage; page++)
{
PdfImportedPage importedPage = copy.GetImportedPage(reader, page);
copy.AddPage(importedPage);
}
}
}
}
}
MessageBox.Show($"PDF has Splitted Completely.");
}
}
}
if (pageRanges.Count == 0)
{
MessageBox.Show("Invalid page ranges format.");
return;
}
}
}
}
why there is getting error
please Help Me team