
Java에서 PowerPoint에 텍스트 워터마크 추가
2022-10-19 last update
19 minutes reading watermark java powerpoint apiPowerPoint의 워터마크는 슬라이드에 표시되는 반투명 텍스트입니다. 문서의 상태(기밀, 초안 등)를 나타내거나 문서의 소유권을 나타내는 데 사용됩니다. PowerPoint는 기본 제공 워터마크 기능을 제공하지 않지만 이 기사에서는 Java용 Free Spire.Presentation을 사용하여 PowerPoint 슬라이드에 텍스트 워터마크 또는 여러 줄 워터마크를 프로그래밍 방식으로 추가하는 방법을 공유합니다.
● free library을 다운로드하여 압축을 푼 다음 Spire.Presentation.jar 파일을 프로젝트에 종속성으로 추가합니다.
● pom.xml에 다음 구성을 추가하여 maven 프로젝트에 jar 종속성을 직접 추가합니다.

가져오기 종속성(2가지 방법)
● free library을 다운로드하여 압축을 푼 다음 Spire.Presentation.jar 파일을 프로젝트에 종속성으로 추가합니다.
● pom.xml에 다음 구성을 추가하여 maven 프로젝트에 jar 종속성을 직접 추가합니다.
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.presentation.free</artifactId>
<version>5.1.0</version>
</dependency>
</dependencies>
PowerPoint에 텍스트 워터마크 삽입
import com.spire.presentation.*;
import com.spire.presentation.drawing.FillFormatType;
import java.awt.*;
import java.awt.geom.Rectangle2D;
public class TextWatermark {
public static void main(String[] args) throws Exception {
//Create a PPT document and load the sample file
Presentation presentation = new Presentation();
presentation.loadFromFile("E:\\Files\\input.pptx");
//Set the width and height of watermark string
int width= 400;
int height= 300;
//Define a rectangle range
Rectangle2D.Double rect = new Rectangle2D.Double((presentation.getSlideSize().getSize().getWidth() - width) / 2,
(presentation.getSlideSize().getSize().getHeight() - height) / 2, width, height);
//Add a rectangle shape with a defined range
IAutoShape shape = presentation.getSlides().get(0).getShapes().appendShape(ShapeType.RECTANGLE, rect);
//Set the style of shape
shape.getFill().setFillType(FillFormatType.NONE);
shape.getShapeStyle().getLineColor().setColor(Color.white);
shape.setRotation(-45);
shape.getLocking().setSelectionProtection(true);
shape.getLine().setFillType(FillFormatType.NONE);
//Add text to shape
shape.getTextFrame().setText("Confidential");
PortionEx textRange = shape.getTextFrame().getTextRange();
//Set the style of the text range
textRange.getFill().setFillType(FillFormatType.SOLID);
textRange.getFill().getSolidColor().setColor(new Color(176, 48, 96));
textRange.setFontHeight(50);
//Save the document
presentation.saveToFile("AddTextWatermark.pptx", FileFormat.PPTX_2010);
}
}

PowerPoint에 여러 줄 워터마크 추가
import com.spire.pdf.graphics.PdfTrueTypeFont;
import com.spire.presentation.*;
import com.spire.presentation.drawing.FillFormatType;
import java.awt.*;
import java.awt.geom.Dimension2D;
import java.awt.geom.Rectangle2D;
public class AddMultilineWatermarks {
public static void main(String[] args) throws Exception {
//Create a Presentation object
Presentation presentation = new Presentation();
//Load the sample PowerPoint file
presentation.loadFromFile("E:\\Files\\input.pptx");
//Specify watermark text
String watermarkText = "Draft";
//Get the size of the watermark text
Font font = new java.awt.Font("Arial", java.awt.Font.BOLD, 20);
PdfTrueTypeFont trueTypeFont = new PdfTrueTypeFont(font);
Dimension2D strSize = trueTypeFont.measureString(watermarkText);
//Initialize x and y coordinate
float x = 30;
float y = 80;
for (int rowNum = 0; rowNum < 4; rowNum++) {
for (int colNum = 0; colNum < 5; colNum++) {
//Add a rectangle shape
Rectangle2D rect = new Rectangle2D.Float(x, y, (float) strSize.getWidth() + 10, (float) strSize.getHeight());
IAutoShape shape = presentation.getSlides().get(0).getShapes().appendShape(ShapeType.RECTANGLE, rect);
//Set the style of the shape
shape.getFill().setFillType(FillFormatType.NONE);
shape.getShapeStyle().getLineColor().setColor(new Color(1, 1, 1, 0));
shape.setRotation(-45);
shape.getLocking().setSelectionProtection(true);
shape.getLine().setFillType(FillFormatType.NONE);
//Add watermark text to the shape
shape.getTextFrame().setText(watermarkText);
PortionEx textRange = shape.getTextFrame().getTextRange();
//Set the style of the text range
textRange.getFill().setFillType(FillFormatType.SOLID);
textRange.getFill().getSolidColor().setColor(new Color(176, 48, 96));
textRange.setLatinFont(new TextFont(trueTypeFont.getName()));
textRange.setFontMinSize(trueTypeFont.getSize());
x += (100 + strSize.getWidth());
}
x = 30;
y += (100 + strSize.getHeight());
}
//Save the document
presentation.saveToFile("MultilineWatermark.pptx", FileFormat.PPTX_2013);
}
}
