java itext html转pdf后居中问题解决

12 篇文章 0 订阅
6 篇文章 1 订阅

在整体的div上加上代码:

padding-left: 20pt;
padding-right: -20pt;

亲测管用

itextpdf中,可以使用`PdfPCell`类来设置单元格中文字的水平和垂直居中方式。具体的方法是通过`setHorizontalAlignment()`和`setVerticalAlignment()`方法来设置对齐方式。 以下是一个示例代码,演示了如何将单元格中的文字水平和垂直居中: ```java import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Font; import com.itextpdf.text.FontFactory; import com.itextpdf.text.Paragraph; import com.itextpdf.text.pdf.PdfPCell; import com.itextpdf.text.pdf.PdfPTable; import com.itextpdf.text.pdf.PdfWriter; import java.io.FileNotFoundException; import java.io.FileOutputStream; public class TableCellAlignmentExample { public static void main(String[] args) { Document document = new Document(); try { PdfWriter.getInstance(document, new FileOutputStream("TableCellAlignmentExample.pdf")); document.open(); PdfPTable table = new PdfPTable(1); PdfPCell cell = new PdfPCell(new Paragraph("哈哈哈哈哈", FontFactory.getFont(FontFactory.HELVETICA, 12))); cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER); cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE); table.addCell(cell); document.add(table); document.close(); System.out.println("TableCellAlignmentExample.pdf created successfully."); } catch (DocumentException | FileNotFoundException e) { e.printStackTrace(); } } } ``` 这段代码创建了一个单元格,并将文字设置为"哈哈哈哈哈"。然后,通过`setHorizontalAlignment()`方法将文字水平居中,通过`setVerticalAlignment()`方法将文字垂直居中。最后,将单元格添加到表格中,并将表格添加到文档中。 运行代码后,将生成一个名为"TableCellAlignmentExample.pdf"的PDF文件,其中单元格中的文字将水平和垂直居中
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值