Aspose::Words::NodeCollection класс
NodeCollection class
Представляет коллекцию узлов определённого типа. Чтобы узнать больше, посетите статью документации Aspose.Words Document Object Model (DOM).
class NodeCollection : public Aspose::Words::INodeCollection,
public System::Collections::Generic::IEnumerable<System::SharedPtr<Aspose::Words::Node>>
Методы
| Метод | Описание |
|---|---|
| Add(const System::SharedPtr<Aspose::Words::Node>&) | Добавляет узел в конец коллекции. |
| Clear() | Удаляет все узлы из этой коллекции и из документа. |
| Contains(const System::SharedPtr<Aspose::Words::Node>&) | Определяет, находится ли узел в коллекции. |
| get_Count() | Получает количество узлов в коллекции. |
| GetEnumerator() override | Предоставляет простую итерацию в стиле “foreach” по коллекции узлов. |
| GetType() const override | |
| idx_get(int32_t) | Получает узел по заданному индексу. |
| IndexOf(const System::SharedPtr<Aspose::Words::Node>&) | Возвращает нулевой индекс указанного узла. |
| Insert(int32_t, const System::SharedPtr<Aspose::Words::Node>&) | Вставляет узел в коллекцию по указанному индексу. |
| Is(const System::TypeInfo&) const override | |
| Remove(const System::SharedPtr<Aspose::Words::Node>&) | Удаляет узел из коллекции и из документа. |
| RemoveAt(int32_t) | Удаляет узел по указанному индексу из коллекции и из документа. |
| ToArray() | Копирует все узлы из коллекции в новый массив узлов. |
| static Type() |
Примечания
NodeCollection does not own the nodes it contains, rather, is just a selection of nodes of the specified type, but the nodes are stored in the tree under their respective parent nodes.
NodeCollection supports indexed access, iteration and provides add and remove methods.
Коллекция NodeCollection является «живой», т.е. изменения дочерних элементов объекта узла, из которого она была создана, немедленно отражаются в узлах, возвращаемых свойствами и методами NodeCollection.
NodeCollection is returned by GetChildNodes() and also serves as a base class for typed node collections such as SectionCollection, ParagraphCollection etc.
NodeCollection can be “flat” and contain only immediate children of the node it was created from, or it can be “deep” and contain all descendant children.
Примеры
Показывает, как заменить все формы текстовых полей формами изображений.
auto doc = System::MakeObject<Aspose::Words::Document>(get_MyDir() + u"Textboxes in drawing canvas.docx");
System::ArrayPtr<System::SharedPtr<Aspose::Words::Drawing::Shape>> shapes = doc->GetChildNodes(Aspose::Words::NodeType::Shape, true)->LINQ_OfType<System::SharedPtr<Aspose::Words::Drawing::Shape> >()->LINQ_ToArray();
ASSERT_EQ(3, shapes->LINQ_Count(static_cast<System::Func<System::SharedPtr<Aspose::Words::Drawing::Shape>, bool>>(static_cast<std::function<bool(System::SharedPtr<Aspose::Words::Drawing::Shape> s)>>([](System::SharedPtr<Aspose::Words::Drawing::Shape> s) -> bool
{
return s->get_ShapeType() == Aspose::Words::Drawing::ShapeType::TextBox;
}))));
ASSERT_EQ(1, shapes->LINQ_Count(static_cast<System::Func<System::SharedPtr<Aspose::Words::Drawing::Shape>, bool>>(static_cast<std::function<bool(System::SharedPtr<Aspose::Words::Drawing::Shape> s)>>([](System::SharedPtr<Aspose::Words::Drawing::Shape> s) -> bool
{
return s->get_ShapeType() == Aspose::Words::Drawing::ShapeType::Image;
}))));
for (System::SharedPtr<Aspose::Words::Drawing::Shape> shape : shapes)
{
if (shape->get_ShapeType() == Aspose::Words::Drawing::ShapeType::TextBox)
{
auto replacementShape = System::MakeObject<Aspose::Words::Drawing::Shape>(doc, Aspose::Words::Drawing::ShapeType::Image);
replacementShape->get_ImageData()->SetImage(get_ImageDir() + u"Logo.jpg");
replacementShape->set_Left(shape->get_Left());
replacementShape->set_Top(shape->get_Top());
replacementShape->set_Width(shape->get_Width());
replacementShape->set_Height(shape->get_Height());
replacementShape->set_RelativeHorizontalPosition(shape->get_RelativeHorizontalPosition());
replacementShape->set_RelativeVerticalPosition(shape->get_RelativeVerticalPosition());
replacementShape->set_HorizontalAlignment(shape->get_HorizontalAlignment());
replacementShape->set_VerticalAlignment(shape->get_VerticalAlignment());
replacementShape->set_WrapType(shape->get_WrapType());
replacementShape->set_WrapSide(shape->get_WrapSide());
shape->get_ParentNode()->InsertAfter<System::SharedPtr<Aspose::Words::Drawing::Shape>>(replacementShape, shape);
shape->Remove();
}
}
shapes = doc->GetChildNodes(Aspose::Words::NodeType::Shape, true)->LINQ_OfType<System::SharedPtr<Aspose::Words::Drawing::Shape> >()->LINQ_ToArray();
ASSERT_EQ(0, shapes->LINQ_Count(static_cast<System::Func<System::SharedPtr<Aspose::Words::Drawing::Shape>, bool>>(static_cast<std::function<bool(System::SharedPtr<Aspose::Words::Drawing::Shape> s)>>([](System::SharedPtr<Aspose::Words::Drawing::Shape> s) -> bool
{
return s->get_ShapeType() == Aspose::Words::Drawing::ShapeType::TextBox;
}))));
ASSERT_EQ(4, shapes->LINQ_Count(static_cast<System::Func<System::SharedPtr<Aspose::Words::Drawing::Shape>, bool>>(static_cast<std::function<bool(System::SharedPtr<Aspose::Words::Drawing::Shape> s)>>([](System::SharedPtr<Aspose::Words::Drawing::Shape> s) -> bool
{
return s->get_ShapeType() == Aspose::Words::Drawing::ShapeType::Image;
}))));
doc->Save(get_ArtifactsDir() + u"Shape.ReplaceTextboxesWithImages.docx");
См. также
- Namespace Aspose::Words
- Library Aspose.Words for C++