For Bruno { This is not a duplicate of "I have normal PDF file, i want to insert blank pages at the end of PDF using itext LIBRARY, without disturbing the PDF contents."
I'm trying to add a blank page after each page in the source PDF - not just 1 blank page at the end of the source PDF document. } Using C# (NOT Java) - Does anyone know how to insert a blank page (preferably A4 - Portrait 8.5 x 11) After each page in a PDF using iTextSharp regardless of the page size and orientation of the Source PDF? Each page of the Source PDF can have a different size and orientation.
I've tried the following. It seems to make the blank page following each page the orientation and size of the Source PDF Page but the page from the Source PDF seems to be the orientation and size of the previous blank page:
private string DocumentWithBlankPagesInserted(string fileName, string userComments)
{
string outputFileName = v.tmp + @"\" + v.tmpDir + @"\" + Guid.NewGuid().ToString() + ".pdf";
Document document = new Document();
try
{
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(outputFileName, FileMode.Create));
document.Open();
PdfContentByte cb = writer.DirectContent;
PdfReader reader = new PdfReader(fileName);
for (int pageNumber = 1; pageNumber < reader.NumberOfPages + 1; pageNumber++)
{
document.SetPageSize(reader.GetPageSizeWithRotation(pageNumber));
Chunk fileRef = new Chunk();
fileRef.SetLocalDestination(fileName);
PdfImportedPage page1 = writer.GetImportedPage(reader, pageNumber);
Rectangle psize = reader.GetPageSizeWithRotation(pageNumber);
switch (psize.Rotation)
{
case 0:
cb.AddTemplate(page1, 1f, 0, 0, 1f, 0, 0);
break;
case 90:
cb.AddTemplate(page1, 0, -1f, 1f, 0, 0, psize.Height);
break;
case 180:
cb.AddTemplate(page1, -1f, 0, 0, -1f, 0, 0);
break;
case 270:
cb.AddTemplate(page1, 0, 1.0F, -1.0F, 0, psize.Width, 0);
break;
default:
break;
}
document.NewPage();
document.Add(fileRef);
document.NewPage();
}
}
catch (Exception e)
{
throw e;
}
finally
{
document.Close();
}
return outputFileName;
}