当前位置:网站首页>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
边栏推荐
- OpenGL job coordinate system
- L2: current situation, prospects and pain points of ZK Rollup
- Win11游戏模式怎么开启?Win11开启游戏模式的方法
- Crawler (17) - Interview (2) | crawler interview question bank
- SAR image quality evaluation
- Can I open a stock account directly online now? Is it safe?
- Reinforcement learning - learning notes 9 | multi step TD target
- How does win11 unblock the keyboard? Method of unlocking keyboard in win11
- Typescript TS basic knowledge type declaration
- Two kinds of updates lost and Solutions
猜你喜欢
ByteDance senior engineer interview, easy to get started, fluent
Add get disabled for RC form
Build your own website (18)
Anti climbing killer
IP网络主动测评系统——X-Vision
How does win11 time display the day of the week? How does win11 display the day of the week today?
Use json Stringify() to realize deep copy, be careful, there may be a huge hole
Paint basic graphics with custompaint
Preparing for the interview and sharing experience
An in-depth understanding of fp/fn/precision/recall
随机推荐
MIT6.S081-Lab9 FS [2021Fall]
嵌入式开发:如何为项目选择合适的RTOS?
Remember an experience of using selectmany
Embedded development: how to choose the right RTOS for the project?
Matplotlib快速入门
L'enregistreur de disque dur NVR est connecté à easycvr par le Protocole GB 28181. Quelle est la raison pour laquelle l'information sur le canal de l'appareil n'est pas affichée?
An in-depth understanding of fp/fn/precision/recall
Lingyun going to sea | saihe & Huawei cloud: jointly help the sustainable development of cross-border e-commerce industry
Firefox browser installation impression notes clipping
How polardb-x does distributed database hotspot analysis
Add get disabled for RC form
PDF文档签名指南
Tsconfig of typescript TS basics JSON configuration options
如何选择合适的自动化测试工具?
Typeorm automatically generates entity classes
OpenGL jobs - shaders
Two methods of calling WCF service by C #
Can I open a stock account directly online now? Is it safe?
Which futures company is the safest to open a futures account?
The whole network "chases" Zhong Xuegao