<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
	<channel>
		<title><![CDATA[Latest posts for the topic "iText在產生pdf檔時，遇到一些特殊中文字(例如:珉、峯、喆等)，都會有unicode字碼(&#xxxxx;)輸出，該怎麼辦?"]]></title>
		<link>https://forum.andowson.com/posts/list/5.page</link>
		<description><![CDATA[Latest messages posted in the topic "iText在產生pdf檔時，遇到一些特殊中文字(例如:珉、峯、喆等)，都會有unicode字碼(&#xxxxx;)輸出，該怎麼辦?"]]></description>
		<generator>JForum - http://www.jforum.net</generator>
			<item>
				<title>iText在產生pdf檔時，遇到一些特殊中文字(例如:珉、峯、喆等)，都會有unicode字碼(&amp;#xxxxx;)輸出，該怎麼辦?</title>
				<description><![CDATA[ 我的資料庫裡頭儲存是Big5資料，想從資料庫SELECT一些資料，然後用iText來產生pdf檔，但遇到一些特殊中文字時，就會產生如附件(樣本檔)所示錯誤現象，請問版主該如何解決?謝謝!]]></description>
				<guid isPermaLink="true">https://forum.andowson.com/posts/preList/483/1002.page</guid>
				<link>https://forum.andowson.com/posts/preList/483/1002.page</link>
				<pubDate><![CDATA[Tue, 10 May 2011 16:49:53]]> GMT</pubDate>
				<author><![CDATA[ collectWu]]></author>
			</item>
			<item>
				<title>回覆:iText在產生pdf檔時，遇到一些特殊中文字(例如:珉、峯、喆等)，都會有unicode字碼(&amp;#xxxxx;)輸出，該怎麼辦?</title>
				<description><![CDATA[ 我猜測這個現象會發生的原因可能如下， 
<br>
1.如果資料庫內碼是使用Big5編碼時，透過網頁上輸入的中文字(UTF-8)進入到應用伺服器時，被轉了一次碼(ISO-8859-1)，再存到資料庫時，又轉了一次碼(Big5)，例如下列的順序： 
<br>
UTF-8-&gt;ISO-8859-1-&gt;Big5 
<br>
當UTF-8轉成ISO-8859-1時，Unicode被轉成10進制的HTML Entity格式（如珉），然後跟其他可以轉成Big5的字元一起存到資料庫去。 
<br>
<br>
2.等到從資料庫取回這個字串時，就會得到一個夾雜著HTML Entity格式的Big5字串，如果把這個字串直接傳給iText 去產生 pdf 檔，這些HTML Entity字串應該就會當做英文字母一樣輸出。於是就產生類似您所附上的sample.pdf 般的內容了。 
<br>
<br>
好，了解原因後，要解決這個問題就很簡單了，只要在步驟2時補上將HTML Entity字串轉碼回原始編碼，再傳給iText去產生pdf即可。而要將HTML Entity字串轉碼回原始編碼只要呼叫 
<br>
[url=http://commons.apache.org/lang/]Apache Commons Lang[/url]的[url=http://commons.apache.org/lang/api-release/org/apache/commons/lang/StringEscapeUtils.html#unescapeHtml(java.lang.String)]StringEscapeUtils.unescapeHtml(java.lang.String)[/url] method即可。 
<br>
<br>
在此以修改[url=http://itextpdf.com/book/index.php]iText in Action 2nd Edition[/url]所提供的[url=http://examples.itextpdf.com/src/part1/chapter01/HelloWorld.java]HelloWorld.java[/url]作為範例 
<br>
HelloWorld.java: 
<br>
[code] 
<br>
/* 
<br>
 * This class is part of the book "iText in Action - 2nd Edition" 
<br>
 * written by Bruno Lowagie (ISBN: 9781935182610) 
<br>
 * For more info, go to: http://itextpdf.com/examples/ 
<br>
 * This example only works with the AGPL version of iText. 
<br>
 */ 
<br>
<br>
package com.andowson.pdf; 
<br>
<br>
import java.io.FileOutputStream; 
<br>
import java.io.IOException; 
<br>
<br>
import org.apache.commons.lang.StringEscapeUtils; 
<br>
<br>
import com.itextpdf.text.Document; 
<br>
import com.itextpdf.text.DocumentException; 
<br>
import com.itextpdf.text.Font; 
<br>
import com.itextpdf.text.Paragraph; 
<br>
import com.itextpdf.text.pdf.BaseFont; 
<br>
import com.itextpdf.text.pdf.PdfWriter; 
<br>
<br>
/** 
<br>
 * First iText example: Hello World. 
<br>
 */ 
<br>
public class HelloWorld { 
<br>
<br>
 /** Path to the resulting PDF file. */ 
<br>
 public static final String RESULT 
<br>
 = "D:/hello.pdf"; 
<br>
<br>
 /** 
<br>
 * Creates a PDF file: hello.pdf 
<br>
 * @param args no arguments needed 
<br>
 */ 
<br>
 public static void main(String[] args) 
<br>
 throws DocumentException, IOException { 
<br>
 new HelloWorld().createPdf(RESULT); 
<br>
 } 
<br>
<br>
 /** 
<br>
 * Creates a PDF document. 
<br>
 * @param filename the path to the new PDF document 
<br>
 * @throws DocumentException 
<br>
 * @throws IOException 
<br>
 */ 
<br>
 public void createPdf(String filename) 
<br>
 throws DocumentException, IOException { 
<br>
 // step 1 
<br>
 Document document = new Document(); 
<br>
 // step 2 
<br>
 PdfWriter.getInstance(document, new FileOutputStream(filename)); 
<br>
 // step 3 
<br>
 document.open(); 
<br>
<br>
 // Assumes that source string comes from database 
<br>
 String source = "測試物品-珉彣峯喆献"; 
<br>
 // Unescapes the Unicode characters from HTML entities (decimal format) to string 
<br>
 String output = StringEscapeUtils.unescapeHtml(source); 
<br>
<br>
 // Specifies the file path to the Chinese font file 
<br>
 BaseFont bfChinese = BaseFont.createFont("C:/Windows/Fonts/kaiu.ttf", 
<br>
 BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); 
<br>
 Font font = new Font(bfChinese, 12); // The font size is 12. 
<br>
<br>
 // step 4 
<br>
 document.add(new Paragraph(output, font)); 
<br>
 // step 5 
<br>
 document.close(); 
<br>
 } 
<br>
} 
<br>
[/code] 
<br>
<br>
下載函式庫： 
<br>
http://itextpdf.com/ 
<br>
http://commons.apache.org/lang/ 
<br>
<br>
參考網址： 
<br>
* Unicode 編碼字集 
<br>
http://theorem.ca/~mvcorks/cgi-bin/unicode.pl.cgi?start=4E00&amp;end=9FFF 
<br>
* 解決iText輸出中文問題 
<br>
http://blog.yam.com/rexmen/article/1888474 
<br>
* Hello World example 
<br>
http://itextpdf.com/examples/iia.php?id=12]]></description>
				<guid isPermaLink="true">https://forum.andowson.com/posts/preList/483/1008.page</guid>
				<link>https://forum.andowson.com/posts/preList/483/1008.page</link>
				<pubDate><![CDATA[Tue, 17 May 2011 23:24:47]]> GMT</pubDate>
				<author><![CDATA[ andowson]]></author>
			</item>
	</channel>
</rss>