עבודה עם צורות
נושא זה דן כיצד לעבוד באופן פרוגרמטי עם צורות באמצעות Aspose.Words.
הצורות ב Aspose.Words מייצגות אובייקט בשכבת הציור, כגון AutoShape, תיבת טקסט, צורה חופשית, OLE אובייקט, בקרת אקטיבקס או תמונה. מסמך Word יכול להכיל צורה אחת או יותר. צורות המסמך מיוצגות על ידי הכיתה Shape.
הכנס צורה באמצעות בונה מסמכים
ניתן להוסיף צורה מוטבעת עם סוג וגודל מוגדרים וצורה צפה חופשית עם המיקום, הגודל וסוג גלישת הטקסט שצוינו למסמך בשיטת InsertShape. שיטת InsertShape מאפשרת הכנסת DML צורה למודל המסמך. יש לשמור את המסמך בפורמט, התומך בצורות DML, אחרת, צמתים כאלה יומרו לצורה VML, תוך שמירת מסמך.
דוגמת הקוד הבאה מראה כיצד להכניס צורות מסוג זה למסמך:
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); |
הגדר יחס גובה-רוחב נעול
באמצעות Aspose.Words, אתה יכול לציין אם יחס הגובה-רוחב של הצורה נעול דרך המאפיין AspectRatioLocked.
דוגמת הקוד הבאה מראה כיצד לעבוד עם המאפיין 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); |
הגדר פריסת צורה בתא
ניתן גם לציין אם הצורה מוצגת בתוך טבלה או מחוצה לה באמצעות המאפיין IsLayoutInCell.
דוגמת הקוד הבאה מראה כיצד לעבוד עם המאפיין 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); |
צור מלבן פינת חיתוך
ניתן ליצור מלבן פינתי חיתוך באמצעות Aspose.Words. סוגי הצורות הם SingleCornerSnipped, TopCornersSnipped, DiagonalCornersSnipped, TopCornersOneRoundedOneSnipped, SingleCornerRounded, TopCornersRounded, ו DiagonalCornersRounded.
הצורה DML נוצרת באמצעות שיטת InsertShape עם סוגי צורות אלה. לא ניתן להשתמש בסוגים אלה כדי ליצור צורות VML. ניסיון ליצור צורה באמצעות הבונה הציבורי של כיתת " צורה “מעלה את החריג” NotSupportedException".
דוגמת הקוד הבאה מראה כיצד להכניס צורות מסוג זה למסמך:
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); |
קבל נקודות גבולות צורה בפועל
באמצעות Aspose.Words API, אתה יכול לקבל את המיקום והגודל של הצורה המכילה בלוק בנקודות, ביחס לעוגן של הצורה העליונה ביותר. לשם כך, השתמש במאפיין BoundsInPoints.
דוגמת הקוד הבאה מראה כיצד לעבוד עם המאפיין 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; |
פורמט כלל אופקי
Aspose.Words API מספק את המאפיין HorizontalRuleFormat כדי לגשת למאפיינים של צורת הכלל האופקי. הכיתה HorizontalRuleFormat חושפת תכונות בסיסיות כמו גובה, צבע, צל, וכו'. לעיצוב של כלל אופקי.
הדוגמה הבאה של הקוד מראה כיצד להגדיר 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"); |
הכנס OLE אובייקט כסמל
Aspose.Words API מספק Shape InsertOleObjectAsIcon פונקציה להכניס אובייקט מוטבע או מקושר OLE כסמל למסמך. פונקציה זו מאפשרת לציין את קובץ הסמל ואת הכיתוב. סוג האובייקט OLE
יזוהה באמצעות סיומת הקובץ.
דוגמת הקוד הבאה מראה כיצד להגדיר הוספה OLE אובייקט כסמל למסמך:
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; |