Insert page into existing PDF using C#

I would like to include a page to an existing PDF file containing easy text.

I have actually attempted the following code that I discovered on the internet, however up until now I haven’t got it to work:

PdfReader reader = new PdfReader("1.pdf");
Document document = new Document(reader.GetPageSize(1));
PdfCopy copier = new PdfCopy(doc, new FileStream("2.pdf", FileMode.Create));

for (int pageCounter = 1; pageCounter < reader.NumberOfPages + 1; pageCounter++)
{
//byte[] page = reader.GetPageContent(pageCounter);
copier.AddPage(copier.GetImportedPage(reader, pageCounter));
}

copier.NewPage();
copier.Add(new Paragraph("This is added text"));

While I am using” document.NewPage();” method to produce new page, It will overlap my content with header. So the best ways to prevent overlapping with header while adding brand-new page?

You can’t use PdfCopy to include Pages. If you require to do that (PdfCopy is the finest for merging pdf), you will need to create a brand-new pdf and include it.

We are utilizing itextsharp to develop a single PDF from several PDF files. How do I create a new page into a PDF file that has numerous pages currently in the file? When I use include page it is overwriting the existing pages and only saves the 1 page that was selected.

Here is the code that I am utilizing to add the page to the existing PDF.

I have actually attempted this code, and it works for me, but remember to do some recognitions of the variety of pages and presence of the paths you use

here is the code:

private static void AppendToDocument(string sourcePdfPath, string outputPdfPath, List neededPages)
    {

        var sourceDocumentStream = new FileStream(sourcePdfPath, FileMode.Open);
        var destinationDocumentStream = new FileStream(outputPdfPath, FileMode.Create);
        var pdfConcat = new PdfConcatenate(destinationDocumentStream);

        var pdfReader = new PdfReader(sourceDocumentStream);
        pdfReader.SelectPages(neededPages);
        pdfConcat.AddPages(pdfReader);

        pdfReader.Close();
        pdfConcat.Close();
    }

PdfCopy is meant for use with an empty File. You must include whatever you want, one page at a time.

The option is to utilize PdfStamper.InsertPage(pageNum, rectangular shape) and after that draw a PdfImportedPage onto that brand-new page.

Keep in mind that PdfImportedPage only consists of the page contents, not the annotations or doc-level information (“file structure”, doc-level javascripts, etc) that page may have originally utilized … unless you use one with PdfCopy.

A Stamper would most likely be more effective and utilize less code, however PdfCopy will import all the page-level details, not simply the page’s contents.

This might be crucial, it may not. It depends upon what page you’re aiming to import.

You could use something like this, where src is the IEnumerable<string> of input pdf filenames. Simply ensure that your existing pdf file is among those sources. I’m trying to get just certain pages from a source PDF and include them to a existing PDF. The only issue is that it is not including it to the existing PDF however just over composing it.

Leave a Reply

Your email address will not be published. Required fields are marked *