Şekillerle Çalışmak
Bu konu, şekillerle programatik çalışmayı nasıl yapacağınızı tartışır Aspose.Words.
Şekiller, Aspose.Words katmanındaki bir nesneyi, örneğin bir AutoShape, metin kutusu, serbest biçim, OLE nesnesi, ActiveX denetimi veya resim temsil eder. Bir Word belgesi bir ya da daha fazla farklı şekil içerebilir. Dokümantadaki şekiller Shape sınıfı tarafından temsil edilir.
Document Builder kullanarak şekil ekle
Belirtilen tür ve boyutla yerleşik şekil ve serbest yüzen şekil ile belirtilen konum, boyut ve metin sarma türü bir belgeye InsertShape yöntemi kullanılarak eklenebilir. InsertShape yöntemi, DML şeklini belge modeline eklemeyi sağlar. Belge, DML şeklini destekleyen bir biçimde kaydedilmezse, bu düğümler VML şekli olurken belge kaydetme sırasında dönüştürülür.
Aşağıdaki örnek kod, bu tür şekilleri belgenin içine eklemeyi gösterir:
// 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); |
Aspect Ratio Kilitli Ayarlarını Belirle
Kullanarak Aspose.Words, şeklinizin yön oranı kilitli olup olmadığını belirtmek için AspectRatioLocked özelliğini kullanabilirsiniz.
Aşağıdaki kod örneği AspectRatioLocked özelliğini nasıl kullanacağınızı gösterir:
// 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); |
Hücre İçinde Şekil Düzeni Ayarla
Şekli bir tabloda mı yoksa dışında mı görüntüleneceğini belirtmek için IsLayoutInCell özelliğini de kullanabilirsiniz.
Aşağıdaki kod örneği, IsLayoutInCell özelliğini nasıl kullanacağınızı gösterir:
// 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); |
Kenarları Ekle Kesildi
Bir snip köşe dikdörtgen oluşturmak için Aspose.Words kullanabilirsiniz. Şekil türleri *SingleCornerSnipped, TopCornersSnipped, DiagonalCornersSnipped, TopCornersOneRoundedOneSnipped, SingleCornerRounded, TopCornersRounded ve DiagonalCornersRounded. ‘dir
DML şekli bu şekil türleri ile InsertShape yöntemini kullanarak oluşturulur. Bu türler VML şekilleri oluşturmak için kullanılamaz. ‘Shape’ sınıfının kamuya açık oluşturucusunu kullanarak şekil oluşturmaya çalışmak ‘NotSupportedException’ özel durumuna neden olur.
Aşağıdaki kod örneği bu tür şekilleri belgenin içine nasıl ekleyeceğinizi göstermektedir:
// 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); |
Asıl Şekil Sınır Noktalarını Al
Aspose.Words API kullanarak, en üstteki şeklin anکر konumuna göre, şekil kapsayıcısının konumunu ve boyutunu noktalar halinde alabilirsiniz. Bunu yapmak için, BoundsInPoints özelliğini kullanın.
Aşağıdaki kod örneği, BoundsInPoints özelliğini nasıl kullanacağınızı gösterir:
// 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()); |
Dikey Anahtarı Belirt
Bir şekil içinde metin dikey hizalamasını VerticalAnchor özelliği kullanarak belirtebilirsiniz.
Aşağıdaki kod örneği nasıl VerticalAnchor özelliğini kullanacağınızı gösterir:
// 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 Şekli Algıla
Aspose.Words ayrıca şeklin bir SmartArt
nesnesi olup olmadığını tespit etmenize de izin verir. Bunu yapmak için HasSmartArt özelliğini kullanın.
Aşağıdaki örnek kod, nasıl HasSmartArt özelliğine çalışılacağını göstermektedir:
// 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."); |
Yatay Kural Biçimi
Bir belgeye yatay kural şekil InsertHorizontalRule yöntemiyle ekleyebilirsiniz.
Aspose.Words API HorizontalRuleFormat özelliğini yatay çizgi şeklin özelliklerine erişmek için sağlar. HorizontalRuleFormat sınıfı Yükseklik, Renk, NoShade vb. gibi temel özellikleri açığa çıkarır bir yatay kuralın biçimlendirilmesi için.
Aşağıdaki kod örneği HorizontalRuleFormat‘i nasıl ayarlayacağını gösterir
// 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"); |