Domanda

I have the requirement to merge pdf together. I need to import a pdf at a specific page into another one.

Let me illustrate this to you.

I have two pdf, first one is 50 pages long and the second one is 4pages long. I need to import the second one at the 13th page of the first pdf.

I don't find any exemple. There are plenty exemple on how to merge pdf but nothing about merging at a specific page.

Based on this exemple it look like I need to iterate over all pages one by one and import them in a new pdf. That look a bit painfull espicially if you have big pdf and need to merge many. I would create x new pdf to merge x+1 pdf.

Is there something I don't understand or is it really the way to go?

È stato utile?

Soluzione

Borrowing from the example, this should be easy to do with a few modifications. You just need to add all the pages before the merge, then all the pages from the second document, then all the rest of the original pages.

Try something like this (not tested or robust - just a starting point maybe):

// Used the ExtractPages as a starting point.
public void MergeDocuments(string sourcePdfPath1, string sourcePdfPath2, 
    string outputPdfPath, int insertPage) {
    PdfReader reader1 = null;
    PdfReader reader2 = null;
    Document sourceDocument1 = null;
    Document sourceDocument2 = null;
    PdfCopy pdfCopyProvider = null;
    PdfImportedPage importedPage = null;

    try {
        reader1 = new PdfReader(sourcePdfPath1);
        reader2 = new PdfReader(sourcePdfPath2);

        // Note, I'm assuming pages are 0 based.  If that's not the case, change to 1.
        sourceDocument1 = new Document(reader1.GetPageSizeWithRotation(0));
        sourceDocument2 = new Document(reader2.GetPageSizeWithRotation(0));

        pdfCopyProvider = new PdfCopy(sourceDocument1, 
            new System.IO.FileStream(outputPdfPath, System.IO.FileMode.Create));

        sourceDocument1.Open();
        sourceDocument2.Open();

        int length1 = reader1.NumberOfPages;
        int length2 = reader2.NumberOfPages;
        int page1 = 0; // Also here I'm assuming pages are 0-based.

        // Having these three loops is the key.  First is pages before the merge.          
        for (;page1 < insertPage && page1 < length1; page1++) {
            importedPage = pdfCopyProvider.GetImportedPage(reader1, page1);
            pdfCopyProvider.AddPage(importedPage);
        }

        // These are the pages from the second document.
        for (int page2 = 0; page2 < length2; page2++) {
            importedPage = pdfCopyProvider.GetImportedPage(reader2, page2);
            pdfCopyProvider.AddPage(importedPage);
        }

        // Finally, add the remaining pages from the first document.
        for (;page1 < length1; page1++) {
            importedPage = pdfCopyProvider.GetImportedPage(reader1, page1);
            pdfCopyProvider.AddPage(importedPage);
        }

        sourceDocument1.Close();
        sourceDocument2.Close();
        reader1.Close();
        reader2.Close();
    } catch (Exception ex) {
        throw ex;
    }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top