GradientPaint 不适用于 itextPdf
GradientPaint not working with itextPdf
我使用的是 itextPdf 的最新版本。而且以前都还好不知道什么时候开始出问题了
我有将 GraphePinScene 对象转换为 pdf 的方法。
public void toPdf(Scene scene, float clipWidth, float clipHeight, String path)
{
float pageWidth = (float) scene.getView().getWidth();
float pageHeight = (float) scene.getView().getHeight();
double xScale = 1.0;
double yScale = 1.0;
Document d = new Document();
PdfWriter writer = PdfWriter.getInstance(d, new FileOutputStream(new File(path)));
d.open();
PdfContentByte cb = writer.getDirectContent();
//scaling
if (pageWidth > PDF_MAX_WIDTH) {
xScale = (double) (pageWidth / PDF_MAX_WIDTH);
pageWidth = PDF_MAX_WIDTH;
}
if (pageHeight > PDF_MAX_HEIGHT) {
xScale = (double) (pageHeight / PDF_MAX_HEIGHT);
pageHeight = PDF_MAX_HEIGHT;
}
Graphics2D g2d = new PdfGraphics2D(cb, pageWidth + clipWidth, pageHeight + clipHeight);
g2d.scale(xScale, yScale);
scene.paint(g2d);
g2d.dispose();
d.close();
}
并且场景对象包含一些Widgets。所有小部件都包含在 pdf 中,只有使用 GradientPaint 创建的小部件未在 pdf 文件中成像,这是其中一个小部件:
public final class SecondaryStationWidget extends IconNodeWidget {
//Staff
@Override
protected void paintWidget() {
Graphics2D g = getGraphics();
RenderingHints rh = new RenderingHints(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
rh.put(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
g.setRenderingHints(rh);
g.setPaint(new GradientPaint(44, 29, new Color(154, 191, 239), 88, 58, new Color(17, 74, 148), true));
g.fill(new RoundRectangle2D.Double(1, 2,
88,
58,
10, 10));
}
}
问题已通过更改(强制)这些小部件的背景(在构造函数中)得到解决。
setBackground(new Color(0, 0, 0, 1)); //Remove
setBackground(Color.WHITE); //Add
我使用的是 itextPdf 的最新版本。而且以前都还好不知道什么时候开始出问题了
我有将 GraphePinScene 对象转换为 pdf 的方法。
public void toPdf(Scene scene, float clipWidth, float clipHeight, String path)
{
float pageWidth = (float) scene.getView().getWidth();
float pageHeight = (float) scene.getView().getHeight();
double xScale = 1.0;
double yScale = 1.0;
Document d = new Document();
PdfWriter writer = PdfWriter.getInstance(d, new FileOutputStream(new File(path)));
d.open();
PdfContentByte cb = writer.getDirectContent();
//scaling
if (pageWidth > PDF_MAX_WIDTH) {
xScale = (double) (pageWidth / PDF_MAX_WIDTH);
pageWidth = PDF_MAX_WIDTH;
}
if (pageHeight > PDF_MAX_HEIGHT) {
xScale = (double) (pageHeight / PDF_MAX_HEIGHT);
pageHeight = PDF_MAX_HEIGHT;
}
Graphics2D g2d = new PdfGraphics2D(cb, pageWidth + clipWidth, pageHeight + clipHeight);
g2d.scale(xScale, yScale);
scene.paint(g2d);
g2d.dispose();
d.close();
}
并且场景对象包含一些Widgets。所有小部件都包含在 pdf 中,只有使用 GradientPaint 创建的小部件未在 pdf 文件中成像,这是其中一个小部件:
public final class SecondaryStationWidget extends IconNodeWidget {
//Staff
@Override
protected void paintWidget() {
Graphics2D g = getGraphics();
RenderingHints rh = new RenderingHints(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
rh.put(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
g.setRenderingHints(rh);
g.setPaint(new GradientPaint(44, 29, new Color(154, 191, 239), 88, 58, new Color(17, 74, 148), true));
g.fill(new RoundRectangle2D.Double(1, 2,
88,
58,
10, 10));
}
}
问题已通过更改(强制)这些小部件的背景(在构造函数中)得到解决。
setBackground(new Color(0, 0, 0, 1)); //Remove
setBackground(Color.WHITE); //Add