当前位置:网站首页>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
边栏推荐
- The difference between NPM uninstall and RM direct deletion
- Jerry's initiation of ear pairing, reconnection, and opening of discoverable and connectable cyclic functions [chapter]
- Reinforcement learning - learning notes 9 | multi step TD target
- Display optimization when the resolution of easycvr configuration center video recording plan page is adjusted
- The strongest installation of the twin tower model, Google is playing "antique" again?
- The whole network "chases" Zhong Xuegao
- Which financial products will yield high returns in 2022?
- Jerry's power on automatic pairing [chapter]
- Can I open a stock account directly online now? Is it safe?
- Jerry's fast pairing does not support canceling pairing [article]
猜你喜欢
【Azure微服务 Service Fabric 】在SF节点中开启Performance Monitor及设置抓取进程的方式
Use json Stringify() to realize deep copy, be careful, there may be a huge hole
强化学习-学习笔记9 | Multi-Step-TD-Target
Tcp/ip protocol stack
建立自己的网站(18)
NVR hard disk video recorder is connected to easycvr through the national standard gb28181 protocol. What is the reason why the device channel information is not displayed?
三元表达式、各生成式、匿名函数
Pdf document signature Guide
What if the win11u disk does not display? Solution to failure of win11 plug-in USB flash disk
Why can't win11 display seconds? How to solve the problem that win11 time does not display seconds?
随机推荐
Win11如何解禁键盘?Win11解禁键盘的方法
MIT6.S081-Lab9 FS [2021Fall]
The latest Android interview collection, Android video extraction audio
为什么Win11不能显示秒数?Win11时间不显示秒怎么解决?
OpenGL configure assimp
[开源] .Net ORM 访问 Firebird 数据库
VTOL in Px4_ att_ Control source code analysis [supplement]
Cannot find module 'xxx' or its corresponding type declaration
What if the win11u disk does not display? Solution to failure of win11 plug-in USB flash disk
Embedded development: how to choose the right RTOS for the project?
How to choose the appropriate automated testing tools?
Revit secondary development - project file to family file
null == undefined
Jerry's configuration of TWS cross pairing [article]
[open source] Net ORM accessing Firebird database
How to quickly check whether the opening area ratio of steel mesh conforms to ipc7525
Win11时间怎么显示星期几?Win11怎么显示今天周几?
Pre sale 179000, hengchi 5 can fire? Product power online depends on how it is sold
The strongest installation of the twin tower model, Google is playing "antique" again?
operator