0

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;
    }
Scott
  • 53
  • 3
  • 14
  • Please stop using unofficial examples. They are so wrong? Why would you ignore the official iText web site and give preference to examples from people who don't know the first thing about iText??? Inserting blank pages is done with `PdfStamper` (**NOT** `PdfWriter`) and using the `InsertPage()` method. – Bruno Lowagie Jan 06 '17 at 06:48
  • @Bruno - I didn't see an Offical iTextSharp Example on their site for this particular issue. Would you be so kind as to provide a link? – Scott Jan 06 '17 at 06:50
  • For an example on the official site, see [How to add blank pages to an existing PDF in java?](http://developers.itextpdf.com/question/how-add-blank-pages-existing-pdf-java) Change some lower cases in upper cases (such as change `close()` to `Close()` and `insertPage()` to `InsertPage()`) and you have your solution. (Please don't tell me that making such changes is difficult.) – Bruno Lowagie Jan 06 '17 at 06:52
  • Seriously? You changed your question saying that you need a C# example because you don't know how to change a `c` into a `C` and a `i` into an `I`? – Bruno Lowagie Jan 06 '17 at 06:53
  • The example you provided is in Java and it's only for the last page of the document. I'm using C# and I'm trying to insert a blank page after each page in the document. Please do not mark this as duplicate as it is not a duplicate question. – Scott Jan 06 '17 at 06:55
  • RE: Seriously? You changed your question saying that you need a C# example because you don't know how to change a c into a C and a i into an I? - I'm trying to insert a blank page after each page in the source PDF regardless of size and rotation. Why are you being so rude and giving me such a hard time? – Scott Jan 06 '17 at 06:56
  • Dear @Scott, you have looked at the duplicate answer for 2 minutes. This tells me that you haven't even tried implementing a solution with `PdfStamper` and `InsertPage()`. Have you no shame? – Bruno Lowagie Jan 06 '17 at 06:56
  • @Bruno - The question you point to is "I have normal PDF file, i want to insert blank pages at the end of PDF using itext LIBRARY, without disturbing the PDF contents." - That is not the same question I'm asking. – Scott Jan 06 '17 at 06:59
  • I will prove that it **is** the same question. Just give me a moment. – Bruno Lowagie Jan 06 '17 at 07:00
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/132460/discussion-between-bruno-lowagie-and-scott). – Bruno Lowagie Jan 06 '17 at 07:05

1 Answers1

3

As I explained in the comments, this question is a duplicate of How to add blank pages to an existing PDF in java?

You should use PdfStamper instead of PdfWriter (this has been explained a zillion times before in different answers on StackOverflow). Using the InsertPage() method you can add pages of any size you want:

PdfReader reader = new PdfReader(src);
PdfStamper stamper=new PdfStamper(reader, new FileStream(dest, FileMode.Create));
int total = reader.NumberOfPages + 1
for (int pageNumber = total; pageNumber > 0; pageNumber--) {
     stamper.InsertPage(pageNumber, PageSize.A4);
}
stamper.Close();
reader.Close();

Note that I add the pages in reverse order. This is elementary logic: adding a page changes the page count and it is harder to keep track of pageNumber if you go from page 1 to total. It's easier do go in the reverse direction.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165