Free Support Forum - aspose.com

Extra paragraph character inserted into header


#1

We are adding a paragraph to the header of a document. When we add the paragraph to this document, an extra paragraph character is inserted as well, which makes the header taller, which pushes the document onto more pages and alters the spacing. This is Aspose.Words for .NET.

Here’s our code:

Aspose.Words.Document doc = new Document(".....");

Shape watermark = new Shape(doc, ShapeType.TextBox);
watermark.Width = 500;
watermark.Height = 30;
watermark.BehindText = false;

watermark.Fill.On = false;

watermark.Stroked = false;

Paragraph paragraph = new Paragraph(doc);

Run run = new Run(doc, "test test test");
run.Font.Size = 9;

paragraph.AppendChild(run);

// Insert paragraph into the textbox.
watermark.AppendChild(paragraph);

watermark.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;
watermark.RelativeVerticalPosition = RelativeVerticalPosition.Page;
watermark.WrapType = WrapType.None;

watermark.Left = 25;
watermark.Top = 12;

watermark.RelativeVerticalPosition = RelativeVerticalPosition.Page;
watermark.VerticalAlignment = VerticalAlignment.Bottom;
watermark.Fill.Color = Color.Gray;

// Create a new paragraph and append the watermark to this paragraph.
Paragraph watermarkPara = new Paragraph(doc);
watermarkPara.AppendChild(watermark);

// Insert the watermark into all headers of each document section.
foreach (Section sect in doc.Sections)
{
    // There could be up to three different headers in each section, since we want
    // the watermark to appear on all pages, insert into all headers.

    InsertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderPrimary);
    InsertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderFirst);
    InsertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderEven);
}


private static void InsertWatermarkIntoHeader(Paragraph watermarkPara, Section sect, HeaderFooterType headerType)
{
    HeaderFooter header = sect.HeadersFooters[headerType];

    if (header == null)
    {
        // There is no header of the specified type in the current section, create it.
        header = new HeaderFooter(sect.Document, headerType);
        sect.HeadersFooters.Add(header);
    }

    // Insert a clone of the watermark into the header.
    header.AppendChild(watermarkPara.Clone(true));
}

#2

@timwestover,

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

  • Your simplified input Word document
  • Aspose.Words 19.1 generated output DOCX document showing the undesired behavior
  • Your expected document showing the correct output. You can create expected document by using MS Word.

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.


#3

wordissue.zip (1.7 MB)


#4

@timwestover,

Thanks for sharing the documents. We are working over your query and will get back to you soon.


#5

@timwestover,

In your code, you are adding new paragraph with textbox into header and the it is not empty. This is the reason you are getting extra space in the header. You need to add the textbox into existing paragraph of header when it is not empty. Please use the following code example to get the desired output. Hope this helps you.

Aspose.Words.Document doc = new Document(MyDir + "original.docx");

Shape watermark = new Shape(doc, ShapeType.TextBox);
watermark.Width = 500;
watermark.Height = 30;
watermark.BehindText = false;

watermark.Fill.On = false;

watermark.Stroked = false;

Paragraph paragraph = new Paragraph(doc);

Run run = new Run(doc, "test test test");
run.Font.Size = 9;

paragraph.AppendChild(run);

// Insert paragraph into the textbox.
watermark.AppendChild(paragraph);

watermark.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;
watermark.RelativeVerticalPosition = RelativeVerticalPosition.Page;
watermark.WrapType = WrapType.None;

watermark.Left = 25;
watermark.Top = 12;

watermark.Fill.Color = Color.Gray;
// Insert the watermark into all headers of each document section.
foreach (Section sect in doc.Sections)
{
    InsertWatermarkIntoHeader(watermark, sect, HeaderFooterType.HeaderPrimary);
    InsertWatermarkIntoHeader(watermark, sect, HeaderFooterType.HeaderFirst);
    InsertWatermarkIntoHeader(watermark, sect, HeaderFooterType.HeaderEven);
}

doc.Save(MyDir + @"19.1.docx");

private static void InsertWatermarkIntoHeader(Shape watermark, Section sect, HeaderFooterType headerType)
{
    HeaderFooter header = sect.HeadersFooters[headerType];

    if (header == null)
    {
        // There is no header of the specified type in the current section, create it.
        header = new HeaderFooter(sect.Document, headerType);
        sect.HeadersFooters.Add(header);
    }

    Paragraph watermarkPara = (Paragraph)header.GetChild(NodeType.Paragraph, 0, true);

    if (watermarkPara == null)
    {
        watermarkPara = new Paragraph(sect.Document);
        watermarkPara.AppendChild(watermark);
        watermarkPara.ParagraphBreakFont.Size = .5;
        // Insert a clone of the watermark into the header.
        header.AppendChild(watermarkPara.Clone(true));
    }
    else
    {
        watermarkPara.AppendChild(watermark.Clone(true));
    }
            
}