目录
1.书签赋值
提前创建好word文档,插入书签,在程序中给书签赋值
/// <summary>
/// 书签赋值
/// </summary>
/// <param name="labelId">书签名</param>
/// <param name="content"></param>
public void WriteBookMark(string labelId, string content)
{
if (wordDoc.Range.Bookmarks[labelId] != null)
{
wordDoc.Range.Bookmarks[labelId].Text = content;
}
}
2.新起一行添加文本
/// <summary>
/// 添加文本
/// </summary>
/// <param name="wordDoc"></param>
/// <param name="value">文字</param>
/// <param name="title">Heading 1/Heading 2/Normal</param>
/// <param name="alignment">对齐方式</param>
public void AddParagraph(string value, string title = "Normal", ParagraphAlignment alignment = ParagraphAlignment.Left)
{
DocumentBuilder builder = new DocumentBuilder(wordDoc);
builder.MoveToDocumentEnd();
StyleCollection style = wordDoc.Styles;
builder.ParagraphFormat.Style = style[title];
builder.ParagraphFormat.Alignment = alignment;
builder.Writeln(value);
}
3.查找文档内标题
//记录word文档中标题信息,在pdf中制作目录
public class PdfBookModel
{
public int level;//标题层级
public string title;//标题文本
public int page;//所在页码
public List<PdfBookModel> child;//子标题
public PdfBookModel()
{
child = new List<PdfBookModel>();
}
}
/// <summary>
/// 查找标题
/// </summary>
/// <param name="wordDoc"></param>
/// <returns></returns>
public List<PdfBookModel> GetTitle(Document wordDoc)
{
List<PdfBookModel> data = new List<PdfBookModel>();
wordDoc.UpdateListLabels();
Aspose.Words.Layout.LayoutCollector layout = new Aspose.Words.Layout.LayoutCollector(wordDoc);
foreach (Section section in wordDoc.Sections)
{
foreach (Paragraph paragraph in section.Body.Paragraphs)
{
string text = "";
int pageIndex = layout.GetEndPageIndex(paragraph);
foreach (Run run in paragraph.Runs)
{
text += run.Text;
}
if (text == "") continue;
string[] str = paragraph.ParagraphFormat.StyleName.Split(' ');
int level = -1;
try
{ level = System.Convert.ToInt32(str[1]); }
catch
{ continue; }
if (level < 0) continue;
PdfBookModel model = new PdfBookModel();
model.level = level;
model.title = text;
model.page = pageIndex;
if (level == 1)
{
data.Add(model);
}
else
{
GetChildTitle(data[data.Count - 1], model);
}
}
}
return data;
}
private void GetChildTitle(PdfBookModel parent, PdfBookModel child)
{
if (parent.level + 1 == child.level)
{
parent.child.Add(child);
return;
}
else
{
GetChildTitle(parent.child[parent.child.Count - 1], child);
}
}
4. 往pdf中插入目录
/// <summary>
/// 往pdf文件中插入目录
/// </summary>
/// <param name="pdfPath"></param>
/// <param name="data"></param>
public void PDFAddMark(List<PdfBookModel> data)
{
foreach (PdfBookModel item in data)
{
Aspose.Pdf.OutlineItemCollection outlineItem = new Aspose.Pdf.OutlineItemCollection(pdfDoc.Outlines);
outlineItem.Title = item.title;
outlineItem.Italic = true;
outlineItem.Bold = true;
outlineItem.Action = new Aspose.Pdf.InteractiveFeatures.GoToAction(item.page);
pdfDoc.Outlines.Add(outlineItem);
PDFAddChildMark(outlineItem, item);
}
}
private void PDFAddChildMark(Aspose.Pdf.OutlineItemCollection Parent, PdfBookModel model)
{
if (model.child.Count > 0)
{
foreach (PdfBookModel item in model.child)
{
Aspose.Pdf.OutlineItemCollection outlineItem = new Aspose.Pdf.OutlineItemCollection(pdfDoc.Outlines);
outlineItem.Title = item.title;
outlineItem.Italic = true;
outlineItem.Bold = true;
outlineItem.Action = new Aspose.Pdf.InteractiveFeatures.GoToAction(item.page);
Parent.Add(outlineItem);
PDFAddChildMark(outlineItem, item);
}
}
}
5.将word转成图片
/// <summary>
/// 将word转成图片
/// </summary>
/// <returns></returns>
public List<Image> ExportImages()
{
List<Image> images = new List<Image>();
if (wordDoc == null) return images;
ImageSaveOptions option = new ImageSaveOptions(SaveFormat.Png);
option.PrettyFormat = true;
for (int i = 0; i < wordDoc.PageCount; i++)
{
option.PageIndex = i;
using (Stream stream = new MemoryStream())
{
try
{
wordDoc.Save(stream, option);
images.Add(Image.FromStream(stream));
}
catch
{
continue;
}
}
}
return images;
}