以下代码,如有问题,请大家不吝指出,如有更优实现方案,欢迎一起讨论。
最近在做一个需求:在pdf和word右上角添加一个二维码
其中,pdf可以灵活实现二维码的位置,但是word由于我是在页眉处添加的图片,因此我的方法只能在页眉处添加图片。
首先定义一个接口(先抛出Exception,还没优化)
public interface DocAddQRCodeI {
int QRCodeLength = 70;
void addQRCode(String beforePath, String afterPath, String imagePath) throws Exception;
}
1.pdf
Maven中添加如下依赖:
<dependency>
<groupId>com.lowagie</groupId>
<artifactId>itext</artifactId>
<version>2.1.7</version>
</dependency>
import com.lowagie.text.Image;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;
import java.io.FileOutputStream;
public class PdfAddQRCode implements DocAddQRCodeI {
private static final int