S
Sudip_inn
Guest
My question is how to split a pdf file in specific way. i am using ITextSharp library. i have folder where many pdf files will exist. i need to iterate in pdf file collection in loop and read each file one by one and split in specific way.
1) suppose i have pdf file called abc.pdf which has 10 pages. user pass 3 to split routine. so i need to split pdf file in 3 small files. like abc-1.pdf will consist 3 pages, abc-2.pdf will consist 3 pages, abc-3.pdf will consist 4 pages.
so i need to extract 3 pages from abc.pdf file and create a new pdf file called abc-1.pdf. again i will extract 3 more pages from abc.pdf file and will create another pdf file called abc-2.pdf. again i will extract 4 instead of 3 pages and will create another pdf file called abc-3.pdf.
what will happen if user pass 4 to split routine. say user pass 4 to split routine. so i need to split pdf file like this way abc-1.pdf will consist 4 pages, abc-2.pdf will consist 6 pages.
my below routine is working but if user pass 3 then it is creating 4 pdf files instead of 3. if user pass 4 then routine is creating 3 pdf file instead of 2.
i know few modification is required in split routine but what and where to modify to achieve my goal not clear. so please help and show me what to modify in split routine to get my desire output.
looking for guidance.
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...
1) suppose i have pdf file called abc.pdf which has 10 pages. user pass 3 to split routine. so i need to split pdf file in 3 small files. like abc-1.pdf will consist 3 pages, abc-2.pdf will consist 3 pages, abc-3.pdf will consist 4 pages.
so i need to extract 3 pages from abc.pdf file and create a new pdf file called abc-1.pdf. again i will extract 3 more pages from abc.pdf file and will create another pdf file called abc-2.pdf. again i will extract 4 instead of 3 pages and will create another pdf file called abc-3.pdf.
what will happen if user pass 4 to split routine. say user pass 4 to split routine. so i need to split pdf file like this way abc-1.pdf will consist 4 pages, abc-2.pdf will consist 6 pages.
my below routine is working but if user pass 3 then it is creating 4 pdf files instead of 3. if user pass 4 then routine is creating 3 pdf file instead of 2.
i know few modification is required in split routine but what and where to modify to achieve my goal not clear. so please help and show me what to modify in split routine to get my desire output.
looking for guidance.
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...