This project has moved. For the latest updates, please go here.

[Bug?] Paragraph mark itself not formatted

Mar 1, 2013 at 12:20 PM
Hi,

at first, many thanks for this great library! Saved me hours!

I found a bug(?) in paragraph formatting. When you insert a paragraph with formatting, this only gets assigned to the text itself, not to the paragraph mark. The formatting for the paragraph mark will always be the default formatting.

The best way to see this is to insert two tables to an document with a paragraph in between, having no text:
using (DocX document = DocX.Create(Path.GetTempFileName() + ".docx"))
{
    Formatting formatting = new Formatting();
    formatting.Size = 1;
    document.InsertTable(1, 1);
    document.InsertParagraph("", false, formatting);
    document.InsertTable(1, 1);
    document.Save();
}
You will get a document having a default sized space between the two tables. Putting text into this paragraph makes Word showing it with the right space. But still the paragraph mark will be default sized.

Looking into the document.xml you will see the following between the two tables:
<w:p>
    <w:pPr />
    <w:r>
        <w:rPr>
            <w:sz w:val="2" />
            <w:szCs w:val="2" />
        </w:rPr>
            <w:t xml:space="preserve"> 

            </w:t>
    </w:r>
</w:p>
After editing this document in Word and setting the paragraph formatting to the right size it looks like this:
<w:p>
    <w:pPr>
        <w:rPr>
            <w:sz w:val="2" />
            <w:szCs w:val="2" />
        </w:rPr>
    </w:pPr>
    <w:r>
        <w:rPr>
            <w:sz w:val="2" />
            <w:szCs w:val="2" />
        </w:rPr>
        <w:t xml:space="preserve">

        </w:t>
    </w:r>
</w:p>
The difference here is that also that the pPr gets formatted.

I tracked this down in the code to Container.cs an this function:
public virtual Paragraph InsertParagraph(string text, bool trackChanges, Formatting formatting)
        {
            XElement newParagraph = new XElement
            (
                XName.Get("p", DocX.w.NamespaceName), new XElement(XName.Get("pPr", DocX.w.NamespaceName)), HelperFunctions.FormatInput(text, formatting.Xml)
            );

            if (trackChanges)
                newParagraph = HelperFunctions.CreateEdit(EditType.ins, DateTime.Now, newParagraph);

            Xml.Add(newParagraph);

            return Paragraphs.Last();
        }
Here the empty pPr gets inserted without any formatting.

I changed this to:
public virtual Paragraph InsertParagraph(string text, bool trackChanges, Formatting formatting)
        {
            XElement newParagraphFormatting = new XElement(
                XName.Get("pPr", DocX.w.NamespaceName),
                formatting.Xml
            );

            XElement newParagraph = new XElement
            (
                XName.Get("p", DocX.w.NamespaceName), 
                newParagraphFormatting, 
                HelperFunctions.FormatInput(text, formatting.Xml)
            );

            if (trackChanges)
                newParagraph = HelperFunctions.CreateEdit(EditType.ins, DateTime.Now, newParagraph);

            Xml.Add(newParagraph);

            return Paragraphs.Last();
        }
After that patch, also the paragraph mark gets the right formatting and running the above example will result in two tables having nearly no space between them.

I hope, I did not fix a bug thats not a bug but by design but for me this perfectly works.

Regards
sc911