ใช้DocumentBuilderเพื่อแก้ไขเอกสารได้อย่างง่ายดาย
การระบุการจัดรูปแบบ
การจัดรูปแบบแบบอักษร
การจัดรูปแบบแบบอักษรปัจจุบันจะแสดงโดยออบเจกต์Font
ที่ส่งคืนโดยคุณสมบัติDocumentBuilder.Font
คลาสFont
ประกอบด้วยคุณสมบัติแบบอักษรที่หลากหลายที่เป็นไปได้ในMicrosoft Word ตัวอย่างด้านล่างแสดงวิธีการตั้งค่าการจัดรูปแบบแบบอักษร.
System::SharedPtr<Document> doc = System::MakeObject<Document>(); | |
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc); | |
// Set font formatting properties | |
System::SharedPtr<Font> font = builder->get_Font(); | |
font->set_Bold(true); | |
font->set_Color(System::Drawing::Color::get_DarkBlue()); | |
font->set_Italic(true); | |
font->set_Name(u"Arial"); | |
font->set_Size(24); | |
font->set_Spacing(5); | |
font->set_Underline(Underline::Double); | |
// Output formatted text | |
builder->Writeln(u"I'm a very nice formatted string."); | |
System::String outputPath = outputDataDir + u"DocumentBuilderSetFormatting.SetFontFormatting.doc"; | |
doc->Save(outputPath); |
การจัดรูปแบบย่อหน้า
การจัดรูปแบบย่อหน้าปัจจุบันจะแสดงโดยออบเจกต์ParagraphFormat
ที่ถูกส่งคืนโดยคุณสมบัติDocumentBuilder.ParagraphFormat
วัตถุนี้ประกอบด้วยคุณสมบัติการจัดรูปแบบย่อหน้าต่างๆที่มีอยู่ในMicrosoft Word คุณสามารถรีเซ็ตการจัดรูปแบบย่อหน้าเพื่อเริ่มต้นให้เป็นรูปแบบปกติชิดซ้ายเยื้องไม่มีระยะห่างไม่มีเส้นขอบและไม่มีการแรเงาโดยการโทรParagraphFormat.ClearFormatting
ตัวอย่างด้านล่างแสดงวิธีการตั้งค่าการจัดรูปแบบย่อหน้า.
System::SharedPtr<Document> doc = System::MakeObject<Document>(); | |
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc); | |
// Set paragraph formatting properties | |
System::SharedPtr<ParagraphFormat> paragraphFormat = builder->get_ParagraphFormat(); | |
paragraphFormat->set_Alignment(ParagraphAlignment::Center); | |
paragraphFormat->set_LeftIndent(50); | |
paragraphFormat->set_RightIndent(50); | |
paragraphFormat->set_SpaceAfter(25); | |
// Output text | |
builder->Writeln(u"I'm a very nice formatted paragraph. I'm intended to demonstrate how the left and right indents affect word wrapping."); | |
builder->Writeln(u"I'm another nice formatted paragraph. I'm intended to demonstrate how the space after paragraph looks like."); | |
System::String outputPath = outputDataDir + u"DocumentBuilderSetFormatting.SetParagraphFormatting.doc"; | |
doc->Save(outputPath); |
การทำงานกับตัวอักษรเอเชีย
ปรับช่องว่างโดยอัตโนมัติระหว่างข้อความเอเชียและภาษาละติน
ของแม่แบบฟอร์มโดยการควบคุมช่องว่างระหว่างข้อความทั้งสองชนิดคุณสามารถกำหนดค่ คุณสามารถใช้AddSpaceBetweenFarEastAndAlphaและAddSpaceBetweenFarEastAndDigitคุณสมบัติของParagraphFormatชั้นเรียนเพื่อให้บรรลุนี้.
ตัวอย่างรหัสต่อไปนี้แสดงวิธีใช้ParagraphFormat.AddSpaceBetweenFarEastAndAlpha
และParagraphFormat.AddSpaceBetweenFarEastAndDigit
คุณสมบัติ.
System::SharedPtr<Document> doc = System::MakeObject<Document>(); | |
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc); | |
// Set paragraph formatting properties | |
System::SharedPtr<ParagraphFormat> paragraphFormat = builder->get_ParagraphFormat(); | |
paragraphFormat->set_AddSpaceBetweenFarEastAndAlpha(true); | |
paragraphFormat->set_AddSpaceBetweenFarEastAndDigit(true); | |
builder->Writeln(u"Automatically adjust space between Asian and Latin text"); | |
builder->Writeln(u"Automatically adjust space between Asian text and numbers"); | |
System::String outputPath = outputDataDir + u"DocumentBuilderSetFormatting.SetSpacebetweenAsianandLatintext.doc"; | |
doc->Save(outputPath); |
เปลี่ยนระยะห่างย่อหน้าเอเชียและเยื้อง
ตัวอย่างรหัสต่อไปนี้แสดงให้เห็นถึงวิธีการเปลี่ยนระยะห่างย่อหน้าเอเชียและเยื้อง.
System::SharedPtr<Document> doc = System::MakeObject<Document>(); | |
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc); | |
// Set paragraph formatting properties | |
System::SharedPtr<ParagraphFormat> paragraphFormat = builder->get_ParagraphFormat(); | |
paragraphFormat->set_AddSpaceBetweenFarEastAndAlpha(true); | |
paragraphFormat->set_AddSpaceBetweenFarEastAndDigit(true); | |
builder->Writeln(u"Automatically adjust space between Asian and Latin text"); | |
builder->Writeln(u"Automatically adjust space between Asian text and numbers"); | |
System::String outputPath = outputDataDir + u"DocumentBuilderSetFormatting.SetSpacebetweenAsianandLatintext.doc"; | |
doc->Save(outputPath); |
ตั้งค่าตัวเลือกแบ่งบรรทัด
แท็บการพิมพ์เอเชียของกล่องโต้ตอบคุณสมบัติParagraph
ในMSคำมีกลุ่มแบ่งบรรทัด ตัวเลือกของกลุ่มนี้สามารถตั้งค่าได้โดยใช้FarEastLineBreakControl, WordWrap, HangingPunctuation คุณสมบัติของParagraphFormatชั้น เป็นlow codeตัวอย่างแสดงวิธีใช้คุณสมบัติเหล่านี้.
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"Input.docx"); | |
System::SharedPtr<ParagraphFormat> format = doc->get_FirstSection()->get_Body()->get_Paragraphs()->idx_get(0)->get_ParagraphFormat(); | |
format->set_FarEastLineBreakControl(false); | |
format->set_WordWrap(true); | |
format->set_HangingPunctuation(false); | |
System::String outputPath = outputDataDir + u"DocumentBuilderSetFormatting.SetAsianTypographyLinebreakGroupProp.docx"; | |
doc->Save(outputPath); |
การจัดรูปแบบเซลล์
การจัดรูปแบบเซลล์ถูกนำมาใช้ในระหว่างการสร้างตาราง ออบเจกต์CellFormat
ที่ส่งคืนโดยคุณสมบัติDocumentBuilder.CellFormat
CellFormatห่อหุ้มคุณสมบัติของเซลล์ตารางต่างๆเช่นความกว้างหรือแนวตั้ง ตัวอย่างด้านล่างแสดงวิธีการสร้างตารางที่ประกอบด้วยเซลล์ที่จัดรูปแบบเดียว.
System::SharedPtr<Document> doc = System::MakeObject<Document>(); | |
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc); | |
builder->StartTable(); | |
builder->InsertCell(); | |
// Set the cell formatting | |
System::SharedPtr<CellFormat> cellFormat = builder->get_CellFormat(); | |
cellFormat->set_Width(250); | |
cellFormat->set_LeftPadding(30); | |
cellFormat->set_RightPadding(30); | |
cellFormat->set_TopPadding(30); | |
cellFormat->set_BottomPadding(30); | |
builder->Writeln(u"I'm a wonderful formatted cell."); | |
builder->EndRow(); | |
builder->EndTable(); | |
System::String outputPath = outputDataDir + u"DocumentBuilderSetFormatting.SetTableCellFormatting.doc"; | |
doc->Save(outputPath); |
การจัดรูปแบบแถว
การจัดรูปแบบแถวปัจจุบันจะถูกกำหนดโดยRowFormat
วัตถุที่ถูกส่งคืนโดยคุณสมบัติDocumentBuilder.RowFormat
วัตถุห่อหุ้มข้อมูลเกี่ยวกับการจัดรูปแบบแถวตารางทั้งหมด ตัวอย่างด้านล่างแสดงวิธีการสร้างตารางที่มีเซลล์เดียวและใช้การจัดรูปแบบแถว.
System::SharedPtr<Document> doc = System::MakeObject<Document>(); | |
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc); | |
System::SharedPtr<Table> table = builder->StartTable(); | |
builder->InsertCell(); | |
// Set the row formatting | |
System::SharedPtr<RowFormat> rowFormat = builder->get_RowFormat(); | |
rowFormat->set_Height(100); | |
rowFormat->set_HeightRule(HeightRule::Exactly); | |
// These formatting properties are set on the table and are applied to all rows in the table. | |
table->set_LeftPadding(30); | |
table->set_RightPadding(30); | |
table->set_TopPadding(30); | |
table->set_BottomPadding(30); | |
builder->Writeln(u"I'm a wonderful formatted row."); | |
builder->EndRow(); | |
builder->EndTable(); | |
System::String outputPath = outputDataDir + u"DocumentBuilderSetFormatting.SetTableRowFormatting.doc"; | |
doc->Save(outputPath); |
การจัดรูปแบบรายการ
Aspose.Wordsช่วยให้การสร้างง่ายของรายการโดยการใช้การจัดรูปแบบรายการ DocumentBuilderให้คุณDocumentBuilder.ListFormat
ที่ส่งกลับวัตถุListFormat
วัตถุนี้มีหลายวิธีในการเริ่มต้นและสิ้นสุดรายการและการเพิ่ม/ลดการเยื้อง มีสองประเภททั่วไปของรายการในMicrosoft Word:สัญลักษณ์แสดงหัวข้อย่อยและหมายเลข.
- ในการเริ่มต้นรายการที่มีสัญลักษณ์แสดงหัวข้อย่อยให้โทร
ListFormat.ApplyBulletDefault
. - ในการเริ่มรายการหมายเลขให้โทร
ListFormat.ApplyNumberDefault
.
นย่อหน้าปัจจุบันและย่อหน้าต่อไปทั้งหมดที่สร้างขึ้นโดยใช้DocumentBuilderจนกระทั่งListFormat.RemoveNumbers
ถูกเรียกว่าเพื่อหยุดกา ในคำเอกสารรายการอาจประกอบด้วยถึงเก้าระดับ การจัดรูปแบบรายการสำหรับแต่ละระดับระบุสิ่งที่กระสุนหรือหมายเลขที่ใช้เยื้องซ้ายช่องว่.
- หากต้องการเพิ่มระดับรายการของย่อหน้าปัจจุบันโดยหนึ่งระดับให้โทร
ListFormat.ListIndent
. - เมื่อต้องการลดระดับรายการของย่อหน้าปัจจุบันโดยหนึ่งระดับให้โทร
ListFormat.ListOutdent
.
เมธอดเปลี่ยนระดับรายการและใช้คุณสมบัติการจัดรูปแบบของระดับใหม่.
ListFormat.ListLevelNumber
เพื่อรับหรือตั้งค่าระดับรายการสำหรับย่อหน้า ระดับรายการมีหมายเลข0ถึง8.
ตัวอย่างด้านล่างแสดงให้เห็นถึงวิธีการสร้างรายการหลายระดับ.
System::SharedPtr<Document> doc = System::MakeObject<Document>(); | |
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc); | |
builder->get_ListFormat()->ApplyNumberDefault(); | |
builder->Writeln(u"Item 1"); | |
builder->Writeln(u"Item 2"); | |
builder->get_ListFormat()->ListIndent(); | |
builder->Writeln(u"Item 2.1"); | |
builder->Writeln(u"Item 2.2"); | |
builder->get_ListFormat()->ListIndent(); | |
builder->Writeln(u"Item 2.2.1"); | |
builder->Writeln(u"Item 2.2.2"); | |
builder->get_ListFormat()->ListOutdent(); | |
builder->Writeln(u"Item 2.3"); | |
builder->get_ListFormat()->ListOutdent(); | |
builder->Writeln(u"Item 3"); | |
builder->get_ListFormat()->RemoveNumbers(); | |
System::String outputPath = outputDataDir + u"DocumentBuilderSetFormatting.SetMultilevelListFormatting.doc"; | |
doc->Save(outputPath); |
การตั้งค่าหน้าเว็บและการจัดรูปแบบส่วน
ตั้งค่าหน้าเว็บและส่วนคุณสมบัติถูกห่อหุ้มในPageSetup
วัตถุที่ถูกส่งคืนโดยคุณสมบัติDocumentBuilder.PageSetup
วัตถุประกอบด้วยแอตทริบิวต์การตั้งค่าหน้าเว็บทั้งหมดของส่วน(ขอบซ้ายขอบล่างขนาดกระด ตัวอย่างด้านล่างแสดงวิธีการตั้งค่าคุณสมบัติเช่นขนาดหน้าและการวางแนวสำหรับส่วนปัจ.
System::SharedPtr<Document> doc = System::MakeObject<Document>(); | |
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc); | |
// Set page properties | |
builder->get_PageSetup()->set_Orientation(Orientation::Landscape); | |
builder->get_PageSetup()->set_LeftMargin(50); | |
builder->get_PageSetup()->set_PaperSize(PaperSize::Paper10x14); | |
System::String outputPath = outputDataDir + u"DocumentBuilderSetFormatting.SetPageSetupAndSectionFormatting.doc"; | |
doc->Save(outputPath); |
การใช้สไตล์
บางรูปแบบวัตถุเช่นแบบอักษรหรือParagraphFormatรูปแบบการสนับสนุน สไตล์ที่ผู้ใช้กำหนดในตัวเดียวจะแสดงโดยออบเจกต์Style
ที่มีคุณสมบัติลักษณะที่สอดคล้องกันเช่น.
นอกจากนี้ออบเจกต์StyleมีคุณสมบัติStyle.StyleIdentifier
ที่ส่งคืนตัวระบุสไตล์อิสระโลแคลที่แสดงโดยค่าการแจงนับStyle.StyleIdentifier ประเด็นคือชื่อของลักษณะในตัวในMicrosoft Wordเป็นภาษาท้องถิ่นสำหรับภาษาที่แตกต่างกัน ใช้ตัวระบุลักษณะคุณสามารถค้นหาลักษณะที่ถูกต้องโดยไม่คำนึงถึงภาษาของเอกสาร ค่าการแจงนับสอดคล้องกับMicrosoft Wordรูปแบบในตัวเช่นปกติหัวข้อ1หัวข้อ2ฯลฯ รูปแบบที่ผู้ใช้กำหนดทั้งหมดจะถูกกำหนดStyleIdentifier.User value ตัวอย่างด้านล่างแสดงวิธีใช้สไตล์ย่อหน้า.
System::SharedPtr<Document> doc = System::MakeObject<Document>(); | |
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc); | |
// Set paragraph style | |
builder->get_ParagraphFormat()->set_StyleIdentifier(StyleIdentifier::Title); | |
builder->Write(u"Hello"); | |
System::String outputPath = outputDataDir + u"DocumentBuilderSetFormatting.ApplyParagraphStyle.doc"; | |
doc->Save(outputPath); |
เส้นขอบและการแรเงา
เส้นขอบจะแสดงด้วยBorderCollection นี่คือคอลเลกชันของวัตถุเส้นขอบที่เข้าถึงโดยดัชนีหรือตามชนิดของเส้นขอบ ประเภทเส้นขอบจะแสดงโดยการแจงนับBorderType
ค่าบางอย่างของการนับมีผลบังคับใช้กับหลายหรือเพียงองค์ประกอบเอกสารหนึ่ง ตัวอย่างเช่นBorderType.Bottom
ใช้ได้กับย่อหน้าหรือเซลล์ตารางในขณะที่BorderType.DiagonalDown
ระบุขอบเส้นทแยงมุมในเซลล์ตาราง.
ทั้งคอลเลกชันชายแดนและแต่ละชายแดนแยกต่างหากมีคุณลักษณะที่คล้ายกันเช่นสีรูปแบบเส้ คุณสมบัติของชื่อเดียวกัน คุณสามารถบรรลุประเภทเส้นขอบที่แตกต่างกันโดยการรวมค่าคุณสมบัติ นอกจากนี้วัตถุทั้งBorderCollectionและBorderช่วยให้คุณสามารถรีเซ็ตค่าเหล่านี้เป็นค่าเริ่มต้นโดยการเรียกวิธีการBorder.ClearFormatting
โปรดทราบว่าเมื่อรีเซ็ตคุณสมบัติเส้นขอบเป็นค่าเริ่มต้นเส้นขอบจะมองไม่เห็น คลาสShading
ประกอบด้วยแอตทริบิวต์การแรเงาสำหรับอิลิเมนต์เอกสาร คุณสามารถตั้งค่าพื้นผิวการแรเงาที่ต้องการและสีที่ใช้กับพื้นหลังและเบื้องหน้าขององค์ป.
พื้นผิวการแรเงาถูกตั้งค่าด้วยค่าการแจงนับTextureIndex
ที่ช่วยให้การประยุกต์ใช้รูปแบบต่างๆไปยังวัตถุShading ตัวอย่างเช่นในการตั้งค่าสีพื้นหลังสำหรับองค์ประกอบเอกสารให้ใช้ค่าTextureIndex.TextureSolid
และตั้งค่าสีแรเงา ตัวอย่างด้านล่างแสดงวิธีการใช้เส้นขอบและการแรเงากับย่อหน้า.
System::SharedPtr<Document> doc = System::MakeObject<Document>(); | |
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc); | |
// Set paragraph borders | |
System::SharedPtr<BorderCollection> borders = builder->get_ParagraphFormat()->get_Borders(); | |
borders->set_DistanceFromText(20); | |
borders->idx_get(BorderType::Left)->set_LineStyle(LineStyle::Double); | |
borders->idx_get(BorderType::Right)->set_LineStyle(LineStyle::Double); | |
borders->idx_get(BorderType::Top)->set_LineStyle(LineStyle::Double); | |
borders->idx_get(BorderType::Bottom)->set_LineStyle(LineStyle::Double); | |
// Set paragraph shading | |
System::SharedPtr<Shading> shading = builder->get_ParagraphFormat()->get_Shading(); | |
shading->set_Texture(TextureIndex::TextureDiagonalCross); | |
shading->set_BackgroundPatternColor(System::Drawing::Color::get_LightCoral()); | |
shading->set_ForegroundPatternColor(System::Drawing::Color::get_LightSalmon()); | |
builder->Write(u"I'm a formatted paragraph with double border and nice shading."); | |
System::String outputPath = outputDataDir + u"DocumentBuilderSetFormatting.ApplyBordersAndShadingToParagraph.doc"; | |
doc->Save(outputPath); |
สแนปไปยังตาราง
Aspose.WordsมีสองคุณสมบัติParagraphFormat.SnapToGrid
และFont.SnapToGrid
ที่จะได้รับและการตั้งค่าคุณสมบัติย่อหน้าสแน็ปในตาราง.
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"Input.docx"); | |
System::SharedPtr<Paragraph> par = doc->get_FirstSection()->get_Body()->get_FirstParagraph(); | |
par->get_ParagraphFormat()->set_SnapToGrid(true); | |
par->get_Runs()->idx_get(0)->get_Font()->set_SnapToGrid(true); | |
const System::String outputPath = outputDataDir + u"SetSnapToGrid_out.doc"; | |
doc->Save(outputPath); |
การย้ายเคอร์เซอร์
การตรวจหาตำแหน่งเคอร์เซอร์ปัจจุบัน
คุณสามารถขอรับตำแหน่งเคอร์เซอร์ของผู้สร้างในปัจจุบันได้ตลอดเวลา คุณสมบัติDocumentBuilder.CurrentNode
จะส่งคืนโหนดที่เลือกอยู่ในตัวสร้างนี้ในปัจจุบัน โหนดเป็นลูกโดยตรงของย่อหน้า การแทรกการดำเนินการใดๆที่คุณดำเนินการโดยใช้DocumentBuilder
จะแทรกก่อนDocumentBuilder.CurrentNode
เมื่อย่อหน้าปัจจุบันว่างเปล่าหรือวางเคอร์เซอร์ไว้ก่อนสิ้นย่อหน้าDocumentBuilder.CurrentNode
จะเป็นโมฆะ.
นอกจากนี้คุณสามารถใช้คุณสมบัติDocumentBuilder.CurrentParagraph
ซึ่งได้รับย่อหน้าที่ถูกเลือกในDocumentBuilderนี้ ตัวอย่างด้านล่างแสดงวิธีเข้าถึงโหนดปัจจุบันในตัวสร้างเอกสาร คุณสามารถดาวน์โหลดไฟล์แม่แบบของตัวอย่างนี้ได้จากที่นี่.
// Shows how to access the current node in a document builder. | |
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"DocumentBuilder.doc"); | |
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc); | |
System::SharedPtr<Node> curNode = builder->get_CurrentNode(); | |
System::SharedPtr<Paragraph> curParagraph = builder->get_CurrentParagraph(); |
ย้ายไปยังโหนดใดๆ(ย่อหน้าและบุตรหลานของพวกเขา)
ถ้าคุณมีโหนดวัตถุเอกสารซึ่งเป็นย่อหน้าหรือลูกโดยตรงของย่อหน้าคุณสามารถชี้เคอร์เซอร์ ใช้วิธีการDocumentBuilder.MoveTo
เพื่อดำเนินการนี้ ตัวอย่างด้านล่างแสดงวิธีย้ายตำแหน่งเคอร์เซอร์ไปยังโหนดที่ระบุ คุณสามารถดาวน์โหลดไฟล์แม่แบบของตัวอย่างนี้ได้จากที่นี่.
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"DocumentBuilder.doc"); | |
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc); | |
builder->MoveTo(doc->get_FirstSection()->get_Body()->get_LastParagraph()); |
การย้ายไปยังเอกสารเริ่มต้น/สิ้นสุด
หากคุณต้องการย้ายไปยังจุดเริ่มต้นของเอกสารโทรDocumentBuilder.MoveToDocumentStart
ถ้าคุณต้องการที่จะย้ายไปยังส่วนท้ายของเอกสาร,โทรDocumentBuilder.MoveToDocumentEnd
. ตัวอย่างด้านล่างแสดงวิธีการย้ายตำแหน่งเคอร์เซอร์ไปที่จุดเริ่มต้นหรือจุดสิ้นสุดของเอกส คุณสามารถดาวน์โหลดไฟล์แม่แบบของตัวอย่างนี้ได้จากที่นี่.
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"DocumentBuilder.doc"); | |
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc); | |
builder->MoveToDocumentEnd(); | |
std::cout << "This is the end of the document." << std::endl; | |
builder->MoveToDocumentStart(); | |
std::cout << "This is the beginning of the document." << std::endl; |
การย้ายไปยังส่วน
หากคุณกำลังทำงานกับเอกสารที่มีหลายส่วนคุณสามารถย้ายไปยังส่วนที่ต้องการโดยใช้DocumentBuilder.MoveToSection
เมธอดนี้จะเลื่อนเคอร์เซอร์ไปที่จุดเริ่มต้นของส่วนที่ระบุและยอมรับดัชนีของส่วนที่ต้องการ เมื่อดัชนีส่วนมากกว่าหรือเท่ากับ0จะระบุดัชนีจากจุดเริ่มต้นของเอกสารด้วย0เป็นส่วนแรก เมื่อดัชนีส่วนน้อยกว่า0จะระบุดัชนีจากส่วนท้ายของเอกสารด้วย -1 เป็นส่วนสุดท้าย ตัวอย่างด้านล่างแสดงวิธีการย้ายตำแหน่งเคอร์เซอร์ไปยังส่วนที่ระบุ คุณสามารถดาวน์โหลดไฟล์แม่แบบของตัวอย่างนี้ได้จากที่นี่.
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"DocumentBuilder.doc"); | |
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc); | |
// Parameters are 0-index. Moves to third section. | |
builder->MoveToSection(2); | |
builder->Writeln(u"This is the 3rd section."); |
การย้ายไปยังส่วนหัว/ส่วนท้าย
เมื่อคุณต้องการวางข้อมูลบางอย่างลงในส่วนหัวหรือส่วนท้าย,คุณควรย้ายไปที่นั่นก่อนโดยใช้DocumentBuilder.MoveToHeaderFooter
. เมธอดยอมรับค่าการแจงนับHeaderFooterTypeที่ระบุชนิดของส่วนหัวหรือส่วนท้ายที่ควรย้ายเคอร์เซอร์ หากคุณต้องการสร้างส่วนหัวและท้ายกระดาษที่แตกต่างกันสำหรับหน้าแรกคุณต้องตั้งค่าคุณสมบัติPageSetup.DifferentFirstPageHeaderFooter
เป็นtrue หากคุณต้องการสร้างส่วนหัวและท้ายกระดาษที่แตกต่างกันสำหรับหน้าคู่และหน้าคี่คุณต้องตั้งPageSetup.OddAndEvenPagesHeaderFooter
ถึงtrue.
หากคุณต้องการกลับไปที่เรื่องราวหลักให้ใช้DocumentBuilder.MoveToSectionเพื่อย้ายออกจากส่วนหัวหรือส่วนท้าย ตัวอย่างด้านล่างจะสร้างส่วนหัวและท้ายกระดาษในเอกสารโดยใช้DocumentBuilder.
การย้ายไปยังย่อหน้า
ใช้DocumentBuilder.MoveToParagraph
เพื่อย้ายเคอร์เซอร์ไปยังย่อหน้าที่ต้องการในส่วนปัจจุบัน คุณควรส่งพารามิเตอร์สองตัวไปยังวิธีนี้:paragraphIndex(ดัชนีของย่อหน้าที่จะย้ายไป)และcharacterIndex(ดัชนีของอักขระภาย.
การนำทางจะดำเนินการภายในเรื่องปัจจุบันของส่วนปัจจุบัน อร์เซอร์ไปยังส่วนหัวหลักของส่วนแรกแล้วparagraphIndexจะระบุดัชนีของย่อหน้าภายในส่วนหัวของส่วนนั้น.
เมื่อparagraphIndexมากกว่าหรือเท่ากับ0จะระบุดัชนีจากจุดเริ่มต้นของส่วนที่มี0เป็นย่อหน้าแรก เมื่อparagraphIndexน้อยกว่า0จะระบุดัชนีจากส่วนท้ายของส่วนที่มี -1 เป็นย่อหน้าสุดท้าย.
ขณะนี้ดัชนีอักขระสามารถระบุเป็น0เพื่อย้ายไปยังจุดเริ่มต้นของย่อหน้าหรือ -1 เพื่อย้ายไปยังจุดสิ้นสุดของย่อหน้า ตัวอย่างด้านล่างแสดงวิธีการย้ายตำแหน่งเคอร์เซอร์ไปยังย่อหน้าที่ คุณสามารถดาวน์โหลดไฟล์แม่แบบของตัวอย่างนี้ได้จากที่นี่.
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"DocumentBuilder.doc"); | |
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc); | |
// Parameters are 0-index. Moves to third paragraph. | |
builder->MoveToParagraph(2, 0); | |
builder->Writeln(u"This is the 3rd paragraph."); |
การย้ายไปยังเซลล์ตาราง
ใช้DocumentBuilder.MoveToCell
หากคุณต้องการเลื่อนเคอร์เซอร์ไปยังเซลล์ตารางในส่วนปัจจุบัน วิธีนี้ยอมรับสี่พารามิเตอร์:
- tableIndex-ดัชนีของตารางที่จะย้ายไป.
- rowIndex-ดัชนีของแถวในตาราง.
- columnIndex-ดัชนีของคอลัมน์ในตาราง.
- characterIndex-ดัชนีของอักขระภายในเซลล์.
การนำทางจะดำเนินการภายในเรื่องปัจจุบันของส่วนปัจจุบัน สำหรับพารามิเตอร์ดัชนีเมื่อดัชนีมากกว่าหรือเท่ากับ0จะระบุดัชนีตั้งแต่ต้นด้วย0เป็นองค์ประก เมื่อดัชนีน้อยกว่า0จะระบุดัชนีจากจุดสิ้นสุดด้วย -1 เป็นองค์ประกอบสุดท้าย.
นอกจากนี้โปรดทราบว่าcharacterIndexขณะนี้สามารถระบุ0เพื่อย้ายไปยังจุดเริ่มต้นของเซลล์หรือ -1 เพื่อย้ายไปยังจุดสิ้นสุดของเซลล์ ตัวอย่างด้านล่างแสดงวิธีการย้ายตำแหน่งเคอร์เซอร์ไปยังเซลล์ตารางที่ระบุ คุณสามารถดาวน์โหลดไฟล์แม่แบบของตัวอย่างนี้ได้จากที่นี่.
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"DocumentBuilder.doc"); | |
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc); | |
// All parameters are 0-index. Moves to the 2nd table, 3rd row, 5th cell. | |
builder->MoveToCell(1, 2, 4, 0); | |
builder->Writeln(u"Hello World!"); |
การย้ายไปยังบุ๊กมาร์ก
ที่คั่นหน้าจะใช้บ่อยเพื่อทำเครื่องหมายสถานที่เฉพาะในเอกสารที่จะแทรกองค์ประกอบใหม่ เมื่อต้องการย้ายไปยังบุ๊กมาร์กใช้DocumentBuilder.MoveToBookmark
วิธีการนี้มีสองเกินพิกัด ที่ง่ายที่สุดยอมรับอะไรแต่ชื่อของบุ๊คมาร์คที่เคอร์เซอร์จะถูกย้าย ตัวอย่างด้านล่างแสดงวิธีการย้ายตำแหน่งเคอร์เซอร์ไปยังบุ๊กมาร์ก คุณสามารถดาวน์โหลดไฟล์แม่แบบของตัวอย่างนี้ได้จากที่นี่.
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"DocumentBuilder.doc"); | |
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc); | |
builder->MoveToBookmark(u"CoolBookmark"); | |
builder->Writeln(u"This is a very cool bookmark."); |
การโอเวอร์โหลดนี้จะเลื่อนเคอร์เซอร์ไปยังตำแหน่งหลังจากที่เริ่มต้นของบุ๊กมาร์กที่มีชื่อ เกินพิกัดอีกDocumentBuilder.MoveToBookmark
เลื่อนเคอร์เซอร์ไปยังบุ๊กมาร์กที่มีความแม่นยำมากขึ้น มันยอมรับสองพารามิเตอร์บูลีน:
- isStartกำหนดว่าจะย้ายเคอร์เซอร์ไปยังจุดเริ่มต้นหรือจุดสิ้นสุดของบุ๊กมาร์ก.
- isAfterกำหนดว่าจะย้ายเคอร์เซอร์ไปหลังตำแหน่งเริ่มต้นหรือจุดสิ้นสุดของบุ๊กมาร์กหรือเลื่อนเคอร์เซอร์ไปก่อนตำแหน่งเริ่มต้นหรือจุดสิ้นสุดของบุ๊กมาร์ก.
ตัวอย่างด้านล่างแสดงวิธีการย้ายตำแหน่งเคอร์เซอร์ไปหลังจากจบบุ๊กมาร์กแล้ว.
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"DocumentBuilder.doc"); | |
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc); | |
builder->MoveToBookmark(u"CoolBookmark", false, true); | |
builder->Writeln(u"This is a very cool bookmark."); |
การเปรียบเทียบสำหรับทั้งสองวิธีไม่ใช่ตัวพิมพ์เล็ก.
การแทรกข้อความใหม่ในลักษณะนี้ไม่ได้แทนที่ข้อความที่มีอยู่ของบุ๊กมาร์ก โปรดทราบว่าบุ๊กมาร์กบางอย่างในเอกสารถูกกำหนดให้กับเขตข้อมูลแบบฟอร์ม ย้ายไปยังบุ๊คมาร์คดังกล่าวและการแทรกข้อความที่มีแทรกข้อความลงในรหัสฟิลด์แบบฟอร์ม แม้ว่านี้จะไม่ทำให้ฟิลด์ฟอร์มเป็นโมฆะข้อความที่แทรกจะไม่สามารถมองเห็นได้เนื่องจาก.
วิธีการแปลงระหว่างหน่วยวัด
คุณสมบัติของวัตถุส่วนใหญ่ที่ให้ไว้ในAspose.WordsAPIที่แสดงถึงการวัด(ความกว้าง/ความสูงขอบและระยะทางต่างๆ)acceptค่าในจุด(1 นิ้วเท่ากับ 72 จุด) บางครั้งนี้ไม่สะดวกจึงมีชั้นเรียนConvertUtil
ที่ให้ฟังก์ชั่นผู้ช่วยในการแปลงระหว่างหน่วยวัดต่างๆ จะช่วยให้การแปลงนิ้วเป็นจุด,จุดนิ้วพิกเซลไปยังจุดและจุดที่จะพิกเซล เมื่อพิกเซลถูกแปลงเป็นจุดและในทางกลับกันก็สามารถทำได้ที่ความละเอียด 96 จุดต่อนิ้ว(จุด.
ConvertUtil
System::SharedPtr<Document> doc = System::MakeObject<Document>(); | |
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc); | |
System::SharedPtr<PageSetup> pageSetup = builder->get_PageSetup(); | |
pageSetup->set_TopMargin(ConvertUtil::InchToPoint(1.0)); | |
pageSetup->set_BottomMargin(ConvertUtil::InchToPoint(1.0)); | |
pageSetup->set_LeftMargin(ConvertUtil::InchToPoint(1.5)); | |
pageSetup->set_RightMargin(ConvertUtil::InchToPoint(1.5)); | |
pageSetup->set_HeaderDistance(ConvertUtil::InchToPoint(0.2)); | |
pageSetup->set_FooterDistance(ConvertUtil::InchToPoint(0.2)); |