עבודה עם Paragraphs

פסקה היא קבוצה של דמויות המשולבות לבלוק לוגי וסיום עם אופי מיוחד - הפסקה של פסקאות*. In In In Aspose.Words, סעיף מיוצג על ידי Paragraph מעמד.

הכנס סעיף

כדי להכניס פסקה חדשה למסמך, למעשה, עליך להכניס אליו את הדמות של פסקה. DocumentBuilder.Writeln הוספת לא רק מחרוזת של טקסט לתוך המסמך, אלא גם מוסיף הפסקה.

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

דוגמה לקוד הבא מראה כיצד להכניס פסקה למסמך:

// 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();
// Initialize document.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Specify font formatting
Font font = builder.Font;
font.Size = 16;
font.Bold = true;
font.Color = System.Drawing.Color.Blue;
font.Name = "Arial";
font.Underline = Underline.Dash;
// Specify paragraph formatting
ParagraphFormat paragraphFormat = builder.ParagraphFormat;
paragraphFormat.FirstLineIndent = 8;
paragraphFormat.Alignment = ParagraphAlignment.Justify;
paragraphFormat.KeepTogether = true;
builder.Writeln("A whole paragraph.");
dataDir = dataDir + "DocumentBuilderInsertParagraph_out.doc";
doc.Save(dataDir);

תבנית Paragraph

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

דוגמה לקוד הבא מראה כיצד להגדיר את סעיף הפורמט:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Set paragraph formatting properties
ParagraphFormat paragraphFormat = builder.ParagraphFormat;
paragraphFormat.Alignment = ParagraphAlignment.Center;
paragraphFormat.LeftIndent = 50;
paragraphFormat.RightIndent = 50;
paragraphFormat.SpaceAfter = 25;
// Output text
builder.Writeln("I'm a very nice formatted paragraph. I'm intended to demonstrate how the left and right indents affect word wrapping.");
builder.Writeln("I'm another nice formatted paragraph. I'm intended to demonstrate how the space after paragraph looks like.");
dataDir = dataDir + "DocumentBuilderSetParagraphFormatting_out.doc";
doc.Save(dataDir);

המונחים: Paragraph Style

כמה אובייקטים מעוצבים, כגון Font או ParagraphFormat, סגנונות תמיכה סגנון מובנה או מוגדר למשתמש מיוצג על ידי Style אובייקט, המכיל את תכונות הסגנון המתאים כמו שם, סגנון בסיס, גופן, סגנון סעיף עיצוב, וכן הלאה.

בנוסף, Style האובייקט חושף את StyleIdentifier רכוש, אשר מחזיר את מזהה הסגנון המקומי עצמאי מיוצג על ידי StyleIdentifier ערך enumeration למעשה, השמות של הסגנונות הבנויים Microsoft Word הם מקומיים לשפות שונות. באמצעות מזהה סגנון, אתה יכול למצוא את הסגנון הנכון ללא קשר לשפת המסמך. ערכי ההנצחה תואמים לערכי המובנה Microsoft Word סגנונות כגון Normal, 1 *הופנה מהדף Heading 2 וכן הלאה. כל סגנונות המוגדרים על ידי המשתמש מוגדרים StyleIdentifier.User ערך enumeration

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

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Set paragraph style
builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Title;
builder.Write("Hello");
dataDir = dataDir + "DocumentBuilderApplyParagraphStyle_out.doc";
doc.Save(dataDir);

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

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

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

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Style paraStyle = builder.Document.Styles.Add(StyleType.Paragraph, "MyParaStyle");
paraStyle.Font.Bold = false;
paraStyle.Font.Size = 8;
paraStyle.Font.Name = "Arial";
// Append text with "Heading 1" style.
builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading1;
builder.Write("Heading 1");
builder.InsertStyleSeparator();
// Append text with another style.
builder.ParagraphFormat.StyleName = paraStyle.Name;
builder.Write("This is text with some other formatting ");
dataDir = dataDir + "InsertStyleSeparator_out.doc";
// Save the document to disk.
doc.Save(dataDir);

זיהוי Paragraph סגנון ספרדי

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

// 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_RenderingAndPrinting();
// Initialize document.
string fileName = "TestFile.doc";
Document doc = new Document(dataDir + fileName);
foreach (Paragraph paragraph in doc.GetChildNodes(NodeType.Paragraph, true))
{
if (paragraph.BreakIsStyleSeparator)
{
Console.WriteLine("Separator Found!");
}
}

החל את הגבולות והמשך לפסקה

גבולות Aspose.Words מיוצגים על ידי BorderCollection מעמד – זהו אוסף של Border פריטים אשר נגישים על ידי אינדקס או על ידי סוג גבול. סוג הגבול הוא בתורו מיוצג על ידי BorderType אזהרה. כמה ערכי enumeration חלים על מספר רב או רק אלמנט מסמך אחד. לדוגמה, BorderType.Bottom חל על סעיף או תא שולחן, BorderType.DiagonalDown מציג גבול דיגוני בתא שולחן בלבד.

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

Aspose.Words יש גם את Shading שיעור המכיל תכונות קידוד עבור רכיבי מסמך. אתה יכול להגדיר את המרקם והצבעים הרצויים מוחלים על הרקע ואת הבסיס של אלמנט באמצעות האלמנט תוך שימוש TextureIndex ערך enumeration TextureIndex גם מאפשר לך ליישם דפוסים שונים Shading אובייקט. לדוגמה, כדי להגדיר את צבע הרקע עבור אלמנט מסמך, השתמש ב TextureIndex.TextureSolid ערך ולהגדיר את הצבע הקדמי מתאים.

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

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Set paragraph borders
BorderCollection borders = builder.ParagraphFormat.Borders;
borders.DistanceFromText = 20;
borders[BorderType.Left].LineStyle = LineStyle.Double;
borders[BorderType.Right].LineStyle = LineStyle.Double;
borders[BorderType.Top].LineStyle = LineStyle.Double;
borders[BorderType.Bottom].LineStyle = LineStyle.Double;
// Set paragraph shading
Shading shading = builder.ParagraphFormat.Shading;
shading.Texture = TextureIndex.TextureDiagonalCross;
shading.BackgroundPatternColor = System.Drawing.Color.LightCoral;
shading.ForegroundPatternColor = System.Drawing.Color.LightSalmon;
builder.Write("I'm a formatted paragraph with double border and nice shading.");
dataDir = dataDir + "DocumentBuilderApplyBordersAndShadingToParagraph_out.doc";
doc.Save(dataDir);

Count Paragraph Lines

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

// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_WorkingWithDocument();
string fileName = "Properties.doc";
Document document = new Document(dataDir + fileName);
var collector = new LayoutCollector(document);
var it = new LayoutEnumerator(document);
foreach (Paragraph paragraph in document.GetChildNodes(NodeType.Paragraph, true))
{
var paraBreak = collector.GetEntity(paragraph);
object stop = null;
var prevItem = paragraph.PreviousSibling;
if (prevItem != null)
{
var prevBreak = collector.GetEntity(prevItem);
if (prevItem is Paragraph)
{
it.Current = collector.GetEntity(prevItem); // para break
it.MoveParent(); // last line
stop = it.Current;
}
else if (prevItem is Table)
{
var table = (Table)prevItem;
it.Current = collector.GetEntity(table.LastRow.LastCell.LastParagraph); // cell break
it.MoveParent(); // cell
it.MoveParent(); // row
stop = it.Current;
}
else
{
throw new Exception();
}
}
it.Current = paraBreak;
it.MoveParent();
// We move from line to line in a paragraph.
// When paragraph spans multiple pages the we will follow across them.
var count = 1;
while (it.Current != stop)
{
if (!it.MovePreviousLogical())
break;
count++;
}
const int MAX_CHARS = 16;
var paraText = paragraph.GetText();
if (paraText.Length > MAX_CHARS)
paraText = $"{paraText.Substring(0, MAX_CHARS)}...";
Console.WriteLine($"Paragraph '{paraText}' has {count} line(-s).");
}