带有渐变效果的文字也是属于艺术字的一种,实现效果:
实现代码:
public void artTextWithGradient() throws Exception {
String cmpPdf = "E:/pdfTest/artText/artText_02.pdf";
PdfWriter writer = new PdfWriter(cmpPdf);
PdfDocument doc = new PdfDocument(writer);
PageSize pageSize = new PageSize(800,800);
PdfPage page = doc.addNewPage(pageSize);
PdfFont font = PdfFontFactory.createFont("E:/pdfTest/artText/FreeSans.ttf", "WinAnsi", true);
PdfCanvas canvas = new PdfCanvas(page);
String line = "Hello";
float fontSize = 60;
float top=getDrawTop(fontSize);
float w=font.getWidth(line,fontSize);
//设置渐变
PdfShading.Axial axial = new PdfShading.Axial(new PdfDeviceCs.Rgb(), 36, 700, Color.WHITE.getColorValue(),
36+w, 700+top, Color.RED.getColorValue());
PdfPattern.Shading pattern = new PdfPattern.Shading(axial);
canvas.saveState()
.setFillColorShading(pattern)
.beginText()
.moveText(36, 700)
.setFontAndSize(font, fontSize)
.showText(line)
.endText()
.restoreState();
canvas.saveState()
.setFillColor(Color.GREEN)
.beginText()
.moveText(36+w, 700)
.setFontAndSize(font, fontSize)
.showText("world !")
.endText()
.restoreState();
doc.close();
}