Praca z kształtami
W tym temacie omówiono programową pracę z kształtami przy użyciu Aspose.Words.
Kształty w Aspose.Words reprezentują obiekt w warstwie rysunku, taki jak AutoShape, pole tekstowe, dowolny kształt, OLE obiekt, formant ActiveX lub obraz. Dokument Word może zawierać jeden lub więcej różnych kształtów. Kształty dokumentu są reprezentowane przez klasę Shape.
Wstaw Kształt Za Pomocą Narzędzia Do Tworzenia Dokumentów
Możesz wstawić kształt inline z określonym typem i rozmiarem oraz swobodnie pływający kształt z określoną pozycją, rozmiarem i typem zawijania tekstu do dokumentu przy użyciu metody InsertShape. Metoda InsertShape umożliwia wstawienie kształtu DML do modelu dokumentu. Dokument musi być zapisany w formacie, który obsługuje kształty DML, w przeciwnym razie takie węzły zostaną przekonwertowane na kształt VML podczas zapisywania dokumentu.
Poniższy przykład kodu pokazuje, jak wstawić te typy kształtów do dokumentu:
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); | |
//Free-floating shape insertion. | |
System::SharedPtr<Shape> shape = builder->InsertShape(ShapeType::TextBox, RelativeHorizontalPosition::Page, 100, RelativeVerticalPosition::Page, 100, 50, 50, WrapType::None); | |
shape->set_Rotation(30.0); | |
builder->Writeln(); | |
//Inline shape insertion. | |
shape = builder->InsertShape(ShapeType::TextBox, 50, 50); | |
shape->set_Rotation(30.0); | |
System::SharedPtr<OoxmlSaveOptions> so = System::MakeObject<OoxmlSaveOptions>(SaveFormat::Docx); | |
// "Strict" or "Transitional" compliance allows to save shape as DML. | |
so->set_Compliance(OoxmlCompliance::Iso29500_2008_Transitional); | |
System::String outputPath = outputDataDir + u"WorkingWithShapes.InsertShapeUsingDocumentBuilder.docx"; | |
// Save the document to disk. | |
doc->Save(outputPath, so); |
Ustaw Współczynnik Proporcji Zablokowany
Za pomocą Aspose.Words można określić, czy współczynnik kształtu jest zablokowany przez właściwość AspectRatioLocked.
Poniższy przykład kodu pokazuje, jak pracować z właściwością AspectRatioLocked:
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<Shape> shape = builder->InsertImage(inputDataDir + u"Test.png"); | |
shape->set_AspectRatioLocked(false); | |
System::String outputPath = outputDataDir + u"WorkingWithShapes.SetAspectRatioLocked.doc"; | |
// Save the document to disk. | |
doc->Save(outputPath); |
Ustaw Układ Kształtu W Komórce
Możesz także określić, czy kształt jest wyświetlany wewnątrz tabeli, czy poza nią, używając właściwości IsLayoutInCell.
Poniższy przykład kodu pokazuje, jak pracować z właściwością IsLayoutInCell:
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"LayoutInCell.docx"); | |
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc); | |
System::SharedPtr<Shape> watermark = System::MakeObject<Shape>(doc, ShapeType::TextPlainText); | |
watermark->set_RelativeHorizontalPosition(RelativeHorizontalPosition::Page); | |
watermark->set_RelativeVerticalPosition(RelativeVerticalPosition::Page); | |
watermark->set_IsLayoutInCell(true); | |
// Display the shape outside of table cell if it will be placed into a cell. | |
watermark->set_Width(300); | |
watermark->set_Height(70); | |
watermark->set_HorizontalAlignment(HorizontalAlignment::Center); | |
watermark->set_VerticalAlignment(VerticalAlignment::Center); | |
watermark->set_Rotation(-40); | |
watermark->get_Fill()->set_Color(System::Drawing::Color::get_Gray()); | |
watermark->set_StrokeColor(System::Drawing::Color::get_Gray()); | |
watermark->get_TextPath()->set_Text(u"watermarkText"); | |
watermark->get_TextPath()->set_FontFamily(u"Arial"); | |
watermark->set_Name(System::String::Format(u"WaterMark_{0}",System::Guid::NewGuid())); | |
watermark->set_WrapType(WrapType::None); | |
System::SharedPtr<Run> run = System::DynamicCast_noexcept<Run>(doc->GetChildNodes(NodeType::Run, true)->idx_get(doc->GetChildNodes(NodeType::Run, true)->get_Count() - 1)); | |
builder->MoveTo(run); | |
builder->InsertNode(watermark); | |
doc->get_CompatibilityOptions()->OptimizeFor(MsWordVersion::Word2010); | |
System::String outputPath = outputDataDir + u"WorkingWithShapes.SetShapeLayoutInCell.docx"; | |
// Save the document to disk. | |
doc->Save(outputPath); |
Utwórz Prostokąt Narożny Snip
Możesz utworzyć prostokąt narożny snip za pomocą Aspose.Words. Typy kształtów to SingleCornerSnipped, TopCornersSnipped, DiagonalCornersSnipped, TopCornersOneRoundedOneSnipped, SingleCornerRounded, TopCornersRounded, i DiagonalCornersRounded.
Kształt DML jest tworzony przy użyciu metody InsertShape z tymi typami kształtów. Tych typów nie można używać do tworzenia kształtów VML. Próba utworzenia kształtu przy użyciu publicznego konstruktora klasy " Shape “podnosi wyjątek” NotSupportedException".
Poniższy przykład kodu pokazuje, jak wstawić tego typu kształty do dokumentu:
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<Shape> shape = builder->InsertShape(ShapeType::TopCornersSnipped, 50, 50); | |
System::SharedPtr<OoxmlSaveOptions> so = System::MakeObject<OoxmlSaveOptions>(SaveFormat::Docx); | |
so->set_Compliance(OoxmlCompliance::Iso29500_2008_Transitional); | |
System::String outputPath = outputDataDir + u"WorkingWithShapes.AddCornersSnipped.docx"; | |
//Save the document to disk. | |
doc->Save(outputPath, so); |
Uzyskaj Rzeczywiste Punkty Granic Kształtu
Używając Aspose.Words API, możesz uzyskać lokalizację i rozmiar kształtu zawierającego blok w punktach, w stosunku do kotwicy najwyższego kształtu. Aby to zrobić, użyj właściwości BoundsInPoints.
Poniższy przykład kodu pokazuje, jak pracować z właściwością BoundsInPoints:
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<Shape> shape = builder->InsertImage(inputDataDir + u"Test.png"); | |
shape->set_AspectRatioLocked(false); | |
std::cout << "Gets the actual bounds of the shape in points." << shape->GetShapeRenderer()->get_BoundsInPoints().ToString().ToUtf8String() << std::endl; |
Format Reguły Poziomej
Aspose.Words API zapewnia Właściwość HorizontalRuleFormat umożliwiającą dostęp do właściwości kształtu reguły poziomej. Klasa HorizontalRuleFormat ujawnia podstawowe właściwości, takie jak wysokość, kolor, odcień itp. do formatowania reguły poziomej.
Poniższy przykład kodu pokazuje, jak ustawić HorizontalRuleFormat:
For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C | |
auto doc = MakeObject<Document>(); | |
auto builder = MakeObject<DocumentBuilder>(doc); | |
builder->Writeln(u"Insert a horizontal rule shape into the document."); | |
builder->InsertHorizontalRule(); | |
doc->Save(ArtifactsDir + u"AddContentUsingDocumentBuilder.InsertHorizontalRule.docx"); |
Wstaw obiekt OLE jako ikonę
Aspose.Words API zapewnia Shape → InsertOleObjectAsIcon funkcję wstawiania osadzonego lub połączonego obiektu OLE jako ikony do dokumentu. Ta funkcja umożliwia określenie pliku ikony i podpisu. Typ obiektu OLE
jest wykrywany przy użyciu rozszerzenia pliku.
Poniższy przykład kodu pokazuje, jak ustawić obiekt insert OLE jako ikonę w dokumencie:
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<Shape> shape = builder->InsertOleObjectAsIcon(inputDataDir + u"embedded.xlsx", false, inputDataDir + u"icon.ico", u"My embedded file"); | |
doc->Save(outputDataDir + u"WorkingWithShapes.InsertOLEObjectAsIcon.docx"); | |
std::cout << "The document has been saved with OLE Object as an Icon." << std::endl; |