首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >itext7不自动创建新pdf页

itext7不自动创建新pdf页
EN

Stack Overflow用户
提问于 2016-11-08 08:44:10
回答 1查看 629关注 0票数 0

当我将多个表添加到一个iText7 pdf中,并且表的数量超过一个页面时,就会抛出以下异常。

例外情况:“对象引用未设置为对象的实例。”来源:“iText.Layout.Renderer.TableRenderer.Layout(LayoutContext”StackTrace:"at itext.layout layoutContext) at iText.Layout.Renderer.RootRenderer.AddChild(IRenderer呈现器( at iText.Layout.RootElement1.Add[T2](BlockElement1 element) at iText.Layout.Document.AddT at iTextSharp7_Test.Controllers.PdfController.ReplicateBug(String pdfFile) ( C:\Users\me\Documents\Visual2015\Projects\App_Test\iTextSharp7_Test\Controllers\PdfController.cs:line 443“

在提供的示例中,当添加第6个表时抛出异常。

我能够处理这个问题的唯一方法是假设异常是通过向pdf中添加更多的数据而抛出的,而不是在一个页面中添加数据,并在try catch块中添加一个新的页面,这是一个非常丑陋的解决方案,从长远来看是不好的。

示例:

代码语言:javascript
运行
复制
public void ReplicateBug(string pdfFile)
{
    iText.Kernel.Pdf.PdfWriter writer = null;
    iText.Kernel.Pdf.PdfDocument pdf = null;
    iText.Layout.Document document = null;

    try
    {
        writer = new iText.Kernel.Pdf.PdfWriter(pdfFile);
        pdf = new iText.Kernel.Pdf.PdfDocument(writer);
        document = new iText.Layout.Document(pdf, iText.Kernel.Geom.PageSize.A4);
        document.Add(new iText.Layout.Element.Paragraph("*** PRODUCTS ***"));
        iText.Kernel.Pdf.Canvas.Draw.DashedLine dashedLine = new iText.Kernel.Pdf.Canvas.Draw.DashedLine();
        document.Add(new iText.Layout.Element.LineSeparator(dashedLine));

        iText.Layout.Element.Table table = null;

        for (int i = 0; i < 10; ++i)
        {
            iText.Layout.Element.Cell cell = null;

            table = new iText.Layout.Element.Table(2);
            table.SetMarginTop(10);

            cell = new iText.Layout.Element.Cell().Add("-- PRODUCT --");
            table.AddCell(cell);
            cell = new iText.Layout.Element.Cell(3, 1).Add("image");
            cell.SetHorizontalAlignment(iText.Layout.Properties.HorizontalAlignment.CENTER);
            cell.SetVerticalAlignment(iText.Layout.Properties.VerticalAlignment.MIDDLE);
            table.AddCell(cell);
            cell = new iText.Layout.Element.Cell().Add("Product text." + Environment.NewLine + "Welcome");
            cell.SetHeight(75);
            table.AddCell(cell);
            cell = new iText.Layout.Element.Cell().Add((i + 1).ToString().PadLeft(10, '0'));
            table.AddCell(cell);

            try
            {
                document.Add(table);
                document.Add(new iText.Layout.Element.LineSeparator(dashedLine));   // Default LineWidth is 1
            }
            catch
            {
                // NOTE: After adding 5 tables to the pdf an exception is thrown of the following kind.

                // Exception: "Object reference not set to an instance of an object."
                // Source: "itext.layout"
                // StackTrace: "at iText.Layout.Renderer.TableRenderer.Layout(LayoutContext layoutContext)
                //              at iText.Layout.Renderer.RootRenderer.AddChild(IRenderer renderer)
                //              at iText.Layout.RootElement`1.Add[T2](BlockElement`1 element)
                //              at iText.Layout.Document.Add[T](BlockElement`1 element)
                //              at iTextSharp7_Test.Controllers.PdfController.ReplicateBug(String pdfFile) in C:\\Users\\me\\Documents\\Visual Studio 2015\\Projects\\App_Test\\iTextSharp7_Test\\Controllers\\PdfController.cs:line 443"
            }
        }

        document.Close();
        pdf.Close();
        writer.Close();
    }
    catch
    {
        if (document != null)
        {
            document.Close();
        }

        if (pdf != null)
        {
            pdf.Close();
        }

        if (writer != null)
        {
            writer.Close();
        }

        throw;
    }
}

提前谢谢

EN

回答 1

Stack Overflow用户

发布于 2017-02-07 20:21:00

这个问题已经在7.0.2 (和7.0.2-SNAPSHOT,在.NET版本中称为7.0.1.1 )中得到了解决。快照NuGet包可以从艺术,艺术下载。7.0.2发行版将在最近几周内在NuGet上发布。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40482435

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档