Free Support Forum - aspose.com

Center headings from imported html

Hi,

I used the inserthtml function to import a mass of text.
After this, i formatted my styles with this code, everything works, except the heading 1 & 2 are still not centered:

Does somebody know why ?

foreach (Style s in builder.Document.Styles)
{
try
{
s.Font.Name = “Helvetica”;
s.Font.Color = Color.Black;
s.Font.Bold = false;
s.Font.Size = 9;
s.ParagraphFormat.LineSpacing = 11;
if (s.Name == “Heading 1”)
{
s.Font.Bold = true;
s.Font.Size = 12;
s.ParagraphFormat.Alignment = ParagraphAlignment.Center;
}
else if (s.Name == “Heading 2”)
{
s.Font.Bold = true;
s.Font.Size = 10;
s.ParagraphFormat.Alignment = ParagraphAlignment.Center;
}
else if (s.Name == “Heading 3” || s.Name == “Heading 4” || s.Name == “Heading 5” || s.Name == “Heading 6”)
{
s.Font.Bold = true;
s.Font.Size = 9;
}
}
catch (Exception ex)
{
//HttpContext.Current.Response.Write(ex.Message + “
”);
}
}

Hi

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your inquiry. Could you please attach your html here for testing? I will check the problem on my side and provide you more information.

Best regards,

You can see the html on this page: http://vademecum3.vandenbroele.be/entity.aspx?id=102
(It’s just the content, the H1 is the word “Omzendbrief”)

I attached also the sourcode, because i do a little more than just inserting html. (see method “DoExport”)

Many thanks!





Hi

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your request. The problem occurs because formatting in MS Word can be specified on few levels. For example formatting of paragraphs can be specified via Paragraph Styles or as properties of paragraph itself (inline formatting). If, for example, in style you specify align center, but in paragraph properties you specify align right, the paragraph will be aligned right.

The solution is quite simple. You can just reset inline formatting of paragraphs. For example see the following code:

// Open your HTML document (I saved it to file).

Document doc = new Document(@"Test001\test.html");

// Change Heading 1 style.

doc.Styles[StyleIdentifier.Heading1].ParagraphFormat.Alignment = ParagraphAlignment.Center;

// Now find all paragraphs with Heading 1 style and reset style.

NodeCollection paragraphs = doc.GetChildNodes(NodeType.Paragraph, true);

foreach (Paragraph paragraph in paragraphs)

{

if(paragraph.ParagraphFormat.StyleIdentifier == StyleIdentifier.Heading1)

{

paragraph.ParagraphFormat.ClearFormatting();

paragraph.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading1;

}

}

// Save output document.

doc.Save(@"Test001\out.doc");

Hope this helps.

Best regards,

Thank you,

it works!