Home > Java > javaTutorial > body text

How to add watermark to table in java

WBOY
Release: 2023-04-19 10:25:02
forward
1445 people have browsed it

Add process

1. Load the Excel test document;

2. Set the text and font size;

3. Call DrawText() Method to insert a picture and set the picture as the header;

4. Set the picture as the header and define the picture width and height, text display style and position.

Example

import com.spire.xls.*;
 
import <span><a href="http://www.lanqibing.com/tag/java/" title="View all posts in java" target="_blank">java</a></span>.awt.*;
import <span><a href="http://www.lanqibing.com/tag/java/" title="View all posts in java" target="_blank">java</a></span>.awt.image.BufferedImage;
 
import static <span><a href="http://www.lanqibing.com/tag/java/" title="View all posts in java" target="_blank">java</a></span>.awt.image.BufferedImage.TYPE_INT_ARGB;
 
public class SingleWatermark {
    public static void main(String[] args) {
        //加载Excel测试文档
        Workbook wb = new Workbook();
        wb.loadFromFile("test.xlsx");
 
        //设置文本和字体大小
        Font font = new Font("仿宋", Font.PLAIN, 40);
 
        for (int i =0;i<wb.getWorksheets().getCount();i++)
        {
            Worksheet sheet = wb.getWorksheets().get(i);
            //调用DrawText() 方法插入图片
            BufferedImage imgWtrmrk = drawText("内部专用", font, Color.pink, Color.white, sheet.getPageSetup().getPageHeight(), sheet.getPageSetup().getPageWidth());
 
            //将图片设置为页眉
            sheet.getPageSetup().setCenterHeaderImage(imgWtrmrk);
            sheet.getPageSetup().setCenterHeader("&G");
 
 
            //将显示模式设置为Layout
            sheet.setViewMode(ViewMode.Layout);
        }
 
        //保存文档
        wb.saveToFile("SingleWatermark.xlsx", ExcelVersion.Version2013);
    }
    private static BufferedImage drawText (String text, Font font, Color textColor, Color backColor,double height, double width)
    {
        //定义图片宽度和高度
        BufferedImage img = new BufferedImage((int) width, (int) height, TYPE_INT_ARGB);
 
        Graphics2D loGraphic = img.createGraphics();
 
        //获取文本size
        FontMetrics loFontMetrics = loGraphic.getFontMetrics(font);
        int liStrWidth = loFontMetrics.stringWidth(text);
        int liStrHeight = loFontMetrics.getHeight();
 
        //文本显示样式及位置
        loGraphic.setColor(backColor);
        loGraphic.fillRect(0, 0, (int) width, (int) height);
        loGraphic.translate(((int) width - liStrWidth) / 2, ((int) height - liStrHeight) / 2);
        loGraphic.rotate(Math.toRadians(-45));
 
        loGraphic.translate(-((int) width - liStrWidth) / 2, -((int) height - liStrHeight) / 2);
        loGraphic.setFont(font);
        loGraphic.setColor(textColor);
        loGraphic.drawString(text, ((int) width - liStrWidth) / 2, ((int) height - liStrHeight) / 2);
        loGraphic.dispose();
        return img;
    }
}
Copy after login

The above is the detailed content of How to add watermark to table in java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template