[英]Apply wordstyles to insertedHTML in Aspose.Words for .NET
今天是個好日子。
如何將Word文檔的預定義樣式應用於插入的HTML?
喜歡:
builder.InsertHTML(post.Title)
// apply style from document "Media-title"
builder.InsertHTML(post.Content)
// apply style "Media-content"
請注意,useBuilderFormatting的InsertHtml()重載不會覆蓋具有內聯樣式的HTML文本樣式。 您可以實現INodeChangingCallback,以將樣式/格式應用於HTML文本。 請檢查以下代碼段以供參考。
public static void HtmlFormatting()
{
// Create a blank document object
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Set up and pass the object which implements the handler methods.
doc.NodeChangingCallback = new HandleNodeChanging_FontChanger();
// Insert sample HTML content
builder.InsertHtml("<p>Hello World</p>");
doc.NodeChangingCallback = null;
builder.InsertHtml("<p>Some Test Text</p>");
doc.Save(@"Out.docx");
}
public class HandleNodeChanging_FontChanger : INodeChangingCallback
{
// Implement the NodeInserted handler to set default font settings for every Run node inserted into the Document
void INodeChangingCallback.NodeInserted(NodeChangingArgs args)
{
// Change the font of inserted text contained in the Run nodes.
if (args.Node.NodeType == NodeType.Run)
{
Run run = (Run)args.Node;
Console.WriteLine(run.Text);
run.Font.StyleName = "Intense Emphasis";
// Aspose.Words.Font font = ((Run)args.Node).Font;
// font.Size = 24;
// font.Name = "Arial";
}
}
void INodeChangingCallback.NodeInserting(NodeChangingArgs args)
{
// Do Nothing
}
void INodeChangingCallback.NodeRemoved(NodeChangingArgs args)
{
// Do Nothing
}
void INodeChangingCallback.NodeRemoving(NodeChangingArgs args)
{
// Do Nothing
}
}
我與Aspose合作,擔任開發人員福音。
好了,在找到解決方案幾個小時之后,我有了這個工作:
builder.ParagraphFormat.ClearFormatting();
builder.ParagraphFormat.Style = styles["word_style1"];
builder.Writeln(post.Title);
builder.InsertParagraph();
builder.ParagraphFormat.Style = styles["word_style2"];
builder.InsertHtml(post.Annotation, true);
builder.InsertParagraph();
builder.ParagraphFormat.Style = styles["word_style3"];
builder.InsertHyperlink(post.Url, post.Url, false);
PS:我希望有任何變通辦法或改進可以做得更好。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.