1

I'm using iTextSharp 5.5 to construct PDF documents. The documents start with text information and end with imported JPEG images and multi-page PDF files. Some of the PDFs contain annotations, specifically 3D models.

Edit (3/21/2014): Here is a complete, simplified example that illustrates what I'm trying to accomplish, and where the error occurs in AddPdf().

using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;

namespace PdfTest
{
    class Program
    {
        private const string path = "w:\\tmp\\pdf";

        static void Main(string[] args)
        {
            using (var ms = new MemoryStream())
            {
                var document = new Document(PageSize.LETTER, 40, 40, 30, 30);
                var writer = PdfWriter.GetInstance(document, ms); // Without this, I get a zero-length file

                document.Open();

                AddText(document, "TEST");
                AddImage(document, Path.Combine(path, "import1.jpg"));
                AddPdf(document, ms, Path.Combine(path, "import2.pdf"));

                document.Close();

                File.WriteAllBytes(Path.Combine(path, "test.pdf"), ms.ToArray());
            }
        }

        private static void AddText(Document document, string text)
        {
            document.Add(new Paragraph(text, FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 12f)));
        }

        private static void AddImage(Document document, string sourcePath)
        {
            var pic = Image.GetInstance(sourcePath);
            var maxWidth = document.PageSize.Width - 72f;
            var maxHeight = document.PageSize.Height - 150f;
            if (pic.Width > maxWidth || pic.Height > maxHeight)
            {
                pic.ScaleToFit(maxWidth, maxHeight);
            }

            document.NewPage();
            document.Add(pic);
        }

        private static void AddPdf(Document document, Stream stream, string sourcePath)
        {
            var copy = new PdfCopy(document, stream);

            // Read the source PDF
            var reader = new PdfReader(sourcePath);
            var pageCount = reader.NumberOfPages;

            // Import each page
            for (var i = 0; i < pageCount; i++)
            {
                var pageNum = i + 1;

                document.SetPageSize(reader.GetPageSizeWithRotation(pageNum));
                document.NewPage(); // <--- "Document is not open" error here

                var page = copy.GetImportedPage(reader, pageNum);
                copy.AddPage(page);
            }
        }
    }
}

What is the correct way to construct a document by adding elements and imported pages?

Jared
  • 1,385
  • 11
  • 21
  • I don't have time to write a code sample but 3D models are technically Annotations and `PdfWriter.GetImportedPage()` doesn't bring these over. However, there is a subclass of `PdfWriter` called `PdfCopy` that does bring annotations over and works pretty much the same. This answer has a sample of using it. http://stackoverflow.com/a/4370739/231316 – Chris Haas Mar 20 '14 at 13:10
  • PdfCopy does import the 3D model annotation, but doesn't allow me to then add additional elements to the document (I get a "document has no pages" error). If I try to close the PdfCopy and use a PdfWriter, then the underlying stream is closed. I'm struggling to find the correct way to create a document, add elements, and add pages imported from external image/pdf files. I can do each part individually, but not all together. – Jared Mar 21 '14 at 14:52

0 Answers0