总结之lowagie.text合并PDF文件

通过itext合并PDF

引入依赖
<dependency>
<groupId>com.lowagie.text</groupId>
	<artifactId>com.springsource.com.lowagie.text</artifactId>
	<version>${com.lowagie.text.version}</version>
</dependency>

工具类
public class MergePDFUtil {

    public static void main(String[] args) {
        //String[] files = {"d:\\HT_15965121390002824.pdf", "d:\\HT_159609896500035153.pdf", "d:\\HT_159609993800046541.pdf"};
        String savepath = "d:\\temp.pdf";
        String[] files = new String[1];
        files[0] = "d:\\test1.pdf";
        mergePdfFiles(files, savepath);
    } /*
     * * 合並pdf文件 * * @param files 要合並文件數組(絕對路徑如{ "e:\\1.pdf", "e:\\2.pdf" ,
     * "e:\\3.pdf"}) * @param newfile
     * 合並後新產生的文件絕對路徑如e:\\temp.pdf,請自己刪除用過後不再用的文件請 * @return boolean
     * 產生成功返回true, 否則返回false
     */

    public static boolean mergePdfFiles(String[] files, String newfile) {
        boolean retValue = false;
        Document document = null;
        try {
            document = new Document(new PdfReader(files[0]).getPageSize(1));
            PdfCopy copy = new PdfCopy(document, new FileOutputStream(newfile));
            document.open();
            for (int i = 0; i < files.length; i++) {
                PdfReader reader = new PdfReader(files[i]);
                int n = reader.getNumberOfPages();
                for (int j = 1; j <= n; j++) {
                    document.newPage();
                    PdfImportedPage page = copy.getImportedPage(reader, j);
                    copy.addPage(page);
                }
            }
            retValue = true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            document.close();
        }
        return retValue;
    }
}

要将com.lowagie.text.Document(PDF文档)垂直居中,您可以使用iText库中的PdfWriter和PdfPTable。下面是一个示例代码片段,演示如何将文档的内容垂直居中: ```java import com.lowagie.text.Document; import com.lowagie.text.DocumentException; import com.lowagie.text.PageSize; import com.lowagie.text.Paragraph; import com.lowagie.text.pdf.PdfPCell; import com.lowagie.text.pdf.PdfPTable; import com.lowagie.text.pdf.PdfWriter; import java.io.FileNotFoundException; import java.io.FileOutputStream; public class VerticalAlignmentExample { public static void main(String[] args) { Document document = new Document(PageSize.A4); try { PdfWriter.getInstance(document, new FileOutputStream("vertical_alignment.pdf")); document.open(); PdfPTable table = new PdfPTable(1); table.setWidthPercentage(100); PdfPCell cell = new PdfPCell(new Paragraph("This is vertically centered content.")); cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE); cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER); cell.setMinimumHeight(400); table.addCell(cell); document.add(table); document.close(); } catch (DocumentException | FileNotFoundException e) { e.printStackTrace(); } } } ``` 在上面的示例中,我们创建了一个单元格(PdfPCell),通过设置`setVerticalAlignment(PdfPCell.ALIGN_MIDDLE)`将其垂直居中,通过设置`setHorizontalAlignment(PdfPCell.ALIGN_CENTER)`将其水平居中。通过`setMinimumHeight(400)`设置单元格的最小高度为400。然后将单元格添加到PdfPTable中,并将该表添加到文档中。 运行这个示例代码会生成一个名为"vertical_alignment.pdf"的PDF文件,其中包含一个垂直居中的单元格。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值