Bootstrap

JAVA 使用 itextpdf 构建 PDF

1.首先引入对应的 jar 包    

<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itextpdf</artifactId>
    <version>5.5.13.2</version>
</dependency>

2. 因为要展现中文,所以要指定对应的中文字体,否则中文则显示不了...


 BaseFont baseFont = BaseFont.createFont("C:/Windows/Fonts/simfang.ttf",  
      BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);

       linux 环境下 需要下载对应的字体   (linux 默认存放字体的地址   /usr/share/fonts/)

3.简单的PDF 输入...

        指定文本的 大小,粗体,下划线

import com.gfkj.housingsystem.HousingSystemApplication;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfWriter;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.web.WebAppConfiguration;

import java.io.FileOutputStream;


@RunWith(SpringRunner.class)
@SpringBootTest(classes = HousingSystemApplication.class)
@WebAppConfiguration
public class JavaPDFTest {

    @Test
    public void test() {
        try{
            Document document = new Document();
            BaseFont baseFont = BaseFont.createFont( "C:/Windows/Fonts/simfang.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            Font font1 = new Font(baseFont,18, Font.BOLD);      // 字体大小 , 加粗
            Font font2 = new Font(baseFont,18, Font.NORMAL);   // 字体大小 , 正常
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("test.pdf"));
            document.open();
            document.add(new Paragraph("这是我创建的pdf 加粗",font1));   // 加粗的字体
            document.add(new Paragraph("这是我创建的pdf 正常",font2));  //  正常的字体
            Paragraph  paragraph =  new Paragraph("这是我创建的pdf: ",font2);  // 下划线
            Chunk sig = new Chunk("下划线");
            sig.setUnderline(0.1f,-2f);
            paragraph.add(sig);
            document.add(paragraph);

            document.close();
            writer.close();
        }catch (Exception ex){
            ex.printStackTrace();
        }
    }
}

执行结果

 

4.PDF 中写入图片

        本地图片的写入,以及图片地址的写入

import com.gfkj.housingsystem.HousingSystemApplication;
import com.itextpdf.text.Document;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfWriter;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.web.WebAppConfiguration;

import java.io.FileOutputStream;
import java.net.URL;


@RunWith(SpringRunner.class)
@SpringBootTest(classes = HousingSystemApplication.class)
@WebAppConfiguration
public class JavaPDFTest {

    @Test
    public void test() {
        try{
            Document document = new Document();
            BaseFont baseFont = BaseFont.createFont( "C:/Windows/Fonts/simfang.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            Font font1 = new Font(baseFont,18, Font.BOLD);      // 字体大小 , 加粗
            Font font2 = new Font(baseFont,18, Font.NORMAL);   // 字体大小 , 正常
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("test.pdf"));
            document.open();
            document.add(new Paragraph("PDF 图片",font2));
            Image image1 = Image.getInstance("C:\\Users\\Administrator\\Pictures\\IMG41.jpg");
            image1.setAbsolutePosition(200f, 500f);  // 设置图片位置
            image1.scaleAbsolute(100, 100);   // 设置图片大小
            document.add(image1);
            String imageUrl = "https://tse1-mm.cn.bing.net/th/id/OET.4964b3b853484fed81e1d0b10906db86?w=272&h=135&c=7&rs=1&o=5&pid=1.9";
            Image image2 = Image.getInstance(new URL(imageUrl));
            document.add(image2);
            document.close();
            writer.close();
        }catch (Exception ex){
            ex.printStackTrace();
        }
    }
}

执行结果

 5.PDF 中插入表格  

        设置表格边框,颜色,边距

import com.gfkj.housingsystem.HousingSystemApplication;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.web.WebAppConfiguration;

import java.io.FileOutputStream;


@RunWith(SpringRunner.class)
@SpringBootTest(classes = HousingSystemApplication.class)
@WebAppConfiguration
public class JavaPDFTest {

    @Test
    public void test() {
        try{
            Document document = new Document();
            BaseFont baseFont = BaseFont.createFont( "C:/Windows/Fonts/simfang.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            Font font1 = new Font(baseFont,18, Font.BOLD);      // 字体大小 , 加粗
            Font font2 = new Font(baseFont,18, Font.NORMAL);   // 字体大小 , 正常
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("test.pdf"));
            document.open();
            PdfPTable table = new PdfPTable(2); //  设置二列
            table.setWidthPercentage(100);
            table.setSpacingBefore(15f);
            table.setSpacingAfter(15f);

            PdfPCell cell1 = new PdfPCell(new Paragraph("表格1 ",font2));
            cell1.setHorizontalAlignment(Element.ALIGN_LEFT);
            cell1.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell1.setPadding(4f);   // 设置内边距
            cell1.disableBorderSide(15);// 去掉边框
            PdfPCell cell2 = new PdfPCell(new Paragraph("表格1",font2));
            cell2.setHorizontalAlignment(Element.ALIGN_LEFT);
            cell2.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell2.setPadding(4f);   // 设置内边距
            table.addCell(cell1);
            table.addCell(cell2);
            PdfPCell cell3 = new PdfPCell(new Paragraph("表格2",font2));
            cell3.setHorizontalAlignment(Element.ALIGN_LEFT);
            cell3.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell3.setPadding(4f);
            cell3.setBorderColor(BaseColor.RED);  // 设置边框 红色
            PdfPCell cell4 = new PdfPCell(new Paragraph("表格2",font2));
            cell4.setHorizontalAlignment(Element.ALIGN_LEFT);
            cell4.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell4.setBorderColor(BaseColor.BLUE);  // 设置边框 蓝色
            cell4.setPadding(4f);
            table.addCell(cell3);
            table.addCell(cell4);
            document.add(table);
            document.close();
            writer.close();
        }catch (Exception ex){
            ex.printStackTrace();
        }
    }
}

执行结果

;