获取word文件段落
public static string GetParagraphText(string filePath, int index)
{
using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite))
{
Aspose.Words.Document doc = new Aspose.Words.Document(fs);
Aspose.Words.Paragraph run = (Aspose.Words.Paragraph)doc.GetChild(Aspose.Words.NodeType.Paragraph, index, true);
if (run != null)
{
return run.GetText().TrimEnd(new char[] { '\r', '\n' });
}
return "";
}
}
public static string[] GetParagraphTexts(string filePath)
{
using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite))
{
List<string> texts = new List<string>();
Aspose.Words.Document doc = new Aspose.Words.Document(fs);
Aspose.Words.NodeCollection runs = doc.GetChildNodes(Aspose.Words.NodeType.Paragraph, true);
if (runs != null)
{
foreach (Aspose.Words.Paragraph run in runs)
{
if (!string.IsNullOrEmpty(run.GetText().TrimEnd(new char[] { '\r', '\n' })))
{
texts.Add(run.GetText().TrimEnd(new char[] { '\r', '\n' }));
}
}
}
return texts.ToArray();
}
}
文本替换
public static void ReplaceText(string filename, string[] needReplacestr, string[] replacestr, bool isTextBox)
{
Aspose.Words.Document doc = new Aspose.Words.Document(filename);//打开word文档
for (int i = 0; i < needReplacestr.Length; i++)
{
doc.Range.Replace(needReplacestr[i], replacestr[i], new Aspose.Words.Replacing.FindReplaceOptions());
}
doc.Save(filename);
}
public static void ReplaceText(string filename,string needReplacestr, string replacestr, bool isTextBox)
{
Aspose.Words.Document doc = new Aspose.Words.Document(filename);//打开word文档
doc.Range.Replace(needReplacestr, replacestr, new Aspose.Words.Replacing.FindReplaceOptions());
doc.Save(filename);
}
书签插入文本
public static void InsertTextToMark(string filePath,string _bookMark, string text)
{
Aspose.Words.Document doc = new Aspose.Words.Document(filePath);
Aspose.Words.Bookmark bookmark = doc.Range.Bookmarks[_bookMark];
if (bookmark != null)
{
bookmark.Text = "";
Aspose.Words.DocumentBuilder builder = new Aspose.Words.DocumentBuilder(doc);
builder.MoveToBookmark(bookmark.Name);
builder.Write(text);
doc.Save(filePath);
}
}
public static void InsertTextToMark(string filePath, string[] _bookMark, string[] text)
{
Aspose.Words.Document doc = new Aspose.Words.Document(filePath);
for(int i=0;i< _bookMark.Length;i++)
{
Aspose.Words.Bookmark bookmark = doc.Range.Bookmarks[_bookMark[i]];
if (bookmark != null)
{
bookmark.Text = "";
Aspose.Words.DocumentBuilder builder = new Aspose.Words.DocumentBuilder(doc);
builder.MoveToBookmark(bookmark.Name);
builder.Write(text[i]);
}
}
doc.Save(filePath);
}
设置段落样式
public static void SetWordFontByParagraphIndex(string filePath, int ParagraphIndex, float size, string fontName, AlignmentTypeEnum alignment, bool isBlod, bool isItalic)
{
Aspose.Words.Document doc = new Aspose.Words.Document(filePath);
Aspose.Words.Paragraph runs = (Aspose.Words.Paragraph)doc.GetChild(Aspose.Words.NodeType.Paragraph, ParagraphIndex, true);
if (runs != null)
{
foreach (Aspose.Words.Run run in runs.Runs)
{
run.Font.Size = size;
run.Font.Name = fontName;
run.Font.Bold = isBlod;
run.Font.Italic = isItalic;
}
Aspose.Words.ParagraphAlignment paragraphAlignment = Aspose.Words.ParagraphAlignment.Left;
switch (alignment)
{
case AlignmentTypeEnum.Left:
paragraphAlignment = Aspose.Words.ParagraphAlignment.Left;
break;
case AlignmentTypeEnum.Right:
paragraphAlignment = Aspose.Words.ParagraphAlignment.Right;
break;
case AlignmentTypeEnum.Justify:
paragraphAlignment = Aspose.Words.ParagraphAlignment.Justify;
break;
case AlignmentTypeEnum.Center:
paragraphAlignment = Aspose.Words.ParagraphAlignment.Center;
break;
case AlignmentTypeEnum.JustifyLow:
paragraphAlignment = Aspose.Words.ParagraphAlignment.Justify;
break;
case AlignmentTypeEnum.Distribute:
paragraphAlignment = Aspose.Words.ParagraphAlignment.Distributed;
break;
case AlignmentTypeEnum.JustifyHi:
paragraphAlignment = Aspose.Words.ParagraphAlignment.Justify;
break;
case AlignmentTypeEnum.JustifyMed:
paragraphAlignment = Aspose.Words.ParagraphAlignment.Justify;
break;
case AlignmentTypeEnum.ThaiJustify:
paragraphAlignment = Aspose.Words.ParagraphAlignment.ThaiDistributed;
break;
default:
paragraphAlignment = Aspose.Words.ParagraphAlignment.Left;
break;
}
runs.ParagraphFormat.Alignment = paragraphAlignment;
}
doc.Save(filePath);
}
word转pdf
public static bool WordToPDF(string filePath,string targetPath)
{
bool result = false;
var doc = new Aspose.Words.Document(filePath);
try
{
//如果word文档有自己安装的字体需要追加一下,不然转换出来的pdf文件字体格式会错误
string userfontsfoloder = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\Microsoft\\Windows\\Fonts\\";
if (Directory.Exists(userfontsfoloder))
{
ArrayList fontSources = new ArrayList(Aspose.Words.Fonts.FontSettings.DefaultInstance.GetFontsSources());
//将用户目录字体添加到字体源中
Aspose.Words.Fonts.FolderFontSource folderFontSource = new Aspose.Words.Fonts.FolderFontSource(userfontsfoloder, true);
fontSources.Add(folderFontSource);
Aspose.Words.Fonts.FontSourceBase[] updatedFontSources = (Aspose.Words.Fonts.FontSourceBase[])fontSources.ToArray(typeof(Aspose.Words.Fonts.FontSourceBase));
Aspose.Words.Fonts.FontSettings.DefaultInstance.SetFontsSources(updatedFontSources);
}
}
catch(Exception ex)
{
}
doc.Save(targetPath, Aspose.Words.SaveFormat.Pdf);
result = true;
return result;
}