Aspose.Words for .NET使用文档教程(3):使用文档之管理跟踪更改

Aspose.Words For .Net是一种高级Word文档处理API,用于执行各种文档管理和操作任务。API支持生成,修改,转换,呈现和打印文档,而无需在跨平台应用程序中直接使用Microsoft Word。

Aspose.Words For .Net是一种高级Word文档处理API,用于执行各种文档管理和操作任务。API支持生成,修改,转换,呈现和打印文档,而无需在跨平台应用程序中直接使用Microsoft Word。此外,API支持所有流行的Word处理文件格式,并允许将Word文档导出或转换为固定布局文件格式和最常用的图像/多媒体格式。

下载Aspose.Words for .NET最新试用版

访问样式


您可以使用Document.Styles属性获取文档中定义的样式集合。此集合包含文档中的内置和用户定义样式。可以通过名称/别名,样式标识符或索引获取特定样式。 下面的代码示例显示了如何访问文档中定义的样式集合。


// The path to the documents directory.string dataDir = RunExamples.GetDataDir_WorkingWithDocument();// Load the template document.Document doc = new Document(dataDir + "TestFile.doc");// Get styles collection from document.StyleCollection styles = doc.Styles;string styleName = "";// Iterate through all the styles.foreach (Style style in styles){    if (styleName == "")    {        styleName = style.Name;    }    else    {        styleName = styleName + ", " + style.Name;    }}

获取文档变量


您可以使用Document.Variables属性获取文档变量的集合。变量名称和值是字符串。下面的代码示例显示了如何枚举文档变量。


// The path to the documents directory.string dataDir = RunExamples.GetDataDir_WorkingWithDocument();// Load the template document.Document doc = new Document(dataDir + "TestFile.doc");string variables = "";foreach (KeyValuePairentry in doc.Variables){    string name = entry.Key.ToString();    string value = entry.Value.ToString();    if (variables == "")    {        // Do something useful.        variables = "Name: " + name + "," + "Value: {1}" + value;    }    else    {        variables = variables + "Name: " + name + "," + "Value: {1}" + value;    }}

管理跟踪更改


下面我们一起来了解Aspose.Words如何支持Microsoft Word的Track Changes功能。 Microsoft Word中的“跟踪更改”功能(也称为“审阅”)允许您跟踪用户对内容和格式的更改。启用此功能后,将直观地突出显示文档的所有插入,删除和修改元素,并提供有关更改者,时间和内容的信息。携带有关更改内容的信息的对象称为“跟踪更改”或“修订”。

Aspose.Words保留评论和修订

当您使用Aspose.Words打开Microsoft Word文档然后保存它时,将保留文档中的所有注释和修订。

接受修订

该Document.AcceptAllRevisions方法让你“接受”文档中的所有修订。调用此方法类似于在Microsoft Word中选择“接受所有更改”。Aspose.Words实际上会删除“删除修订版”的片段,保留“插入修订版”的片段并应用格式更改。请注意,此操作期间注释不受影响。在Aspose.Words中,您可以通过调用Document.AcceptAllRevisions方法接受对文档的跟踪更改。 下面的代码示例显示了如何接受文档中的所有跟踪更改。


// The path to the documents directory.string dataDir = RunExamples.GetDataDir_WorkingWithDocument();Document doc = new Document(dataDir + "Document.doc");// Start tracking and make some revisions.doc.StartTrackRevisions("Author");doc.FirstSection.Body.AppendParagraph("Hello world!");// Revisions will now show up as normal text in the output document.doc.AcceptAllRevisions();dataDir = dataDir + "Document.AcceptedRevisions_out.doc";doc.Save(dataDir);
以编程方式访问修订

可以在Word文档中插入,删除和格式化更改修订。Aspose.Words允许您以编程方式检测某些类型的修订.InsInsertRevisionIsDeleteRevisionIsMoveFromRevisionIsMoveToRevision属性可用于Run和Paragraph对象,允许您在更改跟踪时检测此对象是否在Microsoft Word中插入,删除或移动如果文档至少有一个版本,则Document.HasRevisions属性返回true。可以将Document.TrackRevisions属性设置为true,以指示是否启用Microsoft Word中的修订跟踪。


Document doc = new Document(dataDir + "Revisions.docx");ParagraphCollection paragraphs = doc.FirstSection.Body.Paragraphs;for (int i = 0; i < paragraphs.Count; i++){    if (paragraphs[i].IsMoveFromRevision)        Console.WriteLine("The paragraph {0} has been moved (deleted).", i);    if (paragraphs[i].IsMoveToRevision)        Console.WriteLine("The paragraph {0} has been moved (inserted).", i);}
访问修订组

Aspose.Words中的修订是一个文档节点的更改。相邻文档节点中的相同类型的一组顺序修订形成修订组。类似的修订组显示在MS Word的“审阅窗格”中。RevisionGroup类表示一组顺序的Revision对象。 下面的代码示例显示了如何获取修订版及其组。

Document doc = new Document(dataDir + "Revisions.docx");foreach (RevisionGroup group in doc.Revisions.Groups){    Console.WriteLine("{0}, {1}:", group.Author, group.RevisionType);    Console.WriteLine(group.Text);}
获得保护类型

你可以通过获取Document.ProtectionType属性的值来检索文档保护的类型。下面的代码示例显示了如何获取当前在文档中设置的保护类型。


// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NETDocument doc = new Document(inputFileName);ProtectionType protectionType = doc.ProtectionType;
以编程方式访问评论

注释在文档树中表示为Comment类的对象。您可以像Aspose.Words文档对象模型中的任何其他节点一样以编程方式添加,删除或修改注释。Comment是一个复合节点,可以包含构成注释文本的段落和表格。Comment类还提供对注释作者的姓名和首字母的访问。

设置视图选项


在Microsoft Word中打开文档时,可以控制文档的视图。例如,您可能希望切换到打印布局或更改缩放值。使用Document对象的Settings.ViewOptions属性设置视图选项。以下代码显示如何确保在Microsoft Word中打开时文档以50%缩放显示。

// The path to the documents directory.string dataDir = RunExamples.GetDataDir_WorkingWithDocument();// Load the template document.Document doc = new Document(dataDir + "TestFile.doc");// Set view option.doc.ViewOptions.ViewType = ViewType.PageLayout;doc.ViewOptions.ZoomPercent = 50;dataDir = dataDir + "TestFile.SetZoom_out.doc";// Save the finished document.doc.Save(dataDir);

为你推荐:Aspose专题 – Aspose最新资源合集


想要购买正版授权,或者获取更多Aspose.Words相关信息的朋友可以点击” 咨询在线客服“~

标签:

来源:慧都

声明:本站部分文章及图片转载于互联网,内容版权归原作者所有,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2019年4月27日
下一篇 2019年4月27日

相关推荐

发表回复

登录后才能评论