JAVA和Nginx 教程大全

网站首页 > 精选教程 正文

Java 添加多行多列文本水印至PowerPoint幻灯片

wys521 2024-11-25 10:57:11 精选教程 22 ℃ 0 评论

在前文中,我曾介绍过如何使用Spire.Presentation for Java控件来给PowerPoint幻灯片添加图片或文本水印,同时也有删除文档中已有水印的方法。事实上,该控件还支持添加多行多列的文本水印至幻灯片,本文就用代码示例来演示如何实现上述操作。

同样地,我们需要在E-iceblue中文官网上获取产品包,然后将lib文件夹里的Spire.Presentation.jar导入IDEA。接着在IDEA中创建Java class类,引入以下代码。

import com.spire.presentation.*;
import com.spire.presentation.drawing.*;
import java.awt.*;
import java.awt.geom.Rectangle2D;

public class MultipleTextWatermarks {
    public static void main(String[] args) throws Exception {
        //加载示例文档
        Presentation presentation = new Presentation();
        presentation.loadFromFile("C:\\Users\\Test1\\Desktop\\Sample.pptx");

        //设置文本水印的宽和高
        int width= 300;
        int height= 200;

        //绘制文本,设置文本格式并将其添加到第一张幻灯片
        float x = 15;
        float y = 45;
        for (int i = 0; i < 3; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                Rectangle2D.Double rect = new Rectangle2D.Double(x,y,width, height);
                //添加一个shape到定义区域
                IAutoShape shape = presentation.getSlides().get(0).getShapes().appendShape(ShapeType.RECTANGLE, rect);
                //设置shape样式
                shape.getFill().setFillType(FillFormatType.NONE);
                shape.getShapeStyle().getLineColor().setColor(Color.white);
                shape.setRotation(-45);
                shape.getLocking().setSelectionProtection(true);
                shape.getLine().setFillType(FillFormatType.NONE);

                //添加文本到shape
                shape.getTextFrame().setText("内部使用");
                PortionEx textRange = shape.getTextFrame().getTextRange();

                //设置文本水印样式
                textRange.getFill().setFillType(FillFormatType.SOLID);
                textRange.getFill().getSolidColor().setColor(Color.gray);
                textRange.setFontHeight(20);
                x += (100 + presentation.getSlideSize().getSize().getWidth()/5);

            }
            x = 30;
            y += (100 + presentation.getSlideSize().getSize().getHeight()/6);

        }

        //保存文档
        presentation.saveToFile("output/multipleTextWatermarks.pptx", FileFormat.PPTX_2010);

    }
}

注:有关文本水印的内容,颜色,大小及位置等信息,我们可以通过调整代码中的数据来达到所需效果。

通过运行以上代码,我们能够为指定幻灯片添加多行多列文本水印。以下是我为示例文档中的第一张幻灯片添加的文本水印效果:

相关教程推荐:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表