mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-12-07 01:28:34 +08:00
Merge pull request #4164 from CherryRum/fix/set-color-argb
fix(Word07Writer): strip alpha channel from color and update tests
This commit is contained in:
commit
f18a2b512f
@ -14,11 +14,7 @@ import org.apache.poi.xwpf.usermodel.XWPFParagraph;
|
|||||||
import org.apache.poi.xwpf.usermodel.XWPFRun;
|
import org.apache.poi.xwpf.usermodel.XWPFRun;
|
||||||
|
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
import java.io.Closeable;
|
import java.io.*;
|
||||||
import java.io.File;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStream;
|
|
||||||
import java.io.OutputStream;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Word docx生成器
|
* Word docx生成器
|
||||||
@ -156,7 +152,8 @@ public class Word07Writer implements Closeable {
|
|||||||
run.setItalic(font.isItalic());
|
run.setItalic(font.isItalic());
|
||||||
}
|
}
|
||||||
if (null != color) {
|
if (null != color) {
|
||||||
String hexColor = String.format("%02X", color.getRGB());
|
// setColor expects a pure RGB hex string (no alpha channel)
|
||||||
|
String hexColor = String.format("%06X", color.getRGB() & 0xFFFFFF);
|
||||||
run.setColor(hexColor);
|
run.setColor(hexColor);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -5,6 +5,8 @@ import cn.hutool.core.collection.ListUtil;
|
|||||||
import cn.hutool.core.date.DateUtil;
|
import cn.hutool.core.date.DateUtil;
|
||||||
import cn.hutool.core.io.FileUtil;
|
import cn.hutool.core.io.FileUtil;
|
||||||
import cn.hutool.core.lang.Console;
|
import cn.hutool.core.lang.Console;
|
||||||
|
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
|
||||||
|
import org.apache.poi.xwpf.usermodel.XWPFRun;
|
||||||
import org.junit.jupiter.api.Disabled;
|
import org.junit.jupiter.api.Disabled;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
@ -15,6 +17,8 @@ import java.util.LinkedHashMap;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
public class WordWriterTest {
|
public class WordWriterTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -96,4 +100,17 @@ public class WordWriterTest {
|
|||||||
word07Writer.addTable(list);
|
word07Writer.addTable(list);
|
||||||
word07Writer.close();
|
word07Writer.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void addTextShouldStripAlphaAndUseRgbHex() {
|
||||||
|
final Word07Writer writer = new Word07Writer();
|
||||||
|
final Color colorWithAlpha = new Color(0x12, 0x34, 0x56, 0x7F);
|
||||||
|
|
||||||
|
writer.addText(new Font("宋体", Font.PLAIN, 12), colorWithAlpha, "带颜色的段落");
|
||||||
|
|
||||||
|
final XWPFParagraph paragraph = writer.getDoc().getParagraphArray(0);
|
||||||
|
final XWPFRun run = paragraph.getRuns().get(0);
|
||||||
|
assertEquals("123456", run.getColor());
|
||||||
|
writer.close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user