ApachePOI和iTextPDF。将.ppt转换为PDF时,每张PDF幻灯片的边框都很大
我生成的pdf在pdf的左侧、底部和右侧都有很大的空白。我已经通过将我的BuffereImage写入JPEG来确认,我从powerpoint中提取的图像中不存在这些白色边框ApachePOI和iTextPDF。将.ppt转换为PDF时,每张PDF幻灯片的边框都很大,itext,apache-poi,Itext,Apache Poi,我生成的pdf在pdf的左侧、底部和右侧都有很大的空白。我已经通过将我的BuffereImage写入JPEG来确认,我从powerpoint中提取的图像中不存在这些白色边框 我正在缩放图像,否则我会得到一张低分辨率的540图像,我会将1080图像缩放2因为我们已经通过电子邮件进行了沟通(它变成垃圾邮件了吗?),我想知道你为什么不直接在POI中使用新的PDF功能?它应该比PDF中的位图有更好的效果 import com.itextpdf.text.Document; import com.itex
我正在缩放图像,否则我会得到一张低分辨率的540图像,我会将1080图像缩放2因为我们已经通过电子邮件进行了沟通(它变成垃圾邮件了吗?),我想知道你为什么不直接在POI中使用新的PDF功能?它应该比PDF中的位图有更好的效果
import com.itextpdf.text.Document;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.Image;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hslf.usermodel.HSLFSlide;
import org.apache.poi.hslf.usermodel.HSLFSlideShow;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xslf.usermodel.XSLFSlide;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.stream.FileImageOutputStream;
@Service
@Slf4j
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class FileConverterService {
public void convertPptToImages(Double scaling, float compression) throws Exception {
String path = "convert" + scaling + "-compression" + compression + "-";
ClassLoader classLoader = getClass().getClassLoader();
File theDir = new File("convert" + scaling + "-compression" + compression + "/");
theDir.mkdirs();
File file = new File(classLoader.getResource("myppt.ppt").getFile());
Document pdfDocument = new Document();
PdfWriter pdfWriter = PdfWriter.getInstance(pdfDocument, new FileOutputStream(theDir + "/" + path + "converted.pdf"));
PdfPTable table = new PdfPTable(1);
FileInputStream is = new FileInputStream(file);
HSLFSlideShow ppt = new HSLFSlideShow(is);
is.close();
Dimension pgsize = ppt.getPageSize();
Double scaledPageSizeHeightDouble = pgsize.height * scaling;
Double scaledPageSizeWidthDouble = pgsize.width * scaling;
pdfDocument.setPageSize(new Rectangle(scaledPageSizeWidthDouble.floatValue(),scaledPageSizeHeightDouble.floatValue()));
pdfDocument.setMargins(0f,0f,0f,0f);
pdfWriter.open();
pdfDocument.open();
Image slideImage = null;
// convert to images
int idx = 1;
for (HSLFSlide slide : ppt.getSlides()) {
BufferedImage img =
new BufferedImage(pgsize.width * scaling.intValue(), pgsize.height * scaling.intValue(), BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = img.createGraphics();
// these settings make it higher quality
// without it text is very hard to read
graphics.setRenderingHint(
RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
graphics.setRenderingHint(
RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
graphics.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
graphics.setRenderingHint(
RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics.setRenderingHint(
RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
// clear the drawing area
graphics.setPaint(Color.white);
graphics.scale(scaling ,scaling);
graphics.fill(new Rectangle2D.Float(0, 0, scaledPageSizeWidthDouble.floatValue(), scaledPageSizeHeightDouble.floatValue()));
// render
slide.draw(graphics);
// save the output
var pdfCell = new PdfPCell(slideImage,true);
//tried setting these to make borders go away
pdfCell.setPaddingBottom(0f);
pdfCell.setPaddingTop(0f);
pdfCell.setPaddingLeft(0f);
pdfCell.setPaddingRight(0f);
pdfCell.setBorderWidthBottom(0f);
pdfCell.setBorderWidthTop(0f);
pdfCell.setBorderWidthLeft(0f);
pdfCell.setBorderWidthRight(0f);
pdfCell.setBorderWidth(0f);
table.addCell(pdfCell);
idx++;
}
pdfDocument.add(table);
pdfDocument.close();
pdfWriter.close();
}