Question

Here's my code:

public static byte[] MergePdf(List<byte[]> pdfs, float scaleFactor)
        {
            int n=0;
            //Removes the null pdf documents from the list
            while(n < pdfs.Count)
            {
                if (pdfs[n] != null)
                    n++;
                else
                    pdfs.RemoveAt(n);
            }

            if (pdfs.Count == 0)
                return null;

            // Create the output document
            MemoryStream outputStream = new MemoryStream();
            Document outputDocument = new Document();
            try
            {
                PdfWriter writer = PdfWriter.GetInstance(outputDocument, outputStream);
                outputDocument.Open();

                foreach (byte[] singlePdf in pdfs)
                {
                    PdfReader inputReader = null;
                    // Open the input files
                    inputReader = new PdfReader(singlePdf);
                    for (int idx = 1; idx <= inputReader.NumberOfPages; idx++)
                    {
                        Rectangle size = inputReader.GetPageSizeWithRotation(idx);
                        outputDocument.SetPageSize(size);
                        outputDocument.NewPage();

                        PdfImportedPage page = writer.GetImportedPage(inputReader, idx);
                        int rotation = inputReader.GetPageRotation(idx);
                        switch (rotation)
                        {
                            case 90:
                                throw new Exception("unsupported");
                            //writer.DirectContent.AddTemplate(page, scaleFactor, -1, 1, 0, scaleFactor, inputReader.GetPageSizeWithRotation(idx).Height * scaleFactor);
                            //break;
                            case 270:
                                throw new Exception("unsupported");
                            //writer.DirectContent.AddTemplate(page, scaleFactor, 1, -1, scaleFactor, inputReader.GetPageSizeWithRotation(idx).Width * scaleFactor, 0);
                            //break;
                            default:
                                writer.DirectContent.AddTemplate(page, scaleFactor, 0, 0, scaleFactor, (inputReader.GetPageSizeWithRotation(idx).Width - inputReader.GetPageSizeWithRotation(idx).Width * scaleFactor) / 2, (inputReader.GetPageSizeWithRotation(idx).Height - inputReader.GetPageSizeWithRotation(idx).Height * scaleFactor) / 2);
                                break;
                        }
                    }
                    inputReader.Close();
                }
                // Save the document and close objects
                writer.CloseStream = false;
                outputDocument.CloseDocument();
                outputStream.Flush();
                byte[] res = outputStream.ToArray();
                outputStream.Close();
                outputStream.Dispose();
                return res;
            }
            catch
            {
                if (outputDocument.IsOpen())
                    outputDocument.Close();
                if (outputStream != null)
                {
                    outputStream.Close();
                    outputStream.Dispose();
                }
                throw;
            }
        }

I'm upgrading to ItextSharp 5.5.0.0 version but I'm getting an InvalidOperationException in the line code outputDocument.CloseDocument() with the exception message "already closed".

I'm thinking it's something related to the inputReader.Close() method cause when I have just one element in the pdf list and I move the document close before this piece of code, I got no exception.

Obviously the same piece of code works perfectly on the previous version, the 5.3.3.0.

Any ideas? Thanks

Was it helpful?

Solution

It appears 5.4.4 introduced a lot of changes to the library. If you can do without scaleFactor, the following code should work:

public static byte[] MergePDFs(byte[][] sourceFiles) {
    using (var dest = new System.IO.MemoryStream())
    using (var document = new iTextSharp.text.Document()) 
    using (var writer = new iTextSharp.text.pdf.PdfCopy(document, dest)) {
        document.Open();
        foreach (var pdf in sourceFiles) {
            using (var r = new iTextSharp.text.pdf.PdfReader(pdf))
                writer.AddDocument(r);
        }
        document.Close();
        return dest.ToArray();
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top