Data is breaking while adding the header

I am adding the header and footer. If I add the footer it is working fine but while I am adding the header half of the data was breaking. Below is the code.

            builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
            builder.Writeln("Header");

            Header("What is Software");

            builder.Font.Color = System.Drawing.Color.FromArgb(0, 132, 125);
            builder.Bold = true;
            builder.Font.Size = 11;
            builder.Font.Name = "Calibri";
            ParagraphFormat paragraphFormat = builder.ParagraphFormat;

            paragraphFormat.TabStops.Add(350, TabAlignment.Left, TabLeader.None);


            builder.Writeln("1, Computer software, or simply software, is a collection of data or computer instructions that tell the computer how to work. This is in contrast to physical hardware, from which the system is built and actually performs the work.");
            builder.Writeln("2, Computer software, or simply software, is a collection of data or computer instructions that tell the computer how to work. This is in contrast to physical hardware, from which the system is built and actually performs the work.");
            builder.Writeln("3, Computer software, or simply software, is a collection of data or computer instructions that tell the computer how to work. This is in contrast to physical hardware, from which the system is built and actually performs the work.");
            builder.Writeln("4, Computer software, or simply software, is a collection of data or computer instructions that tell the computer how to work. This is in contrast to physical hardware, from which the system is built and actually performs the work.");
            builder.Writeln("5, Computer software, or simply software, is a collection of data or computer instructions that tell the computer how to work. This is in contrast to physical hardware, from which the system is built and actually performs the work.");
            builder.Writeln("6, Computer software, or simply software, is a collection of data or computer instructions that tell the computer how to work. This is in contrast to physical hardware, from which the system is built and actually performs the work.");
            builder.Writeln("7, Computer software, or simply software, is a collection of data or computer instructions that tell the computer how to work. This is in contrast to physical hardware, from which the system is built and actually performs the work.");
            builder.Writeln("8, Computer software, or simply software, is a collection of data or computer instructions that tell the computer how to work. This is in contrast to physical hardware, from which the system is built and actually performs the work.");
            builder.Writeln("9, Computer software, or simply software, is a collection of data or computer instructions that tell the computer how to work. This is in contrast to physical hardware, from which the system is built and actually performs the work.");
            builder.Writeln("10, Computer software, or simply software, is a collection of data or computer instructions that tell the computer how to work. This is in contrast to physical hardware, from which the system is built and actually performs the work.");
            builder.Writeln("11, Computer software, or simply software, is a collection of data or computer instructions that tell the computer how to work. This is in contrast to physical hardware, from which the system is built and actually performs the work.");

            builder.InsertParagraph();
            builder.Font.Color = System.Drawing.Color.Black;
            paragraphFormat = builder.ParagraphFormat;

            paragraphFormat.ClearFormatting();

            paragraphFormat.TabStops.Add(150, TabAlignment.Left, TabLeader.None);

            builder.Font.Color = System.Drawing.Color.FromArgb(0, 132, 125);
            builder.Bold = true;
            builder.Writeln("12, Computer software, or simply software, is a collection of data or computer instructions that tell the computer how to work. This is in contrast to physical hardware, from which the system is built and actually performs the work.");
            builder.Bold = false;
            builder.Font.Color = System.Drawing.Color.Black;
            builder.Writeln("  " + dy + mn + yy + mm + ss);
            builder.Font.Color = System.Drawing.Color.FromArgb(0, 132, 125);
            builder.Bold = true;
            builder.Writeln("13, Computer software, or simply software, is a collection of data or computer instructions that tell the computer how to work. This is in contrast to physical hardware, from which the system is built and actually performs the work.");
            builder.Bold = false;
            builder.Font.Color = System.Drawing.Color.Black;
            builder.Writeln(sDate);

            builder.Write(ControlChar.LineBreak);
            builder.Writeln("14, Computer software, or simply software, is a collection of data or computer instructions that tell the computer how to work. This is in contrast to physical hardware, from which the system is built and actually performs the work.");
            builder.Font.Color = System.Drawing.Color.FromArgb(0, 132, 125);
            paragraphFormat.ClearFormatting();

            builder.StartBookmark("MyBookmark");
            builder.Writeln("15, Computer software, or simply software, is a collection of data or computer instructions that tell the computer how to work. This is in contrast to physical hardware, from which the system is built and actually performs the work.");
            builder.Writeln("16, Computer software, or simply software, is a collection of data or computer instructions that tell the computer how to work. This is in contrast to physical hardware, from which the system is built and actually performs the work.");
            builder.EndBookmark("MyBookmark");


            builder.MoveToHeaderFooter(HeaderFooterType.FooterPrimary);
            builder.Font.Color = System.Drawing.Color.Gray;
            builder.ParagraphFormat.Alignment = ParagraphAlignment.Left;
            builder.Font.Size = 7;
            builder.Font.Bold = true;
            builder.Write("Footer ");

            TabStop tab = new TabStop(6.27 * 72, TabAlignment.Right, TabLeader.None);
            builder.ParagraphFormat.TabStops.Add(tab);

            builder.Write(ControlChar.Tab + "Page: ");

            builder.InsertField(FieldType.FieldPage, false);

            builder.Write(" of ");
            builder.InsertField(FieldType.FieldNumPages, false);

Note:- Remove first 2 line . it’s working fine. I don’t know what is the issue. please help on this.

@dinesh0390

To ensure a timely and accurate response, please attach the following resources here for testing:

  • Your input Word document.
  • Please attach the output Word file that shows the undesired behavior.
  • Please attach the expected output Word file that shows the desired behavior.
  • Please create a standalone console application ( source code without compilation errors ) that helps us to reproduce your problem on our end and attach it here for testing.

As soon as you get these pieces of information ready, we will start investigation into your issue and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip and upload them.

@dinesh0390

The code example shared in your first post is not complete. Please share the simplified source code without compilation errors to reproduce your issue at our end. Thanks for your cooperation.

samplecode.zip (590.9 KB)

I have attached the sample code . Where I have commented the 2 lines of code . execute the code with and without comment you will get to know the what is expected output.

Based on the data, multiple pages will create . I need header and footer common every pages.

Note :- If still you get the error. please change the save path according to your system folder.

@dinesh0390

Please use DocumentBuilder.MoveToSection method as shown below to get the desired output. This method moves the cursor to the beginning of the body in a specified section.

builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
builder.Writeln("Header");

builder.MoveToSection(0);
builder.Write(ControlChar.LineBreak);

Thank you. It’s is working as expected. :slightly_smiling_face:

@dinesh0390

It is nice to hear from you that your problem has been solved. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.