前面我们介绍了如何使用 Spire.PDF for Java 将PDF保存为图片。该文将介绍如何在Java应用程序中将图片(Jpg, PNG, Bmp等)保存为PDF。
import com.spire.pdf.*;
import com.spire.pdf.graphics.PdfImage;
public class imageToPDF {
    public static void main(String[] args) throws Exception {
        //创建Pdf 文档
        PdfDocument pdf = new PdfDocument();
        //创建一个新的页面
        PdfPageBase page = pdf.getPages().add();
        //加载图片
        PdfImage image = PdfImage.fromFile("logo.jpg");
        double widthFitRate = image.getPhysicalDimension().getWidth() / page.getCanvas().getClientSize().getWidth();
        double heightFitRate = image.getPhysicalDimension().getHeight() / page.getCanvas().getClientSize().getHeight();
        double fitRate = Math.max(widthFitRate, heightFitRate);
        //图片大小
        double fitWidth = image.getPhysicalDimension().getWidth() / fitRate;
        double fitHeight = image.getPhysicalDimension().getHeight() / fitRate;
        //绘制图片
        page.getCanvas().drawImage(image, 0, 30, fitWidth, fitHeight);
        pdf.saveToFile("output/ToPDF.pdf");
        pdf.close();
    }
}效果图:

 



 
					



