21xrx.com
2024-09-19 10:05:03 Thursday
登录
文章检索 我的文章 写文章
Java实现PDF生成竖版和横版内容
2023-06-14 23:50:21 深夜i     --     --
Java PDF 生成

PDF是一种通用的文件格式,经常用于打印、阅读和存档。在Java应用程序中生成PDF文件是一个常见的需求,本文将探讨如何使用Java生成既有竖版又有横版的PDF内容。

首先需要导入以下依赖:


   com.itextpdf

   itextpdf

   5.5.13.2

接下来会按照以下步骤生成PDF:

1.创建一个Document对象

2.设置PaperSize大小

3.添加章节标题

4.添加横版表格

5.添加竖版表格

6.关闭Document对象,生成PDF文件


import com.itextpdf.text.Document;

import com.itextpdf.text.HeaderFooter;

import com.itextpdf.text.PageSize;

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.FileOutputStream;

public class PdfGenerator {

  private static final String FILE_PATH = "test.pdf";

  public static void main(String[] args) {

    Document document = new Document();

    try {

      PdfWriter.getInstance(document, new FileOutputStream(FILE_PATH));

      document.open();

      document.setPageSize(PageSize.A4.rotate());

      document.addTitle("Java生成PDF示例");

      Paragraph chapterTitle = new Paragraph("横版表格");

      document.add(chapterTitle);

      PdfPTable table = new PdfPTable(6);

      table.setWidthPercentage(100);

      table.setSpacingBefore(10f);

      table.setSpacingAfter(10f);

      PdfPCell cell1 = new PdfPCell(new Paragraph("姓名"));

      PdfPCell cell2 = new PdfPCell(new Paragraph("性别"));

      PdfPCell cell3 = new PdfPCell(new Paragraph("年龄"));

      PdfPCell cell4 = new PdfPCell(new Paragraph("地址"));

      PdfPCell cell5 = new PdfPCell(new Paragraph("手机号"));

      PdfPCell cell6 = new PdfPCell(new Paragraph("邮箱"));

      table.addCell(cell1);

      table.addCell(cell2);

      table.addCell(cell3);

      table.addCell(cell4);

      table.addCell(cell5);

      table.addCell(cell6);

      for (int i = 0; i < 10; i++) {

        table.addCell("姓名" + i);

        table.addCell("男");

        table.addCell("18");

        table.addCell("北京市海淀区");

        table.addCell("12345678901");

        table.addCell("test@test.com");

      }

      document.add(table);

      Paragraph chapterTitle2 = new Paragraph("竖版表格");

      document.add(chapterTitle2);

      PdfPTable table2 = new PdfPTable(4);

      table2.setWidthPercentage(100);

      table2.setSpacingBefore(10f);

      table2.setSpacingAfter(10f);

      PdfPCell cell12 = new PdfPCell(new Paragraph("姓名"));

      PdfPCell cell22 = new PdfPCell(new Paragraph("性别"));

      PdfPCell cell32 = new PdfPCell(new Paragraph("年龄"));

      PdfPCell cell42 = new PdfPCell(new Paragraph("手机号"));

      table2.addCell(cell12);

      table2.addCell(cell22);

      table2.addCell(cell32);

      table2.addCell(cell42);

      for (int i = 0; i < 10; i++) {

        table2.addCell("姓名" + i);

        table2.addCell("男");

        table2.addCell("18");

        table2.addCell("12345678901");

      }

      document.add(table2);

      document.close();

    } catch (Exception e) {

      e.printStackTrace();

    }

  }

}

通过运行上面的代码,会生成一个既有横版表格又有竖版表格的PDF文件。

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复