워터마크 작업

이 항목에서는Aspose.Words을 사용하여 워터마크로 프로그래밍 방식으로 작업하는 방법에 대해 설명합니다. 워터마크는 문서의 텍스트 뒤에 표시되는 배경 이미지입니다. 워터마크는Watermark클래스로 표시되는 텍스트나 이미지를 포함할 수 있습니다.

문서에 워터마크 추가

Microsoft Word에서 워터마크 삽입 명령을 사용하여 문서에 워터마크를 쉽게 삽입할 수 있습니다. Aspose.Words는 문서에서 워터마크를 추가하거나 제거할watermark클래스를 제공합니다. Aspose.Words는 사용할 수 있는 세 가지 유형의 워터마크(텍스트,이미지 및 없음)를 정의하는WatermarkType열거형을 제공합니다.

텍스트 워터마크 추가

다음 코드 예제에서는SetText메서드를 사용하여TextWatermarkOptions를 정의하여 문서에 텍스트 워터마크를 삽입하는 방법을 보여 줍니다:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
Document doc = new Document(dataDir + "Document.doc");
TextWatermarkOptions options = new TextWatermarkOptions();
options.setFontFamily("Arial");
options.setFontSize(36);
options.setColor(Color.BLACK);
options.setLayout(WatermarkLayout.HORIZONTAL);
options.isSemitrasparent(false);
doc.getWatermark().setText("Test", options);
doc.save(dataDir + "AddTextWatermark_out.docx");

이미지 워터마크 추가

다음 코드 예제에서는SetImage메서드를 사용하여ImageWatermarkOptions를 정의하여 문서에 이미지 워터마크를 삽입하는 방법을 보여 줍니다:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
Document doc = new Document(dataDir + "Document.doc");
ImageWatermarkOptions options = new ImageWatermarkOptions();
options.setScale(5);
options.isWashout(false);
ImageInputStream stream = ImageIO.createImageInputStream(new File(dataDir + "Watermark.png"));
doc.getWatermark().setImage(ImageIO.read(stream), options);
doc.save(dataDir + "AddImageWatermark_out.docx");

워터 마크는 또한 모양 클래스를 사용하여 삽입 할 수 있습니다. 머리글이나 바닥 글에 어떤 모양이나 이미지를 삽입하여 상상할 수있는 유형의 워터 마크를 만드는 것은 매우 쉽습니다.

다음 코드 예제에서는 워드 문서에 워터마크를 삽입합니다:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
@Test
public void addWatermark() throws Exception
{
Document doc = new Document(getMyDir() + "Document.docx");
insertWatermarkText(doc, "CONFIDENTIAL");
doc.save(getArtifactsDir() + "WorkWithWatermark.AddWatermark.docx");
}
/// <summary>
/// Inserts a watermark into a document.
/// </summary>
/// <param name="doc">The input document.</param>
/// <param name="watermarkText">Text of the watermark.</param>
private void insertWatermarkText(Document doc, String watermarkText) throws Exception
{
// Create a watermark shape, this will be a WordArt shape.
Shape watermark = new Shape(doc, ShapeType.TEXT_PLAIN_TEXT); { watermark.setName("Watermark"); }
watermark.getTextPath().setText(watermarkText);
watermark.getTextPath().setFontFamily("Arial");
watermark.setWidth(500.0);
watermark.setHeight(100.0);
// Text will be directed from the bottom-left to the top-right corner.
watermark.setRotation(-40);
// Remove the following two lines if you need a solid black text.
watermark.setFillColor(Color.GRAY);
watermark.setStrokeColor(Color.GRAY);
// Place the watermark in the page center.
watermark.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
watermark.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);
watermark.setWrapType(WrapType.NONE);
watermark.setVerticalAlignment(VerticalAlignment.CENTER);
watermark.setHorizontalAlignment(HorizontalAlignment.CENTER);
// Create a new paragraph and append the watermark to this paragraph.
Paragraph watermarkPara = new Paragraph(doc);
watermarkPara.appendChild(watermark);
// Insert the watermark into all headers of each document section.
for (Section sect : (Iterable<Section>) doc.getSections())
{
// There could be up to three different headers in each section.
// Since we want the watermark to appear on all pages, insert it into all headers.
insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HEADER_PRIMARY);
insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HEADER_FIRST);
insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HEADER_EVEN);
}
}
private void insertWatermarkIntoHeader(Paragraph watermarkPara, Section sect,
/*HeaderFooterType*/int headerType)
{
HeaderFooter header = sect.getHeadersFooters().getByHeaderFooterType(headerType);
if (header == null)
{
// There is no header of the specified type in the current section, so we need to create it.
header = new HeaderFooter(sect.getDocument(), headerType);
sect.getHeadersFooters().add(header);
}
// Insert a clone of the watermark into the header.
header.appendChild(watermarkPara.deepClone(true));
}

문서에서 워터마크 제거

Watermark클래스는 문서에서 워터마크를 제거하는Remove메서드를 제공합니다.

다음 코드 예제에서는 문서에서 워터마크를 제거하는 방법을 보여 줍니다:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
Document doc = new Document(dataDir + "AddTextWatermark_out.docx");
if (doc.getWatermark().getType() == WatermarkType.TEXT) {
doc.getWatermark().remove();
}
doc.save(dataDir + "RemoveWatermark_out.docx");

문서에서 워터마크를 제거하려면 삽입하는 동안 워터마크 모양의 이름만 설정한 다음 할당된 이름으로 워터마크 모양을 제거해야 합니다.

다음 코드 예제에서는 워터마크 모양의 이름을 설정하고 문서에서 제거하는 방법을 보여 줍니다:

// Set name to be able to remove it afterwards
watermark.Name("WaterMark");
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
private static final String dataDir = Utils.getDataDir(RemoveWatermark.class);
public static void main(String[] args) throws Exception {
Document doc = new Document(dataDir + "RemoveWatermark.docx");
removeWatermarkText(doc);
doc.save(dataDir + "RemoveWatermark_out.doc");
}
private static void removeWatermarkText(Document doc) throws Exception {
for (HeaderFooter hf : (Iterable<HeaderFooter>) doc.getChildNodes(NodeType.HEADER_FOOTER, true)) {
for (Shape shape : (Iterable<Shape>) hf.getChildNodes(NodeType.SHAPE, true)) {
if (shape.getName().contains("WaterMark"))
shape.remove();
}
}
}

테이블 셀에 워터마크 추가

때때로 당신은 테이블의 셀에 워터 마크/이미지를 삽입하고 테이블 외부에 표시해야,당신은IsLayoutInCell속성을 사용할 수 있습니다. 이 속성은 셰이프가 테이블 내부 또는 테이블 외부에 표시되는지 여부를 나타내는 플래그를 가져오거나 설정합니다. 이 속성은OptimizeFor방법을 사용하여Microsoft Word2010 에 대한 문서를 최적화할 때만 작동합니다.

다음 코드 예제에서는 이 속성을 사용하는 방법을 보여 줍니다:

// 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);