Itext Digital superscript and subscript for PDF printing

Source: Internet
Author: User

https://kathleen1974.wordpress.com/category/itext-pdf/

In one of my project, we need to give the user a Web UI (a textbox) to enter some text and allow input of superscriptand s Ubscript tag <sup> and <sub>. And this text entered by user would be printed on a PDF document at the backend, using IText PDF. To print the superscript and subscript in IText PDF, we need to use the Chunk ' s Settextrise () method. Below is the function I with transform the user input string into correct IText Phrase object, which would print out the Superscript or subscript accordingly. To use these codes, copy them to your Java file. Performs the PDF generation, and call the function getsubors Upchunks (String temp, font textft, font textriseft) passing in the input String, normal text font, and the font for Supers Cript/subscript text, to obtain the Phrase object.private string Nextsuborsuptag (string input) {int supidx = STRINGUTILS.I Ndexof (input, constants.superscript); int subidx = Stringutils.indexof (input, constants.subscript); if (Subidx = = StrIngutils.index_not_found && Supidx = = stringutils.index_not_found) {return null;} else if (Subidx = = Stringutils.index_not_found) {return constants.superscript;} else if (Supidx = = Stringutils.index_not_ FOUND) {return constants.subscript;} else {if (Subidx < SUPIDX) {return constants.subscript;} else {return constants.su Perscript;}}} private string Nextendtag (string tag) {if (Constants.SUPERSCRIPT.equalsIgnoreCase (tag)) {return constants.superscript _end;}         else {return constants.subscript_end;}} Public Phrase getsuborsupchunks (String temp, font textft, font textriseft) {Phrase Phrase = new Phrase (); String Nexttag = Nextsuborsuptag (temp); String Endtag = Nextendtag (nexttag); int tagcount = stringutils.countmatches (temp, constants.subscript) + Stringutils.countmatches (temp, Constants.superscript), for (int i=0;i<tagcount;i++) {logger.debug ("I i); if (i = = 0) { Phrase.add (New Chunk (Stringutils.substringbefore (temp, Nexttag), Textft));} else {temp = Stringutils.substringaftER (temp, nexttag); Nexttag = Nextsuborsuptag (temp);p Hrase.add (New Chunk (Stringutils.substringbetween (temp, Endtag, Nexttag) (TEXTFT)); Endtag = Nextendtag (Nexttag);} if (Constants.SUBSCRIPT.equalsIgnoreCase (Nexttag)) {Phrase.add (New Chunk (Stringutils.substringbetween (temp, Nexttag, Endtag), Textriseft). Settextrise ( -3f));} else {phrase.add (new Chunk (Stringutils.substringbetween (temp, Nexttag, Endtag), Textriseft). Settextrise (5f));} if (i = = tagCount-1) {temp = Stringutils.substringafter (temp, Nexttag);p Hrase.add (New Chunk (Stringutils.substringafter (temp, Endtag), Textft));}} return phrase;} The content of Constants.java are:public static final string subscript = "<sub>";p ublic static final string Subscript_end = "</sub>";p ublic static final string superscript = "<sup>";p ublic static final string Superscri Pt_end = "</sup>";

Take your own change as follows:

public class Pdfutil {private static Logger Logger = Loggerfactory.getlogger (pdfutil.class);/** handles superscript and small labels for numbers */public Static final String subscript = "<sub>";p ublic static final String subscript_end = "</sub>";p ublic static fin Al string superscript = "<sup>";p ublic static final String superscript_end = "</sup>";
public static Font fontgeneral;//general contents private static string Nextsuborsuptag (string input) {int supidx = Stringutils.indexo F (input, superscript); int subidx = Stringutils.indexof (input, subscript); if (Subidx = = Stringutils.index_not_found && Supidx = = stringutils.index_not_found) {return null;} else if (Subidx = = stringutils.index_not_found) {return superscript;} else if (Supidx = = stringutils.index_not_found) {RET Urn subscript;} else {if (Subidx < SUPIDX) {return subscript;} else {return superscript;}}} private static string Nextendtag (String tag) {if (Superscript.equalsignorecase (tag)) {return superscript_end;} else { return subscript_end;}} public static Phrase getsuborsupchunks (String temp, font textft, font textriseft) {Phrase Phrase = new Phrase (); String Nexttag = Nextsuborsuptag (temp); String Endtag = Nextendtag (nexttag); int tagcount = stringutils.countmatches (temp, subscript) + stringutils.countmatches (temp, superscript); for (int i=0;i<tagcount;i++) {logger.debug ("I:"+ i), if (i = = 0) {phrase.add (New Chunk (Stringutils.substringbefore (temp, Nexttag), Textft));} else {temp = Stringutils.sub Stringafter (temp, nexttag), Nexttag = Nextsuborsuptag (temp);p Hrase.add (New Chunk (Stringutils.substringbetween (temp, Endtag, Nexttag), Textft)); Endtag = Nextendtag (Nexttag);} if (Subscript.equalsignorecase (Nexttag)) {Phrase.add (New Chunk (Stringutils.substringbetween (temp, Nexttag, EndTag), Textriseft). Settextrise ( -3f));} else {phrase.add (new Chunk (Stringutils.substringbetween (temp, Nexttag, Endtag), Textriseft). Settextrise (5f));} if (i = = tagCount-1) {temp = Stringutils.substringafter (temp, Nexttag);p Hrase.add (New Chunk (Stringutils.substringafter (temp, Endtag), Textft));}} return phrase;}}

How to use:

Phrase Phrase = pdfutil.getsuborsupchunks (j62.getstring ("j6202"), fontgeneral,fontgeneral);p = new Paragraph (Phrase); P.setalignment (Element.align_left); cellc.addelement (P); Cellc.setcolspan (3); Tablecontent.addcell (CellC);

A string containing a digital science notation with superscript or a small mark is processed by Pdfutil.getsuborsupchunks (), a Phrase is obtained, and a paragraph object is initialized with it, which can be added to the table.

The principle is to invoke:

New Chunk (XXX, textriseft). Settextrise ( -3f); Small label
New Chunk (XXX), textriseft). Settextrise (5f); Superscript

Itext Digital superscript and subscript for PDF printing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.