pdf file split logic issue

  • Thread starter Thread starter Sudip_inn
  • Start date Start date
S

Sudip_inn

Guest
private int Split(string pdffilelocation, int userinput, int PageCountBeforeSplit)
{

int totalpage = PageCountBeforeSplit;
int pagesize = userinput;

int newpagecount = totalpage % pagesize != 0
? totalpage / pagesize + 1
: totalpage / pagesize;


string pdfFilePath = pdffilelocation;
string outputPath = TargetPdfFileLocation;
int interval = userinput; // newpagecount;
int pageNameSuffix = 0;

PdfReader reader = new PdfReader(pdffilelocation);

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


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

return pageNameSuffix;

}

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

Continue reading...
 
Back
Top