도형 작업

이 항목에서는Aspose.Words을 사용하여 도형을 프로그래밍 방식으로 작업하는 방법에 대해 설명합니다.

Aspose.Words의 모양은AutoShape,텍스트 상자,자유형,OLE개체,액티브 컨트롤 또는 그림과 같이 도면 레이어의 개체를 나타냅니다. 단어 문서에는 하나 이상의 다른 모양이 포함될 수 있습니다. 문서의 모양은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.WordsAPI를 사용하여 최상위 도형의 앵커를 기준으로 블록을 포함하는 도형의 위치 및 크기를 점으로 얻을 수 있습니다. 이렇게 하려면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.WordsAPI는 수평 규칙 모양의 속성에 액세스하기 위해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");