|
|
|
ช่วยด้วยครับ JSP สร้างไฟล์ PDF แล้วไม่แสดง ภาษาไทย ทั้งที่ getFont ภาษาไทยแล้ว |
|
|
|
|
|
|
|
Code
<%--
Document : Create_PDF
Created on : Nov 9, 2016, 12:03:13 PM
Author : Life
--%>
<%@page import="com.itextpdf.text.BaseColor"%>
<%@page import="com.itextpdf.text.pdf.BaseFont"%>
<%@page import="com.itextpdf.text.Phrase"%>
<%@page import="com.itextpdf.text.pdf.PdfPCell"%>
<%@page import="com.itextpdf.text.pdf.PdfPTable"%>
<%@page import="com.itextpdf.text.Section"%>
<%@page import="com.itextpdf.text.Chapter"%>
<%@page import="com.itextpdf.text.Paragraph"%>
<%@page import="java.io.FileOutputStream"%>
<%@page import="com.itextpdf.text.pdf.PdfWriter"%>
<%@page import="com.itextpdf.text.Document"%>
<%@page import="com.itextpdf.text.pdf.CMYKColor"%>
<%@page import="com.itextpdf.text.FontFactory"%>
<%@page import="com.itextpdf.text.Font"%>
<%@ page import="java.io.File"%>
<%@ page import="org.apache.pdfbox.pdmodel.PDDocument" %>
<%@ page import="org.apache.pdfbox.pdmodel.PDPage" %>
<%@ page import="org.apache.pdfbox.pdmodel.edit.PDPageContentStream" %>
<%@ page import="org.apache.pdfbox.pdmodel.font.PDFont" %>
<%@ page import="org.apache.pdfbox.pdmodel.font.PDType1Font" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="java.util.Calendar" %>
<%@ page import="java.util.Date" %>
<%@ page import="java.util.GregorianCalendar" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<% request.setCharacterEncoding("UTF-8"); %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
Calendar c = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy");
String currentDate = df.format(c.getTime());
Font blueFont = FontFactory.getFont("C:\\Users\\Life\\Downloads\\CS_PraJad-3\\CS_PraJad\\CSPraJad.TTF", 8, Font.NORMAL, new CMYKColor(255, 0, 0, 0));
Font font = FontFactory.getFont("C:\\Users\\Life\\Downloads\\CS_PraJad-3\\CS_PraJad\\CSPraJad.TTF",
BaseFont.IDENTITY_H, BaseFont.EMBEDDED, 8, Font.NORMAL, BaseColor.BLACK);
BaseFont baseFont = font.getBaseFont();
Font redFont = FontFactory.getFont(FontFactory.COURIER, 12, Font.BOLD, new CMYKColor(0, 255, 0, 0));
Font yellowFont = FontFactory.getFont(FontFactory.COURIER, 14, Font.BOLD, new CMYKColor(0, 0, 255, 0));
Document document = new Document();
try
{
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("C:\\pdf\\StylingExample.pdf"));
document.open();
PdfPTable my_report_table = new PdfPTable(4);
PdfPCell table_cell;
String hdept_id = "กกดกด";
table_cell=new PdfPCell(new Phrase(hdept_id,font));
my_report_table.addCell(table_cell);
String hdept_name="กกกกกก";
table_cell=new PdfPCell(new Phrase(hdept_name,font));
my_report_table.addCell(table_cell);
String hmanager_id="CCCCCC";
table_cell=new PdfPCell(new Phrase(hmanager_id,font));
my_report_table.addCell(table_cell);
String hlocation_id="DDDDDD";
table_cell=new PdfPCell(new Phrase(hlocation_id,font));
my_report_table.addCell(table_cell);
for(int i=0;i<10;i++) {
String dept_id = "DEPARTMENT_ID";
table_cell=new PdfPCell(new Phrase(dept_id,blueFont));
my_report_table.addCell(table_cell);
String dept_name="DEPARTMENT_NAME";
table_cell=new PdfPCell(new Phrase(dept_name,blueFont));
my_report_table.addCell(table_cell);
String manager_id="MANAGER_ID";
table_cell=new PdfPCell(new Phrase(manager_id,blueFont));
my_report_table.addCell(table_cell);
String location_id="LOCATION_ID";
table_cell=new PdfPCell(new Phrase(location_id,blueFont));
my_report_table.addCell(table_cell);
}
/* Attach report table to PDF */
//document.add(new Paragraph("Styling Example"));
//Paragraph with color and font styles
// Paragraph paragraphOne = new Paragraph("Some colored paragraph text", redFont);
// document.add(paragraphOne);
//Create chapter and sections
Paragraph chapterTitle = new Paragraph(currentDate, blueFont);
Chapter chapter1 = new Chapter(chapterTitle, 1);
chapter1.setNumberDepth(0);
Paragraph sectionContent = new Paragraph("กกกกกกกกกกกกกกกกกกกกกก", blueFont);
chapter1.add(sectionContent);
Paragraph sectionContent2 = new Paragraph("Section Text content\n\n", yellowFont);
sectionContent2.setAlignment(sectionContent2.ALIGN_CENTER);
chapter1.add(sectionContent2);
document.add(chapter1);
document.add(my_report_table);
document.close();
writer.close();
} catch (Exception e)
{
e.printStackTrace();
}
%>
</body>
</html>
รบกวนช่วยด้วยนะครับ
Tag : Java, JAVA, JSP
|
|
|
|
|
|
Date :
2016-11-14 11:16:51 |
By :
jakkrit01 |
View :
4340 |
Reply :
4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ใช้ Eclipse ป่าวครับ ถ้าใช้ให้คลิกขวาที่ ไฟล์ นี้
เลือก properties
เลือก Resource
ตรงหัวข้อ text file encoding ให้เลือกเป็น Other และเลือก UTF-8
ส่วนตัวผมใช้ itext ออก PDF ได้ทั้งไทย ลาว khmer ก็ไม่มีปัญหานะครับ
|
|
|
|
|
Date :
2016-11-24 22:01:28 |
By :
taturk |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Date :
2016-12-27 11:15:09 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 03
|