HanG321 Blog
Be Shine, Be Smile, Be Wild
  • Home
  • Blog
    • 好文閱讀 readings
    • 生活記事 diary
    • 時事評論 commentary
    • 科技資訊 technology
    • 電腦編程 programming
    • 金融財經 finance
    • 音樂電影 music/movie
  • About

[Java] PDF to JPG

July 23, 2007|Java, JPG, PDF|電腦編程
Home » Blog » 電腦編程 » [Java] PDF to JPG

http://forum.java.sun.com/thread.jspa?threadID=359395&start=30&tstart=0

Here is a quick test sample that I ran with for PDF to JPG conversion.
I only had to convert the 1st page, so if you wanted multi-pages you will have to adapt the above example.
I use the ImageIO and jpedal jars.

Quick overview.
give the decoder the filename of the pdf to open
Grab the 1st page, and resize it to a thumbnail version
save the image.

Java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import org.jpedal.*;
 
import java.io.*;
 
import javax.imageio.*;
import javax.imageio.stream.*;
 
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
 
import java.util.*;
 
public class PdfToJpgTest
{
 
    public static void PDFToJpg(String filepath)
    {
        try
        {
            PdfDecoder decoder = new PdfDecoder();
            decoder.openPdfFile(filepath);
            if (decoder.isFileViewable())
            {
                //just grab the first page, and render it as an image.
                BufferedImage thumbnailImage = getThumbnailImage(decoder.getPageAsImage(1));
                saveImage(thumbnailImage,"1.jpg");
            }
        }
        catch (Exception err)
        {
            err.printStackTrace();
        }
    }
 
    public static void saveImage(Image image, String fileName)
    {
        File fileToSave = new File(fileName);
        try
        {
            ImageIO.write((RenderedImage)image,"jpg", fileToSave);
        }
        catch (Exception err)
        {
            err.printStackTrace();
        }
    }
 
    private static BufferedImage getThumbnailImage(BufferedImage image)
    {
        BufferedImage resizedImage = null;
        Graphics g = image.getGraphics();
        int resizeWidth = 0;
        int resizeHeight = 0;
        resizeHeight = 256;
        resizeWidth = (image.getWidth() * resizeHeight) / image.getHeight();
        resizedImage = new BufferedImage(resizeWidth, resizeHeight, BufferedImage.TYPE_INT_RGB);
        resizedImage.createGraphics().drawImage(image, 0, 0, resizeWidth, resizeHeight, null);
        return resizedImage;
    }
 
    public static void main(String args[])
    {
        System.out.println("Staring PDF to JPG test");
        PDFToJpg("Winter 2006.pdf");
        System.out.println("Ending PDF to JPG test");
 
    }
}

 

July 23, 2007 user

Related Posts

  • JAVA_HOME of OpenJDK 1.7 on CentOS 7
    JAVA_HOME of OpenJDK 1.7 on CentOS 7June 7, 2015
  • Install Jenkins on Tomcat 7 (DSM)
    Install Jenkins on Tomcat 7 (DSM)August 18, 2014
  • AsyncWeb & load test
    AsyncWeb & load testJune 2, 2009

Popular Posts

  • Install XPEnology (DSM) 5.1 on ESXi 6 (HP MicroServer Gen 8)
    Install XPEnology (DSM) 5.1 on ESXi 6 (HP MicroServer Gen 8)June 8, 2015
  • assembly
    assemblyFebruary 11, 2006
  • 呢幾日個blogger 有問題….
    呢幾日個blogger 有問題….October 28, 2004
← 不想獨自快樂
書展 x transformer →

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.




最新文章 Recent Posts

  • Serverless Cloud Run Development: Challenge Lab
  • ionic 4: capacitor & pwa
  • Open file limit of tomcat and mysql in Linux server
  • avoid subquery in mysql update statement
  • hacktoberfest

最新留言 Recent Comments

  • admin on LikeCoin
  • Edmond on LikeCoin
  • John Smith on Install macOS High Sierra on VMware
  • admin on Install macOS High Sierra on VMware
  • nana on Install macOS High Sierra on VMware




熱門文章 Popular Posts

  • Install XPEnology (DSM) 5.1 on ESXi 6 (HP MicroServer Gen 8)
    Install XPEnology (DSM) 5.1 on ESXi 6 (HP MicroServer Gen 8) June 8, 2015
  • 呢幾日個blogger 有問題….
    呢幾日個blogger 有問題…. October 28, 2004
  • assembly
    assembly February 11, 2006
  • 新工作
    新工作 January 6, 2009
  • 嫁人要嫁工程師
    嫁人要嫁工程師 April 27, 2006

標籤雲 Tag Cloud

CentOS Character chroot Cluster crash cryptography DD-WRT debug Domino DSM Dual Core DWA email ESXi GCP git google HylaFax IE Java Javascript JRE LikeCoin Linux log LotusScript mint MX MySQL nginx PKI PowerShell Qwiklabs srt telent VMware vpn vSphere WinXP wordpress XPEnology 專欄 網絡資訊 選股 風帆

日曆 Calendar

July 2007
M T W T F S S
  « Jun   Aug »  
 1
2345678
9101112131415
16171819202122
23242526272829
3031  

Follow Me

Follow Us on RSSFollow Us on TwitterFollow Us on YouTube

文章存檔 Archives

Copyright © 2004-2021 hang321.net. All Rights Reserved