We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Big size images in MHT are rendered outside the page in output PDF using C#

Hi,
when we convert the attached mht file to pdf, the pdf result is not identical to the original file.

here is the code :

                Document doc = new Document("c:\\temp\\testeml\\test1.mht", lo);

                DocumentBuilder dBuilder = new DocumentBuilder(doc);
                // définition des marges
                dBuilder.PageSetup.LeftMargin = ConvertUtil.MillimeterToPoint(10);
                dBuilder.PageSetup.RightMargin = ConvertUtil.MillimeterToPoint(10);
                dBuilder.PageSetup.TopMargin = ConvertUtil.MillimeterToPoint(10);
                dBuilder.PageSetup.BottomMargin = ConvertUtil.MillimeterToPoint(10);
                PdfSaveOptions pdfOptions = new PdfSaveOptions();
                pdfOptions.PageIndex = 0;
                pdfOptions.PageCount = Int32.MaxValue;

                pdfOptions.ImageCompression = PdfImageCompression.Jpeg;
                pdfOptions.JpegQuality = 75;
                pdfOptions.TextCompression = PdfTextCompression.Flate;

                //save the document to PDF file
                doc.Save("c:\\temp\\testeml\\test1.pdf", pdfOptions);

testeml.zip (217.0 KB)

thanks in advance for your reply

@tparassin

Please use the following code example to resize the big size images according to page size. Hope this helps you.

Document document = new Document(MyDir + "test1.mht");
DocumentBuilder dBuilder = new DocumentBuilder(document);
// définition des marges
dBuilder.PageSetup.LeftMargin = ConvertUtil.MillimeterToPoint(10);
dBuilder.PageSetup.RightMargin = ConvertUtil.MillimeterToPoint(10);
dBuilder.PageSetup.TopMargin = ConvertUtil.MillimeterToPoint(10);
dBuilder.PageSetup.BottomMargin = ConvertUtil.MillimeterToPoint(10);
PdfSaveOptions pdfOptions = new PdfSaveOptions();
pdfOptions.PageIndex = 0;
pdfOptions.PageCount = Int32.MaxValue;

pdfOptions.ImageCompression = PdfImageCompression.Jpeg;
pdfOptions.JpegQuality = 75;
pdfOptions.TextCompression = PdfTextCompression.Flate;

foreach (Shape shape in document.GetChildNodes(NodeType.Shape, true))
{
    ResizebigImage(shape);
}
document.Save(MyDir + "20.1.pdf", pdfOptions);

public static void ResizebigImage(Shape image)
{
    // Return if this shape is not an image.
    if (!image.HasImage)
        return;

    // Calculate the free space based on an inline or floating image. If inline we must take the page margins into account.
    PageSetup ps = image.ParentParagraph.ParentSection.PageSetup;
    double freePageWidth = image.IsInline ? ps.PageWidth - ps.LeftMargin - ps.RightMargin : ps.PageWidth;
    double freePageHeight = image.IsInline ? ps.PageHeight - ps.TopMargin - ps.BottomMargin : ps.PageHeight;


    ImageSize size = image.ImageData.ImageSize;
    Boolean exceedsMaxPageSize = size.WidthPoints > freePageWidth || size.HeightPoints > freePageHeight
        || image.Width > freePageWidth || image.Height > freePageHeight;

    if (exceedsMaxPageSize)
    {
        // Set the new size.
        image.AspectRatioLocked = true;
        image.Width = ps.PageWidth - ps.LeftMargin - ps.RightMargin;
    }
}

fine !
thank you