Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Tue Sep 30, 2014 4:39 pm

Hi,
What's the easiest way to remove the header from the first page of PDF using Spire.Doc. Also we want to be able to add line breaks between two lines of header.

Here is the code we are using to write the header to all the pages in the PDF.

private void SetHeader(Document document, Color fontColor, string headerText = "", string fontName = "Calibri",
float fontSize = (float)12.0, HorizontalAlignment alignment = HorizontalAlignment.Left)
{

TextRange text;
Section section = document.Sections[0];
HeaderFooter header = section.HeadersFooters.Header;

//insert text to header

Paragraph headerParagraph = header.AddParagraph();

//header text

text = headerParagraph.AppendText(headerText);
text.CharacterFormat.FontName = fontName;
text.CharacterFormat.FontSize = Convert.ToSingle(fontSize);
text.CharacterFormat.TextColor = fontColor;
headerParagraph.Format.HorizontalAlignment = alignment;

//border
headerParagraph.Format.Borders.Bottom.BorderType = (Spire.Doc.Documents.BorderStyle)Spire.Doc.Documents.BorderStyle.Hairline;
headerParagraph.Format.Borders.Bottom.Space = (float)0.03;


}

akhan2003
 
Posts: 11
Joined: Tue Sep 09, 2014 6:47 pm

Wed Oct 01, 2014 8:00 am

Dear akhan2003,

Thanks for your inquiry.

For you requirement, please refer to the code below:
Code: Select all
Document doc = new Document("Test.docx", FileFormat.Docx2010);
//Remove the header
doc.Sections[0].HeadersFooters.Header.ChildObjects.Clear();
......
//Add line breaks
Paragraph headerParagraph = header.AddParagraph();
headerParagraph.AppendBreak(BreakType.LineBreak);


Best regards,
Burning
E-iceblue Support Team
Best Regards,
Burning
E-iceblue Support Team
User avatar

burning.liu
 
Posts: 406
Joined: Mon Aug 04, 2014 6:47 am

Thu Oct 09, 2014 5:36 pm

Hi,
Unfortunately the code supplied removed header from all the sections. The only difference I can tell that in your example, you are opening an existing document while we are creating a document in memory and then creating sections and header and finally outputting as PDF.

Note: in the second code snippet below notice the code to get first section in the document.
We were expecting that it would only add header for the first section but instead it adds a header for ALL the sections. Is this a bug?
"Section section = document.Sections[0];"


Here is sample code snippet:
Code: Select all
Document doc = new Document();
Section section = doc.AddSection();
section.BreakCode = SectionBreakType.NoBreak;
Paragraph paragraph = section.AddParagraph();
paragraph.AppendHTML(<html template>);

//repeat the process of creating a section for each HTML template

 SetHeader(doc, ConvertStrToColor("CCCCCC"), _headertext);

 //Create a byte array
MemoryStream stream = new MemoryStream();
doc.SaveToStream(stream, Spire.Doc.FileFormat.PDF);
 return stream.ToArray();




Code: Select all
private void SetHeader(Document document, Color fontColor, string headerText = "", string fontName = "Calibri",
                        float fontSize = (float)12.0, HorizontalAlignment alignment = HorizontalAlignment.Left)
{

      TextRange text;
      Section section = document.Sections[0];
      HeaderFooter header = section.HeadersFooters.Header;                 
      
      //insert text to header

      Paragraph headerParagraph = header.AddParagraph();

      //header text
      
      text = headerParagraph.AppendText(headerText);
      text.CharacterFormat.FontName = fontName;
      text.CharacterFormat.FontSize = Convert.ToSingle(fontSize);
      text.CharacterFormat.TextColor = fontColor;
      
      headerParagraph.Format.HorizontalAlignment = alignment;

      //border
      headerParagraph.Format.Borders.Bottom.BorderType = (Spire.Doc.Documents.BorderStyle)Spire.Doc.Documents.BorderStyle.Hairline;
      headerParagraph.Format.Borders.Bottom.Space = (float)0.03;
      //set the Line spacing
      headerParagraph.Format.LineSpacing = 0;

      //set the Spacing Before
      headerParagraph.Format.BeforeSpacing = 0;

      //set the Spacing After
      headerParagraph.Format.AfterSpacing = 0;   

}


akhan2003
 
Posts: 11
Joined: Tue Sep 09, 2014 6:47 pm

Fri Oct 10, 2014 3:18 am

Hello,

Please use the following method to remove the header from first page.
Code: Select all
section.PageSetup.DifferentFirstPageHeaderFooter = true;//This is necessary
section.HeadersFooters.FirstPageHeader.ChildObjects.Clear();

From your code snippet, I only see one section. When you would like to set the different header for different sections, you need to set the following property, then set the different header, otherwise it will inherit the previous header.
Code: Select all
section.HeadersFooters.LinkToPrevious = false;

The following codes demonstrates the process are only for your reference, you can modify it accordingly.
Code: Select all
 Document doc = new Document();
        Section section = doc.AddSection();
        section.BreakCode = SectionBreakType.NoBreak;
        Paragraph paragraph = section.AddParagraph();
        paragraph.AppendHTML("test");
        paragraph.AppendBreak(BreakType.PageBreak);
        paragraph.AppendHTML("page2");

        SetHeader(doc, section, Color.Red, "test");
        section.PageSetup.DifferentFirstPageHeaderFooter = true;
        section.HeadersFooters.FirstPageHeader.ChildObjects.Clear();

        section = doc.AddSection();
        paragraph = section.AddParagraph();
        paragraph.AppendHTML("test1");
        paragraph.AppendBreak(BreakType.PageBreak);
        paragraph.AppendHTML("page2");
        section.HeadersFooters.LinkToPrevious = false;
        SetHeader(doc, section, Color.Red, "test");

        doc.SaveToFile("RemoveHeader.pdf", Spire.Doc.FileFormat.PDF);
        System.Diagnostics.Process.Start("RemoveHeader.pdf");

    }
    private void SetHeader(Document document, Section sec, Color fontColor, string headerText = "", string fontName = "Calibri",
                        float fontSize = (float)12.0, HorizontalAlignment alignment = HorizontalAlignment.Left)
    {

        TextRange text;
        Section section = sec;
        HeaderFooter header = section.HeadersFooters.Header;

        //insert text to header

        Paragraph headerParagraph = header.AddParagraph();

        //header text

        text = headerParagraph.AppendText(headerText);
        text.CharacterFormat.FontName = fontName;

        text.CharacterFormat.FontSize = fontSize;
        text.CharacterFormat.TextColor = fontColor;

        headerParagraph.Format.HorizontalAlignment = alignment;

        //border
        headerParagraph.Format.Borders.Bottom.BorderType = (Spire.Doc.Documents.BorderStyle)Spire.Doc.Documents.BorderStyle.Hairline;
        headerParagraph.Format.Borders.Bottom.Space = (float)0.03;
        //set the Line spacing
        headerParagraph.Format.LineSpacing = 0;

        //set the Spacing Before
        headerParagraph.Format.BeforeSpacing = 0;

        //set the Spacing After
        headerParagraph.Format.AfterSpacing = 0;

    }           

If there are question,welcome to get it back to us.
Sincerely,
Gary
E-iceblue support team
User avatar

Gary.zhang
 
Posts: 1380
Joined: Thu Apr 04, 2013 1:30 am

Fri Oct 10, 2014 3:36 pm

Thank you for the great code sample and explanation. It is working now and I'm able to remove the header from the first page. Once you mentioned that header is inherited it all made sense on why it was showing up for all the sections.

akhan2003
 
Posts: 11
Joined: Tue Sep 09, 2014 6:47 pm

Sat Oct 11, 2014 1:36 am

Hello,

Thanks for your response, if there are any questions, welcome to get it back to us.

Sincerely,
Gary
E-iceblue support team
User avatar

Gary.zhang
 
Posts: 1380
Joined: Thu Apr 04, 2013 1:30 am

Return to Spire.Doc