کار با اشکال
این موضوع در مورد چگونگی کار برنامه نویسی با اشکال با استفاده از Aspose.Words بحث می کند.
اشکال در Aspose.Words یک شی را در لایه نقاشی نشان می دهند، مانند AutoShape، textbox، freeform، OLE object، ActiveX control یا picture. یک سند ورد می تواند شامل یک یا چند شکل مختلف باشد. شکل های سند توسط کلاس Shape نشان داده شده است.
شکل را با استفاده از سازنده سند وارد کنید
شما می توانید شکل خطی با نوع و اندازه مشخص شده و شکل شناور آزاد با موقعیت مشخص شده، اندازه و نوع بسته بندی متن را با استفاده از روش InsertShape در یک سند قرار دهید. روش InsertShape اجازه می دهد تا شکل DML را در مدل سند وارد کنید. سند باید در قالب ذخیره شود، که از اشکال DML پشتیبانی می کند، در غیر این صورت، چنین گره هایی به شکل VML تبدیل می شوند، در حالی که ذخیره سند.
مثال کد زیر نشان می دهد که چگونه این نوع اشکال را در سند وارد کنید:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
//Free-floating shape insertion. | |
Shape shape = builder.insertShape(ShapeType.TEXT_BOX, | |
RelativeHorizontalPosition.PAGE, 100, | |
RelativeVerticalPosition.PAGE, 100, | |
50, 50, | |
WrapType.NONE); | |
shape.setRotation(30.0); | |
builder.writeln(); | |
//Inline shape insertion. | |
shape = builder.insertShape(ShapeType.TEXT_BOX, 50, 50); | |
shape.setRotation(30.0); | |
OoxmlSaveOptions so = new OoxmlSaveOptions(SaveFormat.DOCX); | |
// "Strict" or "Transitional" compliance allows to save shape as DML. | |
so.setCompliance(OoxmlCompliance.ISO_29500_2008_TRANSITIONAL); | |
dataDir = dataDir + "Shape_InsertShapeUsingDocumentBuilder_out.docx"; | |
// Save the document to disk. | |
doc.save(dataDir, so); |
تنظیم نسبت ابعاد قفل شده است
با استفاده از Aspose.Words می توانید مشخص کنید که آیا نسبت ابعاد شکل از طریق ویژگی AspectRatioLocked قفل شده است یا خیر.
مثال کد زیر نشان می دهد که چگونه با ویژگی AspectRatioLocked کار کنید:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
Shape shape = builder.insertImage(dataDir + "Test.png"); | |
shape.setAspectRatioLocked(true); | |
// Save the document to disk. | |
dataDir = dataDir + "Shape_AspectRatioLocked_out.doc"; | |
doc.save(dataDir); |
تنظیم طرح شکل در سلول
همچنین می توانید با استفاده از ویژگی IsLayoutInCell مشخص کنید که آیا شکل در داخل یک جدول یا خارج از آن نمایش داده می شود.
مثال کد زیر نشان می دهد که چگونه با ویژگی IsLayoutInCell کار کنید:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
Document doc = new Document(dataDir + "LayoutInCell.docx"); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
Shape watermark = new Shape(doc, ShapeType.TEXT_PLAIN_TEXT); | |
watermark.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE); | |
watermark.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE); | |
watermark.isLayoutInCell(false); // Display the shape outside of table cell if it will be placed into a cell. | |
watermark.setWidth(300); | |
watermark.setHeight(70); | |
watermark.setHorizontalAlignment(HorizontalAlignment.CENTER); | |
watermark.setVerticalAlignment(VerticalAlignment.CENTER); | |
watermark.setRotation(-40); | |
watermark.getFill().setColor(Color.GRAY); | |
watermark.setStrokeColor(Color.GRAY); | |
watermark.getTextPath().setText("watermarkText"); | |
watermark.getTextPath().setFontFamily("Arial"); | |
watermark.setName("WaterMark_0"); | |
watermark.setWrapType(WrapType.NONE); | |
Run run = (Run) doc.getChildNodes(NodeType.RUN, true).get(doc.getChildNodes(NodeType.RUN, true).getCount() - 1); | |
builder.moveTo(run); | |
builder.insertNode(watermark); | |
doc.getCompatibilityOptions().optimizeFor(MsWordVersion.WORD_2010); | |
// Save the document to disk. | |
dataDir = dataDir + "Shape_IsLayoutInCell_out.docx"; | |
doc.save(dataDir); |
گوشه های برش خورده را اضافه کنید
شما می توانید یک مستطیل گوشه ای با استفاده از 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-Java | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
Shape shape = builder.insertShape(ShapeType.TOP_CORNERS_SNIPPED, 50, 50); | |
OoxmlSaveOptions so = new OoxmlSaveOptions(SaveFormat.DOCX); | |
so.setCompliance(OoxmlCompliance.ISO_29500_2008_TRANSITIONAL); | |
dataDir = dataDir + "AddCornersSnipped_out.docx"; | |
//Save the document to disk. | |
doc.save(dataDir, so); |
نقاط واقعی شکل را بدست آورید
با استفاده از Aspose.Words API، می توانید مکان و اندازه شکل حاوی بلوک را در نقاط نسبت به لنگر بالاترین شکل بدست آورید. برای این کار از خاصیت BoundsInPoints استفاده کنید.
مثال کد زیر نشان می دهد که چگونه با ویژگی BoundsInPoints کار کنید:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
Shape shape = builder.insertImage(dataDir + "Test.png"); | |
shape.setAspectRatioLocked(false); | |
System.out.print("\nGets the actual bounds of the shape in points. "); | |
System.out.println(shape.getShapeRenderer().getBoundsInPoints()); |
لنگر عمودی را مشخص کنید
شما می توانید تراز عمودی متن را در یک شکل با استفاده از ویژگی VerticalAnchor مشخص کنید.
مثال کد زیر نشان می دهد که چگونه با ویژگی VerticalAnchor کار کنید:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
Document doc = new Document(dataDir + "VerticalAnchor.docx"); | |
NodeCollection shapes = doc.getChildNodes(NodeType.SHAPE, true); | |
int imageIndex = 0; | |
for (Shape textBoxShape : (Iterable<Shape>) shapes) { | |
if (textBoxShape != null) { | |
textBoxShape.getTextBox().setVerticalAnchor(TextBoxAnchor.BOTTOM); | |
} | |
} | |
doc.save(dataDir + "VerticalAnchor_out.docx"); |
شکل SmartArt را تشخیص دهید
Aspose.Words همچنین اجازه می دهد تا تشخیص دهد که آیا شکل دارای یک شی SmartArt
است. برای این کار از خاصیت HasSmartArt استفاده کنید.
مثال کد زیر نشان می دهد که چگونه با ویژگی HasSmartArt کار کنید:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
Document doc = new Document(dataDir + "input.docx"); | |
NodeCollection shapes = doc.getChildNodes(NodeType.SHAPE, true); | |
int count = 0; | |
for (Shape textBoxShape : (Iterable<Shape>) shapes) { | |
if (textBoxShape.hasSmartArt()) { | |
count++; | |
} | |
} | |
System.out.println("The document has " + count + " shapes with SmartArt."); |
قالب قانون افقی
شما می توانید شکل قانون افقی را با استفاده از روش InsertHorizontalRule در یک سند وارد کنید.
Aspose.Words API ویژگی HorizontalRuleFormat را برای دسترسی به خواص شکل قانون افقی فراهم می کند. کلاس HorizontalRuleFormat خواص اساسی مانند ارتفاع، رنگ، NoShade و غیره را نشان می دهد. برای قالب بندی یک قانون افقی.
مثال کد زیر نشان می دهد که چگونه HorizontalRuleFormatرا تنظیم کنید:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
DocumentBuilder builder = new DocumentBuilder(); | |
Shape shape = builder.insertHorizontalRule(); | |
HorizontalRuleFormat horizontalRuleFormat = shape.getHorizontalRuleFormat(); | |
horizontalRuleFormat.setAlignment(HorizontalRuleAlignment.CENTER); | |
horizontalRuleFormat.setWidthPercent(70); | |
horizontalRuleFormat.setHeight(3); | |
horizontalRuleFormat.setColor(Color.BLUE); | |
horizontalRuleFormat.setNoShade(true); | |
builder.getDocument().save("HorizontalRuleFormat.docx"); |