当前位置:网站首页>Aspose. Word operation word document (I)

Aspose. Word operation word document (I)

2022-07-07 22:23:00 Hey, hey, hey, hey, hey

Catalog

1. Bookmark assignment

2. Add text on a new line

3. Find the title in the document

4.  Go to pdf Insert directory in


1. Bookmark assignment

Create... In advance word file , Insert bookmark , Assign a value to the bookmark in the program

        /// <summary>
        ///  Bookmark assignment 
        /// </summary>
        /// <param name="labelId"> Book signature </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. Add text on a new line

 /// <summary>
///  To add text 
/// </summary>
/// <param name="wordDoc"></param>
/// <param name="value"> written words </param>
/// <param name="title">Heading 1/Heading 2/Normal</param>
/// <param name="alignment"> Alignment mode </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. Find the title in the document

    // Record word Title Information in the document , stay pdf Make a catalog in 
    public class PdfBookModel
    {
        public int level;// Title Hierarchy 
        public string title;// Title Text 
        public int page;// Page number 
        public List<PdfBookModel> child;// Subtitle 
        public PdfBookModel()
        {
            child = new List<PdfBookModel>();
        }
    }
        /// <summary>
        ///  Find the title 
        /// </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.  Go to pdf Insert directory in

        /// <summary>
        ///  Go to pdf Insert directory into the file 
        /// </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. take word Turn to picture

        /// <summary>
        ///  take word Turn to picture 
        /// </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, plug-in unit Click here to pick up . Extraction code : aniv

Aspose.Word operation Word file ( Two )

原网站

版权声明
本文为[Hey, hey, hey, hey, hey]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202130606206781.html