Aspose.Word
Is a Extension for the ASPOS Word API.
Source
/// <summary>
/// Helper Class to make it easier with aspose word
/// </summary>
public static class AsposeWordExtension
{
#region Document
/// <summary>
/// Add a blank paragraph to the document
/// </summary>
/// <param name="doc"></param>
public static void AddBlankParagraph(this Document doc)
{
Paragraph paragraph = new Paragraph(doc);
doc.LastSection.Body.AppendChild(paragraph);
}
#endregion
#region Paragraph
/// <summary>
/// Add one run to the paragraph, and set the run with properties
/// </summary>
/// <param name="paragraph">the paragraph object</param>
/// <param name="text">text for set</param>
/// <param name="size">font size</param>
/// <param name="isBold">is the font style bold</param>
/// <param name="isItalic">is the font style italic</param>
/// <param name="fontName">font name, default is "Arial"</param>
/// <param name="comment">add a comment to the paragraph</param>
public static void AddRun(this Paragraph paragraph, string text, int size, bool isBold, bool isItalic, string fontName = "Arial", Comment comment = null)
{
Run run = new Run(paragraph.Document);
run.SetRun(text, size, isBold, isItalic, fontName);
paragraph.AppendChild(run);
if (comment != null)
{
paragraph.AppendChild(comment);
}
}
/// <summary>
/// Add one run to the paragraph, and set the run with properties
/// </summary>
/// <param name="paragraph">the paragraph object</param>
/// <param name="text">text for set</param>
/// <param name="size">font size</param>
/// <param name="fontName">font name, default is "Arial"</param>
/// <param name="comment">add a comment to the paragraph</param>
public static void AddRun(this Paragraph paragraph, string text, int size, string fontName = "Arial", Comment comment = null)
{
paragraph.AddRun(text, size, false, false, fontName, comment);
}
/// <summary>
/// Add one run to the paragraph, and set the run with font style bold
/// </summary>
/// <param name="paragraph">the paragraph object</param>
/// <param name="text">text for set</param>
/// <param name="size">font size</param>
/// <param name="fontName">font name, default is "Arial"</param>
/// <param name="comment">add a comment to the paragraph</param>
public static void AddRunBold(this Paragraph paragraph, string text, int size, string fontName = "Arial", Comment comment = null)
{
paragraph.AddRun(text, size, true, false, fontName, comment);
}
/// <summary>
/// Add one run to the paragraph, and set the run with font style italic
/// </summary>
/// <param name="paragraph">the paragraph object</param>
/// <param name="text">text for set</param>
/// <param name="size">font size</param>
/// <param name="fontName">font name, default is "Arial"</param>
/// <param name="comment">add a comment to the paragraph</param>
public static void AddRunItalic(this Paragraph paragraph, string text, int size, string fontName = "Arial", Comment comment = null)
{
paragraph.AddRun(text, size, false, true, fontName, comment);
}
/// <summary>
/// Add one run to the paragraph, and set the run with font style bold and italic
/// </summary>
/// <param name="paragraph">the paragraph object</param>
/// <param name="text">text for set</param>
/// <param name="size">font size</param>
/// <param name="fontName">font name, default is "Arial"</param>
/// <param name="comment">add a comment to the paragraph</param>
public static void AddRunBoldItalic(this Paragraph paragraph, string text, int size, string fontName = "Arial", Comment comment = null)
{
paragraph.AddRun(text, size, true, true, fontName, comment);
}
#endregion
#region Cell
/// <summary>
/// Add html text to the cell, if don't have html text add normal text with the font size
/// </summary>
/// <param name="cell">the cell object</param>
/// <param name="htmlText">html text</param>
/// <param name="size">font size</param>
/// <param name="fontName">font name, default is "Arial"</param>
public static void AddHtmlText(this Cell cell, string htmlText, int size, string fontName = "Arial")
{
if (string.IsNullOrEmpty(htmlText))
{
cell.AddBlankParagraph();
return;
}
// Check if has html text
if (htmlText.Contains("<text>") || htmlText.Contains("<p>") || htmlText.Contains("<body>") || htmlText.Contains("<html"))
{
htmlText = "<!DOCTYPE html><html lang='de'><head><style>body { font-family:" + fontName + "; font-size: " + (size + 2) + "px; } </style></head><body>" + htmlText + "</body></html>";
MemoryStream stream = new MemoryStream();
StreamWriter writer = new StreamWriter(stream);
writer.Write(htmlText);
writer.Flush();
stream.Position = 0;
Document htmlDoc = new Document(stream);
NodeCollection paragraphs = htmlDoc.GetChildNodes(NodeType.Paragraph, true);
foreach (Node node in paragraphs)
{
Node newNode = cell.Document.ImportNode(node, true, ImportFormatMode.KeepSourceFormatting);
cell.AppendChild(newNode);
}
}
else
{
cell.AddParagraph(htmlText, size);
}
}
/// <summary>
/// Add a blank paragraph to the cell
/// </summary>
/// <param name="cell">the cell object</param>
public static void AddBlankParagraph(this Cell cell)
{
Paragraph paragraph = new Paragraph(cell.Document);
cell.AppendChild(paragraph);
}
/// <summary>
/// Add one paragraph to the cell, and set the run with properties
/// </summary>
/// <param name="cell">the cell object</param>
/// <param name="text">text for set</param>
/// <param name="size">font size</param>
/// <param name="isBold">is the font style bold</param>
/// <param name="isItalic">is the font style italic</param>
/// <param name="fontName">font name, default is "Arial"</param>
/// <param name="comment">add a comment to the paragraph</param>
public static void AddParagraph(this Cell cell, string text, int size, bool isBold, bool isItalic, string fontName = "Arial", Comment comment = null)
{
Paragraph paragraph = new Paragraph(cell.Document);
paragraph.AddRun(text, size, isBold, isItalic, fontName, comment);
cell.AppendChild(paragraph);
}
/// <summary>
/// Add one paragraph to the cell, and set the run with properties
/// </summary>
/// <param name="cell">the cell object</param>
/// <param name="text">text for set</param>
/// <param name="size">font size</param>
/// <param name="fontName">font name, default is "Arial"</param>
/// <param name="comment">add a comment to the paragraph</param>
public static void AddParagraph(this Cell cell, string text, int size, string fontName = "Arial", Comment comment = null)
{
Paragraph paragraph = new Paragraph(cell.Document);
paragraph.AddRun(text, size, false, false, fontName, comment);
cell.AppendChild(paragraph);
}
/// <summary>
/// Add one paragraph to the cell, and set the run with font style bold
/// </summary>
/// <param name="cell">the cell object</param>
/// <param name="text">text for set</param>
/// <param name="size">font size</param>
/// <param name="fontName">font name, default is "Arial"</param>
/// <param name="comment">add a comment to the paragraph</param>
public static void AddParagraphBold(this Cell cell, string text, int size, string fontName = "Arial", Comment comment = null)
{
Paragraph paragraph = new Paragraph(cell.Document);
paragraph.AddRunBold(text, size, fontName, comment);
cell.AppendChild(paragraph);
}
/// <summary>
/// Add one paragraph to the cell, and set the run with font style italic
/// </summary>
/// <param name="cell">the cell object</param>
/// <param name="text">text for set</param>
/// <param name="size">font size</param>
/// <param name="fontName">font name, default is "Arial"</param>
/// <param name="comment">add a comment to the paragraph</param>
public static void AddParagraphItalic(this Cell cell, string text, int size, string fontName = "Arial", Comment comment = null)
{
Paragraph paragraph = new Paragraph(cell.Document);
paragraph.AddRunItalic(text, size, fontName, comment);
cell.AppendChild(paragraph);
}
/// <summary>
/// Add one paragraph to the cell, and set the run with font style bold and italic
/// </summary>
/// <param name="cell">the cell object</param>
/// <param name="text">text for set</param>
/// <param name="size">font size</param>
/// <param name="fontName">font name, default is "Arial"</param>
/// <param name="comment">add a comment to the paragraph</param>
public static void AddParagraphBoldItalic(this Cell cell, string text, int size, string fontName = "Arial", Comment comment = null)
{
Paragraph paragraph = new Paragraph(cell.Document);
paragraph.AddRunBoldItalic(text, size, fontName, comment);
cell.AppendChild(paragraph);
}
#endregion
#region Run
/// <summary>
/// Set the run with properties
/// </summary>
/// <param name="run">the run object</param>
/// <param name="text">text for set</param>
/// <param name="size">font size</param>
/// <param name="isBold">is the font style bold</param>
/// <param name="isItalic">is the font style italic</param>
/// <param name="fontName">font name, default is "Arial"</param>
public static void SetRun(this Run run ,string text, int size, bool isBold, bool isItalic, string fontName="Arial")
{
run.Text = (!string.IsNullOrEmpty(text)) ? text : string.Empty;
run.Font.Bold = isBold;
run.Font.Italic = isItalic;
run.Font.Size = size;
run.Font.Name = fontName;
}
/// <summary>
/// Set the run with properties
/// </summary>
/// <param name="run">the run object</param>
/// <param name="text">text for set</param>
/// <param name="size">font size</param>
/// <param name="fontName">font name, default is "Arial"</param>
public static void SetRun(this Run run, string text, int size, string fontName = "Arial")
{
run.SetRun(text, size, false, false, fontName);
}
/// <summary>
/// Set the run with font style bold
/// </summary>
/// <param name="run">the run object</param>
/// <param name="text">text for set</param>
/// <param name="size">font size</param>
/// <param name="fontName">font name, default is "Arial"</param>
public static void SetRunBold(this Run run, string text, int size, string fontName = "Arial")
{
run.SetRun(text, size, true, false, fontName);
}
/// <summary>
/// Set the run with font style italic
/// </summary>
/// <param name="run">the run object</param>
/// <param name="text">text for set</param>
/// <param name="size">font size</param>
/// <param name="fontName">font name, default is "Arial"</param>
public static void SetRunItalic(this Run run, string text, int size, string fontName = "Arial")
{
run.SetRun(text, size, false, true, fontName);
}
/// <summary>
/// Set the run with font style bold and italic
/// </summary>
/// <param name="run">the run object</param>
/// <param name="text">text for set</param>
/// <param name="size">font size</param>
/// <param name="fontName">font name, default is "Arial"</param>
public static void SetRunBoldItalic(this Run run, string text, int size, string fontName = "Arial")
{
run.SetRun(text, size, true, true, fontName);
}
#endregion
#region Comment
/// <summary>
/// Add html text to the comment, if don't have html text add normal text with the font size
/// </summary>
/// <param name="comment">the comment object</param>
/// <param name="htmlText">html text</param>
/// <param name="size">font size</param>
/// <param name="fontName">font name, default is "Arial"</param>
public static void AddHtmlText(this Comment comment, string htmlText, int size, string fontName = "Arial")
{
if (string.IsNullOrEmpty(htmlText))
{
return;
}
// Check if has html text
if (htmlText.Contains("<text>") || htmlText.Contains("<p>") || htmlText.Contains("<body>") || htmlText.Contains("<html"))
{
htmlText = "<!DOCTYPE html><html lang='de'><head><style>body { font-family:" + fontName + "; font-size: " + (size + 2) + "px; } </style></head><body>" + htmlText + "</body></html>";
MemoryStream stream = new MemoryStream();
StreamWriter writer = new StreamWriter(stream);
writer.Write(htmlText);
writer.Flush();
stream.Position = 0;
Document htmlDoc = new Document(stream);
NodeCollection paragraphs = htmlDoc.GetChildNodes(NodeType.Paragraph, true);
foreach (Node node in paragraphs)
{
Node newNode = comment.Document.ImportNode(node, true, ImportFormatMode.KeepSourceFormatting);
comment.AppendChild(newNode);
}
}
else
{
comment.AddParagraph(htmlText, size);
}
}
/// <summary>
/// Add one paragraph to the comment
/// </summary>
/// <param name="comment">the comment object</param>
/// <param name="paragraph">the paragraph</param>
public static void AddParagraph(this Comment comment, Paragraph paragraph)
{
comment.Paragraphs.Add(paragraph);
}
/// <summary>
/// Add one paragraph to the comment, and set the run text and the font size
/// </summary>
/// <param name="comment">the comment object</param>
/// <param name="text">text for set</param>
/// <param name="size">font size</param>
public static void AddParagraph(this Comment comment, string text, int size)
{
comment.AddParagraph(text, size, false, false);
}
/// <summary>
/// Add one paragraph to the comment, and set the run with properties
/// </summary>
/// <param name="comment">the comment object</param>
/// <param name="text">text for set</param>
/// <param name="size">font size</param>
/// <param name="isBold">is the font style bold</param>
/// <param name="isItalic">is the font style italic</param>
/// <param name="fontName">font name, default is "Arial"</param>
public static void AddParagraph(this Comment comment, string text, int size, bool isBold, bool isItalic, string fontName = "Arial")
{
Paragraph paragraph = new Paragraph(comment.Document);
paragraph.AddRun(text, size, isBold, isItalic, fontName);
comment.Paragraphs.Add(paragraph);
}
#endregion
}
Example
Document Doc = new Document();
Row row = new Row(Doc);
Cell cell = new Cell(Doc);
Comment comment = new Comment(Doc, "Hans Muster", "HM", DateTime.Now);
comment.AddHtmlText("New comment text", 9);
cell.AddParagraph("Test Text", 9, "Arial", comment);
row.AppendChild(cell);
Author: Alain Schärer
Submitted on: 18 apr. 2017
Language: C#
Type: ASPOS Word Extension
Views: 3990