How to close the header/footer section after adding content to this section

After moving to the header/footer section of a document is there a way to then close this section? I created a document, added content to the header and footer. I used the DocBuilder.MoveToDocumentEnd() function (I also tried MoveToDocumentStart()) to try to close this section.
If I then add content to the document, I get an error message regarding the content of the header/footer being too large. My work around solution was to add the content to the header/footer at the very end so no extra content got added.
Is there a way to formally close the header/footer section?
I’ve included the bulk of the code:

private static void CreateDocument()
{
    m_Document = new Document();
    int docVersionID = (int)m_RR.Params[PARAM_DOC_VERSION_ID];
    PlacementAnnouncementDocVersion version = PlacementAnnouncementDocVersion.GetByID(docVersionID);
    IntakeAssessmentVersion iaVersion = IntakeAssessmentVersion.GetByID(docVersionID);
    DocumentBuilder docBuilder = new DocumentBuilder(m_Document);
    PageSetup pageSetup = docBuilder.PageSetup;
    pageSetup.LeftMargin = 30;
    pageSetup.RightMargin = 30;
    // Calculate table width as total page width with left and right marins subtracted.
    double tableWidth = pageSetup.PageWidth - pageSetup.LeftMargin - pageSetup.RightMargin - 50;
    GenerateHeaderFooter(version, docBuilder, tableWidth);
    docBuilder.Writeln("");
    PrintBehavioralEmotionalNeeds(iaVersion, docBuilder, tableWidth);
    docBuilder.Writeln("");
    PrintLifeDomainFunctioning(iaVersion, docBuilder, tableWidth);
    docBuilder.Writeln("");
    PrintRiskBehaviors(iaVersion, docBuilder, tableWidth);
    docBuilder.Writeln("");
    PrintChildStrengthsNeeds(iaVersion, docBuilder, tableWidth);
    docBuilder.Writeln("");
    PrintChildStrengthsAssets(iaVersion, docBuilder, tableWidth);
    docBuilder.Writeln("");
    PrintCaregiverNeedsStrengths(iaVersion, docBuilder, tableWidth);
    docBuilder.Writeln("");
    PrintFosterCaregiverResourcesAndFit(iaVersion, docBuilder, tableWidth);
    docBuilder.Writeln("");
    // April 8: Waiting to hear from Nate, from SF County re: what content to include in this next section.
    // PrintOtherTreatmentNeeds(iaVersion, docBuilder, tableWidth);
}

private static void GenerateHeaderFooter(PlacementAnnouncementDocVersion version, DocumentBuilder docBuilder, double tableWidth)
{
    docBuilder.MoveToHeaderFooter(HeaderFooterType.FooterPrimary);
    docBuilder.StartTable();
    // Set the font properties for the footer.
    docBuilder.Font.Size = 10;
    docBuilder.Font.Name = "Arial";
    docBuilder.InsertCell();
    // Set first cell to 1/3 of the page width.
    docBuilder.CellFormat.Width = tableWidth / 3;
    // Insert page numbering text here.
    // It uses PAGE and NUMPAGES fields to autocalculate current page number and total number of pages.
    docBuilder.Write("Page ");
    docBuilder.InsertField("PAGE", "");
    docBuilder.Write(" of ");
    docBuilder.InsertField("NUMPAGES", "");
    // Align this text to the left.
    docBuilder.CurrentParagraph.ParagraphFormat.Alignment = ParagraphAlignment.Left;
    docBuilder.InsertCell();
    // Set the second cell to 2/3 of the page width.
    docBuilder.CellFormat.Width = tableWidth * 2 / 3;
    docBuilder.Write("Doc # " + version.Document.ID);
    // Align this text to the right.
    docBuilder.CurrentParagraph.ParagraphFormat.Alignment = ParagraphAlignment.Right;
    docBuilder.EndRow();
    docBuilder.EndTable();
    docBuilder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
    docBuilder.StartTable();
    // Set the font properties of the header
    docBuilder.Font.Size = 16;
    docBuilder.Font.Name = "Arial";
    docBuilder.InsertCell();
    docBuilder.CellFormat.Width = tableWidth;
    docBuilder.Write("Treatment Planning Summary");
    docBuilder.CurrentParagraph.ParagraphFormat.Alignment = ParagraphAlignment.Center;
    docBuilder.EndRow();
    docBuilder.Font.Size = 10;
    docBuilder.Font.Name = "Arial";
    docBuilder.InsertCell();
    docBuilder.CellFormat.Width = tableWidth;
    Client c = ((Enrollment)version.Target).Client;
    docBuilder.Write(c != null ? "Client: " + c.DisplayNameFL : "");
    docBuilder.CurrentParagraph.ParagraphFormat.Alignment = ParagraphAlignment.Left;
    docBuilder.EndRow();
    docBuilder.EndTable();
    docBuilder.InsertCell();
    docBuilder.EndRow();
    docBuilder.MoveToDocumentEnd();
}

private static void PrintBehavioralEmotionalNeeds(IntakeAssessmentVersion iaVersion, DocumentBuilder docBuilder, double tableWidth)
{
    docBuilder.Font.Name = "Arial";
    docBuilder.Font.Size = 12;
    docBuilder.Font.Bold = true;
    docBuilder.Font.Underline = Underline.Single;
    docBuilder.Write("Behavioral/Emotional Needs (& Trauma Symptoms)");
    docBuilder.CurrentParagraph.ParagraphFormat.Alignment = ParagraphAlignment.Center;
    docBuilder.StartTable();
    docBuilder.Font.Name = "Arial";
    docBuilder.Font.Size = 10;
    docBuilder.Font.Bold = false;
    docBuilder.Font.Underline = Underline.None;
    docBuilder.CurrentParagraph.ParagraphFormat.Alignment = ParagraphAlignment.Left;
    docBuilder.InsertCell();
    docBuilder.EndRow();
    IList list = IntakeAssessmentVersion.IA_CANS_Option_Answer.GetForVersionAndGroup(iaVersion, CANS_Group.GROUP_PRESENTATION, false);
    if (list != null)
    {
        foreach (IntakeAssessmentVersion.IA_CANS_Option_Answer optionAnswer in list)
        {
            if (optionAnswer.CANS_ItemID == CANS_Item.ITEM_PSYCHOSIS && (optionAnswer.OptionAnswer == "1" ||
                    optionAnswer.OptionAnswer == "2" || optionAnswer.OptionAnswer == "3"))
            {
                docBuilder.InsertCell();
                docBuilder.CellFormat.Width = tableWidth / 3;
                docBuilder.Write("Item: " + optionAnswer.ItemName);
                docBuilder.InsertCell();
                docBuilder.CellFormat.Width = tableWidth / 3;
                docBuilder.Write("1, 2, or 3:");
                docBuilder.InsertCell();
                docBuilder.CellFormat.Width = tableWidth / 3;
                docBuilder.Write(optionAnswer.OptionAnswer);
                docBuilder.EndRow();
            }
            else if (optionAnswer.CANS_ItemID != CANS_Item.ITEM_PSYCHOSIS && (optionAnswer.OptionAnswer == "2" ||
                    optionAnswer.OptionAnswer == "3"))
            {
                docBuilder.InsertCell();
                docBuilder.CellFormat.Width = tableWidth / 3;
                docBuilder.Write("Item: " + optionAnswer.ItemName);
                docBuilder.InsertCell();
                docBuilder.CellFormat.Width = tableWidth / 3;
                docBuilder.Write("2, or 3:");
                docBuilder.InsertCell();
                docBuilder.CellFormat.Width = tableWidth / 3;
                docBuilder.Write(optionAnswer.OptionAnswer);
                docBuilder.EndRow();
            }
        }
        docBuilder.InsertCell();
        docBuilder.EndRow();
        docBuilder.EndTable();
        docBuilder.StartTable();
        docBuilder.Font.Name = "Arial";
        docBuilder.Font.Size = 10;
        docBuilder.Font.Bold = true;
        docBuilder.Font.Underline = Underline.None;
        docBuilder.InsertCell();
        docBuilder.CellFormat.Width = tableWidth / 2;
        docBuilder.Write("Summary of Behavioral/Emotional Needs:");
        docBuilder.InsertCell();
        docBuilder.CellFormat.Width = tableWidth / 2;
        docBuilder.Write("");
        docBuilder.EndRow();
        docBuilder.EndTable();
    }
}

private static void PrintLifeDomainFunctioning(IntakeAssessmentVersion iaVersion, DocumentBuilder docBuilder, double tableWidth)
{
    docBuilder.Font.Name = "Arial";
    docBuilder.Font.Size = 12;
    docBuilder.Font.Bold = true;
    docBuilder.Font.Underline = Underline.Single;
    docBuilder.Write("Life Domain Functioning");
    docBuilder.CurrentParagraph.ParagraphFormat.Alignment = ParagraphAlignment.Center;
    docBuilder.StartTable();
    docBuilder.Font.Name = "Arial";
    docBuilder.Font.Size = 10;
    docBuilder.Font.Bold = false;
    docBuilder.Font.Underline = Underline.None;
    docBuilder.CurrentParagraph.ParagraphFormat.Alignment = ParagraphAlignment.Left;
    docBuilder.InsertCell();
    docBuilder.EndRow();
    IList list = IntakeAssessmentVersion.IA_CANS_Option_Answer.GetForVersionAndGroup(iaVersion, CANS_Group.GROUP_IMPACT_ON_FUNCTIONING, false);
    if (list != null)
    {
        foreach (IntakeAssessmentVersion.IA_CANS_Option_Answer optionAnswer in list)
        {
            if (optionAnswer.OptionAnswer == "2" ||
                optionAnswer.OptionAnswer == "3")
            {
                docBuilder.InsertCell();
                docBuilder.CellFormat.Width = tableWidth / 3;
                docBuilder.Write("Item: " + optionAnswer.ItemName);
                docBuilder.InsertCell();
                docBuilder.CellFormat.Width = tableWidth / 3;
                docBuilder.Write("2, or 3:");
                docBuilder.InsertCell();
                docBuilder.CellFormat.Width = tableWidth / 3;
                docBuilder.Write(optionAnswer.OptionAnswer);
                docBuilder.EndRow();
            }
        }
        docBuilder.InsertCell();
        docBuilder.EndRow();
        docBuilder.EndTable();
        docBuilder.StartTable();
        docBuilder.Font.Name = "Arial";
        docBuilder.Font.Size = 10;
        docBuilder.Font.Bold = true;
        docBuilder.Font.Underline = Underline.None;
        docBuilder.InsertCell();
        docBuilder.CellFormat.Width = tableWidth / 2;
        docBuilder.Write("Summary of Life Domain Functioning:");
        docBuilder.InsertCell();
        docBuilder.CellFormat.Width = tableWidth / 2;
        docBuilder.Write("");
        docBuilder.EndRow();
        docBuilder.EndTable();
    }
}

private static void PrintRiskBehaviors(IntakeAssessmentVersion iaVersion, DocumentBuilder docBuilder, double tableWidth)
{
    docBuilder.Font.Name = "Arial";
    docBuilder.Font.Size = 12;
    docBuilder.Font.Bold = true;
    docBuilder.Font.Underline = Underline.Single;
    docBuilder.Write("Risk Behaviors");
    docBuilder.CurrentParagraph.ParagraphFormat.Alignment = ParagraphAlignment.Center;
    docBuilder.StartTable();
    docBuilder.Font.Name = "Arial";
    docBuilder.Font.Size = 10;
    docBuilder.Font.Bold = false;
    docBuilder.Font.Underline = Underline.None;
    docBuilder.CurrentParagraph.ParagraphFormat.Alignment = ParagraphAlignment.Left;
    docBuilder.InsertCell();
    docBuilder.EndRow();
    IList list = IntakeAssessmentVersion.IA_CANS_Option_Answer.GetForVersionAndGroup(iaVersion, CANS_Group.GROUP_RISK_BEHAVIORS, false);
    if (list != null)
    {
        foreach (IntakeAssessmentVersion.IA_CANS_Option_Answer optionAnswer in list)
        {
            if (optionAnswer.OptionAnswer == "2" ||
                optionAnswer.OptionAnswer == "3")
            {
                docBuilder.InsertCell();
                docBuilder.CellFormat.Width = tableWidth / 3;
                docBuilder.Write("Item: " + optionAnswer.ItemName);
                docBuilder.InsertCell();
                docBuilder.CellFormat.Width = tableWidth / 3;
                docBuilder.Write("2, or 3:");
                docBuilder.InsertCell();
                docBuilder.CellFormat.Width = tableWidth / 3;
                docBuilder.Write(optionAnswer.OptionAnswer);
                docBuilder.EndRow();
            }
        }
        docBuilder.InsertCell();
        docBuilder.EndRow();
        docBuilder.EndTable();
        docBuilder.StartTable();
        docBuilder.Font.Name = "Arial";
        docBuilder.Font.Size = 10;
        docBuilder.Font.Bold = true;
        docBuilder.Font.Underline = Underline.None;
        docBuilder.InsertCell();
        docBuilder.CellFormat.Width = tableWidth / 2;
        docBuilder.Write("Summary of Risk Behaviors:");
        docBuilder.InsertCell();
        docBuilder.CellFormat.Width = tableWidth / 2;
        docBuilder.Write("");
        docBuilder.EndRow();
        docBuilder.EndTable();
    }
}

private static void PrintChildStrengthsNeeds(IntakeAssessmentVersion iaVersion, DocumentBuilder docBuilder, double tableWidth)
{
    docBuilder.Font.Name = "Arial";
    docBuilder.Font.Size = 12;
    docBuilder.Font.Bold = true;
    docBuilder.Font.Underline = Underline.Single;
    docBuilder.Write("Child Strengths -- Needs");
    docBuilder.CurrentParagraph.ParagraphFormat.Alignment = ParagraphAlignment.Center;
    docBuilder.StartTable();
    docBuilder.Font.Name = "Arial";
    docBuilder.Font.Size = 10;
    docBuilder.Font.Bold = false;
    docBuilder.Font.Underline = Underline.None;
    docBuilder.CurrentParagraph.ParagraphFormat.Alignment = ParagraphAlignment.Left;
    docBuilder.InsertCell();
    docBuilder.EndRow();
    IList list = IntakeAssessmentVersion.IA_CANS_Option_Answer.GetForVersionAndGroup(iaVersion, CANS_SubGroup.SUB_GROUP_CHILD_STRENGTHS, true);
    if (list != null)
    {
        foreach (IntakeAssessmentVersion.IA_CANS_Option_Answer optionAnswer in list)
        {
            if (optionAnswer.OptionAnswer == "2" ||
                optionAnswer.OptionAnswer == "3")
            {
                docBuilder.InsertCell();
                docBuilder.CellFormat.Width = tableWidth / 3;
                docBuilder.Write("Item: " + optionAnswer.ItemName);
                docBuilder.InsertCell();
                docBuilder.CellFormat.Width = tableWidth / 3;
                docBuilder.Write("2, or 3:");
                docBuilder.InsertCell();
                docBuilder.CellFormat.Width = tableWidth / 3;
                docBuilder.Write(optionAnswer.OptionAnswer);
                docBuilder.EndRow();
            }
        }
        docBuilder.InsertCell();
        docBuilder.EndRow();
        docBuilder.EndTable();
        docBuilder.StartTable();
        docBuilder.Font.Name = "Arial";
        docBuilder.Font.Size = 10;
        docBuilder.Font.Bold = true;
        docBuilder.Font.Underline = Underline.None;
        docBuilder.InsertCell();
        docBuilder.CellFormat.Width = tableWidth / 2;
        docBuilder.Write("Summary of Needs for Strength Development:");
        docBuilder.InsertCell();
        docBuilder.CellFormat.Width = tableWidth / 2;
        docBuilder.Write("");
        docBuilder.EndRow();
        docBuilder.EndTable();
    }
}

private static void PrintChildStrengthsAssets(IntakeAssessmentVersion iaVersion, DocumentBuilder docBuilder, double tableWidth)
{
    docBuilder.Font.Name = "Arial";
    docBuilder.Font.Size = 12;
    docBuilder.Font.Bold = true;
    docBuilder.Font.Underline = Underline.Single;
    docBuilder.Write("Child Strengths -- Assets");
    docBuilder.CurrentParagraph.ParagraphFormat.Alignment = ParagraphAlignment.Center;
    docBuilder.StartTable();
    docBuilder.Font.Name = "Arial";
    docBuilder.Font.Size = 10;
    docBuilder.Font.Bold = false;
    docBuilder.Font.Underline = Underline.None;
    docBuilder.CurrentParagraph.ParagraphFormat.Alignment = ParagraphAlignment.Left;
    docBuilder.InsertCell();
    docBuilder.EndRow();
    IList list = IntakeAssessmentVersion.IA_CANS_Option_Answer.GetForVersionAndGroup(iaVersion, CANS_SubGroup.SUB_GROUP_CHILD_STRENGTHS, true);
    if (list != null)
    {
        foreach (IntakeAssessmentVersion.IA_CANS_Option_Answer optionAnswer in list)
        {
            if (optionAnswer.OptionAnswer == "0" ||
                optionAnswer.OptionAnswer == "1")
            {
                docBuilder.InsertCell();
                docBuilder.CellFormat.Width = tableWidth / 3;
                docBuilder.Write("Item: " + optionAnswer.ItemName);
                docBuilder.InsertCell();
                docBuilder.CellFormat.Width = tableWidth / 3;
                docBuilder.Write("0, or 1:");
                docBuilder.InsertCell();
                docBuilder.CellFormat.Width = tableWidth / 3;
                docBuilder.Write(optionAnswer.OptionAnswer);
                docBuilder.EndRow();
            }
        }
        docBuilder.InsertCell();
        docBuilder.EndRow();
        docBuilder.EndTable();
    }
}

private static void PrintCaregiverNeedsStrengths(IntakeAssessmentVersion iaVersion, DocumentBuilder docBuilder, double tableWidth)
{
    docBuilder.Font.Name = "Arial";
    docBuilder.Font.Size = 12;
    docBuilder.Font.Bold = true;
    docBuilder.Font.Underline = Underline.Single;
    docBuilder.Write("Caregiver Needs/Strengths");
    docBuilder.CurrentParagraph.ParagraphFormat.Alignment = ParagraphAlignment.Center;
    docBuilder.StartTable();
    docBuilder.Font.Name = "Arial";
    docBuilder.Font.Size = 10;
    docBuilder.Font.Bold = false;
    docBuilder.Font.Underline = Underline.None;
    docBuilder.CurrentParagraph.ParagraphFormat.Alignment = ParagraphAlignment.Left;
    docBuilder.InsertCell();
    docBuilder.EndRow();
    IList list = IntakeAssessmentVersion.IA_CANS_Option_Answer.GetForVersionAndGroup(iaVersion, CANS_SubGroup.SUB_GROUP_PRIMARY_CAREGIVER, true);
    if (list != null)
    {
        foreach (IntakeAssessmentVersion.IA_CANS_Option_Answer optionAnswer in list)
        {
            if (optionAnswer.OptionAnswer == "2" ||
                optionAnswer.OptionAnswer == "3")
            {
                docBuilder.InsertCell();
                docBuilder.CellFormat.Width = tableWidth / 3;
                docBuilder.Write("Item: " + optionAnswer.ItemName);
                docBuilder.InsertCell();
                docBuilder.CellFormat.Width = tableWidth / 3;
                docBuilder.Write("2, or 3:");
                docBuilder.InsertCell();
                docBuilder.CellFormat.Width = tableWidth / 3;
                docBuilder.Write(optionAnswer.OptionAnswer);
                docBuilder.EndRow();
            }
        }
        docBuilder.InsertCell();
        docBuilder.EndRow();
        docBuilder.EndTable();
        docBuilder.StartTable();
        docBuilder.Font.Name = "Arial";
        docBuilder.Font.Size = 10;
        docBuilder.Font.Bold = true;
        docBuilder.Font.Underline = Underline.None;
        docBuilder.InsertCell();
        docBuilder.CellFormat.Width = tableWidth / 2;
        docBuilder.Write("Summary of Caregiver Needs/Strengths:");
        docBuilder.InsertCell();
        docBuilder.CellFormat.Width = tableWidth / 2;
        docBuilder.Write("");
        docBuilder.EndRow();
        docBuilder.EndTable();
    }
}

private static void PrintFosterCaregiverResourcesAndFit(IntakeAssessmentVersion iaVersion, DocumentBuilder docBuilder, double tableWidth)
{
    docBuilder.Font.Name = "Arial";
    docBuilder.Font.Size = 12;
    docBuilder.Font.Bold = true;
    docBuilder.Font.Underline = Underline.Single;
    docBuilder.Write("Foster Caregiver Resources and Fit");
    docBuilder.CurrentParagraph.ParagraphFormat.Alignment = ParagraphAlignment.Center;
    docBuilder.StartTable();
    docBuilder.Font.Name = "Arial";
    docBuilder.Font.Size = 10;
    docBuilder.Font.Bold = false;
    docBuilder.Font.Underline = Underline.None;
    docBuilder.CurrentParagraph.ParagraphFormat.Alignment = ParagraphAlignment.Left;
    docBuilder.InsertCell();
    docBuilder.EndRow();
    IList list = IntakeAssessmentVersion.IA_CANS_Option_Answer.GetForVersionAndGroup(iaVersion, CANS_SubGroup.SUB_GROUP_FOSTER_CAREGIVER, true);
    if (list != null)
    {
        foreach (IntakeAssessmentVersion.IA_CANS_Option_Answer optionAnswer in list)
        {
            if (optionAnswer.OptionAnswer == "2" ||
                optionAnswer.OptionAnswer == "3")
            {
                docBuilder.InsertCell();
                docBuilder.CellFormat.Width = tableWidth / 3;
                docBuilder.Write("Item: " + optionAnswer.ItemName);
                docBuilder.InsertCell();
                docBuilder.CellFormat.Width = tableWidth / 3;
                docBuilder.Write("2, or 3:");
                docBuilder.InsertCell();
                docBuilder.CellFormat.Width = tableWidth / 3;
                docBuilder.Write(optionAnswer.OptionAnswer);
                docBuilder.EndRow();
            }
        }
        docBuilder.InsertCell();
        docBuilder.EndRow();
        docBuilder.EndTable();
        docBuilder.StartTable();
        docBuilder.Font.Name = "Arial";
        docBuilder.Font.Size = 10;
        docBuilder.Font.Bold = true;
        docBuilder.Font.Underline = Underline.None;
        docBuilder.InsertCell();
        docBuilder.CellFormat.Width = tableWidth / 2;
        docBuilder.Write("Summary of Foster Caregiver Resources and Fit:");
        docBuilder.InsertCell();
        docBuilder.CellFormat.Width = tableWidth / 2;
        docBuilder.Write("");
        docBuilder.EndRow();
        docBuilder.EndTable();
    }
}

Hi

Thanks for your request. It seems the problem occurs because you have missed EndTable in your code. Please see the following code:

docBuilder.InsertCell();
docBuilder.EndRow();
docBuilder.EndTable();
docBuilder.MoveToDocumentEnd();

I highlighted line of code that I added.
Hoe this helps.
Best regards.