Insert page into existing PDF using itextsharp

强颜欢笑 提交于 2019-11-29 13:54:55

This code works. You need to have a different file to output the results.

private static void AppendToDocument(string sourcePdfPath1, string sourcePdfPath2, string outputPdfPath)
{
    using (var sourceDocumentStream1 = new FileStream(sourcePdfPath1, FileMode.Open))
    {
        using (var sourceDocumentStream2 = new FileStream(sourcePdfPath2, FileMode.Open))
        {
            using (var destinationDocumentStream = new FileStream(outputPdfPath, FileMode.Create))
            {
                var pdfConcat = new PdfConcatenate(destinationDocumentStream);
                var pdfReader = new PdfReader(sourceDocumentStream1);

                var pages = new List<int>();
                for (int i = 0; i < pdfReader.NumberOfPages; i++)
                {
                    pages.Add(i);
                }

                pdfReader.SelectPages(pages);
                pdfConcat.AddPages(pdfReader);

                pdfReader = new PdfReader(sourceDocumentStream2);

                pages = new List<int>();
                for (int i = 0; i < pdfReader.NumberOfPages; i++)
                {
                    pages.Add(i);
                }

                pdfReader.SelectPages(pages);
                pdfConcat.AddPages(pdfReader);

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

I've tried this code, and it works for me, but don't forget to do some validations of the number of pages and existence of the paths you use

here is the code:

private static void AppendToDocument(string sourcePdfPath, string outputPdfPath, List<int> 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();
    }

You could use something like this, where src is the IEnumerable<string> of input pdf filenames. Just make sure that your existing pdf file is one of those sources. The PdfConcatenate class is in the latest iTextSharp release.

var result = "combined.pdf";
var fs = new FileStream(result, FileMode.Create);
var conc = new PdfConcatenate(fs, true);
foreach(var s in src) {
    var r = new PdfReader(s);
    conc.AddPages(r);
}
conc.Close();

PdfCopy is intended for use with an empty Document. You should add everything you want, one page at a time.

The alternative is to use PdfStamper.InsertPage(pageNum, rectangle) and then draw a PdfImportedPage onto that new page.

Note that PdfImportedPage only includes the page contents, not the annotations or doc-level information ("document structure", doc-level javascripts, etc) that page may have originally used... unless you use one with PdfCopy.

A Stamper would probably be more efficient and use less code, but PdfCopy will import all the page-level info, not just the page's contents.

This might be important, it might not. It depends on what page you're trying to import.

Had to even out the page count with a multiple of 4:

private static void AppendToDocument(string sourcePdfPath)
{
    var tempFileLocation = Path.GetTempFileName();
    var bytes = File.ReadAllBytes(sourcePdfPath);

    using (var reader = new PdfReader(bytes))
    {
        var numberofPages = reader.NumberOfPages;
        var modPages = (numberofPages % 4);
        var pages = modPages == 0 ? 0 : 4 - modPages;

        if (pages == 0)
            return;

        using (var fileStream = new FileStream(tempFileLocation, FileMode.Create, FileAccess.Write))
        {
            using (var stamper = new PdfStamper(reader, fileStream))
            {
                var rectangle = reader.GetPageSize(1);
                for (var i = 1; i <= pages; i++)
                    stamper.InsertPage(numberofPages + i, rectangle);
            }
        }
    }

    File.Delete(sourcePdfPath);
    File.Move(tempFileLocation, sourcePdfPath);
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!