ใช้DocumentBuilderเพื่อแทรกองค์ประกอบของเอกสาร

DocumentBuilderถูกใช้เพื่อแก้ไขเอกสาร บทความนี้อธิบายและอธิบายวิธีการดำเนินการจำนวนของงาน:

การแทรกสตริงข้อความ

เพียงแค่ส่งผ่านสตริงข้อความที่คุณต้องแทรกลงในเอกสารไปยังวิธีการDocumentBuilder.Write การจัดรูปแบบข้อความจะถูกกำหนดโดยคุณสมบัติFont วัตถุนี้มีแอตทริบิวต์แบบอักษรที่แตกต่างกัน(ชื่อแบบอักษรขนาดตัวอักษรสีและอื่นๆ) คุณลักษณะแบบอักษรที่สำคัญบางอย่างจะแสดงด้วยคุณสมบัติDocumentBuilderเพื่อให้คุณสามารถเข้าถึงได้โ เหล่านี้เป็นคุณสมบัติบูลีนFont.Bold,Font.ItalicและFont.Underline.

โปรดทราบว่าการจัดรูปแบบอักขระที่คุณตั้งค่าจะมีผลกับข้อความทั้งหมดที่แทรกจากตำแหน่.

ตัวอย่างด้านล่างแทรกข้อความที่จัดรูปแบบโดยใช้DocumentBuilder.

// The path to the documents directory.
System::String outputDataDir = GetOutputDataDir_WorkingWithDocument();
// Initialize document.
System::SharedPtr<Document> doc = System::MakeObject<Document>();
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc);
// Specify font formatting before adding text.
System::SharedPtr<Font> font = builder->get_Font();
font->set_Size(16);
font->set_Bold(true);
font->set_Color(System::Drawing::Color::get_Blue());
font->set_Name(u"Arial");
font->set_Underline(Underline::Dash);
builder->Write(u"Sample text.");
System::String outputPath = outputDataDir + u"WriteAndFont.doc";
doc->Save(outputPath);

การแทรกย่อหน้า

DocumentBuilder.Writelnแทรกสตริงของข้อความลงในเอกสารเช่นกันแต่นอกจากนี้ยังเพิ่มการแบ่งย่อหน้า การจัดรูปแบบแบบอักษรปัจจุบันจะถูกระบุโดยคุณสมบัติDocumentBuilder.Fontและการจัดรูปแบบย่อหน้าปัจจุบันจะถูกกำหนดโดยคุณสมบัติDocumentBuilder.ParagraphFormat ตัวอย่างด้านล่างแสดงวิธีการแทรกย่อหน้าลงในเอกสาร.

// The path to the documents directory.
System::String outputDataDir = GetOutputDataDir_WorkingWithDocument();
// Initialize document.
System::SharedPtr<Document> doc = System::MakeObject<Document>();
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc);
// Specify font formatting
System::SharedPtr<Font> font = builder->get_Font();
font->set_Size(16);
font->set_Bold(true);
font->set_Color(System::Drawing::Color::get_Blue());
font->set_Name(u"Arial");
font->set_Underline(Underline::Dash);
// Specify paragraph formatting
System::SharedPtr<ParagraphFormat> paragraphFormat = builder->get_ParagraphFormat();
paragraphFormat->set_FirstLineIndent(8);
paragraphFormat->set_Alignment(ParagraphAlignment::Justify);
paragraphFormat->set_KeepTogether(true);
builder->Writeln(u"A whole paragraph.");
System::String outputPath = outputDataDir + u"DocumentBuilderInsertParagraph.doc";
doc->Save(outputPath);

การใส่ตาราง

อัลกอริทึมพื้นฐานสำหรับการสร้างตารางโดยใช้DocumentBuilderเป็นเรื่องง่าย:

  1. เริ่มต้นตารางด้วยDocumentBuilder.StartTable.
  2. แทรกเซลล์โดยใช้DocumentBuilder.InsertCell นี้จะเริ่มต้นแถวใหม่โดยอัตโนมัติ ถ้าจำเป็นให้ใช้คุณสมบัติDocumentBuilder.CellFormatเพื่อระบุการจัดรูปแบบเซลล์.
  3. แทรกเนื้อหาของเซลล์โดยใช้วิธีการDocumentBuilder.
  4. ทำซ้ำขั้นตอน2และ3จนกว่าแถวจะเสร็จสมบูรณ์.
  5. โทรDocumentBuilder.EndRowเพื่อสิ้นสุดแถวปัจจุบัน หากจำเป็นให้ใช้คุณสมบัติDocumentBuilder.RowFormatเพื่อระบุการจัดรูปแบบแถว.
  6. ทำซ้ำขั้นตอน2-5จนกว่าตารางจะเสร็จสมบูรณ์.
  7. โทรDocumentBuilder.EndTableเพื่อเสร็จสิ้นการสร้างโต๊ะ วิธีการสร้างตารางDocumentBuilderที่เหมาะสมอธิบายไว้ด้านล่าง.

การเริ่มต้นตาราง

การโทรDocumentBuilder.StartTableเป็นขั้นตอนแรกในการสร้างโต๊ะ มารถเรียกภายในเซลล์ซึ่งในกรณีนี้จะเริ่มต้นตารางที่ซ้อนกัน วิธีต่อไปในการโทรคือDocumentBuilder.InsertCell.

การใส่เซลล์

หลังจากคุณเรียก DocumentBuilder->InsertCell แล้ว เซลล์ใหม่จะถูกสร้างขึ้น และเนื้อหาใดๆ ที่คุณเพิ่มโดยใช้เมธอดอื่นของคลาส DocumentBuilder จะถูกเพิ่มลงในเซลล์ปัจจุบัน หากต้องการเริ่มเซลล์ใหม่ในแถวเดียวกัน ให้เรียก DocumentBuilder->InsertCell อีกครั้ง ใช้คุณสมบัติ DocumentBuilder.CellFormat เพื่อระบุการจัดรูปแบบเซลล์ คุณสมบัตินี้จะส่งคืนอ็อบเจ็กต์ CellFormat ที่แสดงการจัดรูปแบบทั้งหมดสำหรับเซลล์ตาราง.

สิ้นสุดแถว

โทรDocumentBuilder.EndRowเพื่อจบแถวปัจจุบัน ถ้าคุณโทรDocumentBuilder->InsertCellทันทีหลังจากนั้นตารางจะยังคงอยู่ในแถวใหม่.

ใช้คุณสมบัติDocumentBuilder.RowFormatเพื่อระบุการจัดรูปแบบแถว ออบเจกต์RowFormatที่แสดงการจัดรูปแบบทั้งหมดสำหรับแถวตาราง.

สิ้นสุดตาราง

โทรDocumentBuilder.EndTableเพื่อเสร็จสิ้นตารางปัจจุบัน วิธีนี้ควรจะเรียกว่าเพียงครั้งเดียวหลังจากที่DocumentBuilder->EndRowถูกเรียกว่า เมื่อเรียกDocumentBuilder.EndTableเลื่อนเคอร์เซอร์ออกจากเซลล์ปัจจุบันไปยังตำแหน่งหลังจากตาราง ตัวอย่างต่อไปนี้แสดงให้เห็นถึงวิธีการสร้างตารางที่จัดรูปแบบที่มี2แถวและ2คอลัมน์.

// The path to the documents directory.
System::String outputDataDir = GetOutputDataDir_WorkingWithDocument();
// Initialize document.
System::SharedPtr<Document> doc = System::MakeObject<Document>();
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc);
System::SharedPtr<Table> table = builder->StartTable();
// Insert a cell
builder->InsertCell();
// Use fixed column widths.
table->AutoFit(AutoFitBehavior::FixedColumnWidths);
builder->get_CellFormat()->set_VerticalAlignment(CellVerticalAlignment::Center);
builder->Write(u"This is row 1 cell 1");
// Insert a cell
builder->InsertCell();
builder->Write(u"This is row 1 cell 2");
builder->EndRow();
// Insert a cell
builder->InsertCell();
// Apply new row formatting
builder->get_RowFormat()->set_Height(100);
builder->get_RowFormat()->set_HeightRule(HeightRule::Exactly);
builder->get_CellFormat()->set_Orientation(TextOrientation::Upward);
builder->Writeln(u"This is row 2 cell 1");
// Insert a cell
builder->InsertCell();
builder->get_CellFormat()->set_Orientation(TextOrientation::Downward);
builder->Writeln(u"This is row 2 cell 2");
builder->EndRow();
builder->EndTable();
System::String outputPath = outputDataDir + u"DocumentBuilderBuildTable.doc";
doc->Save(outputPath);

การใส่ตัวหยุดพัก

หากคุณต้องการเริ่มต้นบรรทัดใหม่อย่างชัดเจนย่อหน้าคอลัมน์ส่วนหรือหน้าให้โทรDocumentBuilder.InsertBreak ส่งผ่านไปยังวิธีการนี้ชนิดของการหยุดพักที่คุณจำเป็นต้องแทรกที่จะแสดงโดยการแจงนับBreakType ตัวอย่างด้านล่างแสดงวิธีการแทรกตัวแบ่งหน้าในเอกสาร.

// The path to the documents directory.
System::String outputDataDir = GetOutputDataDir_WorkingWithDocument();
// Initialize document.
System::SharedPtr<Document> doc = System::MakeObject<Document>();
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc);
builder->Writeln(u"This is page 1.");
builder->InsertBreak(BreakType::PageBreak);
builder->Writeln(u"This is page 2.");
builder->InsertBreak(BreakType::PageBreak);
builder->Writeln(u"This is page 3.");
System::String outputPath = outputDataDir + u"DocumentBuilderInsertBreak.doc";
doc->Save(outputPath);

การแทรกรูปภาพ

DocumentBuilderให้การโอเวอร์โหลดหลายวิธีของDocumentBuilder->InsertImageที่ช่วยให้คุณสามารถแทรกรูปภาพแบบอินไลน์หรือแบบลอ หากรูปภาพเป็นEMFหรือWMFรูปภาพจะถูกแทรกลงในเอกสารในรูปแบบเมไฟล์ รูปภาพอื่นๆทั้งหมดจะถูกเก็บไว้ในรูปแบบPNG วิธีการDocumentBuilder->InsertImageสามารถใช้ภาพจากแหล่งที่มาที่แตกต่างกัน:

  • จากแฟ้มหรือURLโดยการส่งผ่านพารามิเตอร์สตริงDocumentBuilder->InsertImage.
  • จากสตรีมโดยผ่านพารามิเตอร์Stream``DocumentBuilder->InsertImage.
  • จากอ็อบเจ็กต์รูปภาพโดยการส่งพารามิเตอร์รูปภาพDocumentBuilder->InsertImage.
  • จากอาร์เรย์ไบต์โดยผ่านพารามิเตอร์อาร์เรย์ไบต์DocumentBuilder.InsertImageสำหรับแต่ละวิธีDocumentBuilder->InsertImageมีโอเวอร์โหลดเพิ่มเติมซึ่งช่วยให้คุณสามารถแทรกรูปภาพที่มีตัวเลือกต่อไปนี้:
  • อินไลน์หรือลอยตัวในตำแหน่งที่เฉพาะเจาะจงตัวอย่างเช่นDocumentBuilder->InsertImage.
  • ขนาดเปอร์เซ็นต์หรือขนาดที่กำหนดเองตัวอย่างเช่นDocumentBuilder.InsertImage นอกจากนี้วิธีการDocumentBuilder->InsertImageส่งกลับวัตถุShapeที่เพิ่งสร้างและแทรกเพื่อให้คุณสามารถปรับเปลี่ยนคุณสมบัติของรูปร่าง.

การแทรกรูปภาพแบบอินไลน์

ส่งสตริงเดียวที่แสดงถึงไฟล์ที่มีรูปภาพเป็นDocumentBuilder->InsertImageเพื่อแทรกรูปภาพลงในเอกสารเป็นกราฟิกแบบอิน ตัวอย่างด้านล่างแสดงวิธีการแทรกรูปภาพแบบอินไลน์ที่ตำแหน่งเคอร์เซอร์ลงในเอกสาร.

System::SharedPtr<Document> doc = System::MakeObject<Document>();
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc);
builder->InsertImage(inputDataDir + u"Watermark.png");
System::String outputPath = outputDataDir + u"DocumentBuilderInsertImage.InsertInlineImage.doc";
doc->Save(outputPath);

การแทรกรูปภาพแบบลอยตัว(ตำแหน่งที่แน่นอน)

ตัวอย่างนี้แทรกรูปภาพที่ลอยจากแฟ้มหรือURLที่ตำแหน่งและขนาดที่ระบุ.

System::SharedPtr<Document> doc = System::MakeObject<Document>();
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc);
builder->InsertImage(inputDataDir + u"Watermark.png", RelativeHorizontalPosition::Margin, 100, RelativeVerticalPosition::Margin, 100, 200, 100, WrapType::Square);
System::String outputPath = outputDataDir + u"DocumentBuilderInsertImage.InsertFloatingImage.doc";
doc->Save(outputPath);

การใส่บุ๊กมาร์ก

เมื่อต้องการแทรกบุ๊กมาร์กลงในเอกสาร,คุณควรทำต่อไปนี้:

  1. โทรDocumentBuilder->StartBookmarkผ่านชื่อที่ต้องการของบุ๊กมาร์ก.
  2. แทรกข้อความที่คั่นหน้าด้วยวิธีการDocumentBuilder.
  3. โทรDocumentBuilder.EndBookmarkผ่านมันชื่อเดียวกันกับที่คุณใช้กับDocumentBuilder->StartBookmark.
  4. ที่คั่นหน้าสามารถทับซ้อนกันและขยายช่วงใดๆ ในการสร้างบุ๊กมาร์กที่ถูกต้องคุณต้องโทรหาทั้งDocumentBuilder->StartBookmarkและDocumentBuilder->EndBookmarkด้วยชื่อบุ๊กมาร์กเดียวกัน.

ตัวอย่างด้านล่างแสดงวิธีการแทรกบุ๊กมาร์กลงในเอกสารโดยใช้ตัวสร้างเอกสาร.

// The path to the documents directory.
System::String outputDataDir = GetOutputDataDir_WorkingWithDocument();
// Initialize document.
System::SharedPtr<Document> doc = System::MakeObject<Document>();
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc);
builder->StartBookmark(u"FineBookmark");
builder->Writeln(u"This is just a fine bookmark.");
builder->EndBookmark(u"FineBookmark");
System::String outputPath = outputDataDir + u"DocumentBuilderInsertBookmark.doc";
doc->Save(outputPath);

การแทรกฟิลด์Form

เขตข้อมูลฟอร์มเป็นกรณีเฉพาะของเขตข้อมูลคำที่ช่วยให้"โต้ตอบ"กับผู้ใช้ เขตข้อมูลแบบฟอร์มในMicrosoft Wordรวมถึงกล่องกล่องคำสั่งผสมและช่องทำเครื่องหมายDocumentBuilderมีวิธีการพิเศษในการแทรกฟิลด์แบบฟอร์มแต่ละชนิดลงในเอกสาร:DocumentBuilder.InsertTextInput,DocumentBuilder->InsertCheckBoxและDocumentBuilder.InsertComboBox โปรดทราบว่าถ้าคุณระบุชื่อสำหรับฟิลด์ฟอร์มบุ๊กมาร์กจะถูกสร้างขึ้นโดยอัตโนมัติด้วยชื่อเดีย.

การใส่การป้อนข้อความ

DocumentBuilder.InsertTextInputเพื่อแทรกกล่องข้อความลงในเอกสาร ตัวอย่างด้านล่างแสดงวิธีการแทรกฟิลด์แบบฟอร์มการป้อนข้อความลงในเอกสาร.

System::SharedPtr<Document> doc = System::MakeObject<Document>();
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc);
builder->InsertTextInput(u"TextInput", TextFormFieldType::Regular, u"", u"Hello", 0);
System::String outputPath = outputDataDir + u"DocumentBuilderInsertElements.InsertTextInputFormField.doc";
doc->Save(outputPath);

การใส่กล่องกาเครื่องหมาย

โทรDocumentBuilder.InsertCheckBoxเพื่อแทรกกล่องกาเครื่องหมายลงในเอกสาร ตัวอย่างด้านล่างแสดงวิธีการแทรกช่องฟิลด์แบบฟอร์มลงในเอกสาร.

System::SharedPtr<Document> doc = System::MakeObject<Document>();
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc);
builder->InsertCheckBox(u"CheckBox", true, true, 0);
System::String outputPath = outputDataDir + u"DocumentBuilderInsertElements.InsertCheckBoxFormField.doc";
doc->Save(outputPath);

การใส่กล่องคำสั่งผสม

โทรDocumentBuilder.InsertComboBoxเพื่อใส่กล่องคำสั่งผสมลงในเอกสาร ตัวอย่างด้านล่างแสดงวิธีการแทรกกล่องคำสั่งผสมฟิลด์แบบฟอร์มลงในเอกสาร.

System::SharedPtr<Document> doc = System::MakeObject<Document>();
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc);
System::ArrayPtr<System::String> items = System::MakeArray<System::String>({u"One", u"Two", u"Three"});
builder->InsertComboBox(u"DropDown", items, 0);
System::String outputPath = outputDataDir + u"DocumentBuilderInsertElements.InsertComboBoxFormField.doc";
doc->Save(outputPath);

การแทรกโลแคลที่ระดับฟิลด์

ลูกค้าสามารถระบุสถานที่ที่ระดับสนามในขณะนี้และสามารถบรรลุการควบคุมที่ดีขึ้น รหัสโลแคลสามารถเชื่อมโยงกับแต่ละฟิลด์ภายในDocumentBuilder ตัวอย่างด้านล่างแสดงให้เห็นถึงวิธีการใช้ตัวเลือกนี้.

System::String outputDataDir = GetOutputDataDir_WorkingWithFields();
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>();
System::SharedPtr<Field> field = builder->InsertField(FieldType::FieldDate, true);
field->set_LocaleId(1049);
builder->get_Document()->Save(outputDataDir + u"SpecifylocaleAtFieldlevel.docx");

การแทรกการเชื่อมโยงหลายมิติ

ใช้DocumentBuilder.InsertHyperlinkเพื่อแทรกการเชื่อมโยงหลายมิติในเอกสาร (URLหรือชื่อของบุ๊กมาร์กภายในเอกสาร)และพารามิเตอร์บูลีนที่ควรเป็นจริงถ้าURLเป็นชื่อของบุ๊กมDocumentBuilder.InsertHyperlinkโทรภายในDocumentBuilder.InsertField.วิธีการเพิ่มเครื่องหมายวรรคตอนที่จุดเริ่มต้นและจุดสิ้นสุดของURLเสมอ โปรดทราบว่าคุณต้องระบุการจัดรูปแบบแบบอักษรสำหรับข้อความแสดงการเชื่อมโยงหลายมิติอย่างชัดเจนโดยใช้คุณสมบัติFont ตัวอย่างด้านล่างแทรกการเชื่อมโยงหลายมิติลงในเอกสารโดยใช้DocumentBuilder.

การแทรกวัตถุโอเล

ถ้าคุณต้องการเรียกวัตถุโอลิDocumentBuilder.InsertOleObject ส่งผ่านไปยังเมธอดนี้ProgIdอย่างชัดเจนกับพารามิเตอร์อื่นๆ ตัวอย่างด้านล่างแสดงวิธีการแทรกวัตถุโอเลต์ลงในเอกสาร.

System::SharedPtr<Document> doc = System::MakeObject<Document>();
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc);
builder->InsertOleObject(u"http://www.aspose.com", u"htmlfile", true, true, nullptr);
System::String outputPath = outputDataDir + u"DocumentBuilderInsertElements.InsertOleObject.doc";
doc->Save(outputPath);

ตั้งชื่อไฟล์และนามสกุลเมื่อแทรกวัตถุโอเล

OLEแพคเกจเป็นมรดกและ"ไม่มีเอกสาร"วิธีการจัดเก็บวัตถุที่ฝังตัวถ้าOLEตัวจัดการไม่เป็นที่รู้จัก ต้นWindowsรุ่นเช่นWindows3.1,95 และ 98 มีห่อ.การประมวลผลข้อมูล,คีย์ข้อมูล,การพิมพ์ดีดคัดลอก ตอนนี้โปรแกรมนี้ได้รับการยกเว้นจากWindowsแต่MSคำและโปรแกรมอื่นๆยังคงใช้ในการฝังข้อมูลถ้าOLEตัวจัดการหายไปหรือไม่ทราบ OlePackageชั้นช่วยให้สามารถเข้าถึงOLEคุณสมบัติแพคเกจ. ตัวอย่างด้านล่างแสดงวิธีการตั้งค่าชื่อไฟล์นามสกุลและชื่อที่แสดงสำหรับแพคเกจOLE.

System::SharedPtr<Document> doc = System::MakeObject<Document>();
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc);
System::ArrayPtr<uint8_t> bs = System::IO::File::ReadAllBytes(inputDataDir + u"input.zip");
System::SharedPtr<System::IO::Stream> stream = System::MakeObject<System::IO::MemoryStream>(bs);
System::SharedPtr<Shape> shape = builder->InsertOleObject(stream, u"Package", true, nullptr);
System::SharedPtr<OlePackage> olePackage = shape->get_OleFormat()->get_OlePackage();
olePackage->set_FileName(u"filename.zip");
olePackage->set_DisplayName(u"displayname.zip");
System::String outputPath = outputDataDir + u"DocumentBuilderInsertElements.InsertOleObjectwithOlePackage.doc";
doc->Save(outputPath);

การแทรกHTML

คุณสามารถแทรกสตริงHTMLที่มีส่วนHTMLหรือเอกสารทั้งหมดHTMLลงในเอกสารคำ เพียงแค่ผ่านสตริงนี้ไปยังวิธีการDocumentBuilder->InsertHtml หนึ่งในการใช้งานที่มีประโยชน์ของวิธีนี้คือการจัดเก็บสตริงของHTMLในฐานข้อมูลและแทรกลงในเอกสารระหว่างmail mergeเพื่อรับเนื้อหาที่จัดรูปแบบเพิ่มขึ้นแทนที่จะสร้างโดยใช้วิธีการต่างๆของตัวสร้างเอกสาร ตัวอย่างด้านล่างแสดงแทรกHTMLลงในเอกสารโดยใช้DocumentBuilder.

System::SharedPtr<Document> doc = System::MakeObject<Document>();
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc);
builder->InsertHtml(u"<P align='right'>Paragraph right</P><b>Implicit paragraph left</b><div align='center'>Div center</div><h1 align='left'>Heading 1 left.</h1>");
System::String outputPath = outputDataDir + u"DocumentBuilderInsertElements.InsertHtml.doc";
doc->Save(outputPath);

แทรกกฎแนวนอนลงในเอกสาร

เป็นlow codeตัวอย่างแสดงวิธีการแทรกรูปร่างกฎแนวนอนลงในเอกสารโดยใช้วิธีการDocumentBuilder->InsertHorizontalRule.

// Initialize document.
System::SharedPtr<Document> doc = System::MakeObject<Document>();
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc);
builder->Writeln(u"Insert a horizontal rule shape into the document.");
builder->InsertHorizontalRule();
System::String outputPath = outputDataDir + u"DocumentBuilderInsertHorizontalRule.doc";
doc->Save(outputPath);