Image to pdf

Hi ,

I got the below code from aspose site to convert image to PDF.
But after conversion the image is showing at the top left position of the pdf document.
Ho can i make the image fit to the pdf document.

Please see the code which i am using.

public static void ConvertImageToPdf(string inputFileName, string outputFileName)
{
    Aspose.Words.Document doc = new Aspose.Words.Document();
    DocumentBuilder builder = new DocumentBuilder(doc);
 
    // Read the image from file, ensure it is disposed.
    using (System.Drawing.Image image = System.Drawing.Image.FromFile(inputFileName))
    {
        FrameDimension dimension = new FrameDimension(image.FrameDimensionsList[0]);
        //int framesCount = image.GetFrameCount(dimension);
 
        int framesCount = 1;
 
        try
        {
            framesCount = image.GetFrameCount(FrameDimension.Page);
        }
        catch
        {
            // do nothing.
        }
 
        // 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 TIFF.
            if (frameIdx != 0)
                builder.InsertBreak(BreakType.SectionBreakNewPage);
 
            // Select active frame.
            image.SelectActiveFrame(FrameDimension.Page, 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.PageSetup;
            ps.PageWidth = ConvertUtil.PixelToPoint(image.Width, image.HorizontalResolution);
            ps.PageHeight = ConvertUtil.PixelToPoint(image.Height, image.VerticalResolution);
 
            // Insert the image into the document and position it at the top left corner of the page.
            //builder.InsertImage(image, ps.PageWidth, ps.PageHeight);
 
            builder.InsertImage(image,
                RelativeHorizontalPosition.Page,
                0,
                RelativeVerticalPosition.Page,
                0,
                ps.PageWidth,
                ps.PageHeight,
                WrapType.None);
        }
    }
 
    // Save the document to PDF.
    doc.Save(outputFileName);
} 

Regards
Anish

Hi,

I downloaded the latest version of aspose.word.dll v13.5

But now the generated tif from JPG is showing as blank.

Can you please help me to correct this.

Please find the attached file. Which is having the jpg image and converted tif image

Regards
Anish

Hi Anish,

Thanks for your inquiry. The problem occurs because the size of image is bigger than max allowed size of MS Word page. Please use the following code snippet to achieve your requirements.

public static void ConvertImageToPdf(string inputFileName, string outputFileName)
{
    // 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);
    // Read the image from file, ensure it is disposed.
    using (Image image = Image.FromFile(inputFileName))
    {
        // Get the number of frames in the image.
        int framesCount = image.GetFrameCount(FrameDimension.Page);
        // 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 TIFF.
            if (frameIdx != 0)
                builder.InsertBreak(BreakType.SectionBreakNewPage);
            // Select active frame.
            image.SelectActiveFrame(FrameDimension.Page, frameIdx);
            // Max page size
            const double maxPageHeight = 1584;
            const double maxPageWidth = 1584;
            double currentImageHeight = ConvertUtil.PixelToPoint(image.Height, image.VerticalResolution);
            double currentImageWidth = ConvertUtil.PixelToPoint(image.Width, image.HorizontalResolution);
            if (currentImageWidth >= maxPageWidth || currentImageHeight >= maxPageHeight)
            {
                // Get max image size.
                CalculateImageSize(image, maxPageHeight, maxPageWidth, out currentImageHeight, out currentImageWidth);
            }
            // 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.PageSetup;
            ps.PageWidth = currentImageWidth;
            ps.PageHeight = currentImageHeight;
            // 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.PageWidth,
                ps.PageHeight,
                WrapType.None);
        }
    }
    // Save the document to PDF.
    doc.Save(outputFileName);
}

/// Calculates size of Image
///
/// Original image
/// Height of container where image should be inserted.
/// Width of container where image should be inserted.
/// Height of the image
/// Width of the image
public static void CalculateImageSize(Image img, double containerHeight, double containerWidth, out double targetHeight, out double targetWidth)
{
    // Calculate width and height
    targetHeight = containerHeight;
    targetWidth = containerWidth;
    // Get size of an image
    double imgHeight = ConvertUtil.PixelToPoint(img.Height);
    double imgWidth = ConvertUtil.PixelToPoint(img.Width);
    if (imgHeight < targetHeight && imgWidth < targetWidth)
    {
        targetHeight = imgHeight;
        targetWidth = imgWidth;
    }
    else
    {
        // Calculate size of an image in the document
        double ratioWidth = imgWidth / targetWidth;
        double ratioHeight = imgHeight / targetHeight;
        if (ratioWidth > ratioHeight)
            targetHeight = (targetHeight * (ratioHeight / ratioWidth));
        else
            targetWidth = (targetWidth * (ratioWidth / ratioHeight));
    }
}

Hi Tahir
Still the pdf document generate from JPG is showing blank only.
I include all the above mentioned code in my application.

Can you please convert the JPG file which is already attached in my previous post and try to convert to PDF.

Regards
Anish

Hi Anish,

Thanks for your inquiry. Please find the output Pdf file in attachment. Please share the following detail for investigation purpose.

  • OS (Windows Version or Linux Version)
  • Architecture (32 / 64 bit)
  • .NET Framework version
ConvertImageToPdf(MyDir + "in.jpg", MyDir + "out.pdf");

Hi Tahir,
Please find the details.

OS - Windows XP
Architecture - 32 bit
.NET Framework version - 4.0

Regards
Anish

Hi Anish,

Thanks for sharing the detail. I have tested the scenario at Windows XP (32 bit), .NET Framework 4.0 and have not found the shared issue.

Perhaps, you are using an older version of Aspose.Words; as with Aspose.Words v13.5.0, I am unable to reproduce this problem on my side. I would suggest you please upgrade to the latest version of Aspose.Words i.e. v13.5.0 and let us know how it goes on your side. I hope, this will help. I have attached the output file generated at Windows XP with this post for your kind reference.

Hi Tahir,

I am using the latest version of Aspose.Words.
Please see the attached screenshot.
But still the generated PDF is blank. But it having some size.
Do i need to do something else?

Regards
Anish

Hi Anish,

Thanks for your inquiry. Are you using the same image file which you shared in this post? If you are using some different image, please share it here along with output Pdf file for testing purposes.

It would be great if you please test this scenario by creating a separate test application with the code shared here:
https://forum.aspose.com/t/53497

Hi Tahir,
Thanks for your help.

I am able to convert the file.

I didn’t added a a line of code . So it came as blank

Regards
Anish

Hi Anish,

Thanks for your feedback. It is nice to hear from you that your problem has been solved. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.