I have this method that split a pdf of many pages into many files for each page. I noticed that if a pdf contains a digital signature or a shape(rectangle for example) iTextsharp don't copy those things into the splitted file. Are there any reason about it? Am I missing something here?
private static void MultipleSplitPdf(IResult result, List<string> fileNames, string directoryforsplit, out List<string> outFileNames)
{
if (fileNames.Count == 0)
{
result.SetError("no files!");
outFileNames = null;
return;
}
else
{
try
{
outFileNames = new List<string>();
if (FileManager.DeleteDir(result, directoryforsplit) == false)
return;
DirectoryInfo dirInfo;
if (FileManager.GetDirectory(result, directoryforsplit, out dirInfo) == false)
return;
int counter = 1;
foreach (string fileName in fileNames)
{
// we create a reader for a certain document
PdfReader reader = new PdfReader(fileName);
// we retrieve the total number of pages
int n = reader.NumberOfPages;
//Console.WriteLine("There are " + n + " pages in the original file.");
int pagenumber = n;
//if (pagenumber == 1)
//{
// FileInfo a = new FileInfo(fileName);
// a.CopyTo(directoryforsplit);
//}
//else
for (int i = 1; i <= pagenumber; i++)
{
iTextSharp.text.Document document1 = new iTextSharp.text.Document(reader.GetPageSizeWithRotation(i));
string splittedFilePath = directoryforsplit + "\\Split" + counter.ToString() + ".pdf";
PdfWriter writer1 = PdfWriter.GetInstance(document1, new FileStream(splittedFilePath, FileMode.Create));
document1.Open();
PdfContentByte cb1 = writer1.DirectContent;
PdfImportedPage page;
int rotation;
document1.SetPageSize(reader.GetPageSizeWithRotation(i));
document1.NewPage();
page = writer1.GetImportedPage(reader, i);
rotation = reader.GetPageRotation(i);
if (rotation == 90 || rotation == 270)
{
cb1.AddTemplate(page, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation(i).Height);
}
else
{
cb1.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
}
counter++;
document1.Close();
}
}
// step 1: creation of a document-object
//iTextSharp.text.Document document2 = new iTextSharp.text.Document(reader.GetPageSizeWithRotation(pagenumber));
// step 2: we create a writer that listens to the document
//PdfWriter writer2 = PdfWriter.GetInstance(document2, new FileStream(args[2], FileMode.Create));
// step 3: we open the document
//document2.Open();
//PdfContentByte cb2 = writer2.DirectContent;
// step 4: we add content
//while (i < pagenumber - 1)
//{
//}
//while (i < n)
//{
// i++;
// document2.SetPageSize(reader.GetPageSizeWithRotation(i));
// document2.NewPage();
// page = writer2.GetImportedPage(reader, i);
// rotation = reader.GetPageRotation(i);
// if (rotation == 90 || rotation == 270)
// {
// cb2.AddTemplate(page, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation(i).Height);
// }
// else
// {
// cb2.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
// }
// Console.WriteLine("Processed page " + i);
//}
//// step 5: we close the document
//document2.Close();
FileInfo[] wordFiles = dirInfo.GetFiles();
foreach (var item in wordFiles.OrderBy(i => i.LastWriteTime))
outFileNames.Add(dirInfo.FullName + "\\" + item.Name);
}
catch (Exception e)
{
outFileNames = null;
result.SetError(e.Message + "\n" + e.StackTrace);
return;
}
}
}