本文介绍了使用 OpenXml & 去除 word 中的水印C# 损坏文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试了以下代码块从文档中删除水印
I have tried following code block to delete the watermark from the document
代码 1:
private static void DeleteCustomWatermark(WordprocessingDocument package, string watermarkId)
{
MainDocumentPart maindoc = package.MainDocumentPart;
if(maindoc!=null)
{
var headers = maindoc.GetPartsOfType<HeaderPart>();
if(headers!=null)
{
var head = headers.First(); //we are sure that this header part contains the Watermark with id=watermarkId
var watermark = head.GetPartById(watermarkId);
if(watermark!=null)
head.DeletePart(watermark);
}
}
}
代码 2:
public static void DeleteCustomWatermark(WordProcessingDocument package, string headerId)
{
//headerId is the id of the header section which contains the watermark
MainDocumentPart = maindoc = package.MainDocumentPart;
if(maindoc!=null)
{
var header = maindoc.HeaderParts.First(i=>maindoc.GetIdOfPart(i).Equals(headerId));
if(header!=null)
maindoc.DeletePart(header)
}
}
这两个代码块我都试过了.它会删除水印,但会使文档损坏.在这之后我需要恢复.恢复后,文档很好.但我想要适当的解决方案,以便我可以使用 C# 代码删除水印,而不会损坏文档.请帮忙.
I have tried both the code blocks. it removes watermark but leaves the document corrupted. I need to recover after this. After recovery the docs are fine. But I want proper solution so that I can remove watermark with C# code without leaving the document corrupted. Please help.
谢谢
推荐答案
您还需要删除标题部分中的图片"或绘图".
You also need to remove the "Picture" or "Drawing" in the header parts.
例如
List<Picture> pictures = new List<Picture>(headerPart.RootElement.Descendants<Picture>());
...
foreach(Picture p in pictures) {
p.Remove();
}
...
headerPart.DeleteParts(imagePartList);
这篇关于使用 OpenXml & 去除 word 中的水印C# 损坏文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!