如何在iText 7中将方向设置为横向 [英] How to set orientation to Landscape in iText 7

查看:456
本文介绍了如何在iText 7中将方向设置为横向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用iText7的convertToPdf()方法将html转换为pdf. PDF生成正确,但是横向模式无法正常工作.

I am converting html to pdf using iText7 with method convertToPdf(). PDF is getting generated properly but Landscape mode is not working.

有人可以告诉您如何获得横向模式吗?

Can some one tell how to get Landscape mode?

import com.itextpdf.html2pdf.ConverterProperties;
import com.itextpdf.html2pdf.HtmlConverter;
import com.itextpdf.styledxmlparser.css.media.MediaDeviceDescription;
import com.itextpdf.styledxmlparser.css.media.MediaType;

import java.io.File;
import java.io.IOException;

import static com.itextpdf.html2pdf.css.CssConstants.LANDSCAPE;

public class htmlToPDF {

    public static void main(String args[]) throws IOException {

        ConverterProperties properties = new ConverterProperties();

        MediaDeviceDescription med = new MediaDeviceDescription(MediaType.ALL);
        med.setOrientation(LANDSCAPE);
        properties.setMediaDeviceDescription(med);

        HtmlConverter.convertToPdf(new File("D:\\test.html"), new File("D:\\test.pdf"),properties);
    }
}

推荐答案

请仅使用将PdfDocument作为参数的转换器方法.例如,下一个:convertToPdf(InputStream htmlStream, PdfDocument pdfDocument, ConverterProperties converterProperties)

Please just use a converter method that takes PdfDocument as a parameter. For example, the next one : convertToPdf(InputStream htmlStream, PdfDocument pdfDocument, ConverterProperties converterProperties)

现在,您唯一需要做的就是在转换html文件之前为文档设置页面大小.

Now the only thing you need is to set the page size to the document before converting the html file.

    PdfDocument pdfDocument = new PdfDocument(new PdfWriter(new File(sourcePath)));
    pdfDocument.setDefaultPageSize(PageSize.A4.rotate());
    HtmlConverter.convertToPdf(new FileInputStream(destPath), pdfDocument, props);

这篇关于如何在iText 7中将方向设置为横向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆