Cause: The following code is only possible because HTML to PDF does not wrap automatically.
Note: Fully simulate the automatic wrapping of HTML pages!
Packagetest;Importjava.io.UnsupportedEncodingException;/*** Solve PDF wrap problem, insert specified string at specified location, compatible with Chinese and English and special characters * *@authorXG June **/ Public classApp { Public Static voidMain (string[] args)throwsunsupportedencodingexception {System.out.println (Addstr (Ten, "<br/>", "as Assad DSA Asidifen FLADADASDSJF Asidifen ljdsljkjlfdsklfd What place is Skljdsasfasdfads")); } /*** Insert Method * *@paramnum * Inserts a string every few characters *@paramSplitstr * String to be specified *@paramSTR * Original String *@returninserts a string after the specified string *@throwsunsupportedencodingexception*/ Public StaticString Addstr (intnum, string splitstr, String str)throwsunsupportedencodingexception {stringbuffer sb=NewStringBuffer (); String Temp=str; intLen =str.length (); while(Len > 0) { intIDX =getendindex (temp, num); Sb.append (Temp.substring (0, IDX + 1) . Append (SPLITSTR); Temp= temp.substring (idx + 1); Len=temp.length (); } returnsb.tostring (); } /*** Two digits/English * *@paramstr * String *@paramnum * Inserts a string every few characters *@returnint Final Index *@throwsunsupportedencodingexception*/ Public Static intGetendindex (String str,DoubleNumthrowsunsupportedencodingexception {intIDX = 0; Doubleval = 0.00; //Judging whether it is English/Chinese for(inti = 0; I < str.length (); i++) { if(String.valueof (Str.charat (i)). GetBytes ("UTF-8"). Length >= 3) { //Chinese characters or symbolsVal + = 1.00; } Else { //English characters or symbolsVal + = 0.50; } if(Val >=num) {idx=i; if(Val-num = = 0.5) {idx= I-1; } Break; } } if(idx = = 0) {idx= Str.length ()-1; } returnidx; }}
Effect:
Java format string, inserts the specified string at the specified location, compatible with both Chinese and English, and special characters, such as: line break, to resolve issues such as generating PDF line breaks