|
|
|
package cn.teammodel.utils;
|
|
|
|
|
|
|
|
import com.itextpdf.text.DocumentException;
|
|
|
|
import com.itextpdf.text.Image;
|
|
|
|
import com.itextpdf.text.Rectangle;
|
|
|
|
import com.itextpdf.text.pdf.AcroFields;
|
|
|
|
import com.itextpdf.text.pdf.PdfContentByte;
|
|
|
|
import com.itextpdf.text.pdf.PdfStamper;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author winter
|
|
|
|
* @create 2024-01-24 16:02
|
|
|
|
*/
|
|
|
|
public class PdfUtil {
|
|
|
|
public static void fillPdfForm(PdfStamper stamper, Map<String, String> data) throws IOException, DocumentException {
|
|
|
|
AcroFields fields = stamper.getAcroFields();
|
|
|
|
for (Map.Entry<String, String> entry : data.entrySet()) {
|
|
|
|
fields.setField(entry.getKey(), entry.getValue());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void fillImage(PdfStamper stamper, String imgFieldName, byte[] data) throws DocumentException, IOException {
|
|
|
|
AcroFields acroFields = stamper.getAcroFields();
|
|
|
|
Image image = Image.getInstance(data);
|
|
|
|
int pageNo = acroFields.getFieldPositions(imgFieldName).get(0).page;
|
|
|
|
Rectangle rectangle = acroFields.getFieldPositions(imgFieldName).get(0).position;
|
|
|
|
float x = rectangle.getLeft();
|
|
|
|
float y = rectangle.getBottom();
|
|
|
|
PdfContentByte content = stamper.getOverContent(pageNo);
|
|
|
|
image.scaleToFit(rectangle.getWidth(), rectangle.getHeight());
|
|
|
|
image.setAbsolutePosition(x, y);
|
|
|
|
content.addImage(image);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|