עבודה עם סגנונות ונושאים

The The The StyleCollection הכיתה משמשת לניהול הגדרות בנויות וליישם הגדרות מוגדרות למשתמש לסגנונות.

כיצד להפיק תוכן מבוסס על סגנונות

ברמה פשוטה, החזרת התוכן מבוסס על סגנונות מ מסמך Word יכול להיות שימושי לזהות, רשימה ספירה פסקאות וריצה של טקסט מעוצב בסגנון ספציפי. לדוגמה, ייתכן שיהיה עליך לזהות סוגים מסוימים של תוכן במסמך, כגון דוגמאות, כותרים, מילות מפתח, שמות דמויות ומחקרי מקרה.

כדי לקחת את זה כמה צעדים קדימה, זה יכול לשמש גם כדי למנף את המבנה של המסמך, המוגדר על ידי הסגנונות שהוא משתמש, כדי לנסח מחדש את המסמך עבור פלט אחר, כגון HTML. למעשה, כך נוצר תיעוד As נניח, שם Aspose.Words למבחן. כלי שנבנה באמצעות Aspose.Words לוקח את מקור מסמכי Word ומתפצל אותם לנושאים ברמות כותרת מסוימות. קובץ XML מיוצר באמצעות Aspose.Words משמש לבניית עץ הניווט שאתה יכול לראות בצד שמאל. ואז Aspose.Words הופך כל נושא ל-HTML. הפתרון לחידוש הטקסט בפורמט עם סגנונות ספציפיים במסמך Word הוא בדרך כלל כלכלי ופשוט באמצעות שימוש פשוט באמצעות שימוש Aspose.Words.

להראות כמה בקלות Aspose.Words טיפול חוזר תוכן מבוסס על סגנונות, בואו נסתכל על דוגמה. בדוגמה זו, אנו הולכים לאחזר טקסט מעוצב עם סגנון סעיף מסוים וסגנון אופי של מסמך מדגם Word.

ברמה גבוהה, זה יהיה כרוך:

1.1 1. פתיחת מסמך Word באמצעות Document מעמד. 1.1 1. מקבל אוספים של כל הסעיפים וכל פועל במסמך. 1.1 1. בחר רק את הסעיפים הנדרשים ורץ.

באופן ספציפי, אנו נחזיר טקסט מעוצב בסגנון “Heading 1” ס"ק ואת סגנון האופי “Intense Emphasis” מ מסמך זה מדגם Word

working-with-styles-aspose-words-java-1

במסמך מדגם זה, הטקסט הפורמטיבי בסגנון “Heading 1” סעיף הוא “Insert Tab”, “Quick Styles” ו-“Theme”, והטקסט המתואם לסגנון האופי “הדגשה” הוא מספר מקרים של כחול, מוקרן, טקסט נועז כמו “מגורים” ו"כל המראה".

יישום של שאילתה המבוססת על סגנון הוא פשוט למדי. Aspose.Words מסמך מודל אובייקט, כפי שהוא פשוט משתמש בכלים שכבר נמצאים במקום. שתי שיטות ייצוגיות יושמו לפתרון זה:

1.1 1. ParagraphsByStyleName - - - שיטה זו משחזרת מערך של פסקאות אלה במסמך שיש לו שם סגנון מסוים. 1.1 1. RunsByStyleName - - - שיטה זו משחזרת מערך של אלה רצים במסמך שיש להם שם סגנון מסוים.

שתי השיטות הללו דומות מאוד, ההבדלים היחידים הם סוגי הצומת והייצוג של פרטי הסגנון בתוך הסעיף ועושים צמתים. הנה יישום של Paragraphs ByStyle שם המוצג בדוגמה הקוד שניתן להלן כדי למצוא את כל פסקאות מעוצבות בסגנון שצוין.

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
public static ArrayList<Paragraph> paragraphsByStyleName(Document doc, String styleName) throws Exception {
// Create an array to collect paragraphs of the specified style.
ArrayList<Paragraph> paragraphsWithStyle = new ArrayList();
// Get all paragraphs from the document.
NodeCollection paragraphs = doc.getChildNodes(NodeType.PARAGRAPH, true);
// Look through all paragraphs to find those with the specified style.
for (Paragraph paragraph : (Iterable<Paragraph>) paragraphs) {
if (paragraph.getParagraphFormat().getStyle().getName().equals(styleName))
paragraphsWithStyle.add(paragraph);
}
return paragraphsWithStyle;
}

יישום זה גם משתמש Document.getChildNodes() שיטת Document שיעור, אשר מחזיר אוסף של כל בלוטות הילד המיידיות.

כמו כן, ראוי לציין כי אוסף פסקאות אינו יוצר כותרת מיידית כי פסקאות טעונים באוסף זה רק כאשר אתה ניגש פריטים בהם. אז, כל מה שאתה צריך לעשות הוא לעבור את האוסף, באמצעות מפעיל סטנדרטי עבור כל אחד ולהוסיף פסקאות שיש להם את הסגנון שצוין לפסקאות. עם מערך Style The The The Paragraph שם סגנון ניתן למצוא Style.getName() רכוש Paragraph.getParagraphFormat() אובייקט

היישום של Runs ByStyle שם הוא כמעט זהה, למרות שאנחנו בהחלט משתמשים NodeType.Run עקבו אחרי Nodes The The The Font.getStyle() רכוש של Run האובייקט משמש לגישה למידע בסגנון Run צומת

הדוגמה הקודית הבאה מוצאת את כל הפונקציות מעוצבות בסגנון שצוין.

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
public static ArrayList<Run> runsByStyleName(Document doc, String styleName) throws Exception {
// Create an array to collect runs of the specified style.
ArrayList<Run> runsWithStyle = new ArrayList();
// Get all runs from the document.
NodeCollection runs = doc.getChildNodes(NodeType.RUN, true);
// Look through all runs to find those with the specified style.
for (Run run : (Iterable<Run>) runs) {
if (run.getFont().getStyle().getName().equals(styleName))
runsWithStyle.add(run);
}
return runsWithStyle;
}

דוגמא הקוד הבאה מפעילה שאילתות ותוצאות תצוגה.

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
// Open the document.
Document doc = new Document(dataDir + "TestFile.doc");
// Define style names as they are specified in the Word document.
final String PARA_STYLE = "Heading 1";
final String RUN_STYLE = "Intense Emphasis";
// Collect paragraphs with defined styles.
// Show the number of collected paragraphs and display the text of this paragraphs.
ArrayList<Paragraph> paragraphs = paragraphsByStyleName(doc, PARA_STYLE);
System.out.println(java.text.MessageFormat.format("Paragraphs with \"{0}\" styles ({1}):", PARA_STYLE, paragraphs.size()));
for (Paragraph paragraph : paragraphs)
System.out.print(paragraph.toString(SaveFormat.TEXT));
// Collect runs with defined styles.
// Show the number of collected runs and display the text of this runs.
ArrayList<Run> runs = runsByStyleName(doc, RUN_STYLE);
System.out.println(java.text.MessageFormat.format("\nRuns with \"{0}\" styles ({1}):", RUN_STYLE, runs.size()));
for (Run run : runs)
System.out.println(run.getRange().getText());

כאשר הכל נעשה, הפעלת הדגימה תציג את הפלט הבא:

working-with-styles-aspose-words-java-2

כפי שאתה יכול לראות, זו דוגמה פשוטה מאוד, מראה את המספר והטקסט של פסקאות שנאספו ורץ במסמך Word המדגם.

המונחים: style Separator to Put different Paragraph

ניתן להוסיף לסיומו של סעיף באמצעות Ctrl + Alt + Enter Keyboard Shortcut into MS Word. תכונה זו מאפשרת שני סגנונות שונים של פסקה בשימוש בפסקה מודפסת הגיונית אחת. אם אתה רוצה טקסט מההתחלה של כותרת מסוימת להופיע בטבלה של תוכן אבל לא רוצה את הכותרת כולה בטבלה של תוכן, אתה יכול להשתמש תכונה זו

הדוגמה הבאה של הקוד מראה כיצד להוסיף מפריד סגנון כדי להציב סגנונות שונים של פסקה

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Style paraStyle = builder.getDocument().getStyles().add(StyleType.PARAGRAPH, "MyParaStyle");
paraStyle.getFont().setBold(false);
paraStyle.getFont().setSize(8);
paraStyle.getFont().setName("Arial");
// Append text with "Heading 1" style.
builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_1);
builder.write("Heading 1");
builder.insertStyleSeparator();
// Append text with another style.
builder.getParagraphFormat().setStyleName(paraStyle.getName());
builder.write("This is text with some other formatting ");
dataDir = dataDir + "InsertStyleSeparator_out.doc";
doc.save(dataDir);

העתק את כל הסגנונות מתבנית

ישנם מקרים שבהם אתה רוצה להעתיק את כל הסגנונות של מסמך אחד למשנהו. אתה יכול להשתמש Document.CopyStylesFromTemplate שיטה להעתיק סגנונות מן התבנית המפורטת במסמך. כאשר סגנונות מועתקים מתבנית למסמך, סגנונות דמויי שם במסמך מוגדרים מחדש כדי להתאים את תיאורי הסגנון בתבנית. סגנונות ייחודיים מן התבנית מועתקים אל המסמך. סגנונות ייחודיים במסמך נשארים שלמים

הדוגמה הבאה של הקוד מראה כיצד להעתיק סגנונות ממכתב אחד למשנהו.

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
Document doc = new Document(dataDir + "template.docx");
Document target = new Document(dataDir + "TestFile.doc");
target.copyStylesFromTemplate(doc);
dataDir = dataDir + "CopyStyles_out.docx";
doc.save(dataDir);

איך להגות תכונות נושא

הוספנו בסיסי API פנימה Aspose.Words גישה למאפייני נושא. לעת עתה, זה API כולל את האובייקטים הציבוריים הבאים:

    • נושא
  • תגית: ThemeFonts
  • תגית: ThemeColors

הנה איך אתה יכול לקבל תכונות נושא:

// 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(GetThemeProperties.class);
Document doc = new Document(dataDir + "Document.doc");
Theme theme = doc.getTheme();
// Major (Headings) font for Latin characters.
System.out.println(theme.getMajorFonts().getLatin());
// Minor (Body) font for EastAsian characters.
System.out.println(theme.getMinorFonts().getEastAsian());
// Color for theme color Accent 1.
System.out.println(theme.getColors().getAccent1());

הנה איך אתה יכול להגדיר תכונות נושא:

// 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(SetThemeProperties.class);
Document doc = new Document(dataDir + "Document.doc");
Theme theme = doc.getTheme();
// Set Times New Roman font as Body theme font for Latin Character.
theme.getMinorFonts().setLatin("Algerian");
// Set Color.Gold for theme color Hyperlink.
theme.getColors().setHyperlink(java.awt.Color.DARK_GRAY);
doc.save(dataDir + "output.doc");