การทำงานกับรูปแบบ

คลาสStyleCollectionจะใช้ในการจัดการในตัวและใช้การตั้งค่าที่ผู้ใช้กำหนดกับรูปแบบ.

วิธีการแยกเนื้อหาตามรูปแบบ

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

เพื่อใช้ขั้นตอนต่อไปนี้อีกสองสามขั้นตอนนอกจากนี้ยังสามารถใช้เพื่อใช้ประโยชน์จากโครงสร้างของเอกสารที่กำหนดโดยสไตล์ที่ใช้เพื่อวัตถุประสงค์เอกสารใหม่สำหรับเอาต์พุตอื่นเช่นHTML ในความเป็นจริงวิธีการสร้างเอกสารแอสโพส,วางAspose.Wordsเพื่อทดสอบ. เครื่องมือที่สร้างขึ้นโดยใช้Aspose.Wordsจะใช้เอกสารคำต้นฉบับและแยกออกเป็นหัวข้อในระดับหัวข้อ ไฟล์XMLถูกผลิตโดยใช้Aspose.Wordsซึ่งใช้ในการสร้างต้นไม้นำทางที่คุณสามารถดูได้ทางด้านซ้าย แล้วAspose.Wordsแปลงแต่ละหัวข้อในHTML.

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

การแก้ปัญหา

เพื่อแสดงให้เห็นAspose.Wordsจัดการกับการดึงเนื้อหาตามสไตล์ได้อย่างง่ายดายเพียงใดลองดูตัวอย่าง ในตัวอย่างนี้เรากำลังจะดึงข้อความที่จัดรูปแบบด้วยลักษณะย่อหน้าเฉพาะและลักษณะตัวอัก ในระดับสูงนี้จะเกี่ยวข้องกับ:

  • การเปิดเอกสารคำโดยใช้คลาสDocument.
  • การรับคอลเล็กชันของย่อหน้าทั้งหมดและทั้งหมดทำงานในเอกสาร.
  • เลือกเฉพาะย่อหน้าที่ต้องการและรัน โดยเฉพาะอย่างยิ่งเราจะดึงข้อความที่จัดรูปแบบด้วยรูปแบบย่อหน้า’หัวข้อ1’และรูปแบบตัวอัก.

working-with-styles-aspose-words-cpp-1

ในเอกสารตัวอย่างนี้ข้อความที่จัดรูปแบบด้วยรูปแบบ’หัวข้อ1’ย่อหน้าคือ’แทรกแท็บ',‘รูปแบบด่วน’และ’ธีม’และข้อความที่จัดรูปแบบด้วยรูปแบบอักขระ’เน้นเข้มข้น’คือหลายกรณีของข้อความสีฟ้าตัวเอียงตัวหนาเช่น’แกลเลอรี่'และ’รูปลักษณ์โดยรวม’.

รหัส

การดำเนินงานของแบบสอบถามตามสไตล์ค่อนข้างง่ายในAspose.Wordsรูปแบบวัตถุเอกสาร,มันเป็นเพีย ใช้สองวิธีแบบคลาสสำหรับโซลูชันนี้:#ParagraphsByStyleName-วิธีนี้ดึงอาร์เรย์ของย่อหน้าเหล่านั้นในเอกสารที่มีชื่อลัก#RunsByStyleName-วิธีนี้ดึงอาร์เรย์ของผู้ที่รันในเอกสารที่มีชื่อสไตล์ที่เฉพาะเจาะจง ทั้งสองวิธีเหล่านี้จะคล้ายกันมากความแตกต่างเพียงอย่างเดียวคือชนิดโหนดและการแส นี่คือการดำเนินงานของParagraphsByStyleName ตัวอย่างด้านล่างค้นหาย่อหน้าทั้งหมดที่จัดรูปแบบด้วยสไตล์ที่ระบุ.

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
std::vector<System::SharedPtr<Paragraph>> ParagraphsByStyleName(System::SharedPtr<Document> doc, System::String const &styleName)
{
// Create an array to collect paragraphs of the specified style.
std::vector<System::SharedPtr<Paragraph>> paragraphsWithStyle;
// Get all paragraphs from the document.
System::SharedPtr<NodeCollection> paragraphs = doc->GetChildNodes(NodeType::Paragraph, true);
// Look through all paragraphs to find those with the specified style.
for (System::SharedPtr<Paragraph> paragraph : System::IterateOver<System::SharedPtr<Paragraph>>(paragraphs))
{
if (paragraph->get_ParagraphFormat()->get_Style()->get_Name() == styleName)
{
paragraphsWithStyle.push_back(paragraph);
}
}
return paragraphsWithStyle;
}

นอกจากนี้ยังมีมูลค่าชี้ให้เห็นว่าคอลเลกชันย่อหน้าไม่ได้สร้างค่าใช้จ่ายทันทีเนื่องจากย่อห จากนั้นสิ่งที่คุณต้องทำคือผ่านคอลเล็กชันโดยใช้มาตรฐานสำหรับแต่ละโอเปอเรเตอร์และเพิ่มย่อหน้าที่มีลักษณะที่ระบุไว้ในอาร์เรย์paragraphsWithStyle ชื่อสไตล์Paragraphจะพบได้ในสไตล์ คุณสมบัติชื่อของออบเจกต์Paragraph.ParagraphFormat การดำเนินงานของRunsByStyleNameเกือบจะเหมือนกันแม้ว่าเราจะเห็นได้ชัดว่าใช้NodeType.Runเพื่อดึงโหนดเรียกใช้ คุณสมบัติFont.Styleของออบเจกต์Runถูกใช้เพื่อเข้าถึงข้อมูลลักษณะในโหนดRun ตัวอย่างเป็นlow codeค้นหารันทั้งหมดที่จัดรูปแบบด้วยสไตล์ที่ระบุ.

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
std::vector<System::SharedPtr<Run>> RunsByStyleName(System::SharedPtr<Document> doc, System::String const &styleName)
{
// Create an array to collect runs of the specified style.
std::vector<System::SharedPtr<Run>> runsWithStyle;
// Get all runs from the document.
System::SharedPtr<NodeCollection> runs = doc->GetChildNodes(NodeType::Run, true);
// Look through all runs to find those with the specified style.
for (System::SharedPtr<Run> run : System::IterateOver<System::SharedPtr<Run>>(runs))
{
if (run->get_Font()->get_Style()->get_Name() == styleName)
{
runsWithStyle.push_back(run);
}
}
return runsWithStyle;
}

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

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
// The path to the documents directory.
System::String inputDataDir = GetInputDataDir_WorkingWithStyles();
// Open the document.
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"TestFile.doc");
// Define style names as they are specified in the Word document.
const System::String paraStyle = u"Heading 1";
const System::String runStyle = u"Intense Emphasis";
// Collect paragraphs with defined styles.
// Show the number of collected paragraphs and display the text of this paragraphs.
std::vector<System::SharedPtr<Paragraph>> paragraphs = ParagraphsByStyleName(doc, paraStyle);
std::cout << "Paragraphs with \"" << paraStyle.ToUtf8String() << "\" styles (" << paragraphs.size() << "):" << std::endl;
for (System::SharedPtr<Paragraph> paragraph : paragraphs)
{
std::cout << paragraph->ToString(SaveFormat::Text).ToUtf8String();
}
std::cout << std::endl;
// Collect runs with defined styles.
// Show the number of collected runs and display the text of this runs.
std::vector<System::SharedPtr<Run>> runs = RunsByStyleName(doc, runStyle);
std::cout << "Runs with \"" << runStyle.ToUtf8String() << "\" styles (" << runs.size() << "):" << std::endl;
for (System::SharedPtr<Run> run : runs)
{
std::cout << run->get_Range()->get_Text().ToUtf8String() << std::endl;
}

ผลลัพธ์สุดท้าย

เมื่อทุกอย่างเสร็จสิ้นการทำงานตัวอย่างจะแสดงผลลัพธ์ต่อไปนี้:

working-with-styles-aspose-words-cpp-2

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

วิธีการแทรกและทำงานกับตารางของฟิลด์เนื้อหา

บ่อยครั้งที่คุณจะทำงานกับเอกสารที่มีสารบัญ(TOC) โดยใช้Aspose.Wordsคุณสามารถแทรกตารางเนื้อหาของคุณเองหรือสร้างตารางเนื้อหาที่มีอยู่ในเอกสารโดยใช้เพียงไม่กี่บรรทัดของรหัส บทความนี้แสดงวิธีการทำงานกับตารางของฟิลด์เนื้อหาและสาธิต:

  • วิธีการแทรกแบรนด์ใหม่TOC
  • อัปเดตใหม่หรือที่มีอยู่TOCsในเอกสาร.
  • ระบุสวิตช์เพื่อควบคุมการจัดรูปแบบและโครงสร้างโดยรวมของTOC.
  • วิธีการปรับเปลี่ยนรูปแบบและลักษณะของสารบัญ.
  • วิธีการลบฟิลด์TOCทั้งหมดพร้อมกับรายการทั้งหมดจากเอกสาร.

แทรกฟิลด์TC

มักจะมีการกำหนดบรรทัดข้อความเฉพาะสำหรับTOCและถูกทำเครื่องหมายด้วยฟิลด์TC วิธีง่ายๆในการทำเช่นนี้ในMSคำคือการเน้นข้อความและกดALT+SHIFT+O นี้จะสร้างฟิลด์TCโดยอัตโนมัติโดยใช้ข้อความที่เลือก เทคนิคเดียวกันสามารถทำได้ผ่านรหัส รหัสด้านล่างจะพบข้อความที่ตรงกับการป้อนข้อมูลและแทรกช่องTCในตำแหน่งเดียวกับข้อค รหัสจะขึ้นอยู่กับเทคนิคเดียวกับที่ใช้ในบทความ ตัวอย่างด้านล่างแสดงวิธีการค้นหาและแทรกฟิลด์TCที่ข้อความในเอกสาร.

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
System::SharedPtr<Document> doc = System::MakeObject<Document>();
System::SharedPtr<FindReplaceOptions> options = System::MakeObject<FindReplaceOptions>();
// Highlight newly inserted content.
options->get_ApplyFont()->set_HighlightColor(System::Drawing::Color::get_DarkOrange());
options->set_ReplacingCallback(System::MakeObject<InsertTCFieldHandler>(u"Chapter 1", u"\\l 1"));
// Insert a TC field which displays "Chapter 1" just before the text "The Beginning" in the document.
doc->get_Range()->Replace(System::MakeObject<System::Text::RegularExpressions::Regex>(u"The Beginning"), u"", options);
For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
class InsertTCFieldHandler : public IReplacingCallback
{
typedef InsertTCFieldHandler ThisType;
typedef IReplacingCallback BaseType;
typedef ::System::BaseTypesInfo<BaseType> ThisTypeBaseTypesInfo;
public:
InsertTCFieldHandler(const System::String& text, const System::String& switches)
: mFieldText(text), mFieldSwitches(switches) {}
InsertTCFieldHandler(const System::String& switches)
: mFieldText(System::String::Empty), mFieldSwitches(switches) {}
ReplaceAction Replacing(System::SharedPtr<ReplacingArgs> args) override;
private:
System::String mFieldText;
System::String mFieldSwitches;
};
ReplaceAction InsertTCFieldHandler::Replacing(System::SharedPtr<ReplacingArgs> args)
{
// Create a builder to insert the field.
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(System::DynamicCast<Document>(args->get_MatchNode()->get_Document()));
// Move to the first node of the match.
builder->MoveTo(args->get_MatchNode());
// If the user specified text to be used in the field as display text then use that, otherwise use the
// Match string as the display text.
System::String insertText;
if (!System::String::IsNullOrEmpty(mFieldText))
{
insertText = mFieldText;
}
else
{
insertText = args->get_Match()->get_Value();
}
// Insert the TC field before this node using the specified string as the display text and user defined switches.
builder->InsertField(System::String::Format(u"TC \"{0}\" {1}", insertText, mFieldSwitches));
// We have done what we want so skip replacement.
return ReplaceAction::Skip;
}

แก้ไขสารบัญ

เปลี่ยนรูปแบบของรูปแบบ

การจัดรูปแบบของรายการในTOCไม่ได้ใช้สไตล์ดั้งเดิมของรายการที่ทำเครื่องหมายไว้แต่ละระดับจะถูกจัดรูปแบบโดยใช้สไตล์ที่เทียบเท่าTOC ตัวอย่างเช่นระดับแรกในTOCถูกจัดรูปแบบด้วยสไตล์TOC1ระดับที่สองจัดรูปแบบด้วยสไตล์TOC2และอื่นๆ ลี่ยนรูปลักษณ์ของTOCลักษณะเหล่านี้ต้องถูกแก้ไข ในAspose.Wordsลักษณะเหล่านี้จะแสดงโดยโลแคลอิสระStyleIdentifier.TOC1ถึงStyleIdentifier.TOC9และสามารถดึงข้อมูลจากคอลเล็กชันDocument.Stylesโดยใ เมื่อมีการดึงข้อมูลลักษณะที่เหมาะสมของเอกสารแล้วการจัดรูปแบบสำหรับลักษณะนี้สามา การเปลี่ยนแปลงลักษณะเหล่านี้จะปรากฏขึ้นโดยอัตโนมัติบนTOCsในเอกสาร ตัวอย่างพีlow codeเปลี่ยนคุณสมบัติการจัดรูปแบบที่ใช้ในลักษณะระดับแรกTOC.

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
System::SharedPtr<Document> doc = System::MakeObject<Document>();
// Retrieve the style used for the first level of the TOC and change the formatting of the style.
doc->get_Styles()->idx_get(StyleIdentifier::Toc1)->get_Font()->set_Bold(true);

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

ใช้Styleคลาสที่ดึงสำหรับระดับTOCเฉพาะที่คุณต้องการปรับเปลี่ยน,คุณยังสามารถปรับเปลี่ยนวิธี เมื่อต้องการเปลี่ยนวิธีการนี้ปรากฏขึ้นในตอนแรกStyle.ParagraphFormatต้องถูกเรียกใช้เพื่อเรียกการจัดรูปแบบย่อหน้าสำหรับสไตล์ จากนี้แท็บหยุดสามารถเรียกดูได้โดยเรียกParagraphFormat.TabStopsและแท็บหยุดที่เหมาะสมแก้ไข ใช้เทคนิคเดียวกันนี้แท็บตัวเองสามารถย้ายหรือลบออกโดยสิ้นเชิง ตัวอย่างเป็นlow codeแสดงวิธีการปรับเปลี่ยนตำแหน่งของแท็บขวาหยุดในTOCย่อหน้าที่เกี่ยวข้อง. คุณสามารถดาวน์โหลดไฟล์แม่แบบของตัวอย่างนี้ได้จากที่นี่.

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
// The path to the documents directories.
System::String inputDataDir = GetInputDataDir_WorkingWithStyles();
System::String outputDataDir = GetOutputDataDir_WorkingWithStyles();
// Open the document.
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"Document.TableOfContents.doc");
// Iterate through all paragraphs in the document
for (System::SharedPtr<Paragraph> para : System::IterateOver<System::SharedPtr<Paragraph>>(doc->GetChildNodes(NodeType::Paragraph, true)))
{
// Check if this paragraph is formatted using the TOC result based styles. This is any style between TOC and TOC9.
if (para->get_ParagraphFormat()->get_Style()->get_StyleIdentifier() >= StyleIdentifier::Toc1 && para->get_ParagraphFormat()->get_Style()->get_StyleIdentifier() <= StyleIdentifier::Toc9)
{
// Get the first tab used in this paragraph, this should be the tab used to align the page numbers.
System::SharedPtr<TabStop> tab = para->get_ParagraphFormat()->get_TabStops()->idx_get(0);
// Remove the old tab from the collection.
para->get_ParagraphFormat()->get_TabStops()->RemoveByPosition(tab->get_Position());
// Insert a new tab using the same properties but at a modified position.
// We could also change the separators used (dots) by passing a different Leader type
para->get_ParagraphFormat()->get_TabStops()->Add(tab->get_Position() - 50, tab->get_Alignment(), tab->get_Leader());
}
}
System::String outputPath = outputDataDir + u"ChangeTOCTabStops.doc";
doc->Save(outputPath);

การลบสารบัญออกจากเอกสาร

สารบัญสามารถลบออกจากเอกสารโดยการลบโหนดทั้งหมดที่พบระหว่างFieldStartและFieldEndโหนดของTOC รหัสด้านล่างแสดงให้เห็นถึงนี้ การกำจัดของฟิลด์TOCจะง่ายกว่าฟิลด์ปกติเนื่องจากเราไม่ได้ติดตามเขตข้อมูลที่ซ้อนกัน แต่เราตรวจสอบFieldEndโหนดเป็นประเภทFieldType.FieldTOCซึ่งหมายความว่าเราได้พบจุดสิ้นสุดของปัจจุบันTOC เทคนิคนี้สามารถใช้ในกรณีนี้โดยไม่ต้องกังวลเกี่ยวกับเขตข้อมูลที่ซ้อนกันใดๆที่เราสามารถสมมติว่าเอกสารใดๆที่เกิดขึ้นอย่างถูกต้องจะไม่มีเขตข้อมูลที่ซ้อนกันอย่างเต็มที่TOCภายในอีกเขตข้อมูลTOC. ประการแรกFieldStartโหนดของแต่ละTOCจะถูกรวบรวมและเก็บไว้ จากนั้นระบุTOCที่ระบุไว้เพื่อให้โหนดทั้งหมดภายในฟิลด์ถูกเยี่ยมชมและเก็บไว้ จากนั้นโหนดจะถูกลบออกจากเอกสาร ตัวอย่างเป็นlow codeแสดงวิธีการลบTOCที่ระบุจากเอกสาร คุณสามารถดาวน์โหลดไฟล์แม่แบบของตัวอย่างนี้ได้จากที่นี่.

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
// The path to the documents directories.
System::String inputDataDir = GetInputDataDir_WorkingWithStyles();
System::String outputDataDir = GetOutputDataDir_WorkingWithStyles();
// Open a document which contains a TOC.
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"Document.TableOfContents.doc");
// Remove the first table of contents from the document.
RemoveTableOfContents(doc, 0);
System::String outputPath = outputDataDir + u"RemoveTOCFromDocument.doc";
// Save the output.
doc->Save(outputPath);

แทรกตัวคั่นสไตล์ที่จะนำรูปแบบที่แตกต่างกันย่อหน้า

สามารถเพิ่มตัวคั่นลักษณะไปยังส่วนท้ายของย่อหน้าได้โดยใช้แป้นพิมพ์ลัดMSคำ คุณลักษณะนี้ช่วยให้ลักษณะย่อหน้าต่างๆสองใช้ในย่อหน้าตรรกะหนึ่งพิมพ์ ถ้าคุณต้องการให้ข้อความบางส่วนจากจุดเริ่มต้นของหัวข้อเฉพาะปรากฏในตารางของเนื้อ ตัวอย่างเป็นlow codeแสดงวิธีแทรกตัวคั่นลักษณะเพื่อใส่ลักษณะย่อหน้าต่าง.

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
System::SharedPtr<Document> doc = System::MakeObject<Document>();
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc);
System::SharedPtr<Style> paraStyle = builder->get_Document()->get_Styles()->Add(StyleType::Paragraph, u"MyParaStyle");
paraStyle->get_Font()->set_Bold(false);
paraStyle->get_Font()->set_Size(8);
paraStyle->get_Font()->set_Name(u"Arial");
// Append text with "Heading 1" style.
builder->get_ParagraphFormat()->set_StyleIdentifier(StyleIdentifier::Heading1);
builder->Write(u"Heading 1");
builder->InsertStyleSeparator();
// Append text with another style.
builder->get_ParagraphFormat()->set_StyleName(paraStyle->get_Name());
builder->Write(u"This is text with some other formatting ");
System::String outputPath = outputDataDir + u"InsertStyleSeparator.doc";
// Save the document to disk.
doc->Save(outputPath);

คัดลอกสไตล์ทั้งหมดจากแม่แบบ

มีกรณีเมื่อคุณต้องการคัดลอกลักษณะทั้งหมดจากเอกสารหนึ่งไปยังอีก คุณสามารถใช้วิธีการDocument.CopyStylesFromTemplateเพื่อคัดลอกลักษณะจากเทมเพลตที่ระบุไปยังเอกสาร เมื่อสไตล์ถูกคัดลอกจากเทมเพลทไปยังเอกสารลักษณะที่เหมือนชื่อในเอกสารจะถูกนิยาม รูปแบบที่ไม่ซ้ำกันจากแม่แบบจะถูกคัดลอกไปยังเอกสาร รูปแบบที่ไม่ซ้ำกันในเอกสารยังคงเหมือนเดิม ตัวอย่างเป็นlow codeแสดงวิธีการคัดลอกลักษณะจากเอกสารหนึ่งไปยังอีก.

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
System::String fileName = inputDataDir + u"template.docx";
System::SharedPtr<Document> doc = System::MakeObject<Document>(fileName);
// Open the document.
System::SharedPtr<Document> target = System::MakeObject<Document>(inputDataDir + u"TestFile.doc");
target->CopyStylesFromTemplate(doc);
System::String outputPath = outputDataDir + u"CopyStyles.doc";
doc->Save(outputPath);