0

I'm traying to test this method that checks for specific tag footer to show.

here is the function:

    public void CheckForOrderOfFooterTags(PdfDocument pdfDoc, out TagTreePointer autoP, out TagTreePointer posBackup, out TagTreeInfo p)
    {
        //  the following logic takes care of changing the order of the footer tags
        //  so that footers appear in the proper order in the tag tree structure
        autoP =pdfDoc.GetTagStructureContext().GetAutoTaggingPointer();
        posBackup =new TagTreePointer(autoP);
        PagesTag.TryGetValue(PageNumber, out p);
        if (p?.TagTreePointer != null)
        {
            //  Make sure that content that will be tagged is placed in tag structure specifically where we need it.
            int indexInParentKidsList = p.TagTreePointer.GetIndexInParentKidsList();
            autoP.MoveToPointer(p.TagTreePointer).MoveToParent();
            //  setting new index only works on taggable items
            if (p.Tagged)
            {
                autoP.SetNextNewKidIndex(indexInParentKidsList + 1);
            }
        }
    }

and this is the test:

    public void Check_For_Specific_Tag_Footer_To_Show_Successfully_Called()
    {
        var ir = new TestIllustrationReport().Report;
        var reportProperties = new TestDocument().ReportProperties;
        var sec = new FooterSection(new ProductContent(ir));
        sec.Build();
        Dictionary<int, TagTreeInfo> dict = new Dictionary<int, TagTreeInfo>();
        reportProperties.FooterTag=dict;
        FooterEventHandler footerEvent = new FooterEventHandler(sec, reportProperties);

        // Must have write permissions to the path folder
        var path = System.IO.Path.GetTempPath();
        var fileName = System.IO.Path.ChangeExtension("Test", ".pdf");
        var com = System.IO.Path.Combine(path, fileName);

        // pdf
        PdfWriter writer = new PdfWriter(com);
        PdfDocument pdf = new PdfDocument(writer);
        Event headerEvent= new PdfDocumentEvent("pdf", pdf);
        PdfDocumentEvent docEvent = (PdfDocumentEvent)headerEvent;
        iText.Layout.Document document = new iText.Layout.Document(pdf);
        pdf.GetCatalog().SetLang(new PdfString("en-US"));

        // Header and Paragraph
        Paragraph header = new Paragraph(TestData.Header)
     .SetTextAlignment(TextAlignment.CENTER)
     .SetFontSize(20);

        document.Add(header);

        pdf.SetTagged();
        pdf.GetCatalog().SetViewerPreferences(new PdfViewerPreferences().SetDisplayDocTitle(true));

        PdfDocumentInfo info = pdf.GetDocumentInfo();
        info.SetTitle("Testing tags");

        Paragraph p = new Paragraph();
        p.Add("The quick brown ");
        PdfPage page = docEvent.GetPage();

        // Rectangle and canvas
        Rectangle rectangle = new Rectangle(
    pdf.GetDefaultPageSize().GetX() + document.GetLeftMargin(),
    pdf.GetDefaultPageSize().GetTop() - 80,
    page.GetPageSize().GetWidth() - document.GetLeftMargin() - document.GetRightMargin(),
    50);
        Div canvas = new Div().SetFixedPosition(pdf.GetPageNumber(page), rectangle.GetLeft(), rectangle.GetBottom(), rectangle.GetWidth());
        Paragraph pFooter = new Paragraph(TestData.Paragraph);
        pFooter.GetAccessibilityProperties().SetRole("H");
        canvas.Add(pFooter);
        document.Add(canvas);

        // Don't close document itself! It would close the PdfDocument!
        document.GetRenderer().Close();

        var pPointer = new TagTreePointer(pdf);
        var pInfo = new TagTreeInfo { Tagged = true };
        // Act 
        footerEvent.CheckForOrderOfFooterTags(pdf, out pPointer, out pPointer, out pInfo);
        document.Close();

        // Assert 
        Assert.NotNull(pdf);

    }

For this test, I needed to create the pdf, set and build the footer section. I added the header to the pdf and the "footer test", I also set the document to be tagged.

I don't know why I always get the page is null if I'm adding a header and a paragraph to it.

  • Where is page null? `PdfPage page = docEvent.GetPage();` here GetPage() returns null? I don't know this library: why are you fetching the page from the header event and not the PdfDocument? – Rup Nov 04 '22 at 13:04
  • 1
    Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Klaus Gütter Nov 04 '22 at 14:00

1 Answers1

1

solution for itext7 itext pdf document System.NullReferenceException: 'Object reference not set to an instance of an object.'

Finally, I found the solution from Westley Bennett:

Apparently iText has some internal errors/exceptions that they are just sort of "skipping" and "pushing past", because I realized by accident that I had "Enable Just My Code" in Visual Studios disabled, and so my system was trying to debug iText7's code as well as mine. The moment that I re-enabled it in my Visual Studio settings (Tools > Options > Debugging > General > Enable Just My Code checkbox), the problem magically went away.

Settings in Visual Studio

So I spent four hours trying to troubleshoot a problem that was in THEIR code, but they apparently found some way to work around and push through the method anyways even on a null reference failure.

My convert to PDF function is now working just fine.

Djordje Nedovic
  • 559
  • 8
  • 20
volk-UA
  • 11
  • 1