文檔半島外圍網(wǎng)上直營(yíng)>>Aspose中文文檔>>從文檔中刪除隱藏文本
                從文檔中刪除隱藏文本
Aspose.Words是一種高級(jí)Word文檔處理API,用于執(zhí)行各種文檔管理和操作任務(wù)。API支持生成,修改,轉(zhuǎn)換,呈現(xiàn)和打印文檔,而無(wú)需在跨平臺(tái)應(yīng)用程序中直接使用Microsoft Word。
Aspose API支持流行文件格式處理,并允許將各類文檔導(dǎo)出或轉(zhuǎn)換為固定布局文件格式和最常用的圖像/多媒體格式。
使用Aspose.Words
在 Aspose.Words 中,使用ParagraphBreakFont和Hidden屬性查找所有隱藏文本。
以下代碼示例演示如何從 Word 文檔中刪除隱藏文本:
Document doc = new Document(MyDir + "Remove hidden text.docx");
foreach (Paragraph par in doc.GetChildNodes(NodeType.Paragraph, true))
{
par.ParagraphBreakFont.Hidden = false;
foreach (Run run in par.GetChildNodes(NodeType.Run, true))
{
if (run.Font.Hidden)
run.Font.Hidden = false;
}
}
doc.Save(ArtifactsDir + "Remove hidden text - Aspose.Words.docx");
	點(diǎn)擊復(fù)制
	
使用 Open XML SDK
需要添加的命名空間:
using System.IO; using System.Xml; using DocumentFormat.OpenXml.Packaging; using NUnit.Framework;
點(diǎn)擊復(fù)制
以下代碼示例演示如何從 Word 文檔中刪除隱藏文本:
public void RemoveHiddenTextFeature()
{
const string wordmlNamespace = "http://schemas.openxmlformats.org/wordprocessingml/2006/main";
using (WordprocessingDocument wdDoc = WordprocessingDocument.Open(MyDir + "Remove hidden text.docx", true))
{
// Manage namespaces to perform XPath queries.
NameTable nt = new NameTable();
XmlNamespaceManager nsManager = new XmlNamespaceManager(nt);
nsManager.AddNamespace("w", wordmlNamespace);
// Get the document part from the package.
// Load the XML in the document part into an XmlDocument instance.
XmlDocument xdoc = new XmlDocument(nt);
xdoc.Load(wdDoc.MainDocumentPart.GetStream());
XmlNodeList hiddenNodes = xdoc.SelectNodes("http://w:vanish", nsManager);
foreach (XmlNode hiddenNode in hiddenNodes)
{
XmlNode topNode = hiddenNode.ParentNode.ParentNode;
XmlNode topParentNode = topNode.ParentNode;
topParentNode.RemoveChild(topNode);
if (!(topParentNode.HasChildNodes))
topParentNode.ParentNode.RemoveChild(topParentNode);
}
using (Stream stream = File.Create(ArtifactsDir + "Remove hidden text - OpenXML.docx"))
{
xdoc.Save(stream);
}
}
}
	點(diǎn)擊復(fù)制
	

 QQ交談
QQ交談 在線咨詢
在線咨詢 
                 
                
 渝公網(wǎng)安備
            50010702500608號(hào)
渝公網(wǎng)安備
            50010702500608號(hào)
             
            
 客服熱線
客服熱線