JAVA开发 利用代码生成奖状pdf-英文版

1、图片模板

请添加图片描述
请添加图片描述

2、实现代码

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.font.PDType0Font;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import static org.apache.pdfbox.pdmodel.common.PDRectangle.*;
//生成pdf文件版本的奖状
//只能识别英文字符,插入中文会报错
public class MakeCertificate {

    public static final  String TEMP_PATH = "D:\\OA_Temp\\templates\\";
    public static final  String ATTRACH_PATH = "D:\\OA_Temp\\attachments\\pdf_attachs\\";
    public static final  String FONT_PATH = "C:\\Windows\\Fonts\\Arial.ttf";

    public static void MakePfdOfCertificate(){
        float width = 842;
        float height = 595;
        try (PDDocument document = new PDDocument()) {
            // 创建一个 A4 大小的页面,并旋转 90 度使其变为横向
            PDRectangle pageSizeLandscape = new PDRectangle(width, height);
            PDPage page = new PDPage(pageSizeLandscape);
            document.addPage(page);

            // 加载图片-签章
            PDImageXObject pdImage = PDImageXObject.createFromFile(TEMP_PATH + "CompanySeal.png", document);

            // 加载图片-奖状
            PDImageXObject pdImage2 = PDImageXObject.createFromFile(TEMP_PATH + "Certificate.jpg", document);

            // 创建一个Date对象(这里使用当前时间作为示例)
            Date date = new Date();

            // 创建一个SimpleDateFormat对象并指定日期格式
            SimpleDateFormat sdf = new SimpleDateFormat(" yyyy MM ");
            String formattedDate = sdf.format(date);

            // 开始一个内容流以绘制图片
            try (PDPageContentStream contentStream = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.APPEND, true, true)) {
                float scale = 0.5f; // 缩放因子,如果图片太大可以调整这个值

                // 奖状背景
                contentStream.drawImage(pdImage2, 0, 0, width, height);

                // 签章
                contentStream.drawImage(pdImage, 550, 75, pdImage.getWidth() * scale, pdImage.getHeight() * scale);

                File fontFile = new File(FONT_PATH); 
                PDType0Font font = PDType0Font.load(document, fontFile);

                //设置字体和大小
                contentStream.setFont(font, 13);

                //写入文本--奖励者
                contentStream.beginText();
                contentStream.newLineAtOffset(60, 365);
                contentStream.showText("Dear Mrs.LI:");
                contentStream.endText();

                //写入文本--公司
                contentStream.beginText();
                contentStream.newLineAtOffset(560, 135);
                contentStream.showText("This is a company name (China) Co., LTD");
                contentStream.endText();

                //写入文本--年月
                contentStream.beginText();
                contentStream.newLineAtOffset(615, 120);
                contentStream.showText("YEAR MONTH :");
                contentStream.endText();

                //写入文本--公司
                String content = "Beijing, May 29 (Xinhua)  The sense of gain, happiness and security of the rural masses has been continuously enhanced, and the rural road has become the road of prosperity, happiness, connecting hearts and rejuvenation at the doorstep of the people." +
                        "Beijing, May  (Xinhua)  The sense of gain, happiness and security of the rural masses has been continuously enhanced, and the rural road has become the road of prosperity, happiness, connecting hearts and rejuvenation at the doorstep of the people." +
                        "Beijing, May  (Xinhua)  The sense of gain, happiness and security of the rural masses has been continuously enhanced, and the rural road has become the road of prosperity, happiness, connecting hearts and rejuvenation at the doorstep of the people." +
                        "Beijing, May  (Xinhua)  The sense of gain, happiness and security of the rural masses has been continuously enhanced, and the rural road has become the road of prosperity, happiness, connecting hearts and rejuvenation at the doorstep of the people." +
                        "";
                content = "    " + content;
                int strLength = content.length();
                int index = 0;   //字符串下标
                int rowNum = 330;   //内容填充位置
                int rowCount = 118;
                while(index < strLength+rowCount){
                    String conStr;
                    if(strLength > index+rowCount){
                        conStr = content.substring(index,index+rowCount);
                    }else{
                        conStr = content.substring(index,strLength);
                        index = index +rowCount;
                    }
                    contentStream.beginText();
                    contentStream.newLineAtOffset(60, rowNum); // 设置起始位置(X, Y)
                    contentStream.showText(conStr);
                    contentStream.endText();

                    index = index + rowCount;
                    rowNum = rowNum - 15;
                }
            }
            sdf = new SimpleDateFormat("yyyyMMdd HHmmss ");
            formattedDate = sdf.format(date);
            String fileName = formattedDate + "temp.pdf";

            // 保存修改后的 PDF 文件
            document.save(ATTRACH_PATH + fileName);

            System.out.println("图片已成功插入到PDF中!");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        MakePfdOfCertificate();
    }

}

3、生成模板(pdf文件截取)

在这里插入图片描述

4、添加pdfbox依赖库

4.1.pdfbox依赖库

Pdfbox.jar 依赖库是一个开源的Java库,主要用于处理PDF文件,为Java开发者提供了一个强大而灵活的工具来处理PDF文件,并支持广泛的应用场景,其作用可以归纳为以下几个方面:

  1. PDF文档的开发和转换:
    Pdfbox.jar提供了API和工具来处理PDF文档,支持创建、修改、转换和提取PDF文档的内容。
    开发者可以使用这个库来开发创建、转换和操作PDF文档的Java程序。
  2. 功能丰富:
    提取文本:从PDF文件中提取Unicode文本。
    分割与合并:将单个PDF文件分成多个文件,或将多个文件合并为单个文件。
    填写表单:在文档中填写表单数据。
    打印:使用标准Java打印API打印PDF文件。
    保存为图像:将PDF保存为图像文件,如PNG或JPEG。
    创建PDF:通过Java程序创建新的PDF文件,并可以包含图像和字体。
    签名:向PDF文件添加数字签名。
  3. 支持处理各种PDF特性:
    Pdfbox支持处理文本、字体、图像、表单字段、注释、书签、页面布局等PDF的各种特性。
    它还提供了对加密和数字签名PDF文档的支持,以及对PDF文档的高级操作,如提取文本位置信息、提取图像和字体等。
  4. 开源免费:
    Pdfbox是一个开源项目,可以免费使用和修改。
  5. 跨平台:
    由于Pdfbox是基于Java开发的,因此它可以在各种操作系统上运行。
  6. 应用场景广泛:
    Pdfbox的功能使其适用于多种应用场景,如文档处理、打印和发布、数字签名、表单处理等。
    版本更新与兼容性:
    Pdfbox有多个版本,每个版本可能具有不同的功能和特性。因此,在引入Pdfbox依赖时,需要注意版本之间的兼容性和差异。
  7. 错误处理:
    在使用Pdfbox时,可能会遇到由于PDF文件复杂性、PDFBox版本不兼容或代码编写错误等问题导致的错误。因此,需要仔细检查和调试代码,确保正确使用Pdfbox库,并参考官方文档和示例代码来解决问题。

4.2. IDEA添加pdfbox依赖库方式

Intellij IDEA版本:Interllij IDEA Community Edition 2023.2.1
pdfbox版本:pdfbox-3.0.2

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 18
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值