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

Free Support Forum - aspose.com

How to wrap images in aspose.html when converting html to pdf for .NET

Hii

I want to wrap my images in the pdf file. could you please help me to wrap images like below code by using aspose.html.???

Document doc = new Document(@"C:\Temp\in.html");
NodeCollection shapes = doc.GetChildNodes(NodeType.Shape, true);
foreach (Shape s in shapes)
{
s.WrapType = WrapType.Square;
s.Top += s.Height + 10;
}
doc.Save(@"C:\Temp\out.docx");

How to do this with aspose.html??

@nethmi

Can you please share the sample HTML file in .zip format along with an expected output PDF? We will test the scenario in our environment and address it accordingly.

HTML HTML.zip (2.9 KB)

Current output current.PNG (138.9 KB)

Expected outcomeexpected.PNG (128.0 KB)

@nethmi

It looks like you shared the HTML without resources e.g. images and CSS files. Can you please share the complete package with us so that we can render HTML in the browser in its actual form and test the scenario accordingly?

images are there with the links. I can’t share exact images because of the privacy reasons. I Send this html to aspose.word team also. They gave me solutions for my problems with this HTML.

src=“https://localhost:48300/content/api/Contents/fa197b43-5b2a-4c53-bddd
I used images like this. But I can’t share because of the privacy. Could you Please use your own sample image to test?

@nethmi

We replaced the images with our sample image and used below code snippet to generate a PDF file. Please check it and let us know in case it still does not resolve your issue:

string file = dataDir + "CurrentHTMLCode.html";
MemoryStream ms = new MemoryStream(File.ReadAllBytes(file));
Aspose.Html.Saving.PdfSaveOptions options = new Html.Saving.PdfSaveOptions()
{
 JpegQuality = 100,
};
options.PageSetup.PageLayoutOptions = Html.Rendering.PageLayoutOptions.FitToWidestContentWidth;
//options.PageSetup.AnyPage.Margin = new Html.Drawing.Margin(30);
//options.PageSetup.AdjustToWidestPage = true;
//options.BackgroundColor = System.Drawing.Color.White;
using (var document = new Aspose.Html.HTMLDocument(ms, dataDir))
{
 Aspose.Html.Converters.Converter.ConvertHTML(document, options, dataDir + "ouptut.pdf");
}

Input/Output Files
inputoutputfiles.zip (200.6 KB)

No No. this is not my expected outcome. I want to wrap my images with the text

like this imageWrapping.png (57.7 KB)

@nethmi

We are trying to produce the output similar to your expected one and will get back to you soon. Please spare us little time.

1 Like

@nethmi

We were able to achieve the expected output PDF using the code below:

using (var document = new Aspose.Html.HTMLDocument(File.ReadAllText(dataDir + "CurrentHTMLCode.html"), "."))
{
 // Find the paragraph element to set a style attribute
 var images = document.GetElementsByTagName("img").Skip(1);

 foreach(var img in images)
 {
  // Set the style attribute
  img.SetAttribute("style", "float: left;");
 }

 // Save the HTML document to a file 
 document.Save(Path.Combine(dataDir, "edit-inline-css.html"));

 string file = dataDir + "edit-inline-css.html";
 MemoryStream ms = new MemoryStream(File.ReadAllBytes(file));
 Aspose.Html.Saving.PdfSaveOptions options = new Html.Saving.PdfSaveOptions()
 {
  JpegQuality = 100,
 };
 options.PageSetup.PageLayoutOptions = Html.Rendering.PageLayoutOptions.FitToWidestContentWidth;
 using (var htmldocument = new Aspose.Html.HTMLDocument(ms, dataDir))
 {
  Aspose.Html.Converters.Converter.ConvertHTML(htmldocument, options, dataDir + "ouptut.pdf");
 }
}

ouptut.pdf (150.6 KB)

1 Like

I’ll check this and inform you

@nethmi

Please take your time to test the scenario and feel free to reach us in case you face any issues.