OpenXML - 有没有办法去除 Microsoft Word 水印的冲刷功能?
OpenXML - Is there a way to remove the washout feature of Microsoft Word watermark?
在word文档的页眉添加水印时,会自动添加不透明度。我想去除不透明度,在检查 Microsoft Word 本身后,我发现可以通过取消选中 Washout 选项来去除它。可以使用 OpenXML 删除吗?
我遵循以下水印代码:
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using DocumentFormat.OpenXml;
using V = DocumentFormat.OpenXml.Vml;
using System.Diagnostics;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
using (WordprocessingDocument package = WordprocessingDocument.Open(@"C:\Users\*****\****.docx", true))
{
InsertCustomWatermark(package, @"C:\Users\*******\****.jpg");
}
}
private void InsertCustomWatermark(WordprocessingDocument package, string p)
{
SetWaterMarkPicture(p);
MainDocumentPart mainDocumentPart1 = package.MainDocumentPart;
if (mainDocumentPart1 != null)
{
mainDocumentPart1.DeleteParts(mainDocumentPart1.HeaderParts);
HeaderPart headPart1 = mainDocumentPart1.AddNewPart<HeaderPart>();
GenerateHeaderPart1Content(headPart1);
string rId = mainDocumentPart1.GetIdOfPart(headPart1);
ImagePart image = headPart1.AddNewPart<ImagePart>("image/jpeg", "rId999");
GenerateImagePart1Content(image);
IEnumerable<SectionProperties> sectPrs = mainDocumentPart1.Document.Body.Elements<SectionProperties>();
foreach (var sectPr in sectPrs)
{
sectPr.RemoveAllChildren<HeaderReference>();
sectPr.PrependChild<HeaderReference>(new HeaderReference() { Id = rId });
}
}
else
{
MessageBox.Show("alert");
}
}
private void GenerateHeaderPart1Content(HeaderPart headerPart1)
{
Header header1 = new Header();
Paragraph paragraph2 = new Paragraph();
Run run1 = new Run();
Picture picture1 = new Picture();
V.Shape shape1 = new V.Shape() { Id = "WordPictureWatermark75517470", Style = "position:absolute;left:0;text-align:left;margin-left:0;margin-top:0;width:415.2pt;height:456.15pt;z-index:-251656192;mso-position-horizontal:center;mso-position-horizontal-relative:margin;mso-position-vertical:center;mso-position-vertical-relative:margin", OptionalString = "_x0000_s2051", AllowInCell = false, Type = "#_x0000_t75" };
V.ImageData imageData1 = new V.ImageData() { Gain = "19661f", BlackLevel = "22938f", Title = "水印", RelationshipId = "rId999" };
shape1.Append(imageData1);
picture1.Append(shape1);
run1.Append(picture1);
paragraph2.Append(run1);
header1.Append(paragraph2);
headerPart1.Header = header1;
}
private void GenerateImagePart1Content(ImagePart imagePart1)
{
System.IO.Stream data = GetBinaryDataStream(imagePart1Data);
imagePart1.FeedData(data);
data.Close();
}
private string imagePart1Data = "";
private System.IO.Stream GetBinaryDataStream(string base64String)
{
return new System.IO.MemoryStream(System.Convert.FromBase64String(base64String));
}
public void SetWaterMarkPicture(string file)
{
FileStream inFile;
byte[] byteArray;
try
{
inFile = new FileStream(file, FileMode.Open, FileAccess.Read);
byteArray = new byte[inFile.Length];
long byteRead = inFile.Read(byteArray, 0, (int)inFile.Length);
inFile.Close();
imagePart1Data = Convert.ToBase64String(byteArray, 0, byteArray.Length);
}
catch (Exception ex)
{
Debug.Print(ex.Message);
}
}
}
}
我们已经找到了解决办法。我们比较了 2 个 .docx 文件的 XML 个文件,有水印和没有水印。不同之处在于,ImageData 中的 属性 Gain 和 BlackLevel 现在出现在带有水印的 .docx 中。我们尝试删除上述两个属性,但它起作用了。
在word文档的页眉添加水印时,会自动添加不透明度。我想去除不透明度,在检查 Microsoft Word 本身后,我发现可以通过取消选中 Washout 选项来去除它。可以使用 OpenXML 删除吗?
我遵循以下水印代码:
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using DocumentFormat.OpenXml;
using V = DocumentFormat.OpenXml.Vml;
using System.Diagnostics;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
using (WordprocessingDocument package = WordprocessingDocument.Open(@"C:\Users\*****\****.docx", true))
{
InsertCustomWatermark(package, @"C:\Users\*******\****.jpg");
}
}
private void InsertCustomWatermark(WordprocessingDocument package, string p)
{
SetWaterMarkPicture(p);
MainDocumentPart mainDocumentPart1 = package.MainDocumentPart;
if (mainDocumentPart1 != null)
{
mainDocumentPart1.DeleteParts(mainDocumentPart1.HeaderParts);
HeaderPart headPart1 = mainDocumentPart1.AddNewPart<HeaderPart>();
GenerateHeaderPart1Content(headPart1);
string rId = mainDocumentPart1.GetIdOfPart(headPart1);
ImagePart image = headPart1.AddNewPart<ImagePart>("image/jpeg", "rId999");
GenerateImagePart1Content(image);
IEnumerable<SectionProperties> sectPrs = mainDocumentPart1.Document.Body.Elements<SectionProperties>();
foreach (var sectPr in sectPrs)
{
sectPr.RemoveAllChildren<HeaderReference>();
sectPr.PrependChild<HeaderReference>(new HeaderReference() { Id = rId });
}
}
else
{
MessageBox.Show("alert");
}
}
private void GenerateHeaderPart1Content(HeaderPart headerPart1)
{
Header header1 = new Header();
Paragraph paragraph2 = new Paragraph();
Run run1 = new Run();
Picture picture1 = new Picture();
V.Shape shape1 = new V.Shape() { Id = "WordPictureWatermark75517470", Style = "position:absolute;left:0;text-align:left;margin-left:0;margin-top:0;width:415.2pt;height:456.15pt;z-index:-251656192;mso-position-horizontal:center;mso-position-horizontal-relative:margin;mso-position-vertical:center;mso-position-vertical-relative:margin", OptionalString = "_x0000_s2051", AllowInCell = false, Type = "#_x0000_t75" };
V.ImageData imageData1 = new V.ImageData() { Gain = "19661f", BlackLevel = "22938f", Title = "水印", RelationshipId = "rId999" };
shape1.Append(imageData1);
picture1.Append(shape1);
run1.Append(picture1);
paragraph2.Append(run1);
header1.Append(paragraph2);
headerPart1.Header = header1;
}
private void GenerateImagePart1Content(ImagePart imagePart1)
{
System.IO.Stream data = GetBinaryDataStream(imagePart1Data);
imagePart1.FeedData(data);
data.Close();
}
private string imagePart1Data = "";
private System.IO.Stream GetBinaryDataStream(string base64String)
{
return new System.IO.MemoryStream(System.Convert.FromBase64String(base64String));
}
public void SetWaterMarkPicture(string file)
{
FileStream inFile;
byte[] byteArray;
try
{
inFile = new FileStream(file, FileMode.Open, FileAccess.Read);
byteArray = new byte[inFile.Length];
long byteRead = inFile.Read(byteArray, 0, (int)inFile.Length);
inFile.Close();
imagePart1Data = Convert.ToBase64String(byteArray, 0, byteArray.Length);
}
catch (Exception ex)
{
Debug.Print(ex.Message);
}
}
}
}
我们已经找到了解决办法。我们比较了 2 个 .docx 文件的 XML 个文件,有水印和没有水印。不同之处在于,ImageData 中的 属性 Gain 和 BlackLevel 现在出现在带有水印的 .docx 中。我们尝试删除上述两个属性,但它起作用了。