Image Not visible properly while converting image to PDF file

Hello support,

We have requiremnet to convert image to PDF file. initially we have implemented using aspose.pdf, but image scraching to full page size in generated PDF with source image. We have tried to get dimention from source image and set the same in PDF but we are unable to do the same.

We have attached code in txt
Aspose(Image-to-Pdf).zip (269 Bytes)

file for your reference. can you please suggest approach for the same.

Just to summarised, We want to convert image into PDF, the image should be visible inside the PDF with same height and width. Please note that we want to implement the same using aspose.pdf library only and we wanted to use in application hosted on docker with linux.

Please find current and excepted outout along with working demo for your easy reference.

AsposePdf.zip (4.0 MB)

Aspose_Output(Image_to_PDF).pdf (403.3 KB)

Expected_Output(Image_to_PDF).pdf (147.7 KB)

Please let us know in case of any additional information required.

Thanks in advance.

Hi, @devashish55!

If you want to put image in PDF on Linux based container you need:

  • add Aspose.Drawing.Common library to your project;
  • add Aspose.Pdf.Drawing to your project

In case you want to put an image with the same width and height you need to take some extra actions and the code will look like below:

var path = @"image.jpg";
Aspose.Drawing.Image srcImage = Aspose.Drawing.Image.FromFile(path);

// Read Height of input image
int h = srcImage.Height;

// Read Height of input image
int w = srcImage.Width;

// Initialize a new PDF document
Document doc = new();

// Add an empty page
Page page = doc.Pages.Add();
Image image = new()
{
    File = path
};

// Set page dimensions and margins
page.PageInfo.Height = h;
page.PageInfo.Width = w;
page.PageInfo.Margin.Bottom = 0;
page.PageInfo.Margin.Top = 0;
page.PageInfo.Margin.Right = 0;
page.PageInfo.Margin.Left = 0;
page.Paragraphs.Add(image);

// Save output PDF file
doc.Save("ImageToPDF_HeightWidth.pdf");

In case if you need to put small image and preserve aspect ratio the code will be as in following example:

var path = @"image.jpg";
var bitmap = Aspose.Drawing.Image.FromFile(path);
            
int width = bitmap.Width;
int height = bitmap.Height;

var document = new Aspose.Pdf.Document();

var page = document.Pages.Add();

int scaledWidth = 400;
int scaledHeight = scaledWidth * height / width;

page.AddImage(path, new Aspose.Pdf.Rectangle(10, 10, scaledWidth, scaledHeight));

document.Save("sample_image.pdf");

Please let us know if you have a more complex case, like text or table with images; we will propose another solution.

Also can read how to run your project in docker.

We have reviewed Dockerfile and didn’t find any issues. Do you have any issues with running?

We just wanted to understand that, do we need any additional license to use above libraries in our solution?

Thanks,

You can use Aspose.Pdf.Drawing with the same Aspose.PDF licence.
Sorry, I need more time to clarify the situation with Aspose.Drawing.Common.

@devashish55
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): PDFNET-56682

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

The issues you have found earlier (filed as PDFNET-56682) have been fixed in Aspose.PDF for .NET 24.3.

Hi, @devashish55 !

Since Aspose.PDF is for .NET 24.3, you don’t need an extra reference for Aspose.PDF.Drawing.
You can use BitmapSize property.

Here is an example how to do this:

var document = new Aspose.Pdf.Document();
var page = document.Pages.Add();

var image = new Aspose.Pdf.Image
{
    Margin = { Top = 10, Bottom = 10 },
    File = "sample.jpg",
    FixWidth = page.PageInfo.Width
    - page.PageInfo.Margin.Left
    - page.PageInfo.Margin.Right
};

image.FixHeight = image.FixWidth 
    * image.BitmapSize.Height / image.BitmapSize.Width;
page.Paragraphs.Add(image);

document.Save("sample_image.pdf");