Làm việc với Dấu trang
Dấu trang xác định trong tài liệu Microsoft Word các vị trí hoặc đoạn mà bạn đặt tên và xác định để tham khảo trong tương lai. Ví dụ: bạn có thể sử dụng dấu trang để xác định văn bản mà bạn muốn sửa lại sau. Thay vì cuộn qua tài liệu để định vị văn bản, bạn có thể truy cập văn bản đó bằng cách sử dụng hộp thoại Dấu trang.
Các hành động có thể được thực hiện với dấu trang bằng Aspose.Words cũng giống như những hành động bạn có thể thực hiện bằng Microsoft Word. Bạn có thể chèn dấu trang mới, xóa, di chuyển đến dấu trang, lấy hoặc đặt tên dấu trang, lấy hoặc đặt văn bản kèm theo trong đó.
Chèn một dấu trang
Sử dụng StartBookmark và EndBookmark để tạo dấu trang bằng cách đánh dấu điểm bắt đầu và kết thúc tương ứng. Đừng quên chuyển tên dấu trang giống nhau cho cả hai phương pháp. Dấu trang trong tài liệu có thể chồng lên nhau và trải dài bất kỳ phạm vi nào. Dấu trang bị định dạng sai hoặc dấu trang có tên trùng lặp sẽ bị bỏ qua khi tài liệu được lưu.
Ví dụ mã sau đây cho thấy cách tạo dấu trang mới:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir_WorkingWithBookmarks(); | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
builder.StartBookmark("My Bookmark"); | |
builder.Writeln("Text inside a bookmark."); | |
builder.StartBookmark("Nested Bookmark"); | |
builder.Writeln("Text inside a NestedBookmark."); | |
builder.EndBookmark("Nested Bookmark"); | |
builder.Writeln("Text after Nested Bookmark."); | |
builder.EndBookmark("My Bookmark"); | |
PdfSaveOptions options = new PdfSaveOptions(); | |
options.OutlineOptions.BookmarksOutlineLevels.Add("My Bookmark", 1); | |
options.OutlineOptions.BookmarksOutlineLevels.Add("Nested Bookmark", 2); | |
dataDir = dataDir + "Create.Bookmark_out.pdf"; | |
doc.Save(dataDir, options); |
Lấy dấu trang
Đôi khi cần có một bộ sưu tập dấu trang để duyệt qua các dấu trang hoặc cho các mục đích khác. Sử dụng thuộc tính Node.Range được hiển thị bởi bất kỳ nút tài liệu nào trả về đối tượng Range biểu thị phần tài liệu có trong nút này. Sử dụng đối tượng này để truy xuất BookmarkCollection và sau đó sử dụng bộ chỉ mục bộ sưu tập để lấy dấu trang cụ thể.
Ví dụ mã sau đây cho thấy cách lấy dấu trang từ bộ sưu tập dấu trang:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir_WorkingWithBookmarks(); | |
Document doc = new Document(dataDir + "Bookmarks.doc"); | |
// By index. | |
Bookmark bookmark1 = doc.Range.Bookmarks[0]; | |
// By name. | |
Bookmark bookmark2 = doc.Range.Bookmarks["Bookmark2"]; |
Ví dụ về mã sau đây cho biết cách nhận hoặc đặt tên và văn bản dấu trang:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir_WorkingWithBookmarks(); | |
Document doc = new Document(dataDir + "Bookmark.doc"); | |
// Use the indexer of the Bookmarks collection to obtain the desired bookmark. | |
Bookmark bookmark = doc.Range.Bookmarks["MyBookmark"]; | |
// Get the name and text of the bookmark. | |
string name = bookmark.Name; | |
string text = bookmark.Text; | |
// Set the name and text of the bookmark. | |
bookmark.Name = "RenamedBookmark"; | |
bookmark.Text = "This is a new bookmarked text."; |
Ví dụ mã sau đây cho thấy cách đánh dấu một bảng:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
// Create empty document | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
Table table = builder.StartTable(); | |
// Insert a cell | |
builder.InsertCell(); | |
// Start bookmark here after calling InsertCell | |
builder.StartBookmark("MyBookmark"); | |
builder.Write("This is row 1 cell 1"); | |
// Insert a cell | |
builder.InsertCell(); | |
builder.Write("This is row 1 cell 2"); | |
builder.EndRow(); | |
// Insert a cell | |
builder.InsertCell(); | |
builder.Writeln("This is row 2 cell 1"); | |
// Insert a cell | |
builder.InsertCell(); | |
builder.Writeln("This is row 2 cell 2"); | |
builder.EndRow(); | |
builder.EndTable(); | |
// End of bookmark | |
builder.EndBookmark("MyBookmark"); | |
dataDir = dataDir + "Bookmark.Table_out.doc"; | |
doc.Save(dataDir); |
Nếu bạn thay đổi tên của dấu trang thành tên đã tồn tại trong tài liệu thì sẽ không có lỗi xảy ra và chỉ dấu trang đầu tiên sẽ được lưu khi bạn lưu tài liệu.
Lưu ý rằng một số dấu trang trong tài liệu được gán cho các trường biểu mẫu. Di chuyển đến một dấu trang như vậy và chèn văn bản vào đó sẽ chèn văn bản vào mã trường biểu mẫu. Mặc dù điều này sẽ không làm mất hiệu lực trường biểu mẫu nhưng văn bản được chèn sẽ không hiển thị vì nó trở thành một phần của mã trường.
Ví dụ mã sau đây cho thấy cách truy cập các cột của bảng được đánh dấu trang:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
// Create empty document | |
Document doc = new Document(dataDir + "Bookmark.Table_out.doc"); | |
foreach (Bookmark bookmark in doc.Range.Bookmarks) | |
{ | |
Console.WriteLine("Bookmark: {0}{1}", bookmark.Name, bookmark.IsColumn ? " (Column)" : ""); | |
if (bookmark.IsColumn) | |
{ | |
Row row = bookmark.BookmarkStart.GetAncestor(NodeType.Row) as Row; | |
if (row != null && bookmark.FirstColumn < row.Cells.Count) | |
Console.WriteLine(row.Cells[bookmark.FirstColumn].GetText().TrimEnd(ControlChar.CellChar)); | |
} | |
} |
Di chuyển đến một dấu trang
Nếu cần chèn nội dung đa dạng thức (không chỉ văn bản thuần túy) vào dấu trang, bạn nên sử dụng MoveToBookmark để di chuyển con trỏ đến dấu trang, sau đó sử dụng các phương thức và thuộc tính DocumentBuilder’s để chèn nội dung.
Hiển thị Ẩn nội dung đánh dấu
Toàn bộ Dấu trang (bao gồm nội dung được đánh dấu) có thể được gói gọn trong phần True của trường IF
bằng Aspose.Words. Có thể theo cách mà trường IF
chứa Trường Hợp nhất lồng nhau trong biểu thức (Bên trái của Toán tử) và tùy thuộc vào giá trị của Trường Hợp nhất, trường IF
hiển thị hoặc ẩn nội dung của Dấu trang trong Tài liệu Word.
Ví dụ mã sau đây cho thấy cách hiển thị/ẩn dấu trang:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir_WorkingWithBookmarks(); | |
Document doc = new Document(dataDir + "Bookmarks.doc"); | |
ShowHideBookmarkedContent(doc, "Bookmark2", false); | |
doc.Save(dataDir + "Updated_Document.doc"); | |
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
public static void ShowHideBookmarkedContent(Document doc, String bookmarkName, bool showHide) | |
{ | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
Bookmark bm = doc.Range.Bookmarks[bookmarkName]; | |
builder.MoveToDocumentEnd(); | |
// {IF "{MERGEFIELD bookmark}" = "true" "" ""} | |
Field field = builder.InsertField("IF \"", null); | |
builder.MoveTo(field.Start.NextSibling); | |
builder.InsertField("MERGEFIELD " + bookmarkName + "", null); | |
builder.Write("\" = \"true\" "); | |
builder.Write("\""); | |
builder.Write("\""); | |
builder.Write(" \"\""); | |
Node currentNode = field.Start; | |
bool flag = true; | |
while (currentNode != null && flag) | |
{ | |
if (currentNode.NodeType == NodeType.Run) | |
if (currentNode.ToString(SaveFormat.Text).Trim().Equals("\"")) | |
flag = false; | |
Node nextNode = currentNode.NextSibling; | |
bm.BookmarkStart.ParentNode.InsertBefore(currentNode, bm.BookmarkStart); | |
currentNode = nextNode; | |
} | |
Node endNode = bm.BookmarkEnd; | |
flag = true; | |
while (currentNode != null && flag) | |
{ | |
if (currentNode.NodeType == NodeType.FieldEnd) | |
flag = false; | |
Node nextNode = currentNode.NextSibling; | |
bm.BookmarkEnd.ParentNode.InsertAfter(currentNode, endNode); | |
endNode = currentNode; | |
currentNode = nextNode; | |
} | |
doc.MailMerge.Execute(new String[] { bookmarkName }, new Object[] { showHide }); | |
//MailMerge can be avoided by using the following | |
//builder.MoveToMergeField(bookmarkName); | |
//builder.Write(showHide ? "true" : "false"); | |
} |