当前位置:网站首页>Aspose. Word operation word document (I)
Aspose. Word operation word document (I)
2022-07-07 22:23:00 【Hey, hey, hey, hey, hey】
Catalog
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
边栏推荐
- UWA问答精选
- Remember that a development is encountered in the pit of origin string sorting
- Use partial derivatives to display normals in unity
- Kirin Xin'an operating system derivative solution | storage multipath management system, effectively improving the reliability of data transmission
- Reinforcement learning - learning notes 9 | multi step TD target
- Crawler (17) - Interview (2) | crawler interview question bank
- Ueeditor custom display insert code
- 反爬通杀神器
- 强化学习-学习笔记9 | Multi-Step-TD-Target
- Blender exchange group, welcome to the water group ~
猜你喜欢
![VTOL in Px4_ att_ Control source code analysis [supplement]](/img/7a/4ce0c939b9259faf59c52da2587693.jpg)
VTOL in Px4_ att_ Control source code analysis [supplement]

Build your own website (18)
![[开源] .Net ORM 访问 Firebird 数据库](/img/a2/4eff4f0af53bf3b9839a73019a212f.png)
[开源] .Net ORM 访问 Firebird 数据库

如何选择合适的自动化测试工具?

Add get disabled for RC form

强化学习-学习笔记9 | Multi-Step-TD-Target
![[azure microservice service fabric] start the performance monitor in the SF node and set the method of capturing the process](/img/80/11c2b539c217ecd6ba55668d3e71e9.png)
[azure microservice service fabric] start the performance monitor in the SF node and set the method of capturing the process

How to quickly check whether the opening area ratio of steel mesh conforms to ipc7525

Reptile combat (VII): pictures of the king of reptiles' heroes

648. Word replacement
随机推荐
【Azure微服务 Service Fabric 】如何转移Service Fabric集群中的种子节点(Seed Node)
Blender exchange group, welcome to the water group ~
Use blocconsumer to build responsive components and monitor status at the same time
【JDBC Part 1】概述、获取连接、CRUD
客户案例|华律网,通过观测云大幅缩短故障定位时间
Revit secondary development - wall opening
Preparing for the interview and sharing experience
Jerry's configuration of TWS cross pairing [article]
Time standard library
Two kinds of updates lost and Solutions
Programming mode - table driven programming
Tsconfig of typescript TS basics JSON configuration options
IP网络主动测评系统——X-Vision
The essence of analog Servlet
The difference between NPM uninstall and RM direct deletion
How polardb-x does distributed database hotspot analysis
[开源] .Net ORM 访问 Firebird 数据库
Overseas agent recommendation
23. Merge K ascending linked lists -c language
Get the exact offset of the element