Determine the height of HtmlFragment

How to check the height and width of HtmlFragment? Return 0 via getRectangle.

        String htmlStr = "";
        htmlStr = "<span style=\"font-family:STSong,FangSong,SimSun;line-height:1.8;font-size:14px;\">";
        htmlStr += "<style type='text/css'>longFill,.longFill{width:210px;}.fill{width:51px;}.brack, brack, tab{width:40px;}</style>";
        htmlStr += "ABCDEF";
        htmlStr += "</span>";
        
        HtmlFragment html = new HtmlFragment(htmlStr);
        box.getParagraphs().add(html);

        html.getRectangle(); //return x=0.0,y=0.0,w=0.0,h=0.0

@gaohf

There is no particular method to get the height and width of the text at the time of PDF generation. You can however measure the width of a string by using the MeasureString() method. Can you please share the detail of complete scenario that why you need to get the height of the HtmlFragment? Please share the complete code snippet along with complete requirements detail so that we can better understand the situation and provide the feedback accordingly.

Thank you very much.

  1. We want to use HtmlFragment to generate PDF files.

  2. During or after file generation, it is expected to obtain the coordinates of each HtmlFragment in the PDF file for positioning.

@gaohf

Once a PDF file is generated, the added text or HTML string would not be recognized as HTML String. You can extract it as a plain text using TextFragmentAbsorber and obtain its position or coordinates like below:

Aspose.Pdf.Document doc = new Aspose.Pdf.Document(dataDir + "inbput.pdf");

TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber("searchString");
doc.Pages.Accept(textFragmentAbsorber);
TextFragmentCollection textFragmentCollection = textFragmentAbsorber.TextFragments;

// Loop through the fragments
foreach (TextFragment textFragment in textFragmentCollection)
{
 var rect = textFragment.Rectangle;
}

Can you tell me which kernel is used to render HTML?

After the PDF file is generated, I want to render the page in my browser with the same HTML string, expecting the same size and effect.

Are PDF pixels and WEB pixels different in size?

There’s a line wrap. I really shouldn’t break a line here. Look at the red notes.QQ图片20220308161930.png (147.6 KB)

@gaohf

Can you please elaborate a bit more about your this question?

Yes, PDF pixels are different as they are screen dependent. The PDF is rendered according to the resolution of the screen where you are viewing it.

Please share some code snippet and sample files to show how you are generating these results so that we can further proceed to assist you accordingly.

The same HTML code, the div in HTML is the same size, but it looks different in a browser preview than it does in a PDF, and the size is different?

Can text be hidden? I don’t want it to show up.

@gaohf

Can you please share the HTML string for our reference so that we can test the scenario in our environment and address it accordingly?