עבודה עם סגנונות ונושאים
The The The StyleCollection הכיתה משמשת לניהול הגדרות בנויות וליישם הגדרות מוגדרות למשתמש לסגנונות.
גישה לסגנונות
אתה יכול לקבל אוסף של סגנונות המוגדרים במסמך באמצעות Document.Styles רכוש. אוסף זה מחזיק הן בסגנונות המובנים והן בסגנונות המוגדרים למשתמש במסמך. סגנון מסוים ניתן להשיג על ידי שמו /alias, מזהה סגנון, או אינדקס. הדוגמה הבאה של הקוד ממחישה כיצד להגיע לאוסף של סגנונות המוגדרים במסמך.
// 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_WorkingWithDocument(); | |
// Load the template document. | |
Document doc = new Document(dataDir + "TestFile.doc"); | |
// Get styles collection from document. | |
StyleCollection styles = doc.Styles; | |
string styleName = ""; | |
// Iterate through all the styles. | |
foreach (Style style in styles) | |
{ | |
if (styleName == "") | |
{ | |
styleName = style.Name; | |
} | |
else | |
{ | |
styleName = styleName + ", " + style.Name; | |
} | |
} |
כיצד להפיק תוכן מבוסס על סגנונות
ברמה פשוטה, החזרת התוכן מבוסס על סגנונות מ מסמך Word יכול להיות שימושי לזהות, רשימה וספירת פסקאות וריצה של טקסט מעוצב בסגנון ספציפי. לדוגמה, ייתכן שיהיה עליך לזהות סוגים מסוימים של תוכן במסמך, כגון דוגמאות, כותרים, מילות מפתח, שמות דמויות ומחקרי מקרה.
כדי לקחת את זה כמה צעדים קדימה, זה יכול לשמש גם כדי למנף את המבנה של המסמך, המוגדר על ידי הסגנונות שהוא משתמש, כדי ליצור מחדש את המסמך עבור פלט אחר, כגון HTML. למעשה, כך נוצר תיעוד As נניח, שם Aspose.Words למבחן. כלי שנבנה באמצעות Aspose.Words לוקח את מקור מסמכי Word ומתפצל אותם לנושאים ברמות כותרת מסוימות. קובץ XML מיוצר באמצעות Aspose.Words משמש לבניית עץ הניווט שאתה יכול לראות בצד שמאל. ואז Aspose.Words הופך כל נושא ל-HTML.
הפתרון לחידוש הטקסט בפורמט עם סגנונות ספציפיים במסמך Word הוא בדרך כלל כלכלי ופשוט באמצעות שימוש פשוט באמצעות שימוש Aspose.Words.
הפתרון
להראות כמה בקלות Aspose.Words טיפול חוזר תוכן מבוסס על סגנונות, בואו נסתכל על דוגמה. בדוגמה זו, אנו הולכים לאחזר טקסט מעוצב עם סגנון סעיף מסוים וסגנון אופי מתעודה מילה מדגם. ברמה גבוהה, זה יהיה כרוך: # פתיחת מסמך Word באמצעות Document שיעור.# מקבל אוספים של כל הסעיפים וכל פועל במסמך. בחר רק את הסעיפים הנדרשים ורץ. באופן ספציפי, אנו נחזיר טקסט מעוצב בסגנון “Heading 1” ס"ק ואת סגנון האופי “Intense Emphasis” מ מסמך זה מדגם Word.
במסמך מדגם זה, הטקסט הפורמטיבי בסגנון “Heading 1” הוא “Insert Tab”, “Quick Styles” ו-“Theme”, והטקסט התואם לסגנון האופי “הדגשה הסנסציה” הוא מספר מקרים של טקסט כחול, מוקרן, נועז כמו “מגורים” ו"כל המראה".
הקוד
יישום של שאילתה המבוססת על סגנון הוא פשוט למדי. Aspose.Words מסמך מודל אובייקט, כפי שהוא פשוט משתמש בכלים שכבר נמצאים במקום. שתי שיטות ייצוגיות יושמו לפתרון זה: ParagraphsByStyleName - - - שיטה זו משחזרת מערך של פסקאות אלה במסמך שיש לו שם סגנון מסוים. # # RunsByStyleName - - - שיטה זו משחזרת מערך של אלה רצים במסמך שיש להם שם סגנון מסוים. שתי השיטות הללו דומות מאוד, ההבדלים היחידים הם סוגי הצומת והייצוג של פרטי הסגנון בתוך הסעיף ועושים צמתים. הנה יישום של Paragraphs על ידיStylename: להלן דוגמה למצוא את כל פסקאות מעוצבות בסגנון שצוין.
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
public static ArrayList ParagraphsByStyleName(Document doc, string styleName) | |
{ | |
// Create an array to collect paragraphs of the specified style. | |
ArrayList 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. | |
foreach (Paragraph paragraph in paragraphs) | |
{ | |
if (paragraph.ParagraphFormat.Style.Name == styleName) | |
paragraphsWithStyle.Add(paragraph); | |
} | |
return paragraphsWithStyle; | |
} |
יישום זה גם משתמש Document.GetChildNodes
שיטת Document
שיעור, אשר מחזיר אוסף של צמתים עם הסוג שצוין, אשר במקרה זה בכל פסקאות.
שימו לב לפרמטר השני של Document.GetChildNodes השיטה מיועדת true. הכוחות האלה Document.GetChildNodes שיטה לבחור מכל בלוטות הילד חוזרת, במקום לבחור את הילדים המיידיים בלבד.
כמו כן, ראוי לציין כי אוסף פסקאות אינו יוצר כותרת מיידית כי פסקאות טעו באוסף זה רק כאשר אתה ניגש פריטים בהם. אז, כל מה שאתה צריך לעשות הוא לעבור את האוסף, באמצעות מפעיל סטנדרטי עבור כל אחד ולהוסיף פסקאות שיש להם את הסגנון שצוין לפסקאות. עם מערך Style The The The Paragraph
שם סגנון ניתן למצוא Style.Name רכוש Paragraph.ParagraphFormat אובייקט. היישום של Runs ByStyle שם הוא כמעט זהה, למרות שאנחנו בהחלט משתמשים NodeType.Run כדי לאחזר נקודות. The The The Font.Style רכוש של Run האובייקט משמש לגישה למידע בסגנון Run צומת להלן דוגמה למצוא את כל רץ מעוצב עם הסגנון שצוין.
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
public static ArrayList RunsByStyleName(Document doc, string styleName) | |
{ | |
// Create an array to collect runs of the specified style. | |
ArrayList 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. | |
foreach (Run run in runs) | |
{ | |
if (run.Font.Style.Name == styleName) | |
runsWithStyle.Add(run); | |
} | |
return runsWithStyle; | |
} |
כאשר שתי השאילתות יישמו, כל מה שאתה צריך לעשות הוא להעביר אובייקט מסמך ולקבוע את שמות הסגנון של התוכן שאתה רוצה לשחזר: להלן דוגמה לביצוע שאילתות ותוצאות תצוגה. ניתן להוריד את קובץ התבנית של דוגמה זו כאן.
// 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_WorkingWithStyles(); | |
string fileName = "TestFile.doc"; | |
// Open the document. | |
Document doc = new Document(dataDir + fileName); | |
// Define style names as they are specified in the Word document. | |
const string paraStyle = "Heading 1"; | |
const string runStyle = "Intense Emphasis"; | |
// Collect paragraphs with defined styles. | |
// Show the number of collected paragraphs and display the text of this paragraphs. | |
ArrayList paragraphs = ParagraphsByStyleName(doc, paraStyle); | |
Console.WriteLine(string.Format("Paragraphs with \"{0}\" styles ({1}):", paraStyle, paragraphs.Count)); | |
foreach (Paragraph paragraph in paragraphs) | |
Console.Write(paragraph.ToString(SaveFormat.Text)); | |
// Collect runs with defined styles. | |
// Show the number of collected runs and display the text of this runs. | |
ArrayList runs = RunsByStyleName(doc, runStyle); | |
Console.WriteLine(string.Format("\nRuns with \"{0}\" styles ({1}):", runStyle, runs.Count)); | |
foreach (Run run in runs) | |
Console.WriteLine(run.Range.Text); |
תוצאות סוף
כאשר הכל נעשה, הפעלת הדגימה תציג את הפלט הבא:
כפי שאתה יכול לראות, זו דוגמה פשוטה מאוד, מראה את המספר והטקסט של פסקאות שנאספו ורץ במסמך Word המדגם.
העתק את כל הסגנונות מתבנית
ישנם מקרים שבהם אתה רוצה להעתיק את כל הסגנונות של מסמך אחד למשנהו. אתה יכול להשתמש Document.CopyStylesFromTemplate
שיטה להעתיק סגנונות מן התבנית המפורטת במסמך. כאשר סגנונות מועתקים מתבנית למסמך, סגנונות דמויי שם במסמך מוגדרים מחדש כדי להתאים את תיאורי הסגנון בתבנית. סגנונות ייחודיים מן התבנית מועתקים אל המסמך. סגנונות ייחודיים במסמך נשארים שלמים. להיותlow code לדוגמה, כיצד להעתיק סגנונות מ מסמך אחד למשנהו.
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
string fileName = dataDir + "template.docx"; | |
Document doc = new Document(fileName); | |
// Open the document. | |
Document target = new Document(dataDir + "TestFile.doc"); | |
target.CopyStylesFromTemplate(doc); | |
dataDir = dataDir + RunExamples.GetOutputFilePath(fileName); | |
doc.Save(dataDir); |
איך להגות תכונות נושא
הוספנו בסיסי API פנימה Aspose.Words גישה למאפייני נושא. לעת עתה, זה API כולל פריטים ציבוריים:
-
- נושא
- תגית: ThemeFonts
- נושא
הנה איך אתה יכול לקבל תכונות נושא:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
Document doc = new Document(dataDir); | |
Theme theme = doc.Theme; | |
// Major (Headings) font for Latin characters. | |
Console.WriteLine(theme.MajorFonts.Latin); | |
// Minor (Body) font for EastAsian characters. | |
Console.WriteLine(theme.MinorFonts.EastAsian); | |
// Color for theme color Accent 1. | |
Console.WriteLine(theme.Colors.Accent1); |
הנה איך אתה יכול להגדיר תכונות נושא:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
Document doc = new Document(dataDir); | |
Theme theme = doc.Theme; | |
// Set Times New Roman font as Body theme font for Latin Character. | |
theme.MinorFonts.Latin = "Times New Roman"; | |
// Set Color.Gold for theme color Hyperlink. | |
theme.Colors.Hyperlink = Color.Gold; |