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.
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.
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.
@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.