עבודה עם הערות
רשימות קריאה בהן מופיע Microsoft Word לתעד את המיקומים או החלקים שאתה שם וזיהוי עבור התייחסות עתידית. לדוגמה, אתה יכול להשתמש בסימן כדי לזהות טקסט שאתה רוצה לשחזר מאוחר יותר. במקום לגלול באמצעות המסמך כדי לאתר את הטקסט, אתה יכול ללכת אליו באמצעות תיבת הדו-שיח של Bookmark.
עם Aspose.Words, באפשרותך להשתמש בהערות ספרים בדוחות או במסמכים כדי להכניס נתונים מסוימים לסימן הספר או ליישם פורמט מיוחד לתוכן שלו. אתה יכול גם להשתמש סמנים כדי לאחזר טקסט ממקום מסוים במסמך שלך.
הפעולות שניתן לבצע עם סימניות באמצעות Aspose.Words זהה לאלה שאתה יכול לבצע באמצעות Microsoft Word. אתה יכול להוסיף סימן חדש, למחוק, לעבור לסימן ספר, לקבל או להגדיר שם סימן, לקבל או להגדיר טקסט סגור בו.
תגית: Bookmark
שימוש בשימוש startBookmark ו endBookmark כדי ליצור סימן על ידי סימון ההתחלה והסוף שלו, בהתאמה. אל תשכחו להעביר את אותו שם הספר לשתי השיטות. סימני ספר במסמך יכולים לחפוף ולעבור כל טווח. סימני ספר שנוצרו בטעות או סימני ספר עם שמות כפולים יתעלמו כאשר המסמך נשמר.
דוגמה לקוד הבא מראה כיצד ליצור סימן חדש:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(CreateBookmark.class); | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
builder.startBookmark("MyBookmark"); | |
builder.writeln("Text inside a bookmark."); | |
builder.endBookmark("MyBookmark"); | |
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.getOutlineOptions().setDefaultBookmarksOutlineLevel(1); | |
options.getOutlineOptions().setDefaultBookmarksOutlineLevel(2); | |
doc.save(dataDir + "output.pdf", options); | |
System.out.println("\nBookmark created successfully."); |
הערות ספרים
לפעמים יש צורך להשיג אוסף של סימן ספר כדי להסס באמצעות סימניות או למטרות אחרות. השתמש Node.getRange רכוש שנחשף על ידי כל מסמך אשר מחזיר Range האובייקט המייצג את חלקו של המסמך הכלול בצומת זה. השתמש באובייקט הזה כדי להחזיר BookmarkCollection לאחר מכן השתמש באינדקס האוסף כדי לקבל סימן ספר מסוים.
הדוגמה הבאה של הקוד מראה כיצד להשיג סימני ספר מאוסף של סממנים:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(AccessBookmarks.class); | |
Document doc = new Document(dataDir + "Bookmark.doc"); | |
Bookmark bookmark1 = doc.getRange().getBookmarks().get(0); | |
Bookmark bookmark = doc.getRange().getBookmarks().get("MyBookmark"); | |
doc.save(dataDir + "output.doc"); | |
System.out.println("\nTable bookmarked successfully.\nFile saved at " + dataDir); |
הדוגמה הבאה של הקוד מראה כיצד להגיע או להגדיר שם סימן וטקסט:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(BookmarkNameAndText.class); | |
Document doc = new Document(dataDir + "Bookmark.doc"); | |
// Use the indexer of the Bookmarks collection to obtain the desired bookmark. | |
Bookmark bookmark = doc.getRange().getBookmarks().get("MyBookmark"); | |
// Get the name and text of the bookmark. | |
String name = bookmark.getName(); | |
String text = bookmark.getText(); | |
// Set the name and text of the bookmark. | |
bookmark.setName("RenamedBookmark"); | |
bookmark.setText("This is a new bookmarked text."); | |
System.out.println("\nBookmark name and text set successfully."); |
דוגמה לקוד הבא מראה כיצד לסמן שולחן:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
//Create empty document | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
// We call this method to start building the table. | |
builder.startTable(); | |
builder.insertCell(); | |
// Start bookmark here after calling InsertCell | |
builder.startBookmark("MyBookmark"); | |
builder.write("Row 1, Cell 1 Content."); | |
// Build the second cell | |
builder.insertCell(); | |
builder.write("Row 1, Cell 2 Content."); | |
// Call the following method to end the row and start a new row. | |
builder.endRow(); | |
// Build the first cell of the second row. | |
builder.insertCell(); | |
builder.write("Row 2, Cell 1 Content"); | |
// Build the second cell. | |
builder.insertCell(); | |
builder.write("Row 2, Cell 2 Content."); | |
builder.endRow(); | |
// Signal that we have finished building the table. | |
builder.endTable(); | |
//End of bookmark | |
builder.endBookmark("MyBookmark"); | |
doc.save(dataDir + "output.doc"); | |
System.out.println("\nTable bookmarked successfully.\nFile saved at " + dataDir); |
אם תשנה את שמו של סימן ספר בשם שכבר קיים במסמך, לא ייגרם טעות ורק סימן הספר הראשון יישמר בעת שמירת המסמך.
שים לב כי כמה סימני ספר במסמך מוקצה ליצירת שדות. לעבור לסימן ספר כזה ולהכניס טקסט שם מכניס את הטקסט לתוך קוד שדה הטופס. למרות שזה לא יסולא את שדה הטופס, הטקסט המוכנס לא יהיה גלוי כי זה הופך לחלק קוד השדה.
הדוגמה הבאה של הקוד מראה כיצד לגשת לעמודות של השולחן המסומן:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// Create empty document | |
Document doc = new Document(dataDir + "Bookmark.Table_out.doc"); | |
for (Bookmark bookmark : doc.getRange().getBookmarks()) | |
{ | |
System.out.printf("Bookmark: {0}{1}", bookmark.getName(), bookmark.isColumn() ? " (Column)" : ""); | |
if (bookmark.isColumn()) | |
{ | |
Row row = (Row) bookmark.getBookmarkStart().getAncestor(NodeType.ROW); | |
if (row != null && bookmark.getFirstColumn() < row.getCells().getCount()) | |
System.out.print(row.getCells().get(bookmark.getFirstColumn()).getText()); | |
} | |
} |
לעבור לסימן ספר
אם אתה צריך להוסיף תוכן עשיר (לא רק טקסט רגיל) לתוך סימן ספר, אתה צריך להשתמש moveToBookmark להעביר את הcursor לסימן הספר ולאחר מכן להשתמש DocumentBuilder שיטות ונכסים כדי להוסיף תוכן.
תגית: Hide Bookmark
את כל סימן הספר (כולל תוכן מסומן) ניתן לשער בתוך החלק האמיתי של החלק האמיתי של IF
שדה באמצעות Aspose.Words. זה יכול להיות באופן כזה IF
שדה מכיל שדה Merge מקונן בביטוי (Left of Operator) ובהתאם לערך של שדה Merge, השדה. IF
שדה מראה או מסתיר את התוכן של סימן ספר במסמך Word.
דוגמה לקוד הבא מראה כיצד להציג / להסתיר סימני ספר.
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(ShowHideBookmarks.class); | |
Document doc = new Document(dataDir + "Bookmark.doc"); | |
showHideBookmarkedContent(doc,"MyBookmark",false); | |
doc.save(dataDir + "Updated_Document.docx"); | |
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
public static void showHideBookmarkedContent(Document doc, String bookmarkName, boolean showHide) throws Exception { | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
Bookmark bm = doc.getRange().getBookmarks().get(bookmarkName); | |
builder.moveToDocumentEnd(); | |
// {IF "{MERGEFIELD bookmark}" = "true" "" ""} | |
Field field = builder.insertField("IF \"", null); | |
builder.moveTo(field.getStart().getNextSibling().getNextSibling()); | |
builder.insertField("MERGEFIELD " + bookmarkName + "", null); | |
builder.write("\" = \"true\" "); | |
builder.write("\""); | |
builder.write("\""); | |
builder.write(" \"\""); | |
Node currentNode = field.getStart(); | |
boolean flag = true; | |
while (currentNode != null && flag) { | |
if (currentNode.getNodeType() == NodeType.RUN) | |
if (currentNode.toString(SaveFormat.TEXT).trim().equals("\"")) | |
flag = false; | |
Node nextNode = currentNode.getNextSibling(); | |
bm.getBookmarkStart().getParentNode().insertBefore(currentNode, bm.getBookmarkStart()); | |
currentNode = nextNode; | |
} | |
Node endNode = bm.getBookmarkEnd(); | |
flag = true; | |
while (currentNode != null && flag) { | |
if (currentNode.getNodeType() == NodeType.FIELD_END) | |
flag = false; | |
Node nextNode = currentNode.getNextSibling(); | |
bm.getBookmarkEnd().getParentNode().insertAfter(currentNode, endNode); | |
endNode = currentNode; | |
currentNode = nextNode; | |
} | |
doc.getMailMerge().execute(new String[]{bookmarkName}, new Object[]{showHide}); | |
//In case, you do not want to use MailMerge then you may use the following lines of code. | |
//builder.moveToMergeField(bookmarkName); | |
//builder.write(showHide ? "true" : "false"); | |
} |