How to divide one pdf into multiple pdf's(based on no of pages) by using code stage in blue prism

harishpulipati

New Member
Hi all

we have a requirement to create separate Pdf for each page in the main pdf so for this we have used ItextSharp Dll and we have used below code in the code stage however BP is not showing any errors but when we are trying to execute code stage we are getting "Internal : Exception: Object reference not set to an instance of an object."

appreciate your Help.



class Program
{
static void Main(string[] args)
{
string pdfFilePath = @"C:\Users\R7A0001\Desktop\Acrobat Document12.pdf";
string outputPath = @"C:\Users\R7A0001\Desktop\";
int interval = 10;
int pageNameSuffix = 0;

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

FileInfo file = new FileInfo(pdfFilePath);
string pdfFileName = file.Name.Substring(0, file.Name.LastIndexOf(".")) + "-";

Program obj = new Program();

for (int pageNumber = 1; pageNumber <= reader.NumberOfPages; pageNumber += interval)
{
pageNameSuffix++;
string newPdfFileName = string.Format(pdfFileName + "{0}", pageNameSuffix);
obj.SplitAndSaveInterval(pdfFilePath, outputPath, pageNumber, interval, newPdfFileName);
}
}


private void SplitAndSaveInterval(string pdfFilePath, string outputPath, int startPage, int interval, string pdfFileName)
{
using (PdfReader reader = new PdfReader(pdfFilePath))
{
Document document = new Document();
PdfCopy copy = new PdfCopy(document, new FileStream(outputPath + "\\" + pdfFileName + ".pdf", FileMode.Create));
document.Open();

for (int pagenumber = startPage; pagenumber < (startPage + interval); pagenumber++)
{
if (reader.NumberOfPages >= pagenumber)
{
copy.AddPage(copy.GetImportedPage(reader, pagenumber));
}
else
{
break;
}

}

document.Close();
}
}
}
 
Top