c# - 关闭空的 iTextSharp 文档时出错

标签 c# pdf merge itext

我正在成功合并 PDF 文档;现在,当我尝试实现错误处理以防未选择 PDF 文档时,它会在关闭文档时引发错误:The document has no pages
如果在“foreach”循环中没有添加 PDF 文档,我仍然需要关闭文档!?或不?如果您打开一个对象,那么它确实会在某个时候关闭。那么在没有添加页面的情况下如何正确转义呢?

        private void MergePDFs()
    {
        DataSourceSelectArguments args = new DataSourceSelectArguments();
        DataView view = (DataView)SourceCertCockpit.Select(args);

        System.Data.DataTable table = view.ToTable();
        List<PdfReader> readerList = new List<PdfReader>();

        iTextSharp.text.Document document = new iTextSharp.text.Document();
        PdfCopy copy = new PdfCopy(document, Response.OutputStream);
        document.Open();

        int index = 0;
        foreach (DataRow myRow in table.Rows)
        {
            if (ListadoCertificadosCockpit.Rows[index].Cells[14].Text == "0")
            {
                PdfReader Reader = new PdfReader(Convert.ToString(myRow[0]));
                Chapter Chapter = new Chapter(Convert.ToString(Convert.ToInt32(myRow[1])), 0);
                Chapter.NumberDepth = 0;
                iTextSharp.text.Section Section = Chapter.AddSection(Convert.ToString(myRow[10]), 0);
                Section.NumberDepth = 0;
                iTextSharp.text.Section SubSection = Section.AddSection(Convert.ToString(myRow[7]), 0);
                SubSection.NumberDepth = 0;
                document.Add(Chapter);
                readerList.Add(Reader);
                for (int i = 1; i <= Reader.NumberOfPages; i++)
                {
                    copy.AddPage(copy.GetImportedPage(Reader, i));
                }
                Reader.Close();
            }
            index++;
        }

        if (document.PageNumber == 0)
        {
            document.Close();
            return;
        }
        document.Close();
        string SalesID = SALESID.Text;
        Response.ContentType = "application/pdf";
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.AppendHeader("content-disposition", "attachment;filename=" + SalesID + ".pdf");
    }

最佳答案

在过去,当您创建文档并“忘记”添加任何内容时,iText 不会抛出异常。这导致文档只有一个空白页。这被认为是一个错误:人们不喜欢单页、空文档。因此设计决定抛出异常。

newPage() 也做了类似的事情。可以显式触发新页面(当您在代码中添加 document.newPage() 时)或隐式触发(到达页面末尾时)。在过去,这通常会导致不需要的空白页。因此决定忽略 newPage() 以防当前页面为空。

假设你有这个:

document.newPage();
document.newPage();

人们可能希望创建两个新页面。这不是真的。我们做出了忽略第二个 document.newPage() 的设计决定,因为在第一个 document.newPage() 之后没有添加任何内容。

这给我们带来了一个问题:如果我们想插入一个空白页怎么办?或者,在您的情况下:如果可以创建一个只有一个空白页的文档怎么办?

在这种情况下,我们必须告诉 iText 当前页面不应被视为空页面。您可以通过引入以下行来实现:

writer.setPageEmpty(false);

现在当前页面会被误认为它有一些内容,即使它可能是空白的。

将此行添加到您的代码中将避免The document has no pages 异常并解决您的流未关闭的问题。

看看 NewPage例如,如果您想试验 setPageEmpty() 方法。

关于c# - 关闭空的 iTextSharp 文档时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34234677/

相关文章:

c# - 使用 iText7 + C# 从 pdf 读取文本,无法识别文本

由于在两个分支中重命名导致 git merge 冲突 - 将来如何修复和避免?

c# - 正则表达式忽略模式

c# - 如何使用NLog写入多个 "data"文件

java - 使用 Java 从 XML 输出创建可读报告 (PDF)

javascript - 有没有办法检查所有手动解决的 Mercurial merge 冲突?

python - 打开元素包装并添加新元素

c# - 在 Visual C# 中循环单选按钮检查

c# - 检测对象是否为 ValueTuple

javascript - 在浏览器中创建 PDF : Custom Font