Java给pdf文件添加文字等信息

maven依赖

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.ttxit</groupId>
    <artifactId>artifact</artifactId>
    <version>1.0.0</version>
    <packaging>war</packaging>
    <dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
        </dependency>
        <dependency>
            <groupId>org.jodd</groupId>
            <artifactId>jodd-madvoc</artifactId>
            <version>3.7</version>
        </dependency>
        <dependency>
            <groupId>org.jodd</groupId>
            <artifactId>jodd-http</artifactId>
            <version>3.7</version>
        </dependency>
        <dependency>
            <groupId>org.jodd</groupId>
            <artifactId>jodd-core</artifactId>
            <version>3.7</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.38</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.3.1</version>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.6.2</version>
        </dependency>
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itextpdf</artifactId>
            <version>5.5.9</version>
        </dependency>
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itext-asian</artifactId>
            <version>5.2.0</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.mortbay.jetty</groupId>
                <artifactId>maven-jetty-plugin</artifactId>
                <version>6.1.10</version>
                <configuration>
                    <encoding>UTF-8</encoding>
                    <scanIntervalSeconds>10</scanIntervalSeconds>
                    <stopKey>foo</stopKey>
                    <stopPort>9999</stopPort>
                </configuration>
                <executions>
                    <execution>
                        <id>start-jetty</id>
                        <phase>pre-integration-test</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <scanIntervalSeconds>0</scanIntervalSeconds>
                            <daemon>true</daemon>
                        </configuration>
                    </execution>
                    <execution>
                        <id>stop-jetty</id>
                        <phase>post-integration-test</phase>
                        <goals>
                            <goal>stop</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.0.2</version>
                <configuration>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

pdfutils源码

package com.ttxit.util;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

import org.apache.log4j.Logger;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;

public class PdfUtils {
    
    private static final Logger logger = Logger.getLogger(PdfUtils.class);
    static String               PDF    = "/Users/shicheng/Desktop/2_d.pdf";
    public static final String  NAME   = "STSong-Light";
    public static final String  ENCODE = "UniGB-UCS2-H";
    public static final int     SIZE   = 12;
    
    /**
     * 生成添加文字数据流
     * 
     * @param text
     *            输入信息
     * @param width
     *            宽度
     * @param height
     *            高度
     * @return 数据流
     */
    public static byte[] addText(String text, int width, int height) {
        ByteArrayOutputStream byteArrayOutputStream = null;
        PdfReader pdfReader = null;
        PdfStamper pdfStamper = null;
        PdfContentByte pdfContentByte = null;
        try {
            BaseFont baseFont = BaseFont.createFont(NAME, ENCODE, BaseFont.NOT_EMBEDDED);
            InputStream inputStream = new FileInputStream(new File(PDF)); // 读取默认pdf文件
            pdfReader = new PdfReader(inputStream); // 加载文件到pdf引擎
            byteArrayOutputStream = new ByteArrayOutputStream();
            pdfStamper = new PdfStamper(pdfReader, byteArrayOutputStream); // 加载模板
            pdfContentByte = pdfStamper.getOverContent(1); // 获取顶部
            pdfContentByte.beginText(); // 插入文字信息
            pdfContentByte.setFontAndSize(baseFont, SIZE);
            BaseColor baseColor = new BaseColor(0, 0, 0);
            pdfContentByte.setColorFill(baseColor);
            pdfContentByte.setTextMatrix(width, height); // 设置文字在页面中的坐标
            pdfContentByte.showText(text);
            pdfContentByte.endText();
        } catch (Exception e) {
            if (logger.isDebugEnabled()) {
                logger.error("addText(String, int, int) - Exception e=" + e); //$NON-NLS-1$
            }
        } finally {
            try {
                pdfStamper.close();
                pdfReader.close();
                byteArrayOutputStream.close();
            } catch (Exception _e) {
                if (logger.isDebugEnabled()) {
                    logger.error("addText(String, int, int) - Exception e=" + _e); //$NON-NLS-1$
                }
            }
        }
        return byteArrayOutputStream.toByteArray();
    }
    
}

action源码

package com.ttxit.action;

import org.apache.log4j.Logger;

import com.ttxit.util.PdfUtils;

import jodd.madvoc.meta.Action;
import jodd.madvoc.meta.In;
import jodd.madvoc.meta.MadvocAction;
import jodd.madvoc.meta.Out;
import jodd.madvoc.result.RawData;
import jodd.util.MimeTypes;

@MadvocAction
public class ImageAction {
    private static final Logger logger = Logger.getLogger(ImageAction.class);
    
    @Out
    String                      json   = "hello";
    @In
    int                         width;
    @In
    int                         height;
    
    @Action(value = "/image/brokenhearted_card", method = Action.GET)
    public RawData hello() {
        if (logger.isDebugEnabled()) {
            logger.debug("hello() - start"); //$NON-NLS-1$
        }
        RawData returnRawData = new RawData(PdfUtils.addText("AAA", width, height), MimeTypes.lookupMimeType("pdf"));
        if (logger.isDebugEnabled()) {
            logger.debug("hello() - end"); //$NON-NLS-1$
        }
        return returnRawData;
    }
    
}


转载于:https://my.oschina.net/shicheng2014/blog/654672

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值