java中 将byte[]类型数据转为pdf,并在指定坐标位置添加文字,要求使用com.itextpdf.text.pdf
时间: 2024-03-07 07:47:22
浏览: 66
可以使用以下[代码](https://geek.csdn.net/educolumn/1572ef9b473b4e00f6b2bf6d428b7c27?spm=1055.2569.3001.10083)将byte[]类型数据转为pdf,并在指定坐标位置添加文字:
import com.itextpdf.text.Document;
import com.itextpdf.text.[page](https://geek.csdn.net/educolumn/1c66455c37fe0c4b32e4414c4c6aeead?spm=1055.2569.3001.10083)Size;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.*;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
public class PDFTest {
public st[ati](https://geek.csdn.net/educolumn/150d7073277950db5f09620704e791cf?spm=1055.2569.3001.10083)c byte[] addTextToPdf(byte[] pdfBytes, [string](https://geek.csdn.net/edu/8802d631b97a4a6af1f4d0bbf8527465?dp_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6NDQ0MDg2MiwiZXhwIjoxNzA3MzcxOTM4LCJpYXQiOjE3MDY3NjcxMzgsInVzZXJuYW1lIjoid2VpeGluXzY4NjQ1NjQ1In0.RrTYEnMNYPC7AQdoij4SBb0kKEgHoyvF-bZOG2eGQvc&spm=1055.2569.3001.10083) text, float x, float y) throws Exception {
PdfReader reader = new PdfReader(new ByteArrayInputStream(pdfBytes));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PdfStam[per](https://geek.csdn.net/educolumn/2c6ed1be7edac16b3c9a0c3228eaff0c?spm=1055.2569.3001.10083) stamper = new PdfStamper(reader, baos);
PdfContentByte canvas = stamper.getOverContent(1);
BaseFont bf = BaseFont.createFont(BaseFont.
```