/**
* 名称: DocBuilder
* 作者: lizhongxiang
* 时间: 2017-07-08
* 版本: 1
* 说明: DocBuilder Word文档构造类
*
* 历史:
* 版本 时间 修改人 说明
* 1 2017-07-08 lizhongxiang Word文档构造类
* 2 2018-05-31 lizhongxiang 添加自定义文字大小 字体的设置
*/
using System;
using System.Data;
using System.IO;
using Aspose.Words;
using Aspose.Words.Tables;
namespace ResumeTools
{
public class DocBuilder
{
//实例化word文档类
readonly Document _doc = new Document();
private readonly DocumentBuilder _builder;
public DocBuilder()
{
//实例化word生成器
_builder = new DocumentBuilder(_doc);
_builder.Font.Name = "宋体";
_builder.Font.Size = 10.5;//10.5-5号
_builder.ParagraphFormat.LineSpacing = 18;//设置行距 12-单倍行距 18-1.5倍行距
}
#region 设置纸张
public void SetPaperSize(string papersize)
{
switch (papersize)
{
case "A4":
foreach (Section section in _doc)
{
section.PageSetup.PaperSize = PaperSize.A4;
section.PageSetup.Orientation = Orientation.Portrait;
section.PageSetup.VerticalAlignment = PageVerticalAlignment.Top;
}
break;
case "A4H"://A4横向
foreach (Section section in _doc)
{
section.PageSetup.PaperSize = PaperSize.A4;
section.PageSetup.Orientation = Orientation.Landscape;
section.PageSetup.TextColumns.SetCount(2);
section.PageSetup.TextColumns.EvenlySpaced = true;
section.PageSetup.TextColumns.LineBetween = true;
//section.PageSetup.LeftMargin = double.Parse("3.35");
//section.PageSetup.RightMargin =double.Parse("0.99");
}
break;
case "A3":
foreach (Section section in _doc)
{
section.PageSetup.PaperSize = PaperSize.A3;
section.PageSetup.Orientation = Orientation.Portrait;
}
break;
case "A3H"://A3横向
foreach (Section section in _doc)
{
section.PageSetup.PaperSize = PaperSize.A3;
section.PageSetup.Orientation = Orientation.Landscape;
section.PageSetup.TextColumns.SetCount(2);
section.PageSetup.TextColumns.EvenlySpaced = true;
section.PageSetup.TextColumns.LineBetween = true;
}
break;
case "16K":
foreach (Section section in _doc)
{
section.PageSetup.PaperSize = PaperSize.B5;
section.PageSetup.Orientation = Orientation.Portrait;
}
break;
case "8KH":
foreach (Section section in _doc)
{
section.PageSetup.PageWidth = double.Parse("36.4 ");//纸张宽度
section.PageSetup.PageHeight = double.Parse("25.7");//纸张高度
section.PageSetup.Orientation = Orientation.Landscape;
section.PageSetup.TextColumns.SetCount(2);
section.PageSetup.TextColumns.EvenlySpaced = true;
section.PageSetup.TextColumns.LineBetween = true;
//section.PageSetup.LeftMargin = double.Parse("3.35");
//section.PageSetup.RightMargin = double.Parse("0.99");
}
break;
}
}
#endregion
#region 添加内容不换行
/// <summary>
/// 添加内容
/// </summary>
/// <param name="strText"></param>
/// <param name="conSize"></param>
/// <param name="conBold"></param>
/// <param name="conAlign"></param>
public void WriteText(string strText, double conSize, bool conBold, Align conAlign)
{
_builder.Bold = conBold;
_builder.Font.Size = conSize;
if (conAlign.Equals(Align.Left))
{
_builder.ParagraphFormat.Alignment = ParagraphAlignment.Left;
}
if (conAlign.Equals(Align.Center))
{
_builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
}
if (conAlign.Equals(Align.Right))
{
_builder.ParagraphFormat.Alignment = ParagraphAlignment.Right;
}
_builder.Write(strText);
}
#endregion
#region 添加内容换行
public enum Align
{
Left, Center, Right
}
/// <summary>
/// 换行
/// </summary>
public void Writeln()
{
_builder.Writeln();
}
/// <summary>
/// 添加内容
/// </summary>
/// <param name="strText"></param>
/// <param name="conSize"></param>
/// <param name="conBold"></param>
/// <param name="conAlign"></param>
public void InsertText(string strText, double conSize, bool conBold, Align conAlign)
{
_builder.Bold = conBold;
_builder.Font.Size = conSize;
_builder.ParagraphFormat.Alignment = ParagraphAlignment.Left;
if (conAlign.Equals(Align.Left))
{
_builder.ParagraphFormat.Alignment = ParagraphAlignment.Left;
}
if (conAlign.Equals(Align.Center))
{
_builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
}
if (conAlign.Equals(Align.Right))
{
_builder.ParagraphFormat.Alignment = ParagraphAlignment.Right;
}
_builder.Writeln(strText);
}
#endregion
#region 设置文件页眉
/// <summary>
/// 设置页眉
/// </summary>
/// <param name="strBookmarkName"></param>
/// <param name="text"></param>
public void SetHeade(string strBookmarkName, string text)
{
if (_doc.Range.Bookmarks[strBookmarkName] != null)
{
Bookmark mark = _doc.Range.Bookmarks[strBookmarkName];
mark.Text = text;
}
}
#endregion
#region 插入文件
public void InsertFile(string vfilename)
{
Document srcDoc = new Document(vfilename);
Node insertAfterNode = _builder.CurrentParagraph.PreviousSibling;
InsertDocument(insertAfterNode, _doc, srcDoc);
}
#endregion
#region 插入word内容
/// <summary>
/// 插入word内容
/// </summary>
/// <param name="insertAfterNode"></param>
/// <param name="mainDoc"> </param>
/// <param name="srcDoc"> </param>
public static void InsertDocument(Node insertAfterNode, Document mainDoc, Document srcDoc)
{
// Make sure that the node is either a pargraph or table.
if ((insertAfterNode.NodeType != NodeType.Paragraph)
& (insertAfterNode.NodeType != NodeType.Table))
throw new Exception("The destination node should be either a paragraph or table.");
//We will be inserting into the parent of the destination paragraph.
CompositeNode dstStory = insertAfterNode.ParentNode;
//Remove empty paragraphs from the end of document
while (null != srcDoc.LastSection.Body.LastParagraph && !srcDoc.LastSection.Body.LastParagraph.HasChildNodes)
{
srcDoc.LastSection.Body.LastParagraph.Remove();
}
NodeImporter importer = new NodeImporter(srcDoc, mainDoc, ImportFormatMode.KeepSourceFormatting);
//Loop through all sections in the source document.
int sectCount = srcDoc.Sections.Count;
for (int sectIndex = 0; sectIndex < sectCount; sectIndex++)
{
Section srcSection = srcDoc.Sections[sectIndex];
//Loop through all block level nodes (paragraphs and tables) in the body of the section.
int nodeCount = srcSection.Body.ChildNodes.Count;
for (int nodeIndex = 0; nodeIndex < nodeCount; nodeIndex++)
{
Node srcNode = srcSection.Body.ChildNodes[nodeIndex];
Node newNode = importer.ImportNode(srcNode, true);
dstStory.InsertAfter(newNode, insertAfterNode);
insertAfterNode = newNode;
}
}
}
#endregion
#region
private static void InsertDocument(Node insertAfterNode, Document srcDoc)
{
// Make sure that the node is either a paragraph or table.
if ((!insertAfterNode.NodeType.Equals(NodeType.Paragraph)) &
(!insertAfterNode.NodeType.Equals(NodeType.Table)))
throw new ArgumentException("The destination node should be either a paragraph or table.");
// We will be inserting into the parent of the destination paragraph.
CompositeNode dstStory = insertAfterNode.ParentNode;
// This object will be translating styles and lists during the import.
NodeImporter importer = new NodeImporter(srcDoc, insertAfterNode.Document,
ImportFormatMode.KeepSourceFormatting);
// Loop through all sections in the source document.
foreach (Section srcSection in srcDoc.Sections)
{
// Loop through all block level nodes (paragraphs and tables) in the body of the section.
foreach (Node srcNode in srcSection.Body)
{
// Let's skip the node if it is a last empty paragraph in a section.
if (srcNode.NodeType.Equals(NodeType.Paragraph))
{
Paragraph para = (Paragraph)srcNode;
if (para.IsEndOfSection && !para.HasChildNodes)
continue;
}
// This creates a clone of the node, suitable for insertion into the destination document.
Node newNode = importer.ImportNode(srcNode, true);
// Insert new node after the reference node.
dstStory.InsertAfter(newNode, insertAfterNode);
insertAfterNode = newNode;
}
}
}
#endregion
#region 换行
/// <summary>
/// 换行
/// </summary>
public void InsertLineBreak()
{
_builder.InsertBreak(BreakType.LineBreak);
}
#endregion
#region 换多行
/// <summary>
/// 换多行
/// </summary>
/// <param name="nline"></param>
public void InsertLineBreak(int nline)
{
for (int i = 0; i < nline; i++)
_builder.InsertBreak(BreakType.LineBreak);
}
#endregion
#region 插入表格
/// <summary>
/// 插入表格
/// </summary>
/// <param name="dt">表格数据</param>
/// <param name="bold">表格内字体是否加粗</param>
/// <param name="haveBorder">表格是否有边框</param>
/// <returns></returns>
public bool InsertTable(DataTable dt,bool bold,bool haveBorder)
{
Section currentSection = _builder.CurrentSection;
PageSetup pageSetup = currentSection.PageSetup;
double tableWidth = pageSetup.PageWidth - pageSetup.LeftMargin - pageSetup.RightMargin;
Table table = _builder.StartTable();//开始画Table
ParagraphAlignment paragraphAlignmentValue = _builder.ParagraphFormat.Alignment;
_builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
//添加Word表格
for (int row = 0; row < dt.Rows.Count; row++)
{
_builder.RowFormat.Height = 0.61;
for (int col = 0; col < dt.Columns.Count; col++)
{
_builder.InsertCell();
_builder.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;//垂直居中对齐
_builder.ParagraphFormat.Alignment = ParagraphAlignment.Left;//水平左对齐
_builder.CellFormat.Width = tableWidth/dt.Columns.Count;
_builder.Bold = bold;//字体加粗
if (haveBorder)
{
//设置外框样式
_builder.CellFormat.Borders.LineStyle = LineStyle.Single;
//样式设置结束
}
else
{
_builder.CellFormat.Borders.LineStyle = LineStyle.None;
}
_builder.Write(dt.Rows[row][col].ToString());
}
_builder.EndRow();
}
_builder.EndTable();
_builder.ParagraphFormat.Alignment = paragraphAlignmentValue;
table.Alignment = TableAlignment.Center;
table.PreferredWidth = PreferredWidth.Auto;
return true;
}
/// <summary>
/// 插入表格
/// </summary>
/// <param name="dt">DataTable</param>
/// <param name="fontSize">字体大小</param>
/// <param name="font"></param>
/// <param name="bold"></param>
/// <param name="haveBorder"></param>
/// <returns></returns>
public bool InsertTable(DataTable dt,double fontSize,string font, bool bold, bool haveBorder)
{
Section currentSection = _builder.CurrentSection;
PageSetup pageSetup = currentSection.PageSetup;
double tableWidth = pageSetup.PageWidth - pageSetup.LeftMargin - pageSetup.RightMargin;
Table table = _builder.StartTable();//开始画Table
ParagraphAlignment paragraphAlignmentValue = _builder.ParagraphFormat.Alignment;
_builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
//添加Word表格
for (int row = 0; row < dt.Rows.Count; row++)
{
_builder.RowFormat.Height = 0.61;
for (int col = 0; col < dt.Columns.Count; col++)
{
_builder.InsertCell();
_builder.Font.Size = fontSize;//10.5-5号
_builder.Font.Name = font;//字体
_builder.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;//垂直居中对齐
_builder.ParagraphFormat.Alignment = ParagraphAlignment.Left;//水平左对齐
_builder.CellFormat.Width = tableWidth / dt.Columns.Count;
_builder.Bold = bold;//字体加粗
if (haveBorder)
{
//设置外框样式
_builder.CellFormat.Borders.LineStyle = LineStyle.Single;
//样式设置结束
}
else
{
_builder.CellFormat.Borders.LineStyle = LineStyle.None;
}
_builder.Write(dt.Rows[row][col].ToString());
}
_builder.EndRow();
}
_builder.EndTable();
_builder.ParagraphFormat.Alignment = paragraphAlignmentValue;
table.Alignment = TableAlignment.Center;
table.PreferredWidth = PreferredWidth.Auto;
return true;
}
#endregion
#region 添加页眉
public void AddHeader(string header,float fontSize, bool center,bool bold)
{
Section currentSection = _builder.CurrentSection;
PageSetup pageSetup = currentSection.PageSetup;
pageSetup.DifferentFirstPageHeaderFooter = true;
pageSetup.HeaderDistance = 20;
_builder.MoveToHeaderFooter(HeaderFooterType.HeaderFirst);
_builder.ParagraphFormat.Alignment = center ? ParagraphAlignment.Center : ParagraphAlignment.Left;//设置单位居中方式
_builder.Font.Bold = bold; //设置字体是否加粗
_builder.Font.Size = fontSize;//设置默认字体大小为9号字
_builder.Write(header);
_builder.MoveToDocumentEnd();//移动编辑域为文档最后域
}
public void AddHeader(string imgpath, int width, int height, string header, float fontSize, bool center, bool bold)
{
Section currentSection = _builder.CurrentSection;
PageSetup pageSetup = currentSection.PageSetup;
pageSetup.DifferentFirstPageHeaderFooter = true;
pageSetup.HeaderDistance = 20;
_builder.MoveToHeaderFooter(HeaderFooterType.HeaderFirst);
_builder.ParagraphFormat.Alignment = center ? ParagraphAlignment.Center : ParagraphAlignment.Left;//设置单位居中方式
_builder.Font.Bold = bold; //设置字体是否加粗
_builder.Font.Size = fontSize;//设置默认字体大小为9号字
_builder.Write(header);
_builder.Writeln();
_builder.ParagraphFormat.Alignment = ParagraphAlignment.Left;
_builder.InsertImage(imgpath, width, height);
_builder.MoveToDocumentEnd();//移动编辑域为文档最后域
}
public void AddHeader(string imgpath, int width, int height, bool center)
{
Section currentSection = _builder.CurrentSection;
PageSetup pageSetup = currentSection.PageSetup;
pageSetup.DifferentFirstPageHeaderFooter = true;
pageSetup.HeaderDistance = 20;
_builder.MoveToHeaderFooter(HeaderFooterType.HeaderFirst);
_builder.ParagraphFormat.Alignment = center ? ParagraphAlignment.Center : ParagraphAlignment.Left;//设置单位居中方式
_builder.InsertImage(imgpath, width, height);
_builder.MoveToDocumentEnd();//移动编辑域为文档最后域
}
#endregion
public void InsertPagebreak()
{
_builder.InsertBreak(BreakType.PageBreak);
}
/// <summary>
/// 插入书签
/// </summary>
/// <param name="bookMark"></param>
public void InsertBookMark(string bookMark)
{
_builder.StartBookmark(bookMark);
_builder.EndBookmark(bookMark);
}
public void GotoBookMark(string strBookMarkName)
{
_builder.MoveToBookmark(strBookMarkName);
}
public void ClearBookMark()
{
_doc.Range.Bookmarks.Clear();
}
public void ReplaceText(string oleText, string newText)
{
//System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(oleText);
_doc.Range.Replace(oleText, newText, false, false);
}
#region 保存文件
/// <summary>
/// 保存文件
/// </summary>
/// <param name="strFileName"></param>
public void SaveAs(string strFileName)
{
var fileNameWithoutExtension = Path.GetExtension(strFileName);
if (fileNameWithoutExtension != null)
{
string extension = fileNameWithoutExtension.ToLower();
_doc.Save(strFileName, extension.Equals(".docx") ? SaveFormat.Docx : SaveFormat.Doc);
}
}
#endregion
#region 插入图片
/// <summary>
/// 插入图片
/// </summary>
/// <param name="imgpath"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="conAlign"></param>
public void InsertImage(string imgpath, int width, int height, Align conAlign)
{
_builder.ParagraphFormat.Alignment = ParagraphAlignment.Left;
if (conAlign.Equals(Align.Left))
{
_builder.ParagraphFormat.Alignment = ParagraphAlignment.Left;
}
if (conAlign.Equals(Align.Center))
{
_builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
}
if (conAlign.Equals(Align.Right))
{
_builder.ParagraphFormat.Alignment = ParagraphAlignment.Right;
}
_builder.InsertImage(imgpath, width, height);
}
#endregion
public string FontName
{
get
{
return _builder.Font.Name;
}
set
{
_builder.Font.Name = value;
}
}
}
}
* 名称: DocBuilder
* 作者: lizhongxiang
* 时间: 2017-07-08
* 版本: 1
* 说明: DocBuilder Word文档构造类
*
* 历史:
* 版本 时间 修改人 说明
* 1 2017-07-08 lizhongxiang Word文档构造类
* 2 2018-05-31 lizhongxiang 添加自定义文字大小 字体的设置
*/
using System;
using System.Data;
using System.IO;
using Aspose.Words;
using Aspose.Words.Tables;
namespace ResumeTools
{
public class DocBuilder
{
//实例化word文档类
readonly Document _doc = new Document();
private readonly DocumentBuilder _builder;
public DocBuilder()
{
//实例化word生成器
_builder = new DocumentBuilder(_doc);
_builder.Font.Name = "宋体";
_builder.Font.Size = 10.5;//10.5-5号
_builder.ParagraphFormat.LineSpacing = 18;//设置行距 12-单倍行距 18-1.5倍行距
}
#region 设置纸张
public void SetPaperSize(string papersize)
{
switch (papersize)
{
case "A4":
foreach (Section section in _doc)
{
section.PageSetup.PaperSize = PaperSize.A4;
section.PageSetup.Orientation = Orientation.Portrait;
section.PageSetup.VerticalAlignment = PageVerticalAlignment.Top;
}
break;
case "A4H"://A4横向
foreach (Section section in _doc)
{
section.PageSetup.PaperSize = PaperSize.A4;
section.PageSetup.Orientation = Orientation.Landscape;
section.PageSetup.TextColumns.SetCount(2);
section.PageSetup.TextColumns.EvenlySpaced = true;
section.PageSetup.TextColumns.LineBetween = true;
//section.PageSetup.LeftMargin = double.Parse("3.35");
//section.PageSetup.RightMargin =double.Parse("0.99");
}
break;
case "A3":
foreach (Section section in _doc)
{
section.PageSetup.PaperSize = PaperSize.A3;
section.PageSetup.Orientation = Orientation.Portrait;
}
break;
case "A3H"://A3横向
foreach (Section section in _doc)
{
section.PageSetup.PaperSize = PaperSize.A3;
section.PageSetup.Orientation = Orientation.Landscape;
section.PageSetup.TextColumns.SetCount(2);
section.PageSetup.TextColumns.EvenlySpaced = true;
section.PageSetup.TextColumns.LineBetween = true;
}
break;
case "16K":
foreach (Section section in _doc)
{
section.PageSetup.PaperSize = PaperSize.B5;
section.PageSetup.Orientation = Orientation.Portrait;
}
break;
case "8KH":
foreach (Section section in _doc)
{
section.PageSetup.PageWidth = double.Parse("36.4 ");//纸张宽度
section.PageSetup.PageHeight = double.Parse("25.7");//纸张高度
section.PageSetup.Orientation = Orientation.Landscape;
section.PageSetup.TextColumns.SetCount(2);
section.PageSetup.TextColumns.EvenlySpaced = true;
section.PageSetup.TextColumns.LineBetween = true;
//section.PageSetup.LeftMargin = double.Parse("3.35");
//section.PageSetup.RightMargin = double.Parse("0.99");
}
break;
}
}
#endregion
#region 添加内容不换行
/// <summary>
/// 添加内容
/// </summary>
/// <param name="strText"></param>
/// <param name="conSize"></param>
/// <param name="conBold"></param>
/// <param name="conAlign"></param>
public void WriteText(string strText, double conSize, bool conBold, Align conAlign)
{
_builder.Bold = conBold;
_builder.Font.Size = conSize;
if (conAlign.Equals(Align.Left))
{
_builder.ParagraphFormat.Alignment = ParagraphAlignment.Left;
}
if (conAlign.Equals(Align.Center))
{
_builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
}
if (conAlign.Equals(Align.Right))
{
_builder.ParagraphFormat.Alignment = ParagraphAlignment.Right;
}
_builder.Write(strText);
}
#endregion
#region 添加内容换行
public enum Align
{
Left, Center, Right
}
/// <summary>
/// 换行
/// </summary>
public void Writeln()
{
_builder.Writeln();
}
/// <summary>
/// 添加内容
/// </summary>
/// <param name="strText"></param>
/// <param name="conSize"></param>
/// <param name="conBold"></param>
/// <param name="conAlign"></param>
public void InsertText(string strText, double conSize, bool conBold, Align conAlign)
{
_builder.Bold = conBold;
_builder.Font.Size = conSize;
_builder.ParagraphFormat.Alignment = ParagraphAlignment.Left;
if (conAlign.Equals(Align.Left))
{
_builder.ParagraphFormat.Alignment = ParagraphAlignment.Left;
}
if (conAlign.Equals(Align.Center))
{
_builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
}
if (conAlign.Equals(Align.Right))
{
_builder.ParagraphFormat.Alignment = ParagraphAlignment.Right;
}
_builder.Writeln(strText);
}
#endregion
#region 设置文件页眉
/// <summary>
/// 设置页眉
/// </summary>
/// <param name="strBookmarkName"></param>
/// <param name="text"></param>
public void SetHeade(string strBookmarkName, string text)
{
if (_doc.Range.Bookmarks[strBookmarkName] != null)
{
Bookmark mark = _doc.Range.Bookmarks[strBookmarkName];
mark.Text = text;
}
}
#endregion
#region 插入文件
public void InsertFile(string vfilename)
{
Document srcDoc = new Document(vfilename);
Node insertAfterNode = _builder.CurrentParagraph.PreviousSibling;
InsertDocument(insertAfterNode, _doc, srcDoc);
}
#endregion
#region 插入word内容
/// <summary>
/// 插入word内容
/// </summary>
/// <param name="insertAfterNode"></param>
/// <param name="mainDoc"> </param>
/// <param name="srcDoc"> </param>
public static void InsertDocument(Node insertAfterNode, Document mainDoc, Document srcDoc)
{
// Make sure that the node is either a pargraph or table.
if ((insertAfterNode.NodeType != NodeType.Paragraph)
& (insertAfterNode.NodeType != NodeType.Table))
throw new Exception("The destination node should be either a paragraph or table.");
//We will be inserting into the parent of the destination paragraph.
CompositeNode dstStory = insertAfterNode.ParentNode;
//Remove empty paragraphs from the end of document
while (null != srcDoc.LastSection.Body.LastParagraph && !srcDoc.LastSection.Body.LastParagraph.HasChildNodes)
{
srcDoc.LastSection.Body.LastParagraph.Remove();
}
NodeImporter importer = new NodeImporter(srcDoc, mainDoc, ImportFormatMode.KeepSourceFormatting);
//Loop through all sections in the source document.
int sectCount = srcDoc.Sections.Count;
for (int sectIndex = 0; sectIndex < sectCount; sectIndex++)
{
Section srcSection = srcDoc.Sections[sectIndex];
//Loop through all block level nodes (paragraphs and tables) in the body of the section.
int nodeCount = srcSection.Body.ChildNodes.Count;
for (int nodeIndex = 0; nodeIndex < nodeCount; nodeIndex++)
{
Node srcNode = srcSection.Body.ChildNodes[nodeIndex];
Node newNode = importer.ImportNode(srcNode, true);
dstStory.InsertAfter(newNode, insertAfterNode);
insertAfterNode = newNode;
}
}
}
#endregion
#region
private static void InsertDocument(Node insertAfterNode, Document srcDoc)
{
// Make sure that the node is either a paragraph or table.
if ((!insertAfterNode.NodeType.Equals(NodeType.Paragraph)) &
(!insertAfterNode.NodeType.Equals(NodeType.Table)))
throw new ArgumentException("The destination node should be either a paragraph or table.");
// We will be inserting into the parent of the destination paragraph.
CompositeNode dstStory = insertAfterNode.ParentNode;
// This object will be translating styles and lists during the import.
NodeImporter importer = new NodeImporter(srcDoc, insertAfterNode.Document,
ImportFormatMode.KeepSourceFormatting);
// Loop through all sections in the source document.
foreach (Section srcSection in srcDoc.Sections)
{
// Loop through all block level nodes (paragraphs and tables) in the body of the section.
foreach (Node srcNode in srcSection.Body)
{
// Let's skip the node if it is a last empty paragraph in a section.
if (srcNode.NodeType.Equals(NodeType.Paragraph))
{
Paragraph para = (Paragraph)srcNode;
if (para.IsEndOfSection && !para.HasChildNodes)
continue;
}
// This creates a clone of the node, suitable for insertion into the destination document.
Node newNode = importer.ImportNode(srcNode, true);
// Insert new node after the reference node.
dstStory.InsertAfter(newNode, insertAfterNode);
insertAfterNode = newNode;
}
}
}
#endregion
#region 换行
/// <summary>
/// 换行
/// </summary>
public void InsertLineBreak()
{
_builder.InsertBreak(BreakType.LineBreak);
}
#endregion
#region 换多行
/// <summary>
/// 换多行
/// </summary>
/// <param name="nline"></param>
public void InsertLineBreak(int nline)
{
for (int i = 0; i < nline; i++)
_builder.InsertBreak(BreakType.LineBreak);
}
#endregion
#region 插入表格
/// <summary>
/// 插入表格
/// </summary>
/// <param name="dt">表格数据</param>
/// <param name="bold">表格内字体是否加粗</param>
/// <param name="haveBorder">表格是否有边框</param>
/// <returns></returns>
public bool InsertTable(DataTable dt,bool bold,bool haveBorder)
{
Section currentSection = _builder.CurrentSection;
PageSetup pageSetup = currentSection.PageSetup;
double tableWidth = pageSetup.PageWidth - pageSetup.LeftMargin - pageSetup.RightMargin;
Table table = _builder.StartTable();//开始画Table
ParagraphAlignment paragraphAlignmentValue = _builder.ParagraphFormat.Alignment;
_builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
//添加Word表格
for (int row = 0; row < dt.Rows.Count; row++)
{
_builder.RowFormat.Height = 0.61;
for (int col = 0; col < dt.Columns.Count; col++)
{
_builder.InsertCell();
_builder.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;//垂直居中对齐
_builder.ParagraphFormat.Alignment = ParagraphAlignment.Left;//水平左对齐
_builder.CellFormat.Width = tableWidth/dt.Columns.Count;
_builder.Bold = bold;//字体加粗
if (haveBorder)
{
//设置外框样式
_builder.CellFormat.Borders.LineStyle = LineStyle.Single;
//样式设置结束
}
else
{
_builder.CellFormat.Borders.LineStyle = LineStyle.None;
}
_builder.Write(dt.Rows[row][col].ToString());
}
_builder.EndRow();
}
_builder.EndTable();
_builder.ParagraphFormat.Alignment = paragraphAlignmentValue;
table.Alignment = TableAlignment.Center;
table.PreferredWidth = PreferredWidth.Auto;
return true;
}
/// <summary>
/// 插入表格
/// </summary>
/// <param name="dt">DataTable</param>
/// <param name="fontSize">字体大小</param>
/// <param name="font"></param>
/// <param name="bold"></param>
/// <param name="haveBorder"></param>
/// <returns></returns>
public bool InsertTable(DataTable dt,double fontSize,string font, bool bold, bool haveBorder)
{
Section currentSection = _builder.CurrentSection;
PageSetup pageSetup = currentSection.PageSetup;
double tableWidth = pageSetup.PageWidth - pageSetup.LeftMargin - pageSetup.RightMargin;
Table table = _builder.StartTable();//开始画Table
ParagraphAlignment paragraphAlignmentValue = _builder.ParagraphFormat.Alignment;
_builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
//添加Word表格
for (int row = 0; row < dt.Rows.Count; row++)
{
_builder.RowFormat.Height = 0.61;
for (int col = 0; col < dt.Columns.Count; col++)
{
_builder.InsertCell();
_builder.Font.Size = fontSize;//10.5-5号
_builder.Font.Name = font;//字体
_builder.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;//垂直居中对齐
_builder.ParagraphFormat.Alignment = ParagraphAlignment.Left;//水平左对齐
_builder.CellFormat.Width = tableWidth / dt.Columns.Count;
_builder.Bold = bold;//字体加粗
if (haveBorder)
{
//设置外框样式
_builder.CellFormat.Borders.LineStyle = LineStyle.Single;
//样式设置结束
}
else
{
_builder.CellFormat.Borders.LineStyle = LineStyle.None;
}
_builder.Write(dt.Rows[row][col].ToString());
}
_builder.EndRow();
}
_builder.EndTable();
_builder.ParagraphFormat.Alignment = paragraphAlignmentValue;
table.Alignment = TableAlignment.Center;
table.PreferredWidth = PreferredWidth.Auto;
return true;
}
#endregion
#region 添加页眉
public void AddHeader(string header,float fontSize, bool center,bool bold)
{
Section currentSection = _builder.CurrentSection;
PageSetup pageSetup = currentSection.PageSetup;
pageSetup.DifferentFirstPageHeaderFooter = true;
pageSetup.HeaderDistance = 20;
_builder.MoveToHeaderFooter(HeaderFooterType.HeaderFirst);
_builder.ParagraphFormat.Alignment = center ? ParagraphAlignment.Center : ParagraphAlignment.Left;//设置单位居中方式
_builder.Font.Bold = bold; //设置字体是否加粗
_builder.Font.Size = fontSize;//设置默认字体大小为9号字
_builder.Write(header);
_builder.MoveToDocumentEnd();//移动编辑域为文档最后域
}
public void AddHeader(string imgpath, int width, int height, string header, float fontSize, bool center, bool bold)
{
Section currentSection = _builder.CurrentSection;
PageSetup pageSetup = currentSection.PageSetup;
pageSetup.DifferentFirstPageHeaderFooter = true;
pageSetup.HeaderDistance = 20;
_builder.MoveToHeaderFooter(HeaderFooterType.HeaderFirst);
_builder.ParagraphFormat.Alignment = center ? ParagraphAlignment.Center : ParagraphAlignment.Left;//设置单位居中方式
_builder.Font.Bold = bold; //设置字体是否加粗
_builder.Font.Size = fontSize;//设置默认字体大小为9号字
_builder.Write(header);
_builder.Writeln();
_builder.ParagraphFormat.Alignment = ParagraphAlignment.Left;
_builder.InsertImage(imgpath, width, height);
_builder.MoveToDocumentEnd();//移动编辑域为文档最后域
}
public void AddHeader(string imgpath, int width, int height, bool center)
{
Section currentSection = _builder.CurrentSection;
PageSetup pageSetup = currentSection.PageSetup;
pageSetup.DifferentFirstPageHeaderFooter = true;
pageSetup.HeaderDistance = 20;
_builder.MoveToHeaderFooter(HeaderFooterType.HeaderFirst);
_builder.ParagraphFormat.Alignment = center ? ParagraphAlignment.Center : ParagraphAlignment.Left;//设置单位居中方式
_builder.InsertImage(imgpath, width, height);
_builder.MoveToDocumentEnd();//移动编辑域为文档最后域
}
#endregion
public void InsertPagebreak()
{
_builder.InsertBreak(BreakType.PageBreak);
}
/// <summary>
/// 插入书签
/// </summary>
/// <param name="bookMark"></param>
public void InsertBookMark(string bookMark)
{
_builder.StartBookmark(bookMark);
_builder.EndBookmark(bookMark);
}
public void GotoBookMark(string strBookMarkName)
{
_builder.MoveToBookmark(strBookMarkName);
}
public void ClearBookMark()
{
_doc.Range.Bookmarks.Clear();
}
public void ReplaceText(string oleText, string newText)
{
//System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(oleText);
_doc.Range.Replace(oleText, newText, false, false);
}
#region 保存文件
/// <summary>
/// 保存文件
/// </summary>
/// <param name="strFileName"></param>
public void SaveAs(string strFileName)
{
var fileNameWithoutExtension = Path.GetExtension(strFileName);
if (fileNameWithoutExtension != null)
{
string extension = fileNameWithoutExtension.ToLower();
_doc.Save(strFileName, extension.Equals(".docx") ? SaveFormat.Docx : SaveFormat.Doc);
}
}
#endregion
#region 插入图片
/// <summary>
/// 插入图片
/// </summary>
/// <param name="imgpath"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="conAlign"></param>
public void InsertImage(string imgpath, int width, int height, Align conAlign)
{
_builder.ParagraphFormat.Alignment = ParagraphAlignment.Left;
if (conAlign.Equals(Align.Left))
{
_builder.ParagraphFormat.Alignment = ParagraphAlignment.Left;
}
if (conAlign.Equals(Align.Center))
{
_builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
}
if (conAlign.Equals(Align.Right))
{
_builder.ParagraphFormat.Alignment = ParagraphAlignment.Right;
}
_builder.InsertImage(imgpath, width, height);
}
#endregion
public string FontName
{
get
{
return _builder.Font.Name;
}
set
{
_builder.Font.Name = value;
}
}
}
}