Java 使用 Itext 更改 PDF 的方向

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23381148/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 22:19:53  来源:igfitidea点击:

Changing the orientation of PDF using Itext

javaitextjfreechart

提问by Vikram

I have done creating pdf and chart with the help of this link http://viralpatel.net/blogs/generate-pie-chart-bar-graph-in-pdf-using-itext-jfreechart/.

我已经在此链接的帮助下创建了 pdf 和图表http://viralpatel.net/blogs/generate-pie-chart-bar-graph-in-pdf-using-itext-jfreechart/

package net.viralpatel.pdf;

import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.io.FileOutputStream;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;

import com.itextpdf.text.Document;
import com.itextpdf.text.pdf.DefaultFontMapper;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfTemplate;
import com.itextpdf.text.pdf.PdfWriter;

public class PieChartDemo {    
public static void main(String[] args) {
     //TODO: Add code to generate PDFs with charts
    writeChartToPDF(generateBarChart(), 500, 400, "C://barchart.pdf");
    writeChartToPDF(generatePieChart(), 500, 400, "C://piechart.pdf");
}

public static void writeChartToPDF(JFreeChart chart, int width, int height, String fileName) {
    PdfWriter writer = null;

    Document document = new Document();

    try {
        writer = PdfWriter.getInstance(document, new FileOutputStream(
                fileName));
        document.open();
        PdfContentByte contentByte = writer.getDirectContent();
        PdfTemplate template = contentByte.createTemplate(width, height);
        Graphics2D graphics2d = template.createGraphics(width, height,
                new DefaultFontMapper());
        Rectangle2D rectangle2d = new Rectangle2D.Double(0, 0, width,
                height);

        chart.draw(graphics2d, rectangle2d);

        graphics2d.dispose();
        contentByte.addTemplate(template, 0, 0);

    } catch (Exception e) {
        e.printStackTrace();
    }
    document.close();
}

public static JFreeChart generatePieChart() {
    DefaultPieDataset dataSet = new DefaultPieDataset();
    dataSet.setValue("China", 19.64);
    dataSet.setValue("India", 17.3);
    dataSet.setValue("United States", 4.54);
    dataSet.setValue("Indonesia", 3.4);
    dataSet.setValue("Brazil", 2.83);
    dataSet.setValue("Pakistan", 2.48);
    dataSet.setValue("Bangladesh", 2.38);

    JFreeChart chart = ChartFactory.createPieChart(
            "World Population by countries", dataSet, true, true, false);

    return chart;
}

public static JFreeChart generateBarChart() {
    DefaultCategoryDataset dataSet = new DefaultCategoryDataset();
    dataSet.setValue(791, "Population", "1750 AD");
    dataSet.setValue(978, "Population", "1800 AD");
    dataSet.setValue(1262, "Population", "1850 AD");
    dataSet.setValue(1650, "Population", "1900 AD");
    dataSet.setValue(2519, "Population", "1950 AD");
    dataSet.setValue(6070, "Population", "2000 AD");

    JFreeChart chart = ChartFactory.createBarChart(
            "World Population growth", "Year", "Population in millions",
            dataSet, PlotOrientation.VERTICAL, false, true, false);

    return chart;
}

}

}

Now how to change the orientation of the pdf from prtrait to landscape?

现在如何将 pdf 的方向从 prtrait 更改为横向?

回答by Bruno Lowagie

When you create a Documentobject like this:

当您创建这样的Document对象时:

Document document = new Document();

You tell iText to create a document with pages of the A4 format using Portrait orientation. You can add a Rectangleobject as a parameter of the Documentconstructor to create documents with pages in any size you want.

您告诉 iText 使用纵向方向创建一个包含 A4 格式页面的文档。您可以添加一个Rectangle对象作为Document构造函数的参数,以创建具有任意大小页面的文档。

You can also use the PageSizeclass to use a predefined format. For instance: if you want to use an A4 page in landscape, you'd use this:

您还可以使用PageSize该类来使用预定义的格式。例如:如果你想在横向使用 A4 页面,你可以使用这个:

Document document = new Document(PageSize.A4.rotate());

Please download the free eBook "The ABC of PDF"for more info.

下载免费电子书“PDF 的 ABC”以获取更多信息。