您好我想在一个pdf中打印所有语言。我的代码如下所示。我在pdf中无法看到除英语之外的任何字符。请检查生成pdf的附件。
我无法理解存在真正问题的地方
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.ElementList;
import com.itextpdf.tool.xml.XMLWorker;
import com.itextpdf.tool.xml.XMLWorkerFontProvider;
import com.itextpdf.tool.xml.css.StyleAttrCSSResolver;
import com.itextpdf.tool.xml.html.CssAppliers;
import com.itextpdf.tool.xml.html.CssAppliersImpl;
import com.itextpdf.tool.xml.html.Tags;
import com.itextpdf.tool.xml.parser.XMLParser;
import com.itextpdf.tool.xml.pipeline.css.CSSResolver;
import com.itextpdf.tool.xml.pipeline.css.CssResolverPipeline;
import com.itextpdf.tool.xml.pipeline.end.ElementHandlerPipeline;
import com.itextpdf.tool.xml.pipeline.html.HtmlPipeline;
import com.itextpdf.tool.xml.pipeline.html.HtmlPipelineContext;
public class ParseHtml7 {
public static final String DEST = "arabic.pdf";
public static final String HTML = "<html><head></head><body style=\"font-size:12.0pt; font-family:Times New Roman\">"+ "<p>This is hindi language testपरीक्षण </p> "
+"<p>This is tamil language test சோதனை </p>"
+"<p> This is maly language test ujian </p>"
+"<p> This is thai language test ทดสอบ</p>"
+"<p> This is viyatamese language test thử nghiệm</p>"
+"<p> This is khmer language test ការធ្វើតេស្ត</p>"
+"<p> This is arabic language test اختبار</p>"
+"<p> This is french language test tester</p>"
+"<p> This is russian language test контрольная работа</p>"
+"<p> This is korean language test 테스트</p>"
+"<p> This is japanese language test テスト</p>"
+" <p>This is chinese language test 测试</p>"
+"</body></html>";
public static void main(String\[\] args) throws IOException, DocumentException {
File file = new File(DEST);
//file.getParentFile().mkdirs();
new ParseHtml7().createPdf(DEST);
}
/**
* Creates a PDF with the words "Hello World"
* @param file
* @throws IOException
* @throws DocumentException
*/
public void createPdf(String file) throws IOException, DocumentException {
// step 1
Document document = new Document();
// step 2
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
// step 3
document.open();
// step 4
// Styles
CSSResolver cssResolver = new StyleAttrCSSResolver();
XMLWorkerFontProvider fontProvider = new XMLWorkerFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS);
//fontProvider.register("resources/fonts/NotoNaskhArabic-Regular.ttf");
fontProvider.register("/Users/user/Downloads/MyFonts/FreeSans.ttf");
fontProvider.register("/Users/user/Downloads/MyFonts/THSarabunNew.ttf");
fontProvider.register("/Users/user/Downloads/MyFonts/Kh-Content.ttf");
fontProvider.register("/Users/user/Downloads/MyFonts/THSarabunNew Italic.ttf");
fontProvider.register("/Users/user/Downloads/MyFonts/THSarabunNew BoldItalic.ttf");
fontProvider.register("/Users/user/Downloads/MyFonts/THSarabunNew Bold.ttf");
fontProvider.register("/Users/user/Downloads/MyFonts/arial.ttf");
fontProvider.register("/Users/user/Downloads/MyFonts/FreeSans.ttf");
fontProvider.register( "/Users/user/Downloads/MyFonts/NotoSansCJKsc-Regular.otf");
fontProvider.register( "/Users/user/Downloads/MyFonts/arialuni.ttf");
CssAppliers cssAppliers = new CssAppliersImpl(fontProvider);
// HTML
HtmlPipelineContext htmlContext = new HtmlPipelineContext(cssAppliers);
htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());
// Pipelines
ElementList elements = new ElementList();
ElementHandlerPipeline pdf = new ElementHandlerPipeline(elements, null);
HtmlPipeline html = new HtmlPipeline(htmlContext, pdf);
CssResolverPipeline css = new CssResolverPipeline(cssResolver, html);
// XML Worker
XMLWorker worker = new XMLWorker(css, true);
XMLParser p = new XMLParser(worker);
p.parse(new ByteArrayInputStream((HTML.toString().getBytes("UTF-8"))), Charset.forName("UTF-8"));
PdfPTable table = new PdfPTable(1);
PdfPCell cell = new PdfPCell();
for (Element e : elements) {
System.out.println(e);
cell.addElement(e);
}
table.addCell(cell);
document.add(table);
// step 5
document.close();
}
}[![enter image description here][1]][1]