Issue Text : Box child can not be null

Hi,
i have generating the pdf dynamically using c#. After appending the data to document, try to save the document using document.save or memory stream. During the time i have facing the below issue.

Box child can not be null

Could you provide code you are using and input document in case you are using one so we could check this issue?

“Box child can not be null” throwing an error while saving document using aspose.pdf.
Sample code here
Aspose.Pdf.Table questiontable = new Aspose.Pdf.Table();
questiontable.ColumnWidths = “1200”;
questiontable.DefaultCellPadding = margin;
questiontable.DefaultCellTextState.FontSize = 12;
Aspose.Pdf.Row MainQuesrow = questiontable.Rows.Add();
Aspose.Pdf.Cell CellData = MainQuesrow.Cells.Add();
CellData.Paragraphs.Add(new Aspose.Pdf.HtmlFragment(“

” + strFormatStr + “
”));

                                string queshtml = string.Empty;
                                string quesResp = "  ";
                                queshtml = "<div style='max-width: 900px; display: flex; flex-wrap: wrap;padding-left: 20px;'> &nbsp;" + quesResp + "</div>";
                                if (!string.IsNullOrEmpty(qest.QuestionResp))
                                {
                                    page.Paragraphs.Add(new Aspose.Pdf.Text.TextFragment(" "));
                                    quesResp = qest.QuestionResp;
                                    queshtml = "<div style='max-width: 900px; display: flex; flex-wrap: wrap;padding-left: 20px;'>Comments : " + quesResp + "</div>";
                                    CellData.Paragraphs.Add(new Aspose.Pdf.HtmlFragment(queshtml));
                                }
                                else
                                {
                                    page.Paragraphs.Add(new Aspose.Pdf.Text.TextFragment(" "));
                                    CellData.Paragraphs.Add(new Aspose.Pdf.HtmlFragment(queshtml));
                                }

if (document.Pages.Count > 0)
{
//document.Save(docPath);
using (MemoryStream ms = new MemoryStream())
{
document.Save(ms);
System.IO.File.WriteAllBytes(docPath, ms.ToArray());
}
}

@skondur
Thank you, I’ll investigate this issue and write back shortly

@skondur
I modified code a bit but it didn’t reproduce the issue on 25.4 version with error described, could you check if there’s something missing to reproduce the error you mentioned?


static void test_box_child_is_null()
{
string outputPath = GetOutputPath("box_child_is_null_out.pdf");
var doc =  new Document();
var page = doc.Pages.Add();

Aspose.Pdf.HtmlFragment entry1 = new Aspose.Pdf.HtmlFragment(@"Some Html text");
page.Paragraphs.Add(entry1);

Aspose.Pdf.Table questiontable = new Aspose.Pdf.Table();
questiontable.ColumnWidths = "1200";
questiontable.DefaultCellPadding = new MarginInfo(100.0, 100.0, 100.0, 100.0);
questiontable.DefaultCellTextState.FontSize = 12;
Aspose.Pdf.Row MainQuesrow = questiontable.Rows.Add();
Aspose.Pdf.Cell CellData = MainQuesrow.Cells.Add();
CellData.Paragraphs.Add(new Aspose.Pdf.HtmlFragment(@"Some Html text"));

string queshtml = string.Empty;
string quesResp = "  ";
queshtml = "<div style='max-width: 900px; display: flex; flex-wrap: wrap;padding-left: 20px;'> &nbsp;" + quesResp + "</div>";
//if (!string.IsNullOrEmpty(qest.QuestionResp))
if (true)//doesn't matter true or false , the code doesn't fail in both scenario
{
    page.Paragraphs.Add(new Aspose.Pdf.Text.TextFragment(" "));
    //quesResp = qest.QuestionResp;
    queshtml = "<div style='max-width: 900px; display: flex; flex-wrap: wrap;padding-left: 20px;'>Comments : " + quesResp + "</div>";
    CellData.Paragraphs.Add(new Aspose.Pdf.HtmlFragment(queshtml));
}
else
{
    page.Paragraphs.Add(new Aspose.Pdf.Text.TextFragment(" "));
    CellData.Paragraphs.Add(new Aspose.Pdf.HtmlFragment(queshtml));
}

if (doc.Pages.Count > 0)
{
    doc.Save(outputPath);
}

}

Also, what version of Aspose Pdf you’re using ?

Thanks for your reply. I am using 25.2.0. Let me try with new version and changes.

Still issues persist.
I would like to give some more information. Below snippet code will do the inserting pdf/doc objects into the main PDF.

                                    htmlOutputResponse = "<div style='max-width: 900px; display: flex; flex-wrap: wrap;padding-left: 20px;padding-top: 15px'><div style='display: flex; align-items: center; margin-right: 10px;'><span style='color: green;font-size: 14px;'>Supplier Response:</span></div>";
                                
                                foreach (var questdocments in qest.QuestionResponseDocs)
                                {
                                    // Get file extension
                                    string fileExtension = System.IO.Path.GetExtension(questdocments.FileName);
                                    if (fileExtension.StartsWith("."))
                                    {
                                        fileExtension = fileExtension.Substring(1);
                                    }

                                    // Get icon file path based on file extension
                                    string imagePath = GetIconFileName(imagesPath, fileExtension);

                                    // Append the HTML for this document
                                    htmlOutputResponse += $@"<div style='display: flex; align-items: center; margin-right: 20px;'><div style='margin-right: 5px;'><img src='{imagePath}' alt='{fileExtension} Icon' style='width: 24px; height: 24px;'></div><div><span>{questdocments.FileName}</span></div></div>";
                                }
                                htmlOutputResponse += "</div>";

THE ABOVE object “htmlOutputResponse” WILL BE USING TO RENDER INTO THE MAIN PDF USING BELOW CODE.

Aspose.Pdf.Table MainQuestableNonlocal = new Aspose.Pdf.Table();
MainQuestableNonlocal.ColumnWidths = “900”;
MainQuestableNonlocal.DefaultCellPadding = margin;
MainQuestableNonlocal.DefaultCellTextState.FontSize = 12;
Aspose.Pdf.Row MainQuesrow = MainQuestableNonlocal.Rows.Add();
Aspose.Pdf.Cell CellData = MainQuesrow.Cells.Add();
CellData.Paragraphs.Add(new Aspose.Pdf.HtmlFragment(“

” + strFormatStr + “
”));
page.Paragraphs.Add(MainQuestableNonlocal);
if (!isQuestionDocAvailable) { page.Paragraphs.Add(new Aspose.Pdf.Text.TextFragment(" “)); }
if (!isQuestionDocAvailable) { page.Paragraphs.Add(new Aspose.Pdf.HtmlFragment(” “)); }
CellData.Paragraphs.Add(new Aspose.Pdf.HtmlFragment(htmlOutputQuesDocs));
if (!isRespQuestionDocAvailable) { page.Paragraphs.Add(new Aspose.Pdf.Text.TextFragment(” “)); }
if (!isRespQuestionDocAvailable) { page.Paragraphs.Add(new Aspose.Pdf.HtmlFragment(” ")); }
//if (!isRespQuestionDocAvailable) { CellData.Paragraphs.Add(new Aspose.Pdf.HtmlFragment(htmlOutputResponse)); }
CellData.Paragraphs.Add(new Aspose.Pdf.HtmlFragment(htmlOutputResponse));
string queshtml = string.Empty;
string quesResp = " ";
queshtml = “
 " + quesResp + “
”;
if (!string.IsNullOrEmpty(qest.QuestionResp))
{
page.Paragraphs.Add(new Aspose.Pdf.Text.TextFragment(” "));
quesResp = qest.QuestionResp;
queshtml = “
Comments : " + quesResp + “
”;
CellData.Paragraphs.Add(new Aspose.Pdf.HtmlFragment(queshtml));
}
else
{
page.Paragraphs.Add(new Aspose.Pdf.Text.TextFragment(” "));
CellData.Paragraphs.Add(new Aspose.Pdf.HtmlFragment(queshtml));
}

@skondur
Thanks for additional information, I’ll try to reproduce issue with your suggestions

@skondur
Thanks for your patience
I tried to investigate your code and adjust changes but without context and data it didn’t reproduce
Here’s rewritten variant to local files

        static void test_box_child_is_null()
        {
            var QuestionResponseDocs = System.IO.Directory.GetFiles(InputFolder + "testFiles");
            var htmlOutputResponse = "<div style='max-width: 900px; display: flex; flex-wrap: wrap;padding-left: 20px;padding-top: 15px'><div style='display: flex; align-items: center; margin-right: 10px;'><span style='color: green;font-size: 14px;'>Supplier Response:</span></div>";

            foreach (var questdocments in QuestionResponseDocs)
            {
                var filename = System.IO.Path.GetFileName(questdocments);
                // Get file extension
                string fileExtension = System.IO.Path.GetExtension(filename);
                if (fileExtension.StartsWith("."))
                {
                    fileExtension = fileExtension.Substring(1);
                }

                //as you described , issue is connected to doc & pdf documents,
                string imagePath = System.IO.Path.GetFileNameWithoutExtension(filename) + ".png";

                // Append the HTML for this document
                htmlOutputResponse += $@"<div style='display: flex; align-items: center; margin-right: 20px;'><div style='margin-right: 5px;'><img src='{imagePath}' alt='{fileExtension} Icon' style='width: 24px; height: 24px;'></div><div><span>{filename}</span></div></div>";
            }
            htmlOutputResponse += "</div>";

            string outputPath = GetOutputPath("box_child_is_null_out.pdf");
            var doc = new Document();
            var page = doc.Pages.Add();

            Aspose.Pdf.Table MainQuestableNonlocal = new Aspose.Pdf.Table();
            MainQuestableNonlocal.ColumnWidths = "900";
            //popular margin info variant
            MainQuestableNonlocal.DefaultCellPadding = new MarginInfo(0, 5, 0, 5);
            MainQuestableNonlocal.DefaultCellTextState.FontSize = 12;
            Aspose.Pdf.Row MainQuesrow = MainQuestableNonlocal.Rows.Add();
            Aspose.Pdf.Cell CellData = MainQuesrow.Cells.Add();
            var isQuestionDocAvailable = false;
            var isRespQuestionDocAvailable = false;
            var strFormatStr = string.Empty;
            CellData.Paragraphs.Add(new Aspose.Pdf.HtmlFragment("" + strFormatStr + ""));
            page.Paragraphs.Add(MainQuestableNonlocal);
            if (!isQuestionDocAvailable)
            {
                page.Paragraphs.Add(new Aspose.Pdf.Text.TextFragment(" "));
            }
            if (!isQuestionDocAvailable)
            { 
                page.Paragraphs.Add(new Aspose.Pdf.HtmlFragment(" "));
            }

            //as I understood this is where you try to insert above object
            CellData.Paragraphs.Add(new Aspose.Pdf.HtmlFragment(htmlOutputResponse));
            if (!isRespQuestionDocAvailable) 
            {
                page.Paragraphs.Add(new Aspose.Pdf.Text.TextFragment(" "));
            }
            if (!isRespQuestionDocAvailable)
            {
                page.Paragraphs.Add(new Aspose.Pdf.HtmlFragment(" "));
            }
            if (!isRespQuestionDocAvailable) { CellData.Paragraphs.Add(new Aspose.Pdf.HtmlFragment(htmlOutputResponse)); }
            CellData.Paragraphs.Add(new Aspose.Pdf.HtmlFragment(htmlOutputResponse));
            string queshtml = string.Empty;
            string quesResp = " ";
            queshtml = " " + quesResp + "                ";

            var QuestionResp = string.Empty;
            if (!string.IsNullOrEmpty(QuestionResp))
            {
                page.Paragraphs.Add(new Aspose.Pdf.Text.TextFragment(" "));
                quesResp = QuestionResp;
                queshtml = "Comments: " + quesResp + "";
                        CellData.Paragraphs.Add(new Aspose.Pdf.HtmlFragment(queshtml));
            }
            else
            {
                page.Paragraphs.Add(new Aspose.Pdf.Text.TextFragment(" "));
                CellData.Paragraphs.Add(new Aspose.Pdf.HtmlFragment(queshtml));
            }

            if (doc.Pages.Count > 0)
            {
                doc.Save(outputPath);
            }
        }

where I tried to load locally placed pdf-images
Could you try to reproduce issue with something simular and attach required files, please?