การทำงานกับส่วน

บางครั้งคุณต้องการเอกสารที่ไม่มีการจัดรูปแบบเดียวกันในทุกหน้า ตัวอย่างเช่นคุณอาจจำเป็นต้องปรับเปลี่ยนรูปแบบหมายเลขหน้ามีขนาดและการวางแนว คุณสามารถบรรลุที่มีส่วน.

ส่วนคือโหนดระดับที่ควบคุมส่วนหัวและท้ายกระดาษการวางแนวคอลัมน์ขอบการจัดรูปแบบหม.

Aspose.Wordsอนุญาตให้คุณจัดการส่วนแบ่งเอกสารออกเป็นส่วนและทำการเปลี่ยนแปลงการจัดรูปแบบ Aspose.Wordsจัดเก็บข้อมูลเกี่ยวกับการจัดรูปแบบส่วนเช่นส่วนหัวและท้ายกระดาษการตั้งค่าหน้าเว็บแ.

บทความนี้อธิบายวิธีการทำงานกับส่วนและตัวแบ่งส่วน.

สิ่งที่แบ่งส่วนและส่วนคือ

ส่วนเอกสารจะแสดงโดยSectionและSectionCollectionคลาส วัตถุส่วนเป็นเด็กทันทีของโหนดDocumentและสามารถเข้าถึงได้ผ่านทางคุณสมบัติSections คุณสามารถจัดการโหนดเหล่านั้นได้โดยใช้วิธีการบางอย่างเช่นRemove, Add, IndexOf, และอื่นๆ.

แบ่งส่วนเป็นตัวเลือกที่แบ่งหน้าเอกสารออกเป็นส่วนที่มีรูปแบบที่ปรับแต่งได้.

ประเภทของการแบ่งส่วน

Aspose.Wordsช่วยให้คุณสามารถแยกและจัดรูปแบบเอกสารโดยใช้ส่วนแบ่งที่แตกต่างกันของการแจงนับBreakType:

  • SectionBreakContinuous
  • SectionBreakNewColumn
  • SectionBreakNewPage
  • SectionBreakEvenPage
  • SectionBreakOddPage

คุณยังสามารถใช้การแจงนับSectionStartเพื่อเลือกประเภทการหยุดพักที่ใช้ได้เฉพาะส่วนแรกเช่นNewColumn, NewPage, EvenPage, และOddPage.

จัดการส่วน

เนื่องจากส่วนคือโหนดคอมโพสิตปกติการจัดการโหนดทั้งหมดAPIสามารถใช้เพื่อจัดการส่วน:เพิ่ คุณสามารถอ่านเพิ่มเติมเกี่ยวกับโหนดในบทความ Aspose.Wordsรูปแบบวัตถุเอกสาร(DOM).

ในทางกลับกันคุณยังสามารถใช้DocumentBuilderAPIเพื่อทำงานกับส่วนต่างๆได้ ในบทความนี้เราจะมุ่งเน้นไปที่วิธีนี้โดยเฉพาะอย่างยิ่งของการทำงานกับส่วน.

แทรกหรือลบตัวแบ่งส่วน

Aspose.Wordsช่วยให้คุณสามารถแทรกการแบ่งส่วนลงในข้อความโดยใช้วิธีการInsertBreak.

ตัวอย่างรหัสต่อไปนี้แสดงวิธีการแทรกแบ่งส่วนในเอกสาร:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git.
/// <summary>
/// Insert section breaks before the specified paragraphs.
/// </summary>
private void insertSectionBreaks(ArrayList<Paragraph> topicStartParas)
{
DocumentBuilder builder = new DocumentBuilder(mDoc);
for (Paragraph para : topicStartParas)
{
Section section = para.getParentSection();
// Insert section break if the paragraph is not at the beginning of a section already.
if (para != section.getBody().getFirstParagraph())
{
builder.moveTo(para.getFirstChild());
builder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE);
// This is the paragraph that was inserted at the end of the now old section.
// We don't really need the extra paragraph, we just needed the section.
section.getBody().getLastParagraph().remove();
}
}
}

ใช้วิธีการRemoveเพื่อลบตัวแบ่งส่วน หากคุณไม่จำเป็นต้องลบตัวแบ่งส่วนที่เฉพาะเจาะจงและลบเนื้อหาของส่วนนั้นแทน,คุณสามารถใช้วิธีการClearContent.

ตัวอย่างรหัสต่อไปนี้แสดงวิธีการเอาตัวแบ่งส่วน:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git.
private void removeSectionBreaks(Document doc)
{
// Loop through all sections starting from the section that precedes the last one and moving to the first section.
for (int i = doc.getSections().getCount() - 2; i >= 0; i--)
{
// Copy the content of the current section to the beginning of the last section.
doc.getLastSection().prependContent(doc.getSections().get(i));
// Remove the copied section.
doc.getSections().get(i).remove();
}
}

ย้ายส่วน

ถ้าคุณต้องการย้ายส่วนจากตำแหน่งหนึ่งไปยังอีกตำแหน่งหนึ่งในเอกสารของคุณคุณจำเป็นต้ Aspose.Wordsช่วยให้คุณสามารถรับตำแหน่งส่วนจากSectionCollection คุณสามารถใช้คุณสมบัติSectionsเพื่อรับส่วนทั้งหมดในเอกสารของคุณ แต่ถ้าคุณต้องการรับเฉพาะส่วนแรกคุณสามารถใช้คุณสมบัติFirstSection.

ตัวอย่างรหัสต่อไปนี้แสดงวิธีการเข้าถึงส่วนแรกและทำซ้ำผ่านเด็กของโหนดคอมโพสิต:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.write("Section 1");
builder.moveToHeaderFooter(HeaderFooterType.HEADER_PRIMARY);
builder.write("Primary header");
builder.moveToHeaderFooter(HeaderFooterType.FOOTER_PRIMARY);
builder.write("Primary footer");
Section section = doc.getFirstSection();
// A Section is a composite node and can contain child nodes,
// but only if those child nodes are of a "Body" or "HeaderFooter" node type.
for (Node node : section)
{
switch (node.getNodeType())
{
case NodeType.BODY:
{
Body body = (Body)node;
System.out.println("Body:");
System.out.println(MessageFormat.format("\t\"{0}\"", body.getText().trim()));
break;
}
case NodeType.HEADER_FOOTER:
{
HeaderFooter headerFooter = (HeaderFooter)node;
System.out.println(MessageFormat.format("HeaderFooter type: {0}:", headerFooter.getHeaderFooterType()));
System.out.println(MessageFormat.format("\t\"{0}\"", headerFooter.getText().trim()));
break;
}
default:
{
throw new Exception("Unexpected node type in a section.");
}
}
}

ระบุเค้าโครงส่วน

บางครั้งคุณต้องการให้เอกสารของคุณดูดีขึ้นด้วยการสร้างโครงร่างที่สร้างสรรค์สำหรับส่ หากคุณต้องการระบุประเภทของตารางส่วนปัจจุบันคุณสามารถเลือกโหมดเค้าโครงส่วนโดยใช้การแจงนับSectionLayoutMode:

  • ค่าปริยาย
  • กริด
  • LineGrid
  • SnapToChars

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีจำกัดจำนวนบรรทัดที่แต่ละหน้าอาจมี:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Enable pitching, and then use it to set the number of lines per page in this section.
// A large enough font size will push some lines down onto the next page to avoid overlapping characters.
builder.getPageSetup().setLayoutMode(SectionLayoutMode.LINE_GRID);
builder.getPageSetup().setLinesPerPage(15);
builder.getParagraphFormat().setSnapToGrid(true);
for (int i = 0; i < 30; i++)
builder.write("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. ");
doc.save(getArtifactsDir() + "WorkingWithDocumentOptionsAndSettings.LinesPerPage.docx");

แก้ไขส่วน

เมื่อคุณเพิ่มส่วนใหม่ในเอกสารของคุณจะไม่มีเนื้อความหรือย่อหน้าที่คุณสามารถแก้ไขได้ Aspose.Wordsช่วยให้คุณสามารถรับประกันได้ว่าส่วนที่มีเนื้อความที่มีอย่างน้อยหนึ่งย่อหน้าโดยใช้วิธีการEnsureMinimum–มันจะเพิ่มโหนดเนื้อความ(หรือHeaderFooter)ลงในเอกสารโดยอัตโนมัติและเพิ่มย่อหน้าลงไป.

ตัวอย่างรหัสต่อไปนี้แสดงวิธีการเตรียมโหนดส่วนใหม่โดยใช้EnsureMinimum:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git.
Document doc = new Document();
// If we add a new section like this, it will not have a body, or any other child nodes.
doc.getSections().add(new Section(doc));
// Run the "EnsureMinimum" method to add a body and a paragraph to this section to begin editing it.
doc.getLastSection().ensureMinimum();
doc.getSections().get(0).getBody().getFirstParagraph().appendChild(new Run(doc, "Hello world!"));

ผนวกหรือลอกเนื้อหา

ถ้าคุณต้องการวาดรูปร่างหรือเพิ่มข้อความหรือรูปภาพที่จุดเริ่มต้น/จุดสิ้นสุดของส่วนคุณสามารถใช้วิธีการAppendContentและPrependContentของชั้นเรียนSection.

ตัวอย่างรหัสต่อไปนี้แสดงวิธีการผนวกเนื้อหาของส่วนที่มีอยู่:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.write("Section 1");
builder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE);
builder.write("Section 2");
builder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE);
builder.write("Section 3");
// This is the section that we will append and prepend to.
Section section = doc.getSections().get(2);
// Insert the contents of the first section to the beginning of the third section.
Section sectionToPrepend = doc.getSections().get(0);
section.prependContent(sectionToPrepend);
// Insert the contents of the second section to the end of the third section.
Section sectionToAppend = doc.getSections().get(1);
section.appendContent(sectionToAppend);

โคลนส่วน

Aspose.Wordsช่วยให้คุณสามารถทำซ้ำส่วนโดยการสร้างสำเนาเต็มรูปแบบของมันโดยใช้วิธีการdeepClone.

ตัวอย่างรหัสต่อไปนี้แสดงวิธีการโคลนส่วนแรกในเอกสารของคุณ:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git.
Document doc = new Document(getMyDir() + "Document.docx");
Section cloneSection = doc.getSections().get(0).deepClone();

คัดลอกส่วนระหว่างเอกสาร

ในบางกรณีคุณอาจมีเอกสารขนาดใหญ่มีหลายส่วนและคุณต้องการคัดลอกเนื้อหาของส่วนจ.

Aspose.Wordsช่วยให้คุณสามารถคัดลอกส่วนระหว่างเอกสารโดยใช้วิธีการImportNode.

ตัวอย่างรหัสต่อไปนี้แสดงวิธีการคัดลอกส่วนระหว่างเอกสาร:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git.
Document srcDoc = new Document(getMyDir() + "Document.docx");
Document dstDoc = new Document();
Section sourceSection = srcDoc.getSections().get(0);
Section newSection = (Section) dstDoc.importNode(sourceSection, true);
dstDoc.getSections().add(newSection);
dstDoc.save(getArtifactsDir() + "WorkingWithSection.CopySection.docx");

ทำงานร่วมกับส่วนหัวและส่วนท้าย

กฎพื้นฐานสำหรับการแสดงส่วนหัวหรือส่วนท้ายสำหรับแต่ละส่วนจะค่อนข้างง่าย:

  1. ถ้าส่วนไม่ได้มีส่วนหัวของตัวเอง/ส่วนท้ายของบางประเภทแล้วมันจะถูกนำมาจากส่วนก่อนห.
  2. ประเภทของส่วนหัว/ส่วนท้ายที่แสดงบนหน้าจะถูกควบคุมโดย"หน้าแรกที่แตกต่างกัน"และ"หน้.

ตัวอย่างรหัสต่อไปนี้แสดงวิธีการสร้าง 2 ส่วนที่มีส่วนหัวที่แตกต่างกัน:

ถ้าคุณต้องการลบข้อความของส่วนหัวและท้ายกระดาษโดยไม่ต้องลบHeaderFooterวัตถุในเอกสารของคุณคุณสามารถใช้วิธีการClearHeadersFooters นอกจากนี้คุณสามารถใช้วิธีการDeleteHeaderFooterShapesเพื่อลบรูปร่างทั้งหมดออกจากส่วนหัวและท้ายกระดาษในเ.

ตัวอย่างรหัสต่อไปนี้แสดงวิธีการล้างเนื้อหาของส่วนหัวและท้ายกระดาษทั้งหมดในส่วน:

ตัวอย่างรหัสต่อไปนี้วิธีการเอารูปร่างทั้งหมดออกจากส่วนหัวท้ายกระดาษทั้งหมดในส่วน:

ปรับแต่งคุณสมบัติของเพจในส่วน

ก่อนที่จะพิมพ์หน้าหรือเอกสารคุณอาจต้องการปรับแต่งและปรับเปลี่ยนขนาดและเค้าโครงข ด้วยการตั้งค่าหน้าคุณสามารถเปลี่ยนการตั้งค่าของหน้าเอกสารเช่นระยะขอบการวางแนว.

Aspose.Wordsอนุญาตให้คุณปรับแต่งคุณสมบัติของเพจและส่วนโดยใช้คลาสของPageSetup.

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการตั้งค่าคุณสมบัติเช่นขนาดหน้าและการวางแนวสำหรับส่วน:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.getPageSetup().setOrientation(Orientation.LANDSCAPE);
builder.getPageSetup().setLeftMargin(50.0);
builder.getPageSetup().setPaperSize(PaperSize.PAPER_10_X_14);
doc.save(getArtifactsDir() + "WorkingWithDocumentOptionsAndSettings.SetPageSetupAndSectionFormatting.docx");

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีแก้ไขคุณสมบัติของเพจในทุกส่วน:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.writeln("Section 1");
doc.appendChild(new Section(doc));
builder.writeln("Section 2");
doc.appendChild(new Section(doc));
builder.writeln("Section 3");
doc.appendChild(new Section(doc));
builder.writeln("Section 4");
// It is important to understand that a document can contain many sections,
// and each section has its page setup. In this case, we want to modify them all.
for (Section section : doc.getSections())
section.getPageSetup().setPaperSize(PaperSize.LETTER);
doc.save(getArtifactsDir() + "WorkingWithSection.ModifyPageSetupInAllSections.doc");

ดูเพิ่มเติม