C# 关闭空的iTextSharp文档时出错

C# 关闭空的iTextSharp文档时出错,c#,pdf,merge,itextsharp,C#,Pdf,Merge,Itextsharp,我正在成功合并PDF文档;现在,当我试图在没有选择PDF文档的情况下实现错误处理时,关闭文档时会抛出一个错误:文档没有页面 如果在“foreach”-循环中没有添加PDF文档,我仍然需要关闭该文档!?还是不?如果你打开一个对象,那么它一定会在某个点被关闭。那么,如果没有添加页面,如何正确转义呢 private void MergePDFs() { DataSourceSelectArguments args = new DataSourceSelectAr

我正在成功合并PDF文档;现在,当我试图在没有选择PDF文档的情况下实现错误处理时,关闭文档时会抛出一个错误:文档没有页面
如果在“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");
    }
private void MergePDFs()
{
DataSourceSelectArguments args=新DataSourceSelectArguments();
DataView视图=(DataView)SourceCertCaptack.Select(args);
System.Data.DataTable table=view.ToTable();
List readerList=新列表();
iTextSharp.text.Document Document=新的iTextSharp.text.Document();
PdfCopy copy=新的PdfCopy(文档、响应、输出流);
document.Open();
int指数=0;
foreach(表中的DataRow myRow.Rows)
{
if(ListadoCertificadosCockpit.Rows[index].Cells[14].Text==“0”)
{
PdfReader=newpdfReader(Convert.ToString(myRow[0]);
章节=新章节(Convert.ToString(Convert.ToInt32(myRow[1])),0;
Chapter.NumberDepth=0;
iTextSharp.text.Section节=Chapter.AddSection(Convert.ToString(myRow[10]),0;
Section.NumberDepth=0;
iTextSharp.text.Section SubSection=Section.AddSection(Convert.ToString(myRow[7]),0;
小节.NumberDepth=0;
文件.增补(第章);
readerList.Add(读卡器);

对于(int i=1;i您可以在关闭文档之前添加一个空白页,或者捕获异常并忽略它。

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

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

假设你有:

document.newPage();
document.newPage();
可能会创建两个新页面。事实并非如此。我们已经做出了忽略第二个
文档的设计决定。newPage()
,因为在第一个
文档之后没有添加任何内容。newPage()

这就引出了一个问题:如果我们想插入一个空白页,该怎么办?或者,在您的情况下,如果创建一个只包含一个空白页的文档是可以的,该怎么办

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

writer.setPageEmpty(false);
现在,当前页面将被愚弄,认为它有一些内容,即使它可能是空白的

将这一行添加到代码将避免文档无页面异常,并解决流未关闭的问题


如果您想尝试使用
setPageEmpty()
方法,请查看示例。

以防您仍然对解决方案感兴趣,或者可能是其他人。 我遇到了完全相同的问题,我通过以下方式解决了它: 声明一个布尔值,以确定是否至少添加了一个页面,然后在关闭我提到的文档之前。 如果没有页面被复制,我会使用AddPages方法在文档中添加一个新页面,并使用一个矩形作为参数

因此,代码应该如下所示(可能有一些语法错误,因为我不熟悉C#):

private void MergePDFs()
{
DataSourceSelectArguments args=新DataSourceSelectArguments();
DataView视图=(DataView)SourceCertCaptack.Select(args);
System.Data.DataTable table=view.ToTable();
List readerList=新列表();
iTextSharp.text.Document Document=新的iTextSharp.text.Document();
PdfCopy copy=新的PdfCopy(文档、响应、输出流);
document.Open();
int指数=0;
foreach(表中的DataRow myRow.Rows)
{
if(ListadoCertificadosCockpit.Rows[index].Cells[14].Text==“0”)
{
PdfReader=newpdfReader(Convert.ToString(myRow[0]);
章节=新章节(Convert.ToString(Convert.ToInt32(myRow[1])),0;
Chapter.NumberDepth=0;
iTextSharp.text.Section节=Chapter.AddSection(Convert.ToString(myRow[10]),0;
Section.NumberDepth=0;
iTextSharp.text.Section SubSection=Section.AddSection(Convert.ToString(myRow[7]),0;
小节.NumberDepth=0;
文件.增补(第章);
readerList.Add(读卡器);
bool AtLeastOnePage=false;

对于(int i=1;我可能你可以事先检查你是否有任何东西要合并,并且只在有东西要合并时才开始合并。我可能会这样做,以防我找不到更优雅的方式离开合并方法。回答很好,但不是很完整。我添加了一个更详细的解释。解释很好,但在我的例子中,我是using PDFCopy我如何告诉iText对缺少内容保持冷静。无论如何,我不需要创建PDF(请参见代码:if document.PageNumber=0)。我只会通知用户没有选择有效的PDF文档,然后退出该方法。Martin
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);
            bool AtLeastOnePage = false;
            for (int i = 1; i <= Reader.NumberOfPages; i++)
            {
                copy.AddPage(copy.GetImportedPage(Reader, i));
                 AtLeastOnePage = true;
            }
            Reader.Close();
        }
        index++;
    }

     if (AtLeastOnePage)
        {
            document.Close();
            return true;
        }
        else
        {
            Rectangle rec = new Rectangle(10, 10, 10, 10);
            copy.AddPage(rec, 1);
            document.Close();
            return false;
        }
    string SalesID = SALESID.Text;
        Response.ContentType = "application/pdf";
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.AppendHeader("content-disposition", "attachment;filename=" + SalesID + ".pdf");
}