ITextSharp从5.3.3.0升级到5.5.0.0合并文件问题 - 已在文档关闭时关闭

时间:2014-03-21 15:49:01

标签: c# itextsharp invalidoperationexception

这是我的代码:

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;
            }
        }

我升级到ItextSharp 5.5.0.0版本,但我在行代码outputDocument.CloseDocument()中收到 InvalidOperationException ,并显示异常消息&#34 ;已经关闭&#34;

当我在pdf列表中只有一个元素并且在这段代码之前移动文档时,我认为它与inputReader.Close()方法相关的原因是什么,我没有异常。

显然,同一段代码在之前的版本5.3.3.0中完美运行。

有什么想法吗? 感谢

1 个答案:

答案 0 :(得分:2)

看来5.4.4对库引入了很多更改。如果你没有scaleFactor,下面的代码应该可以工作:

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();
    }
}