SDK OpenXML cung cấp các lớp Lock
và LockingValues
liệt kê cho việc thiết lập trình các tùy chọn:
- Kiểm soát nội dung không thể bị xóa và
- Nội dung không thể chỉnh sửa
Vì vậy, để đặt hai tùy chọn đó thành "false" (LockingValues.Unlocked
), tìm kiếm tất cả các phần tử SdtElement
trong tài liệu và đặt Val
thuộc tính đến LockingValues.Unlocked
.
Đoạn code dưới đây cho thấy một ví dụ:
static void UnlockAllSdtContentElements()
{
using (WordprocessingDocument wordDoc =
WordprocessingDocument.Open(@"c:\temp\myword.docx", true))
{
IEnumerable<SdtElement> elements =
wordDoc.MainDocumentPart.Document.Descendants<SdtElement>();
foreach (SdtElement elem in elements)
{
if (elem.SdtProperties != null)
{
Lock l = elem.SdtProperties.ChildElements.First<Lock>();
if (l == null)
{
continue;
}
if (l.Val == LockingValues.SdtContentLocked)
{
Console.Out.WriteLine("Unlock content element...");
l.Val = LockingValues.Unlocked;
}
}
}
}
}
static void Main(string[] args)
{
UnlockAllSdtContentElements();
}
Nên được thêm làm nhận xét cho giải pháp có liên quan –
Xin chào @OriPrice, tôi không thể nhận xét giải pháp vì danh tiếng của tôi dưới 50. Trân trọng. –