CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    May 2013
    Posts
    83

    Convert a Particular PDF Page or All PDF Pages to PNG Image in Java

    This technical tip shows how to convert PDF pages to PNG Image using Aspose.Pdf for Java. Users can choose to convert a particular PDF page to PNG image or convert all PDF pages to PNG Images. In order to convert all page of PDF file to PNG format, user need to iterate through individual page and convert it to PNG format. The given code snippet shows user how to traverse through all the pages of PDF file and convert it to PNG image. The PngDevice class allows user to convert PDF pages to PNG images. This class provides a method named process(..) which allows user to convert a particular page of the PDF file to PNG image. User first need to create an object of Document class, so you could get the particular page which user want to convert to PNG. After that, user need to call the process(..) method to convert the page to PNG image.

    Convert particular PDF page to PNG Image

    [Java]

    Code:
    //open document
    com.aspose.pdf.Document pdfDocument = new com.aspose.pdf.Document("input.pdf");
    // create stream object to save the output image
    java.io.OutputStream imageStream = new java.io.FileOutputStream("Converted_Image.png");
    
    //create Resolution object
    com.aspose.pdf.Resolution resolution = new com.aspose.pdf.Resolution(300);
    //create PngDevice object with particular resolution
    com.aspose.pdf.PngDevice pngDevice = new com.aspose.pdf.PngDevice(resolution);
    //convert a particular page and save the image to stream
    pngDevice.process(pdfDocument.getPages().get_Item(1), imageStream);
    
    //close the stream
    imageStream.close();

    Convert all PDF pages to PNG Images


    [Java]
    Code:
    //open document
    com.aspose.pdf.Document pdfDocument = new com.aspose.pdf.Document("input.pdf");
    
    // loop through all the pages of PDF file
    for (int pageCount = 1; pageCount <= pdfDocument.getPages().size(); pageCount++)
    {
        // create stream object to save the output image
        java.io.OutputStream imageStream = new java.io.FileOutputStream("Converted_Image" + pageCount + ".png");
    
         //create Resolution object
        com.aspose.pdf.Resolution resolution = new com.aspose.pdf.Resolution(300);
        //create PngDevice object with particular resolution
        com.aspose.pdf.PngDevice pngDevice = new com.aspose.pdf.PngDevice(resolution);
        //convert a particular page and save the image to stream
        pngDevice.process(pdfDocument.getPages().get_Item(pageCount), imageStream);
    
        //close the stream
        imageStream.close();
    }

  2. #2
    Join Date
    Jun 2013
    Posts
    2

    Re: Convert a Particular PDF Page or All PDF Pages to PNG Image in Java


Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured