반응형
이미지를byte[]
로 만들어 PDF 문서에 추가합니다.
import java.io.File;
import java.io.FileInputStream;
import com.itextpdf.io.IOException;
import com.itextpdf.io.image.ImageData;
import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.io.source.ByteArrayOutputStream;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Image;
public class Itext7_02 {
public static void main(String[] args) throws IOException, Exception {
File file = new File("iText_Image_Ex2.pdf");
PdfWriter pdfWriter = new PdfWriter(file);
PdfDocument pdfDocument = new PdfDocument(pdfWriter);
Document doc = new Document(pdfDocument);
File imgFile = new File("google.png");
FileInputStream img = new FileInputStream(imgFile);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int i;
byte[] data = new byte[1024];
while ((i = img.read(data, 0, data.length)) != -1) {
baos.write(data, 0, i);
}
baos.flush();
img.close();
ImageData imgData = ImageDataFactory.create(baos.toByteArray());
Image pdfImg = new Image(imgData);
doc.add(pdfImg);
doc.close();
}
}
반응형
'Spring Boot > Unit Code' 카테고리의 다른 글
[spring boot] PDF 출력 - iText7_01(TEXT) (0) | 2023.02.08 |
---|---|
[spring boot] PDF 출력 - iText7_01(Image) (0) | 2020.07.05 |