Praca z Content Control SDT
W Microsoft Word możesz utworzyć formularz, zaczynając od szablonu i dodając kontrolki zawartości, w tym pola wyboru, pola tekstowe, selektory dat i listy rozwijane. W Aspose.Words strukturalny znacznik dokumentu lub kontrolka zawartości z dowolnego dokumentu załadowanego do Aspose.Words jest importowany jako węzeł StructuredDocumentTag. Strukturalne znaczniki dokumentów (SDT lub content control) umożliwiają osadzanie semantyki zdefiniowanej przez Klienta, a także jej zachowania i wyglądu w dokumencie. StructuredDocumentTag może wystąpić w dokumencie w następujących miejscach:
- Block-level-wśród akapitów i tabel, jako dziecko treści, HeaderFooter, komentarza, przypisu lub węzła kształtu
- Row-level-wśród wierszy w tabeli, jako dziecko węzła tabeli
- Cell-level-wśród komórek w wierszu tabeli, jako dziecko węzła wiersza
- Inline-level-wśród treści inline wewnątrz, jako dziecko akapitu
- Zagnieżdżone wewnątrz innego StructuredDocumentTag
Jak ustawić styl formatowania tekstu wpisywanego do kontrolki treści
Jeśli chcesz ustawić styl kontroli zawartości, możesz użyć właściwości StructuredDocumentTag.Style
lub StructuredDocumentTag.StyleName
. Po wpisaniu tekstu do kontrolki treści w dokumencie wyjściowym wpisany tekst będzie miał styl “cytat”.
Poniższy przykład kodu pokazuje, jak ustawić styl kontroli treści:
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>(inputDataDir + u"input.docx"); | |
System::SharedPtr<StructuredDocumentTag> sdt = System::DynamicCast<StructuredDocumentTag>(doc->GetChild(NodeType::StructuredDocumentTag, 0, true)); | |
System::SharedPtr<Style> style = doc->get_Styles()->idx_get(StyleIdentifier::Quote); | |
sdt->set_Style(style); | |
System::String outputPath = outputDataDir + u"WorkingWithSDT.SetContentControlStyle.docx"; | |
// Save the document to disk. | |
doc->Save(outputPath); |
Praca z powtarzającą się kontrolą zawartości sekcji
Kontrola treści sekcji powtarzalnej umożliwia powtarzanie zawartych w niej treści. Za pomocą Aspose.Words można utworzyć strukturalne węzły znaczników dokumentu powtarzających się sekcji i powtarzających się typów elementów sekcji, w tym celu SdtType Typ wyliczenia zapewnia Właściwość RepeatingSectionItem.
Poniższy przykład kodu pokazuje, jak powiązać kontrolkę zawartości sekcji powtarzającej się z tabelą:
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::String xml = System::String(u"<books><book><title>Everyday Italian</title><author>Giada De Laurentiis</author></book>") + | |
u"<book><title>Harry Potter</title><author>J K. Rowling</author></book>" + | |
u"<book><title>Learning XML</title><author>Erik T. Ray</author></book></books>"; | |
System::SharedPtr<CustomXmlPart> xmlPart = doc->get_CustomXmlParts()->Add(u"Books", xml); | |
System::SharedPtr<Table> table = builder->StartTable(); | |
builder->InsertCell(); | |
builder->Write(u"Title"); | |
builder->InsertCell(); | |
builder->Write(u"Author"); | |
builder->EndRow(); | |
builder->EndTable(); | |
System::SharedPtr<StructuredDocumentTag> repeatingSectionSdt = System::MakeObject<StructuredDocumentTag>(doc, Aspose::Words::Markup::SdtType::RepeatingSection, Aspose::Words::Markup::MarkupLevel::Row); | |
repeatingSectionSdt->get_XmlMapping()->SetMapping(xmlPart, u"/books[1]/book", u""); | |
table->AppendChild(repeatingSectionSdt); | |
System::SharedPtr<StructuredDocumentTag> repeatingSectionItemSdt = System::MakeObject<StructuredDocumentTag>(doc, Aspose::Words::Markup::SdtType::RepeatingSectionItem, Aspose::Words::Markup::MarkupLevel::Row); | |
repeatingSectionSdt->AppendChild(repeatingSectionItemSdt); | |
System::SharedPtr<Row> row = System::MakeObject<Row>(doc); | |
repeatingSectionItemSdt->AppendChild(row); | |
System::SharedPtr<StructuredDocumentTag> titleSdt = System::MakeObject<StructuredDocumentTag>(doc, Aspose::Words::Markup::SdtType::PlainText, Aspose::Words::Markup::MarkupLevel::Cell); | |
titleSdt->get_XmlMapping()->SetMapping(xmlPart, u"/books[1]/book[1]/title[1]", u""); | |
row->AppendChild(titleSdt); | |
System::SharedPtr<StructuredDocumentTag> authorSdt = System::MakeObject<StructuredDocumentTag>(doc, Aspose::Words::Markup::SdtType::PlainText, Aspose::Words::Markup::MarkupLevel::Cell); | |
authorSdt->get_XmlMapping()->SetMapping(xmlPart, u"/books[1]/book[1]/author[1]", u""); | |
row->AppendChild(authorSdt); | |
doc->Save(outputDataDir + u"WorkingWithSDT.CreatingTableRepeatingSectionMappedToCustomXmlPart.docx"); |