获取使用 iText 和 Xchart 创建的圆环图之间的线条

Getting lines between donut chart created using iText and Xchart

我使用 Xchart 和 iText 在 PDF 中创建了圆环图。从 Acrobat reader 打开它时,我在甜甜圈区域之间出现了一些线条。下面是我使用过的代码。请提出这些线出现在dontu之间的可能原因。

此外,当我使用其他工具打开同一个 PDF 时,一切正常。

import java.awt.Color;
import java.awt.Font;
import java.io.FileOutputStream;
import java.io.IOException;

import org.knowm.xchart.BitmapEncoder;
import org.knowm.xchart.BitmapEncoder.BitmapFormat;
import org.knowm.xchart.PieChart;
import org.knowm.xchart.PieChartBuilder;
import org.knowm.xchart.PieSeries.PieSeriesRenderStyle;
import org.knowm.xchart.SwingWrapper;
import org.knowm.xchart.VectorGraphicsEncoder;
import org.knowm.xchart.VectorGraphicsEncoder.VectorGraphicsFormat;
import org.knowm.xchart.demo.charts.ExampleChart;
import org.knowm.xchart.style.PieStyler.AnnotationType;
import org.knowm.xchart.style.Styler.LegendPosition;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.pdf.PdfWriter;

public class DonutChart implements ExampleChart<PieChart> {

    public static void main(String[] args) {

        ExampleChart<PieChart> exampleChart = new DonutChart();
        PieChart chart = exampleChart.getChart();
        new SwingWrapper<PieChart>(chart).displayChart();
    }

    /* (non-Javadoc)
     * @see org.knowm.xchart.demo.charts.ExampleChart#getChart()
     */
    @Override
    public PieChart getChart() {

        // Create Chart
        PieChart chart = new PieChartBuilder().width(600).height(400).title("Donut Chart").build();

        // Customize Chart
        chart.getStyler().setLegendVisible(true);
        chart.getStyler().setChartTitleBoxBorderColor(Color.black);
        chart.getStyler().setChartTitleBoxBackgroundColor(new Color(0, 222, 0));
        chart.getStyler().setChartTitleFont(new Font(Font.SANS_SERIF, Font.PLAIN, 18));
        chart.getStyler().setLegendBackgroundColor(new Color(240, 240,230));
        chart.getStyler().setLegendBorderColor(Color.CYAN);
        //chart.getStyler().setChartTitleVisible(false);
        chart.getStyler().setAnnotationType(AnnotationType.Label);
        chart.getStyler().setHasAnnotations(false);
        //chart.getStyler().setAnnotationDistance(.7);
        chart.getStyler().setPlotContentSize(.9);
        chart.getStyler().setDonutThickness(.45);
        chart.getStyler().setChartBackgroundColor(Color.WHITE);
        chart.getStyler().setInfoPanelBorderColor(Color.WHITE);
        //chart.getStyler().setLegendBorderColor(Color.WHITE);
        chart.getStyler().setPlotBorderColor(Color.WHITE);
        chart.getStyler().setDefaultSeriesRenderStyle(PieSeriesRenderStyle.Donut);
        chart.getStyler().setBorderWidth(0);
        chart.getStyler().setLegendFont(new Font(Font.SANS_SERIF, Font.PLAIN, 20));
        chart.getStyler().setLegendPosition(LegendPosition.OutsideE);

        Color[] sliceColors = new Color[] { new Color(31, 43, 135), new Color(9, 179, 162), new Color(0, 0, 0),
                new Color(133, 129, 129), new Color(184, 180, 180),new Color(207, 207, 207) };

        chart.getStyler().setSeriesColors(sliceColors);

        // Series
        chart.addSeries("Office", 50);
        chart.addSeries("Multifamily", 10);
        chart.addSeries("Mixed use", 34);
        chart.addSeries("Hospitality", 22);
        chart.addSeries("Retail", 29);
        chart.addSeries("Industrial", 40);

        try {

            BitmapEncoder.saveBitmapWithDPI(chart, "D:\Akash\Workspace_OMS8.6main\CHARTTEST\Donut_Chart_300_DPI", BitmapFormat.PNG, 300);
            VectorGraphicsEncoder.saveVectorGraphic(chart, "./Donut_Chart", VectorGraphicsFormat.PDF);

            Document document = new Document(PageSize.A4);
            PdfWriter.getInstance(document, new FileOutputStream("D:\Akash\Workspace_OMS8.6main\CHARTTEST\Donut_Chart_300_DPI.pdf"));
            document.open();
            Image img = Image.getInstance("D:\Akash\Workspace_OMS8.6main\CHARTTEST\Donut_Chart_300_DPI.png");
            img.setAbsolutePosition(10,400);
            img.scaleAbsolute(300f, 250f);
            document.add(img);
            document.close();
            System.out.println("Done");

        } catch (IOException e) {
        } catch (DocumentException e1) {
        }


        return chart;
    }

}

此处的问题与 Graphics2D 对象在 PDF 文档中的保存方式有关。 Graphics2D 对象作为绘图指令(矢量图像)保存在 PdfGraphics2D 对象中。这意味着当 PDF reader 打开文件时,它会将这些说明转换为图像。

每个 PDF 查看器显示的内容略有不同的原因是因为它们都有自己的绘制这些图像的实现方式,而且这些单独的 PDF 查看器呈现绘图说明的方式似乎存在一些不一致。不幸的是,解决这个问题的唯一方法是创建一个实际的图像源,例如包含完整图像的 BufferedImage,而不是绘图指令。借助 ImageIO API,可以从 Graphics2D 对象获取 BufferedImage。这是一个关于这个的 SO post: Convert Graphics2D object to BufferedImage.

希望对您有所帮助!