עבודה עם Paragraphs

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

המונחים: a Paragraph

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

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

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

// 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();
DocumentBuilder builder = new DocumentBuilder(doc);
Font font = builder.getFont();
font.setSize(16);
font.setColor(Color.DARK_GRAY);
font.setBold(true);
font.setName("Algerian");
font.setUnderline(2);
ParagraphFormat paragraphFormat = builder.getParagraphFormat();
paragraphFormat.setFirstLineIndent(12);
paragraphFormat.setAlignment(1);
paragraphFormat.setKeepTogether(true);
builder.write("This is a sample Paragraph");
doc.save(dataDir + "InsertParagraph_out.doc");

תבנית Paragraph

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

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

// 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();
DocumentBuilder builder = new DocumentBuilder(doc);
ParagraphFormat paragraphFormat = builder.getParagraphFormat();
paragraphFormat.setAlignment(ParagraphAlignment.CENTER);
paragraphFormat.setLeftIndent(50);
paragraphFormat.setRightIndent(50);
paragraphFormat.setSpaceAfter(25);
paragraphFormat.setKeepTogether(true);
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.");
doc.save(dataDir + "SetParagraphFormatting_out.doc");

המונחים: Paragraph Style

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

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

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

// 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();
// Set paragraph style
DocumentBuilder builder = new DocumentBuilder(doc);
builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.TITLE);
builder.write("Hello");
doc.save(dataDir + "ApplyParagraphStyle_out.doc");

המונחים: 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);

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

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

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

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

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

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

// 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();
DocumentBuilder builder = new DocumentBuilder(doc);
// Set paragraph borders
BorderCollection borders = builder.getParagraphFormat().getBorders();
borders.setDistanceFromText(20);
borders.getByBorderType(BorderType.LEFT).setLineStyle(LineStyle.DOUBLE);
borders.getByBorderType(BorderType.RIGHT).setLineStyle(LineStyle.DOUBLE);
borders.getByBorderType(BorderType.TOP).setLineStyle(LineStyle.DOUBLE);
borders.getByBorderType(BorderType.BOTTOM).setLineStyle(LineStyle.DOUBLE);
// Set paragraph shading
Shading shading = builder.getParagraphFormat().getShading();
shading.setTexture(TextureIndex.TEXTURE_DIAGONAL_CROSS);
shading.setBackgroundPatternColor(Color.YELLOW);
shading.setForegroundPatternColor(Color.GREEN);
builder.write("I'm a formatted paragraph with double border and nice shading.");
doc.save(dataDir + "ApplyBordersAndShading_out.doc");