ParagraphFormat

Hi Aspose,
i have a string html with paragraph and style attribute: fontsize, fontname, etc…
How do i change, for example, the font-name or font-size of paragraph ?
Thanks, Enzo

Hi Enzo,
Thanks for your inquiry, You can format paragraph as you wish using Aspose.Words.
Sample code is provided in below mentioned lines. Kindly do let me know in case of any issue.

// Create an "empty" document. Note that like in Microsoft Word,
// the empty document has one section, body and one paragraph in it.
Document doc = new Document();

// This truly makes the document empty. No sections (not possible in Microsoft Word).
doc.RemoveAllChildren();

// Create a new section node.
// Note that the section has not yet been added to the document,
// but we have to specify the parent document.
Section section = new Section(doc);

// Append the section to the document.
doc.AppendChild(section);

// Lets set some properties for the section.
section.PageSetup.SectionStart = SectionStart.NewPage;
section.PageSetup.PaperSize = PaperSize.Letter;

// The section that we created is empty, lets populate it. The section needs at least the Body node.
Body body = new Body(doc);
section.AppendChild(body);

// The body needs to have at least one paragraph.
// Note that the paragraph has not yet been added to the document,
// but we have to specify the parent document.
// The parent document is needed so the paragraph can correctly work
// with styles and other document-wide information.
Paragraph para = new Paragraph(doc);
body.AppendChild(para);

// We can set some formatting for the paragraph
para.ParagraphFormat.StyleName = "Heading 1";
para.ParagraphFormat.Alignment = ParagraphAlignment.Center;

// So far we have one empty paragraph in the document.
// The document is valid and can be saved, but lets add some text before saving.
// Create a new run of text and add it to our paragraph.
Run run = new Run(doc);
run.Text = "Hello World!";
run.Font.Color = System.Drawing.Color.Red;
para.AppendChild(run);

// As a matter of interest, you can retrieve text of the whole document and
// see that \x000c is automatically appended. \x000c is the end of section character.
Assert.AreEqual("Hello World!\x000c", doc.GetText());

// Save the document.
doc.Save(MyDir + "Section.CreateFromScratch Out.doc");

Thanks and Regards,

dihsarashid

Hi Enzo,
Thanks for your interest in Aspose.Words. In additional, I think in you case you can try using DocumentBuilder. Please see the following link to learn more:
https://reference.aspose.com/words/net/aspose.words/documentbuilder/
Best regards,

thanks for your responses.

I have write this code but not work.The result final of font name e font size of all paragraph is different.Thanks, Enzo

// input STRING HTML
string outhtml = strhtml;
// encoding
htmlBytes = Encoding.UTF8.GetBytes(outhtml);
// create stream
htmlStream = new MemoryStream(htmlBytes);

LoadOpt = new LoadOptions(LoadFormat.Html, "", "");
doc = new Aspose.Words.Document(htmlStream, LoadOpt);
docbuild = new DocumentBuilder(doc);
htmlStream.Close();
// 1.REMOVE ALL IMAGES

Aspose.Words.NodeCollection shapes = doc.GetChildNodes(NodeType.Shape, true, false);
foreach (Aspose.Words.Drawing.Shape shape in shapes)
{
    if (shape.HasImage) shapesToDelete.Add(shape);
}
foreach (Aspose.Words.Drawing.Shape shape in shapesToDelete) shape.Remove();

shapes = null;

// Create a paragraph style and specify some formatting for it.
Style style = doc.Styles.Add(StyleType.Paragraph, "MyStyle1");
style.Font.ClearFormatting();
style.Font.Size = 12;
style.Font.Name = "Verdana";

// 2. MODIFY ALL PARAGRAPHS

Aspose.Words.NodeCollection paragraphs = doc.GetChildNodes(NodeType.Paragraph, true);
foreach (Paragraph par in paragraphs)
{
    par.ParagraphFormat.Style = style;
    par.ParagraphFormat.LineSpacingRule = LineSpacingRule.Exactly;
    par.ParagraphFormat.LineSpacing = 10;
    par.ParagraphFormat.LeftIndent = 0;
    par.ParagraphFormat.RightIndent = 0;
    par.ParagraphFormat.SpaceAfter = 0;
    par.ParagraphFormat.SpaceBefore = 0;
    par.ParagraphFormat.SuppressLineNumbers = true;
}

paragraphs = null;

// SAVE WITH HTML OPTION
SaveOpt = new Aspose.Words.Saving.HtmlSaveOptions();
SaveOpt.SaveFormat = SaveFormat.Html;

SaveOpt.CssStyleSheetType = Aspose.Words.Saving.CssStyleSheetType.Inline;
// htmlopt.CssStyleSheetFileName = sMappedPathCss;
SaveOpt.Encoding = Encoding.UTF8;

SaveOpt.ExportHeadersFootersMode = Aspose.Words.Saving.ExportHeadersFootersMode.None;

using (MemoryStream newhtmlStream = new MemoryStream())
{
    doc.Save(newhtmlStream, SaveOpt);
    // read newHtml
    outhtml = Encoding.UTF8.GetString(newhtmlStream.GetBuffer(), 0, (int)newhtmlStream.Length);
    newhtmlStream.Close();
}

Hi Enzo,
Thanks for your interest in Aspose. The following code snippet should address your scenario:

StringBuilder sb = new StringBuilder();
sb.Append("<p style='font-family: Verdana; font-size: 10pt;'>This text is inside paragraph 1</p>");
sb.Append("<p style='font-family: Tahoma; font-size: 16pt;'>This text is inside paragraph 2</p>");
string inputHtmlStr = sb.ToString();

MemoryStream stream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(inputHtmlStr));
Document document = new Document(stream, new LoadOptions(LoadFormat.Html, string.Empty, string.Empty));
DocumentBuilder builder = new DocumentBuilder(document);

Style style1 = document.Styles.Add(StyleType.Paragraph, "style1");
style1.Font.Size = 10;
style1.Font.Name = "Verdana";

Style style2 = document.Styles.Add(StyleType.Paragraph, "style2");
style2.Font.Size = 16;
style2.Font.Name = "Tahoma"; 

NodeCollection paragraphs = document.GetChildNodes(NodeType.Paragraph, true);
((Paragraph)paragraphs[0]).ParagraphFormat.Style = style1;
((Paragraph)paragraphs[1]).ParagraphFormat.Style = style2; 

document.Save("c:\\test.docx", SaveFormat.Docx);

I hope this will help
Best regards,

Hi Awais, thanks for code.
I have this input string html:
text
.
How to change font-family in this case?
thanks,Enzo

Hi
Thanks for your request. Content inserted by Insert HTML does not inherit formatting specified in DocumentBuilder options. Whole formatting is taken from HTML snippet. If you insert HTML with no formatting specified, default formatting is used for inserted content.
So the easiest way to apply formatting in your case, is simply change formatting in your HTML string.
Also if you looking to combine all formatting from the DocumentBuilder or another node automatically then you may want to look into using this code work around here.
Please let us know if you need more information, we will be glad to help you.
Best regards,