C#开发---Aspose.Word操作Word文档(一)

目录

1.书签赋值

2.新起一行添加文本

3.查找文档内标题

4. 往pdf中插入目录


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;
        }

Aspose.Word,Aspose.Pdf,插件 点击此处自取。提取码: aniv

Aspose.Word操作Word文档(二)

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

嘿呦嘿呦嘿呦嘿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值