Làm việc với hình dạng
Mục này thảo luận về cách làm việc với các hình dạng một cách lập trình bằng Aspose.Words.
Các hình dạng trong Aspose.Words đại diện cho một đối tượng trong lớp vẽ, chẳng hạn như một AutoShape, hộp văn bản, tự do, đối tượng OLE, điều khiển ActiveX hoặc ảnh. Một tài liệu Word có thể chứa một hoặc nhiều hình dạng khác nhau. Các hình dạng của tài liệu được đại diện bởi lớp Shape.
Chèn hình dạng bằng Trình xây dựng tài liệu
Bạn có thể chèn hình dạng inline với loại và kích thước được chỉ định và hình dạng thả nổi tự do với vị trí, kích thước và kiểu bọc văn bản được chỉ định vào một tài liệu bằng phương pháp InsertShape. Phương pháp InsertShape cho phép chèn hình dạng DML vào mô hình tài liệu. Tài liệu phải được lưu ở định dạng hỗ trợ hình dạng DML, nếu không thì các nút sẽ được chuyển đổi thành hình dạng VML khi lưu tài liệu.
Mã ví dụ sau cho thấy cách chèn các loại hình dạng này vào tài liệu:
// 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); |
Cài đặt Tỷ lệ khung hình khóa
Sử dụng Aspose.Words bạn có thể chỉ định xem tỷ số khung hình có bị khóa hay không thông qua thuộc tính AspectRatioLocked.
Đoạn mã ví dụ sau cho thấy cách làm việc với thuộc tính 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); |
Đặt bố cục hình dạng trong ô
Bạn cũng có thể chỉ định xem hình dạng có được hiển thị bên trong bảng hay bên ngoài nó bằng thuộc tính IsLayoutInCell.
Ví dụ mã sau cho thấy cách làm việc với thuộc tính 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); |
Thêm Góc Lấy bớt
Bạn có thể tạo một hình chữ nhật góc lồi bằng cách sử dụng Aspose.Words. Loại hình dạng là: SingleCornerSnipped, TopCornersSnipped, DiagonalCornersSnipped, TopCornersOneRoundedOneSnipped, SingleCornerRounded, TopCornersRounded, và DiagonalCornersRounded.
Hình dạng DML được tạo ra bằng phương pháp InsertShape với các kiểu hình dạng này. Những kiểu này không thể được dùng để tạo ra hình dạng VML. Việc cố gắng tạo hình bằng cách sử dụng công cụ xây dựng công cộng của lớp “Shape” sẽ gây ra ngoại lệ “NotSupportedException.
Ví dụ mã sau cho thấy cách chèn những hình dạng như vậy vào tài liệu:
// 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); |
Lấy Điểm Giới hạn Hình dạng Thực tế
Sử dụng Aspose.Words API, bạn có thể nhận được vị trí và kích thước của khối chứa hình dạng trong điểm, tương đối với điểm neo của hình dạng cao nhất. Để làm điều này, hãy dùng thuộc tính BoundsInPoints.
Ví dụ mã sau cho thấy cách làm việc với thuộc tính 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()); |
Chỉ định Cột neo dọc
Bạn có thể chỉ định căn chỉnh văn bản theo chiều dọc bên trong một hình dạng bằng thuộc tính VerticalAnchor.
Mảnh mã ví dụ sau cho thấy cách làm việc với thuộc tính 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"); |
Tìm hình dạng SmartArt
Aspose.Words cũng cho phép phát hiện nếu hình dạng có SmartArt
đối tượng. Để làm điều này hãy sử dụng tính năng HasSmartArt.
Mẫu mã sau cho thấy cách làm việc với thuộc tính HasSmartArt như thế nào:
// 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."); |
Định dạng Đường ngang
Bạn có thể chèn hình dạng quy tắc ngang vào tài liệu bằng cách sử dụng InsertHorizontalRule phương pháp.
Aspose.Words API cung cấp HorizontalRuleFormat thuộc tính để truy cập vào các thuộc tính của hình dạng thanh ngang. Lớp HorizontalRuleFormat cung cấp các thuộc tính cơ bản như Chiều cao, Màu sắc, Không có bóng tối v.v… để định dạng một quy tắc ngang.
Cái ví dụ mã sau cho thấy cách thiết lập 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"); |