亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 iText 7 中將方向設置為橫向

如何在 iText 7 中將方向設置為橫向

慕標琳琳 2022-05-21 13:52:47
我正在使用帶有方法 convertToPdf() 的 iText7 將 html 轉換為 pdf。PDF 正在正確生成,但橫向模式不起作用。有人可以告訴如何獲得橫向模式嗎?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);    }}
查看完整描述

2 回答

?
天涯盡頭無女友

TA貢獻1831條經驗 獲得超9個贊

請只使用PdfDocument作為參數的轉換器方法。例如下一個:convertToPdf(InputStream htmlStream, PdfDocument pdfDocument, ConverterProperties converterProperties)


現在您唯一需要做的就是在轉換 html 文件之前將頁面大小設置為文檔。


    PdfDocument pdfDocument = new PdfDocument(new PdfWriter(new File(sourcePath)));

    pdfDocument.setDefaultPageSize(PageSize.A4.rotate());

    HtmlConverter.convertToPdf(new FileInputStream(destPath), pdfDocument, props);


查看完整回答
反對 回復 2022-05-21
?
GCT1015

TA貢獻1827條經驗 獲得超4個贊

您可以使用PageOrientationsEventHandler來處理文檔中的方向,例如 -


PdfDocument pdfDoc = new PdfDocument(new PdfWriter(DEST));

PageOrientationsEventHandler eventHandler = new PageOrientationsEventHandler();

pdfDoc.addEventHandler(PdfDocumentEvent.START_PAGE, eventHandler);

Document doc = new Document(pdfDoc);

doc.add(new Paragraph("A simple page in portrait orientation"));

eventHandler.setOrientation(LANDSCAPE);


查看完整回答
反對 回復 2022-05-21
  • 2 回答
  • 0 關注
  • 791 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號