如何删除最后作者和Word的版本号文件
我有下面的代码删除最后作者和Word的版本号文件如何删除最后作者和Word的版本号文件
using Microsoft.Office.Core;
using Word = Microsoft.Office.Interop.Word;
using System.Reflection;
using System.IO;
...
Word.Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = false;
List<string> lstDocFile = new List<string>();
//Add doc files here
List<string> g_lstCheck = new List<string>();
//Add list check here "Last Author" and "Revision Number"
foreach (string path in lstDocFile)
{
oDoc = oWord.Documents.Open(path, ReadOnly: false);
foreach (string chkItem in g_lstCheck)
{
strValue = oDoc.BuiltInDocumentProperties[chkItem].Value;
if (!string.IsNullOrEmpty(strValue))
{
oDoc.BuiltInDocumentProperties[chkItem].Value = string.Empty);
}
}
oDoc.Close(Word.WdSaveOptions.wdSaveChanges);
}
oWord.Quit(Word.WdSaveOptions.wdDoNotSaveChanges);
运行的代码后,我希望最后的作者和版本号来为空字符串。但结果却是最后作者变成了我和版本号增加1。我明白发生,因为我用下面的代码保存Word文档
oDoc.Close(Word.WdSaveOptions.wdSaveChanges);
请帮我删除最后一个作者和版本号为C#。
*根据this article,作者Mr.Vivek Singh为我们提供了一些有用的代码。
**另外我们有微软的this library -Dsofile.dll。
这样走吧。
第1步:下载Dsofile.dll库(**),提取和获取文件Interop.Dsofile.dll(检索日期2017年8月8日)
第2步:添加引用文件的互操作。 Dsofile.dll为您的C#项目。
3步:使用此代码(从第一条编辑* - 由于维韦克·辛格,我只是删除单词类在OleDocumentPropertiesClass防止生成错误,并编辑了一下,解决这个问题)
string fileName = "";//Add the full path of the Word file
OleDocumentProperties myDSOOleDocument = new OleDocumentProperties();
myDSOOleDocument.Open(fileName, false,
DSOFile.dsoFileOpenOptions.dsoOptionOpenReadOnlyIfNoWriteAccess);
myDSOOleDocument.SummaryProperties.LastSavedBy = string.Empty;
//myDSOOleDocument.SummaryProperties.RevisionNumber = string.Empty; //This can't be edit -readonly
myDSOOleDocument.Save();
myDSOOleDocument.Close();
无论如何,我无法编辑RevisionNumber因为它是只读的。好吧,我只能对我能得到的东西感到满意。
对于.docx(Open Xml)文件,最简单的方法是使用官方Open XML SDK nuget package。有了这一点,很容易操作文档属性:
// open for read write
using (var package = WordprocessingDocument.Open("myfile.docx", true))
{
// modify properties
package.PackageProperties.Creator = null;
package.PackageProperties.LastModifiedBy = null;
package.PackageProperties.Revision = null;
}
对于.DOC(字.97-> 2003)的文件,这里是一个小的C#方法,将能够去除属性(在技术上存储completely differently):
RemoveProperties("myfile.doc", SummaryInformationFormatId, PIDSI_AUTHOR, PIDSI_REVNUMBER, PIDSI_LASTAUTHOR);
...
public static void RemoveProperties(string filePath, Guid propertySet, params int[] ids)
{
if (filePath == null)
throw new ArgumentNullException(nameof(filePath));
if (ids == null || ids.Length == 0)
return;
int hr = StgOpenStorageEx(filePath, STGM.STGM_DIRECT_SWMR | STGM.STGM_READWRITE | STGM.STGM_SHARE_DENY_WRITE, STGFMT.STGFMT_ANY, 0, IntPtr.Zero, IntPtr.Zero, typeof(IPropertySetStorage).GUID, out IPropertySetStorage setStorage);
if (hr != 0)
throw new Win32Exception(hr);
try
{
hr = setStorage.Open(propertySet, STGM.STGM_READWRITE | STGM.STGM_SHARE_EXCLUSIVE, out IPropertyStorage storage);
if (hr != 0)
{
const int STG_E_FILENOTFOUND = unchecked((int)0x80030002);
if (hr == STG_E_FILENOTFOUND)
return;
throw new Win32Exception(hr);
}
var props = new List<PROPSPEC>();
foreach (int id in ids)
{
var prop = new PROPSPEC();
prop.ulKind = PRSPEC.PRSPEC_PROPID;
prop.union.propid = id;
props.Add(prop);
}
storage.DeleteMultiple(props.Count, props.ToArray());
storage.Commit(0);
}
finally
{
Marshal.ReleaseComObject(setStorage);
}
}
// "The Summary Information Property Set"
// https://msdn.microsoft.com/en-us/library/windows/desktop/aa380376.aspx
public static readonly Guid SummaryInformationFormatId = new Guid("F29F85E0-4FF9-1068-AB91-08002B27B3D9");
public const int PIDSI_AUTHOR = 4;
public const int PIDSI_LASTAUTHOR = 8;
public const int PIDSI_REVNUMBER = 9;
[Flags]
private enum STGM
{
STGM_READ = 0x00000000,
STGM_READWRITE = 0x00000002,
STGM_SHARE_DENY_NONE = 0x00000040,
STGM_SHARE_DENY_WRITE = 0x00000020,
STGM_SHARE_EXCLUSIVE = 0x00000010,
STGM_DIRECT_SWMR = 0x00400000
}
private enum STGFMT
{
STGFMT_STORAGE = 0,
STGFMT_FILE = 3,
STGFMT_ANY = 4,
STGFMT_DOCFILE = 5
}
[StructLayout(LayoutKind.Sequential)]
private struct PROPSPEC
{
public PRSPEC ulKind;
public PROPSPECunion union;
}
[StructLayout(LayoutKind.Explicit)]
private struct PROPSPECunion
{
[FieldOffset(0)]
public int propid;
[FieldOffset(0)]
public IntPtr lpwstr;
}
private enum PRSPEC
{
PRSPEC_LPWSTR = 0,
PRSPEC_PROPID = 1
}
[DllImport("ole32.dll")]
private static extern int StgOpenStorageEx([MarshalAs(UnmanagedType.LPWStr)] string pwcsName, STGM grfMode, STGFMT stgfmt, int grfAttrs, IntPtr pStgOptions, IntPtr reserved2, [MarshalAs(UnmanagedType.LPStruct)] Guid riid, out IPropertySetStorage ppObjectOpen);
[Guid("0000013A-0000-0000-C000-000000000046"), InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
private interface IPropertySetStorage
{
void Unused1();
[PreserveSig]
int Open([MarshalAs(UnmanagedType.LPStruct)] Guid rfmtid, STGM grfMode, out IPropertyStorage storage);
}
[Guid("00000138-0000-0000-C000-000000000046"), InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
private interface IPropertyStorage
{
void Unused1();
void Unused2();
void DeleteMultiple(int cpspec, [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0)] PROPSPEC[] rgpspec);
void Unused4();
void Unused5();
void Unused6();
void Commit(uint grfCommitFlags);
// rest ommited
}
非常感谢你,我看到Dsofile.dll EULA说:“用户应承担全部风险“,”使用...自负风险“,...。所以我使用Dsofile.dll时有点担心。所以我想问,Open XML SDK Nuget包是否比Dsofile.dll更安全。 – 123iamking
@ 123iamking - 是的,这是一个官方的开源Microsoft包:https://github.com/OfficeDev/Open-XML-SDK –
有一点需要注意的是必须添加WindowsBase.dll来修复构建错误:https:// stackoverflow.com/a/7814593/4608491 – 123iamking
我指的这个解决方案比OpenXML的,因为它更简单删除这两个新老文档和Excel类型 – 123iamking