HEIC to PDF - With Same dimensions and Resolutions - C#.Net

Hi Team, Please find the details below.

Requirement : We want to convert HEIC image to PDF along with Same Dimensions and Resolutions.
Please find the sample code and HEIC image metadata.

private static void ConvertHeicToPdfUsingAspose_Raster(string heicPath, string outputPdfPath)
        {
            try
            {
                HEICImage.Register(); // Register HEIC support

                using (var image = Aspose.Imaging.Image.Load(heicPath))
                {
                    if (!(image is Aspose.Imaging.RasterImage rasterImage))
                    {
                        Console.WriteLine("Unsupported image format.");
                        return;
                    }

                    // Get DPI and dimensions
                    float dpiX = (float)(rasterImage.HorizontalResolution > 0 ? rasterImage.HorizontalResolution : 96f);
                    float dpiY = (float)(rasterImage.VerticalResolution > 0 ? rasterImage.VerticalResolution : 96f);

                    float widthPoints = image.Width * 72f / dpiX;
                    float heightPoints = image.Height * 72f / dpiY;

                    // Save image to stream in a supported format (e.g., PNG)
                    using (MemoryStream ms = new MemoryStream())
                    {
                        image.Save(ms, new PngOptions());
                        ms.Position = 0;

                        // Create PDF and set page size to match image resolution
                        var pdfDocument = new Aspose.Pdf.Document();
                        var page = pdfDocument.Pages.Add();
                        page.SetPageSize(widthPoints, heightPoints);

                        var pdfImage = new Aspose.Pdf.Image
                        {
                            ImageStream = ms,
                            FixWidth = widthPoints,
                            FixHeight = heightPoints,
                            HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Center,
                            VerticalAlignment = Aspose.Pdf.VerticalAlignment.Center
                        };

                        page.Paragraphs.Add(pdfImage);
                        pdfDocument.Save(outputPdfPath);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

HEIC File Meta data :
ExifTool Version Number : 13.32
File Name : sample-heic.heic
File Size : 539 kB
Zone Identifier : Exists
File Modification Date/Time : 2025:08:18 03:33:16-04:00
File Access Date/Time : 2025:08:20 04:46:48-04:00
File Creation Date/Time : 2025:08:18 03:37:18-04:00
File Permissions : -rw-rw-rw-
File Type : HEIC
File Type Extension : heic
MIME Type : image/heic
Major Brand : High Efficiency Image Format HEVC still image (.HEIC)
Minor Version : 0.0.0
Compatible Brands : mif1, heic
Handler Type : Picture
Primary Item Reference : 1
Exif Byte Order : Big-endian (Motorola, MM)
Orientation : Horizontal (normal)
X Resolution : 72
Y Resolution : 72
Resolution Unit : inches
Y Cb Cr Positioning : Centered
XMP Toolkit : Image::ExifTool 12.40
HEVC Configuration Version : 1
General Profile Space : Conforming
General Tier Flag : Main Tier
General Profile IDC : Format Range Extensions
Gen Profile Compatibility Flags : Format Range Extensions
Constraint Indicator Flags : 0 0 0 0 0 0
General Level IDC : 150 (level 5.0)
Min Spatial Segmentation IDC : 0
Parallelism Type : 0
Chroma Format : 4:2:0
Bit Depth Luma : 8
Bit Depth Chroma : 8
Average Frame Rate : 0
Constant Frame Rate : Unknown
Num Temporal Layers : 1
Temporal ID Nested : Yes
Image Width : 2400
Image Height : 1600
Image Spatial Extent : 2400x1600
Media Data Size : 538619
Media Data Offset : 471
Image Size : 2400x1600
Megapixels : 3.8

Sample File :
sample-heic.7z (524.6 KB)

Question :
As per meta data X & Y Resolution is “72” using code we are unable to get this value. When open image and verify the file information / properties we can able to see the “72 dpi”.

rasterImage.HorizontalResolution
rasterImage.VerticalResolution
In these lines of code I am getting 96 instead of 72.
Could you please help me here. Thank you.

Please find the Environment details:

  • .Net framework 4.8
  • Aspose.Imaging (Version : 25.8.0.0)
  • Aspose.Imaging.HEIC.Adapter (Version : 25.6.0.0)

@SwroopXerox

To ensure that you can retrieve the correct DPI values when converting HEIC images to PDF using Aspose, please follow these steps:

  1. Cause: The issue may arise from the way DPI values are retrieved. If rasterImage.HorizontalResolution and rasterImage.VerticalResolution are not returning the expected results, it could be due to the HEIC image lacking properly embedded DPI metadata, or the library may not be reading this information accurately.

  2. Solution: To address this, you can implement a fallback mechanism to ensure that the DPI is set correctly. Modify your code to explicitly set dpiX and dpiY to 72 if the retrieved values are not greater than 0. Here’s a modified code snippet:

    float dpiX = rasterImage.HorizontalResolution > 0 ? rasterImage.HorizontalResolution : 72f;
    float dpiY = rasterImage.VerticalResolution > 0 ? rasterImage.VerticalResolution : 72f;
    
    // Ensure DPI is set to 72 if not valid
    dpiX = dpiX <= 0 ? 72f : dpiX;
    dpiY = dpiY <= 0 ? 72f : dpiY;
    
  3. Next Steps: If the issue persists after making these adjustments, test with different HEIC files to determine if the problem is specific to a particular file. Additionally, ensure that you are using the latest versions of Aspose.Imaging and the HEIC adapter, as updates may include important fixes related to metadata handling.

Please implement these changes and let me know if you need further assistance!

Yes you are correct but, as per requirement we are getting HEIC images from different sources. We have to convert to PDF with same dimensions and resolutions.
here I am taking 72 for an example. Each HEIC image have it’s own dpi right?

As I mentioned / attached heic image, if we convert to PDF the expected page size of pdf document is ( 2400/72 X 1600/72) : 33.34 X 22.23 inches.

Could you suggest any generic solution? Thank you.

Hi @SwroopXerox

At the moment Openize.HEIC doesn’t return resolution data.
As a result the default resolution is set when creating RasterImage. And that is 96.
We have planned it for the future update (not 25.8).

By design - definitely yes.
But in fact I haven’t seen any heic files with 96 dpi. It’s either 72 (iphone and android) or not defined (as there is no exif data).

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): IMAGINGNET-7788

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.

Hi @Nikita_Romanov thank you for your reply. But my testing team using 96 and 72 dpi file. They are are download from online. The code not reading X and Y Resolutions.

@SwroopXerox
Can you please provide a file with 96 dpi that we can use in internal testing?

Sure. Please find the sample HEIC files.
HD-1080x1920_Sample.7z (3.5 MB)

@SwroopXerox Thank you!