การทำงานกับย่อหน้า
ย่อหน้าคือชุดของอักขระรวมกันเป็นบล็อกตรรกะและลงท้ายด้วยอักขระพิเศษ-paragraph break ในAspose.Wordsย่อหน้าจะแสดงโดยคลาสของ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"); |
รูปแบบย่อหน้า
การจัดรูปแบบย่อหน้าปัจจุบันจะแสดงโดยออบเจกต์ParagraphFormatที่ถูกส่งคืนโดยคุณสมบัติParagraphFormat วัตถุนี้ประกอบด้วยคุณสมบัติการจัดรูปแบบย่อหน้าต่างๆที่มีอยู่ใน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"); |
ใช้สไตล์ย่อหน้า
บางรูปแบบวัตถุเช่นแบบอักษรหรือParagraphFormatรูปแบบการสนับสนุน สไตล์ที่ผู้ใช้กำหนดในตัวเดียวจะแสดงโดยออบเจกต์Styleที่มีคุณสมบัติลักษณะที่สอดคล้องกันเช่น.
นอกจากนี้ออบเจกต์StyleมีคุณสมบัติStyleIdentifierที่ส่งคืนตัวระบุสไตล์อิสระโลแคลที่แสดงโดยค่าการแจงนับStyleIdentifier ประเด็นคือชื่อของลักษณะในตัวในMicrosoft Wordเป็นภาษาท้องถิ่นสำหรับภาษาที่แตกต่างกัน ใช้ตัวระบุลักษณะคุณสามารถค้นหาลักษณะที่ถูกต้องโดยไม่คำนึงถึงภาษาของเอกสาร ค่าการแจงนับสอดคล้องกับรูปแบบในตัวMicrosoft Wordเช่นNormal, Heading 1, Heading 2, ฯลฯ รูปแบบที่ผู้ใช้กำหนดทั้งหมดจะถูกกำหนดStyleIdentifier.User value.
ตัวอย่างรหัสต่อไปนี้แสดงวิธีการใช้ลักษณะย่อหน้า:
// 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"); |
แทรกตัวคั่นสไตล์ที่จะนำรูปแบบที่แตกต่างกันย่อหน้า
สามารถเพิ่มตัวคั่นลักษณะไปยังส่วนท้ายของย่อหน้าได้โดยใช้แป้นพิมพ์ลัดMSคำ คุณลักษณะนี้ช่วยให้ลักษณะย่อหน้าต่างๆสองใช้ในย่อหน้าตรรกะหนึ่งพิมพ์ ถ้าคุณต้องการให้ข้อความบางส่วนจากจุดเริ่มต้นของหัวข้อเฉพาะปรากฏในตารางของเนื้อ.
ตัวอย่างรหัสต่อไปนี้แสดงวิธีการแทรกตัวคั่นลักษณะเพื่อรองรับลักษณะย่อหน้าต่าง:
// 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ออบเจกต์ที่เข้าถึงได้โดยดัชนีหรือตาม ประเภทBorder
จะแสดงโดยการแจงนับBorderType ค่าบางอย่างของการนับมีผลบังคับใช้กับหลายหรือเพียงองค์ประกอบเอกสารหนึ่ง ตัวอย่างเช่นBorderType.Bottomใช้ได้กับย่อหน้าหรือเซลล์ตารางในขณะที่BorderType.DiagonalDownระบุขอบเส้นทแยงมุมในเซลล์ตาราง.
ทั้งคอลเลกชันชายแดนและแต่ละชายแดนแยกต่างหากมีคุณลักษณะที่คล้ายกันเช่นสีลักษณะเส้ คุณสมบัติของชื่อเดียวกัน คุณสามารถบรรลุประเภทเส้นขอบที่แตกต่างกันโดยการรวมค่าคุณสมบัติ นอกจากนี้วัตถุทั้งBorderCollectionและBorderช่วยให้คุณสามารถรีเซ็ตค่าเหล่านี้เป็นค่าเริ่มต้นโดยการเรียกวิธีการClearFormatting.
Aspose.Wordsนอกจากนี้ยังมีShadingชั้นมีแอตทริบิวต์การแรเงาสำหรับองค์ประกอบของเอกสาร คุณสามารถตั้งค่าพื้นผิวการแรเงาที่ต้องการและสีที่ใช้กับพื้นหลังและเบื้องหน้าขององค์ป.
พื้นผิวการแรเงาถูกตั้งค่าด้วยค่าการแจงนับTextureIndexที่อนุญาตให้มีการประยุกต์ใช้รูปแบบต่างๆกับวัตถุ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"); |