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

Free Support Forum - aspose.com

Convert multiple images to one word on the same page

Hi,
I need convert multiple images to one word on the same page, if first image on the first page and first image’s height too big, second image can on second page.

but code can not do that, please help me, thank you!

the code ↓

    private Document convertImagesToDocument(string[] base64Images)
    {
        var document = new Document();
        var builder = new DocumentBuilder(document);
        builder.PageSetup.PaperSize = PaperSize.A4;

        for (var count = 0; count < base64Images.Length; count++)
        {
            var bytes = Convert.FromBase64String(base64Images[count]);
            var ms = new MemoryStream(bytes);

            var image = Image.FromStream(ms);

            builder.InsertImage(image,
            RelativeHorizontalPosition.Page,
            0,
            RelativeVerticalPosition.Page,
            0,
            ConvertUtil.PixelToPoint(image.Width, image.HorizontalResolution),
            ConvertUtil.PixelToPoint(image.Height, image.VerticalResolution),
            WrapType.None);

            if (count < base64Images.Length - 1)
            {
                builder.InsertBreak(BreakType.SectionBreakNewPage);
            }
        }

        return document;
    }

Thanks

@thisway

Could you please ZIP and attach your input image and output document here for testing? We will investigate the issue and provide you more information on it.

@tahir.manzoor

And I edit code.

Document2.zip (39.6 KB)

@thisway

You are inserting section break (new page) after each image. We suggest you please insert paragraph break instead of section break to get the desired output. Hope this helps you.

@tahir.manzoor

Success! And it need change more code.

Result.pdf (71.0 KB)

builder.PageSetup.TopMargin = 13.5;
builder.PageSetup.BottomMargin = 13.5;
builder.PageSetup.LeftMargin = 0;
builder.PageSetup.RightMargin = 0;

var shape = builder.InsertImage(image,
ConvertUtil.PixelToPoint(image.Width, image.HorizontalResolution),
ConvertUtil.PixelToPoint(image.Height, image.VerticalResolution));

shape.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;
shape.RelativeVerticalPosition = RelativeVerticalPosition.Paragraph;
shape.WrapType = WrapType.TopBottom;
shape.AllowOverlap = false;
shape.HorizontalAlignment = HorizontalAlignment.Center;

if (count < base64Images.Length - 1)
{
builder.InsertBreak(BreakType.ParagraphBreak);
}

Thank you very much for your help.

@thisway

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.