这是我的代码
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page trimDirectiveWhitespaces="true" %>
<%@page import="com.itextpdf.text.Document"%>
<%@page import="com.itextpdf.text.Paragraph"%>
<%@page import="com.itextpdf.text.PageSize"%>
<%@ page import="javax.servlet.http.*,javax.servlet.*,com.itextpdf.text.DocumentException" %>
<%@page import="java.io.*,java.text.SimpleDateFormat"%>
<%@page import="java.sql.*,java.nio.charset.Charset"%>
<%@ page import="com.itextpdf.tool.xml.ElementHandler,java.util.List,java.util.Arrays,java.util.Collections,java.util.*,com.itextpdf.text.pdf.*,com.itextpdf.tool.xml.ElementList,com.itextpdf.text.Rectangle,com.itextpdf.text.Element,com.itextpdf.text.*,com.itextpdf.text.Font,java.awt.Color,com.itextpdf.text.Font.FontFamily,java.util.Date,java.text.*,com.itextpdf.tool.xml.XMLWorkerHelper" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
String date=request.getParameter("user");
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat myFormat = new SimpleDateFormat("EEE,dd MMM yyyy");
final java.util.Date dtt = df.parse(date);
final String ds = myFormat.format(df.parse(date));
class HeaderAndFooter extends PdfPageEventHelper {
private String name = "";
protected Phrase footer;
protected Phrase header;
Font headerFont = new Font(FontFamily.COURIER, 13, Font.UNDERLINE);
Font footerFont = new Font(FontFamily.HELVETICA, 11, Font.BOLD);
String date1;
public HeaderAndFooter(String name) {
super();
this.name = name;
header = new Phrase("***** Header *****");
footer = new Phrase("**** Footer ****");
}
@Override
public void onEndPage(PdfWriter writer, Document document) {
PdfContentByte cb = writer.getDirectContent();
//header content
String headerContent =name;
//header content
String footerContent = headerContent;
//date selection
ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase("Date:"+ds,headerFont), document.leftMargin() - 1, document.top() + 18, 0);
}
}
OutputStream output=response.getOutputStream();
response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "inline; filename=details.pdf");
try{
Document document = new Document(PageSize.A4);
PdfWriter writer=PdfWriter.getInstance(document, output);
document.open();
writer.setPageEvent(new HeaderAndFooter(date));
XMLWorkerHelper worker = XMLWorkerHelper.getInstance();
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/yre", "root", "root");
Statement st=con.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
List arrlist = new ArrayList();
ResultSet rsta=st.executeQuery("Select * from yu where date='"+date+"' ");
int interval=0;
while(rsta.next()){
interval=rsta.getInt("inter");
}
System.out.println(interval);
ResultSet rs=st.executeQuery("SELECT * FROM ur_start2 where date='"+date+"' ");
while(rs.next()){
arrlist.add(rs.getString("data"));
}
for(int i=0;i<interval+1;i++){
String str =(String) arrlist.get(i);
worker.parseXHtml(writer, document, new StringReader("<html><head></head><body>"+str+"<br></br><br></br></body></html>"));
}
document.close();
writer.flush();
writer.close();
output.close();
}catch(IOException e){e.printStackTrace();}
%>
</body>
</html>
这样在显示的pdf中只给我英文字符。而印地语字体是不可见的。
我在印地文中键入的内容也会在印地文的印地文中恢复。但是在pdf上,我无法在印地文中检索它。
我应该怎么做才能在印地语中检索它?
最佳答案
即使最新的iText版本也不可能支持Devanagari脚本,请参考here。
但是,还有其他一些选择,例如使用awt Graphics2D类,设置支持Unicode字符的字体并将其添加到PDF(尽管不是文本形式)。
示例:here
使用上面的参考输出:
“新印地语PDF”在此处呈现为文本