1. 程式人生 > >pdf及word右上角新增圖片(Java實現)

pdf及word右上角新增圖片(Java實現)

以下程式碼,如有問題,請大家不吝指出,如有更優實現方案,歡迎一起討論。

最近在做一個需求:在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 left = 430; //左邊距
    private static final int button = 720; //底邊距

    public void addQRCode(String beforePath, String afterPath, String imagePath) throws Exception {

        PdfReader reader = new PdfReader(beforePath);
        int pageCount = reader.getNumberOfPages();
        FileOutputStream resOutputStream = new FileOutputStream(afterPath);
        PdfStamper stamper = new PdfStamper(reader, resOutputStream);
        Image image = Image.getInstance(imagePath);
        image.scaleAbsolute(QRCodeLength, QRCodeLength); //二維碼大小
        image.setAbsolutePosition(left, button); //設定 左邊距、底邊距

        //在每一頁都加了二維碼,也可只在某一頁加,大家按需取用
        for (int i = 1; i <= pageCount; i++) {
            PdfContentByte overContent = stamper.getOverContent(i);
            overContent.addImage(image);
            overContent.stroke();
        }

        stamper.close();
        reader.close();
    }
}

2.word

在maven中新增如下依賴:

<dependency>
	<groupId>org.apache.poi</groupId>
	<artifactId>poi-ooxml</artifactId>
	<version>3.15</version>
</dependency>

程式碼如下:

import org.apache.poi.util.Units;
import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy;
import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSectPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTabStop;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STHdrFtr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTabJc;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.math.BigInteger;


public class WordAddQRCode implements DocAddQRCodeI {

    public void addQRCode(String beforePath, String afterPath, String imagePath) throws Exception {
        InputStream is = new FileInputStream(new File(beforePath));
        XWPFDocument doc = new XWPFDocument(is);

        //新增頁首,在頁首上新增二維碼圖片
        CTSectPr sectPr = doc.getDocument().getBody().addNewSectPr();
        XWPFHeaderFooterPolicy headerFooterPolicy = new XWPFHeaderFooterPolicy(doc, sectPr);

        //其實本來想只在第一頁新增二維碼的,但是該方法引數改為STHdrFtr.FIRST後並無效果,我沒有思路,有興趣的同學歡迎研究
        XWPFHeader header = headerFooterPolicy.createHeader(STHdrFtr.DEFAULT);

        XWPFParagraph paragraph = header.getParagraphArray(0);
        paragraph.setAlignment(ParagraphAlignment.LEFT);

        CTTabStop tabStop = paragraph.getCTP().getPPr().addNewTabs().addNewTab();
        tabStop.setVal(STTabJc.RIGHT);

        int twipsPerInch = 1440;
        tabStop.setPos(BigInteger.valueOf(6 * twipsPerInch));

        XWPFRun run = paragraph.createRun();
        run.addTab();

        XWPFPicture picture = run.addPicture(new FileInputStream(imagePath), XWPFDocument.PICTURE_TYPE_PNG, imagePath, Units.toEMU(QRCodeLength), Units.toEMU(QRCodeLength));
        String blipID = "";
        for (XWPFPictureData picturedata : header.getAllPackagePictures()) {
            blipID = header.getRelationId(picturedata);
        }
        picture.getCTPicture().getBlipFill().getBlip().setEmbed(blipID);

        doc.write(new FileOutputStream(afterPath));
    }

}