简体   繁体   中英

append image to pages of PDF using iTextSharp

I am attempting to append images to an existing PDF using the following.

public static byte[] Append(byte[] inputPdf, params Image[] images)
{
    var ms = new MemoryStream();
    ms.Write(inputPdf, 0, inputPdf.Length);
    ms.Seek(0, SeekOrigin.Begin);

    using (Document pdf = new Document(iTextSharp.text.PageSize.A4, 10, 10, 10, 10))
    using (PdfWriter writer = PdfWriter.GetInstance(pdf, ms))
    {
        pdf.Open();
        foreach (var image in images)
        {

            var result = pdf.NewPage();

            ImageFormat format = image.PixelFormat == PixelFormat.Format1bppIndexed
                || image.PixelFormat == PixelFormat.Format4bppIndexed
                || image.PixelFormat == PixelFormat.Format8bppIndexed
                ? ImageFormat.Tiff
                : ImageFormat.Jpeg;
            var pdfImage = iTextSharp.text.Image.GetInstance(image, format);
            pdfImage.Alignment = Element.ALIGN_CENTER;
            pdfImage.ScaleToFit(pdf.PageSize.Width, pdf.PageSize.Height);
            pdf.Add(pdfImage);
        }
        pdf.Close();
    }
    ms.Flush();
    return ms.GetBuffer();
}

The result value is not used, I was debugging it. The value is always true, so the add page is working.

The resulting PDF is the same size as the original, but is not readable. I get invalid root object errors when opening it.

Any suggestions?

Thanks

Method 1 (without PdfStamper)

using (var ms = new MemoryStream())
{
        var pdf = new PdfReader(inputPdf);
        var doc = new Document(pdf.GetPageSizeWithRotation(1));
        using (var writer = PdfWriter.GetInstance(doc, ms))
        {
            doc.Open();

            for (int page = 0; page < pdf.NumberOfPages; page++)
            {
                doc.SetPageSize(pdf.GetPageSizeWithRotation(page + 1));
                doc.NewPage();
                var pg = writer.GetImportedPage(pdf, page + 1);
                int rotation = pdf.GetPageRotation(page + 1);
                if (rotation == 90 || rotation == 270)
                    writer.DirectContent.AddTemplate(
                        pg, 0, -1f, 1f, 0, 0, pdf.GetPageSizeWithRotation(page).Height);
                else
                    writer.DirectContent.AddTemplate(pg, 1f, 0, 0, 1f, 0, 0);
            }
            foreach (var image in images)
            {
                doc.NewPage();

                ImageFormat format = image.PixelFormat == PixelFormat.Format1bppIndexed
                                     || image.PixelFormat == PixelFormat.Format4bppIndexed
                                     || image.PixelFormat == PixelFormat.Format8bppIndexed
                                         ? ImageFormat.Tiff
                                         : ImageFormat.Jpeg;
                var pdfImage = iTextSharp.text.Image.GetInstance(image, format);
                pdfImage.Alignment = Element.ALIGN_CENTER;
                pdfImage.ScaleToFit(doc.PageSize.Width - 10, doc.PageSize.Height - 10);
                doc.Add(pdfImage);
            }
            doc.Close();
            writer.Close();
        }
        ms.Flush();
        return ms.GetBuffer();
}

Method 2 (using PdfStamper)

var pdfReader = new PdfReader(inputPdf);
using (var ms = new MemoryStream())
{
        using (var stamp = new PdfStamper(pdfReader, ms))
        {
            foreach (var image in images)
            {
                var size = pdfReader.GetPageSize(1);
                var page = pdfReader.NumberOfPages + 1;
                stamp.InsertPage(page, pdfReader.GetPageSize(1));
                ImageFormat format = image.PixelFormat == PixelFormat.Format1bppIndexed
                                     || image.PixelFormat == PixelFormat.Format4bppIndexed
                                     || image.PixelFormat == PixelFormat.Format8bppIndexed
                                         ? ImageFormat.Tiff
                                         : ImageFormat.Jpeg;
                var pdfImage = iTextSharp.text.Image.GetInstance(image, format);
                pdfImage.Alignment = Element.ALIGN_CENTER;
                pdfImage.SetAbsolutePosition(0, size.Height - pdfImage.Height);
                pdfImage.ScaleToFit(size.Width, size.Height);
                stamp.GetOverContent(page).AddImage(pdfImage);
            }
        }
        ms.Flush();
        return ms.GetBuffer();
}

You are making the wrong assumption that you can glue the bytes of two PDF documents together.

You have one PDF that looks like this:

%PDF-1.6
%âãÏÓ
1 0 obj <<
... PDF syntax
%%EOF

With another one that looks like this:

%PDF-1.6
%âãÏÓ
1 0 obj <<
... PDF syntax
%%EOF

Resulting in a file that looks like this:

%PDF-1.6
%âãÏÓ
1 0 obj <<
... PDF syntax
%%EOF
%PDF-1.6
%âãÏÓ
1 0 obj <<
... PDF syntax
%%EOF

You really shouldn't expect this to work! Please start by reading chapter 6 of my book and read about called PdfStamper . Then go to this question: How can I insert an image with iTextSharp in an existing PDF?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM