如何用Java語言將圖像轉換爲PDF?Spire.PDF for Java輕鬆搞定!

對於開發人員,在平常工做中常常也會處理許多文檔格式,將圖像轉換爲PDF也是常有的事。那麼,在Java語言開發中,如何將圖像轉換爲PDF呢?spa

Spire.PDF for Java點擊下載)支持將多種圖像格式(例如BMP,JPEG,GIF,PNG,TIFF和ICO)轉換爲PDF。如下示例將展現如何使用Spire.PDF for Java將圖像轉換爲PDF。blog

    導入com.spire.pdf.PdfDocument;    
   導入com.spire.pdf.PdfPageBase;    
    導入com.spire.pdf.graphics.PdfImage;    
        
    公共課程ImageToPDF {    
        公共靜態void main(String [] args){    
            //建立一個PdfDocument實例    
           PdfDocument pdf =新的PdfDocument();    
            //添加頁面    
           PdfPageBase頁面= pdf.getPages()。add();    
        
            //加載圖片    
            PdfImage圖片= PdfImage.fromFile(「 Hydrangeas.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,new Rectangle2D.Double(0,0,fitWidth,fitHeight));    
       
            //保存結果文件    
          pdf.saveToFile(「 ConvertImageToPDF.pdf」);    
       }    
    }

如何用Java語言將圖像轉換爲PDF?Spire.PDF for Java輕鬆搞定!

相關文章
相關標籤/搜索