reza.cse08
reza.cse08

Reputation: 6178

How to create a circle with center text in ItextSharp

I am try to create a OMR sheet. For that need a design like below. I made in html & css.

enter image description here

but in iTextSharp it does not work as html in browser.

<td style="border-width:0 2px 0 4px; border-style:solid; border-color:white; padding:0 20px;">
     <div style="height:17px; width:25px; padding:2px; float:left;">1</div>
     <div style="height:17px; width:18px; padding:2px; margin:5px 2px; float:left; text-align:center; border-radius:50px; border:1px solid black;">A</div>
     <div style="height:17px; width:18px; padding:2px; margin:5px 2px; float:left; text-align:center; border-radius:50px; border:1px solid black;">B</div>
     <div style="height:17px; width:18px; padding:2px; margin:5px 2px; float:left; text-align:center; border-radius:50px; border:1px solid black;">C</div>
     <div style="height:17px; width:18px; padding:2px; margin:5px 2px; float:left; text-align:center; border-radius:50px; border:1px solid black;">D</div>
</td>

border & border-radius don't work

How can I solve that any idea. Thanks

Upvotes: 1

Views: 979

Answers (1)

Bruno Lowagie
Bruno Lowagie

Reputation: 77528

I tried this code:

public static final String SRC = "circle.html";
public static final String DEST = "circle.pdf";

public static void main(String[] args) throws IOException {
    LicenseKey.loadLicenseFile(System.getenv("ITEXT7_LICENSEKEY") + "/itextkey-html2pdf_typography.xml");        
    Alignment app = new Alignment();
    app.createPdf(SRC, DEST);
}

public void createPdf(String src, String dest) throws IOException {
    HtmlConverter.convertToPdf(new File(src), new File(dest));
}

The file circle.pdf looks like this (note that I removed white as border color, or the border would have been invisible):

<table>
<tr>
<td style="border-width:0 2px 0 4px; border-style:solid; padding:0 20px;" colspan="3">
     <div style="height:17px; width:25px; padding:2px; float:left;">1</div>
     <div style="height:17px; width:18px; padding:2px; margin:5px 2px; float:left; text-align:center; border-radius:50px; border:1px solid black;">A</div>
     <div style="height:17px; width:18px; padding:2px; margin:5px 2px; float:left; text-align:center; border-radius:50px; border:1px solid black;">B</div>
     <div style="height:17px; width:18px; padding:2px; margin:5px 2px; float:left; text-align:center; border-radius:50px; border:1px solid black;">C</div>
     <div style="height:17px; width:18px; padding:2px; margin:5px 2px; float:left; text-align:center; border-radius:50px; border:1px solid black;">D</div>
</td>
</tr>
</table>

The result looks like this:

enter image description here

I'd say this looks more or less OK, but:

  • If you don't get this result, you are using an old version of iText, upgrade! (because we didn't support this functionality in older versions of iText, and we have no intention of updating those old versions),
  • If, once you have upgraded to using iText 7 + pdfHTML, you aren't pleased with the way the "circles" are drawn, please admit that your workaround to create circles is rather poor. There are other ways to do this. Either you can create your own implementation of the ITagWorker to draw real circles around the content instead of working with rounded corners, or, if you have a limited set of characters that require circles, use a font of which the glyphs already come with circles.

Upvotes: 2

Related Questions