This forum contains outdated content and is available for reading only. Please contact technical support if you have any questions.

Add new page to existing pdf file

Hi! Could you help with adding new page to existing pdf doc.
When i try (outFileSpec already generated by GM pdf file)

using (PdfWriter writer = new PdfWriter(outFileSpec))
{
writer.AddPage(gc.Width, gc.Height, gc.DpiX, gc.DpiY);
using (var gr = writer.GetGraphics())
{
gr.DrawContainer(gc, 0, 0);
}
}
after writer initialization file already empy..

BTW, its no possibility to generate this file in one loop.
Thanks.


Hi,

Use PdfReader before initalizing PdfWriter in order to add the content of existing document to PdfWriter (as it creates new empty file), like in below example:

using (var reader = new PdfReader(sourceFilePath, dpi, dpi))
using (var writer = new PdfWriter(destinationFilePath))
{
    writer.AddPage(gc.Width, gc.Height, gc.DpiX, gc.DpiY);

    using (var graphics = writer.GetGraphics())
    {
        graphics.DrawContainer(gc, 0, 0);
    }
}


Actually, new file will be created this way.

Regards,
Andrew
Thanks for help!

Another question.
Is it possible to rotate the PDF page 90 degrees? (without rasterizing )

Thanks!


Please try the following code:
using (var reader = new PdfReader(sourceFilePath))
using (var container = reader.Frames[0].GetContent())
using (var writer = new PdfWriter(destinationFilePath))
{
    writer.AddPage(container.Height, container.Width);

    using (var graphics = writer.GetGraphics())
    {
        graphics.Transform.Rotate(-90);
        graphics.DrawContainer(container, 0, container.Width);
    }
}


Regards,
Andrew