Convert JPEG Image File to PDF using Aspose.Words for Java API

When i trying to convert large image to pdf using Aspose. it is create as thumbnail.

@tarun147,

Which Aspose API you are using?

Please also zip and upload your source file(s), sample code and Aspose API generated PDF file showing the undesired behavior here for testing. We will then investigate the issues on our end and provide you more information.

out.zip (265.1 KB)

@tarun147,

We could not find your image. As requested earlier please zip your file(s) and attach it here. Also,

below are dependencies I add in my project

com.aspose aspose-imaging 18.6 jdk16

Hi Amjad,

u rcvd my files?

@tarun147,

First off, we suggest you please upgrade to the latest version of Aspose.Words for Java i.e. 19.12.

Secondly, please see the above PDF file that we generated from your “in.jpg” on our end by using the following Aspose.Words for Java code:

Java Code to Convert JPG or JPEG Image to PDF

convertImageToPdf("E:\\Temp\\out\\in.jpg", "E:\\Temp\\out\\out.pdf");


public static void convertImageToPdf(String inputFileName, String outputFileName) throws Exception
{
    // Create Aspose.Words.Document and DocumentBuilder.
    // The builder makes it simple to add content to the document.
    Document doc = new Document();
    DocumentBuilder builder = new DocumentBuilder(doc);

    // Load images from the disk using the approriate reader.
    // The file formats that can be loaded depends on the image readers available on the machine.
    ImageInputStream iis = ImageIO.createImageInputStream(new File(inputFileName));
    ImageReader reader = ImageIO.getImageReaders(iis).next();
    reader.setInput(iis, false);

    try
    {
        // Get the number of frames in the image.
        int framesCount = reader.getNumImages(true);

        // Loop through all frames.
        for (int frameIdx = 0; frameIdx < framesCount; frameIdx++)
        {
            // Insert a section break before each new page, in case of a multi-frame image.
            if (frameIdx != 0)
                builder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE);

            // Select active frame.
            BufferedImage image = reader.read(frameIdx);

            // We want the size of the page to be the same as the size of the image.
            // Convert pixels to points to size the page to the actual image size.
            PageSetup ps = builder.getPageSetup();

            ps.setPageWidth(ConvertUtil.pixelToPoint(image.getWidth()));
            ps.setPageHeight(ConvertUtil.pixelToPoint(image.getHeight()));

            // Insert the image into the document and position it at the top left corner of the page.
            builder.insertImage(
                    image,
                    RelativeHorizontalPosition.PAGE,
                    0,
                    RelativeVerticalPosition.PAGE,
                    0,
                    ps.getPageWidth(),
                    ps.getPageHeight(),
                    WrapType.NONE);
        }
    }

    finally {
        if (iis != null) {
            iis.close();
            reader.dispose();
        }
    }

    // Save the document to PDF.
    doc.save(outputFileName);
}

I am exactly using your above code with given aspose version, but I am again getting same output, also 10 mb jpeg file converted in 18 MB pdf file.

I want any large file converted in A4 size

Plz suggest

addding 1 more user @sureshgenpact

@tarun147,

Please ZIP the JPEG Image file (you are getting this problem with) and the Aspose.Words for Java generated PDF file showing the undesired behavior, upload the .zip file to Dropbox or any other file hosting service and provide the download link here for testing. We will then investigate the issue on our end and provide you with more information. Thanks for your cooperation.

@awais.hafeez , I upload my output file

https://files.fm/u/bvwc6dny

@tarun147,

The shared PDF file was actually produced by using an old version of Aspose.Words for Java i.e. 18.6 on your end. Please upgrade to the latest version i.e. 19.12 and see how it goes on your end. Hope, this helps.

In case the problem still remains, please also ZIP the JPEG Image file (you are getting this problem with), upload the .zip file to Dropbox or any other file hosting service and provide the download link to image file here for further testing. Thanks for your cooperation.

I update my pom file, may be it still using ols version
let me create fresh project and test.

version.JPG (15.1 KB)

I am using same 19.12 version, still facing same issue.

@tarun147,

We see the following small sized image in your out.pdf file:

But, can you please also ZIP the original JPEG Image file here for further testing? We will test/convert that original JPG Image file to PDF on our end and provide you more information. Thanks for your cooperation.

original file upload on above location

also plz suggest , how to decrease output pdf file size.

@tarun147,

We tested the scenario and have managed to reproduce the same problems on our end. For the sake of correction, we have logged this problem in our issue tracking system. The ID of this issue is WORDSNET-19778. We will further look into the details of this problem and will keep you updated on the status of linked issue. We apologize for your inconvenience.

@tarun147,

Regarding WORDSNET-19778, we have completed the work on your issue and most likely will close this issue with “Not a Bug” status. Please check below the analysis details:

  1. To decrease the output PDF file size you may want to change JpegQuality (by default it is 100%)

Sample code:

PdfSaveOptions opt = new PdfSaveOptions();
opt.setJpegQuality(80);
  1. It is expected behavior. MS Word allows to insert DrawingML shapes (images) with height about 3467,76 cm (98 299 points) and maximum width is 1584 points. So, there is no way to insert images that bigger. MS Word validates it’s size. You have to check this and validate inside your code before inserting the image in document. Also, in MS Word there is no way to set page size bigger then 1584/1584 points.

If we can help you with anything else, please feel free to ask.