我使用itextg 5.5.9生成pdf。所有英文字符都正确显示,但不显示中文字符。有没有办法在pdf上显示中文字符。
这是代码。
Font fontbold = FontFactory.getFont("Times-Roman", 11, Font.BOLD);
Font fontTimes = FontFactory.getFont("Times-Roman", 11);
protected void generatePDF(String txn_id) {
remark=et_customer_remark.getText().toString().trim();
getEmployeeBranchAddress();
SimpleDateFormat df = new SimpleDateFormat("yy"); // Just the year, with 2 digits
String year = df.format(Calendar.getInstance().get(Calendar.YEAR));
int nextYear=1+Integer.parseInt(year);
// TODO Auto-generated method stub
String Filename=txn_id.replaceAll("/", "-");
final Calendar c = Calendar.getInstance();
String yy = String.valueOf(c.get(Calendar.YEAR));
String mm = String.valueOf(c.get(Calendar.MONTH) + 1);
String dd = String.valueOf(c.get(Calendar.DAY_OF_MONTH));
Bitmap signatureBitmap = mSignaturePadEngineer.getSignatureBitmap();
Bitmap signatureBitmapCust = mSignaturePadCustomer.getSignatureBitmap();
if (addSignatureToGallery(signatureBitmap, signatureBitmapCust)) {
/*
* Toast.makeText(ServiceReportActivity.this,
* "Signature saved into the Gallery", Toast.LENGTH_SHORT) .show();
*/
try {
Document document = new Document();
document.open();
File root = new File(Environment.getExternalStorageDirectory(),
"CRM_PDF/PDFReports/");
if (!root.exists()) {
root.mkdirs(); // create root directory in sdcard
}
File gpxfile = new File(root, EIIEMPID + "-" + Filename + ".pdf");
// pdf file
// in that
attachmentPath = gpxfile.getParent();
Log.e("Attachment_path 2211", attachmentPath+EIIEMPID + "-" + Filename + ".pdf");
PdfWriter.getInstance(document, new FileOutputStream(gpxfile));
File file = new File(
getAlbumStorageDir("SignaturePadEnginner"),
"SignatureEnginner.jpg");
InputStream fileInputStream = new FileInputStream(file);
Bitmap bmp = BitmapFactory.decodeStream(fileInputStream);
int height = bmp.getHeight();
int width = bmp.getWidth();
height = height / 4;
bmp = Bitmap.createScaledBitmap(bmp, width, height, false);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
File filecustomer = new File(
getAlbumStorageDir("SignaturePadCustomer"),
"SignatureCustomer.jpg");
InputStream fileInputStreamCustomer = new FileInputStream(
filecustomer);
Bitmap bmpCustomer = BitmapFactory
.decodeStream(fileInputStreamCustomer);
int heightCustomer = bmpCustomer.getHeight();
int widthCustomer = bmpCustomer.getWidth();
heightCustomer = heightCustomer / 4;
bmpCustomer = Bitmap.createScaledBitmap(bmpCustomer,
widthCustomer, heightCustomer, false);
ByteArrayOutputStream streamCutomer = new ByteArrayOutputStream();
bmpCustomer.compress(Bitmap.CompressFormat.PNG, 100,
streamCutomer);
try {
imgSignEngineer = Image.getInstance(stream.toByteArray());
imgSignCustomer = Image
.getInstance(streamCutomer.toByteArray());// imagesign
// ofcustomer
} catch (BadElementException e) {
e.printStackTrace();
}
InputStream inputStream = ServiceReportActivity.this
.getAssets().open("logo.jpeg");
Bitmap bmpLogo = BitmapFactory.decodeStream(inputStream);
ByteArrayOutputStream streamLogo = new ByteArrayOutputStream();
bmpLogo.compress(Bitmap.CompressFormat.JPEG, 100, streamLogo);
Image imgLogo;
imgLogo = Image.getInstance(streamLogo.toByteArray());
imgLogo.scalePercent(35);
document.open();
PdfPTable table = new PdfPTable(10);
table.setWidthPercentage(100);
// document.add(imgSign);
Paragraph p3 = new Paragraph();
//Typeface font = Typeface.createFromAsset(getAssets(), "Fonts/Verdana.ttf");
Font font = new Font(Font.FontFamily.TIMES_ROMAN);
font.setSize(20);
p3.setFont(font);
p3.add("Service Report Cum Proforma Invoice:" + txn_id);
p3.setAlignment(Element.ALIGN_CENTER);
Paragraph paraDate = new Paragraph();
Font fontDate = new Font(Font.FontFamily.TIMES_ROMAN);
fontDate.setSize(10);
paraDate.setFont(fontDate);
StringBuilder strBuildDateCurrent = new StringBuilder("Date : "
+ strEndDateNTime);
String strDate = strBuildDateCurrent.toString();
paraDate.add(strDate);
paraDate.setAlignment(Element.ALIGN_RIGHT);
document.add(p3);
document.add(Chunk.NEWLINE);
PdfPCell cell;
Paragraph parahHeadInfo = new Paragraph(
"No.20,Jingke Rd.,Nantun District,Taichung City 408,Taiwan", fontbold);
Paragraph parahHeadInfo2 = new Paragraph(
strBranchAddress+"\nPh. No : "+ "N/A"+"\nCUSTOMER CARE HELPLINE : N/A \nEmail : info@accutex.com.tw\nWeb site : https://www.accutex.com.tw",
fontTimes);
cell = new PdfPCell();
cell.addElement(imgLogo);
cell.addElement(parahHeadInfo);
cell.addElement(parahHeadInfo2);
cell.setColspan(5);
cell.setRowspan(11);
table.addCell(cell);
Paragraph parahHeadInfo3 = new Paragraph(
"Case No : "
+ CaseID
+"\nPriority : "
+ casePriority
+ "\nService Report Cum Proforma Invoice No : "
+ txn_id
+ "\nDate : "
+ strStartDateNTime
/*+ "\nGST No : "+ "N/A"*/+"\nSERVICE ACCOUNTING CODE (SAC) : N/A / N/A",fontbold);
cell = new PdfPCell();
cell.addElement(parahHeadInfo3);
cell.setColspan(5);
cell.setRowspan(11);
table.addCell(cell);
cell = new PdfPCell(new Phrase("Customer Name: "
+ strCustomerName, fontTimes));
cell.setColspan(6);
table.addCell(cell);
cell = new PdfPCell(new Phrase("Ph. No: " + phn_no, fontTimes));
cell.setColspan(4);
table.addCell(cell);
// table.addCell("Customer Name: " + strCustomerName);
// table.addCell("Ph. No: " + strCustomerPhoneNo);
// table.addCell("Customer Name: " + strCustomerName);
String strTempAddress = custaddress;
strTempAddress = strCustomerAddress.replace("\n", " ");
cell = new PdfPCell(new Phrase("Customer Address: "
+ strTempAddress, fontTimes));
cell.setColspan(10);
table.addCell(cell);
/*cell = new PdfPCell(new Phrase("Customer GST No : "
+ strGSTNo, fontTimes));
cell.setColspan(10);
table.addCell(cell);*/
cell = new PdfPCell(new Phrase("Contact Person & M.No.: "
+ strContactPersonName + " " + strContactPersonMobNo,
fontTimes));
cell.setColspan(6);
table.addCell(cell);
cell = new PdfPCell(new Phrase(
"Designation: " + strDesignation, fontTimes));
cell.setColspan(4);
table.addCell(cell);
cell = new PdfPCell(new Phrase("Machine", fontbold));
cell.setColspan(1);
table.addCell(cell);
cell = new PdfPCell(new Phrase("Principal: " + strMachineMake,
fontTimes));
cell.setColspan(3);
table.addCell(cell);
cell = new PdfPCell(new Phrase("Model: " + strModel, fontTimes));
cell.setColspan(3);
table.addCell(cell);
cell = new PdfPCell(new Phrase("Sr. No: " + strSrNo, fontTimes));
cell.setColspan(3);
table.addCell(cell);
cell = new PdfPCell(new Phrase("Details", fontbold));
cell.setColspan(1);
table.addCell(cell);
cell = new PdfPCell(new Phrase("Controller Make: "
+ strControlMake, fontTimes));
cell.setColspan(3);
table.addCell(cell);
cell = new PdfPCell(new Phrase("System No: " + strSystemNo,
fontTimes));
cell.setColspan(3);
table.addCell(cell);
/*cell = new PdfPCell(new Phrase("Hour Count : " + strHourCount, fontTimes));
cell.setColspan(3);
table.addCell(cell);*/
cell = new PdfPCell(new Phrase("Type of Visit ", fontbold));
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setColspan(1);
table.addCell(cell);
cell = new PdfPCell(new Phrase("Installation:" + strInstalation
+ "\nAMC:" + strAmc, fontTimes));
cell.setColspan(3);
table.addCell(cell);
cell = new PdfPCell(new Phrase("Chargeable:" + strChargable
+ "\nNon Chargeable:" + strNonchargbl, fontTimes));
cell.setColspan(3);
table.addCell(cell);
cell = new PdfPCell(new Phrase("Warranty service:" + strWarrnty
+ "\nOther:" + strOther, fontTimes));
cell.setColspan(3);
table.addCell(cell);
/*
* cursor.getString(cursor.getColumnIndex(MSR_CauseNRemedy)));
* params.put("MSR_Remark",
* cursor.getString(cursor.getColumnIndex(MSR_Remark)));
*/
cell = new PdfPCell(new Phrase("Customer Complaint: "
+ customer_complain + "\n\n", fontTimes));
cell.setColspan(10);
table.addCell(cell);
cell = new PdfPCell(new Phrase("Causes & Remedy: "
+ causes_remedy + "\n\n\n", fontTimes));
cell.setColspan(10);
table.addCell(cell);
Paragraph parahWorkStartDateTime = new Paragraph(
"Work Started date/time : " + strStartDateNTime,
fontTimes);
Paragraph parahWorkEndDateTime = new Paragraph(
"Work ends date/time : " + strEndDateNTime,
fontTimes);
Paragraph parahWorkTotalDays = new Paragraph(
"Total days : " + strTotalDays,
fontTimes);
Paragraph parahChargePerDay = new Paragraph(
"Charges per day*billable days : " + strChargedPerDay
+ " * " + strBilableDays, fontTimes);
Paragraph parahCheckNo_Date = new Paragraph(
"Cheque No. & Date : " + strchqno + " " + strDate,
fontTimes);
Paragraph parahDrawnOn = new Paragraph(
"Drawn on : " + strDrawnOn, fontTimes);
Paragraph parahBankName_Branch = new Paragraph(
"Bank name & branch : " + strbankName, fontTimes);
/*
* Paragraph parahAmount = new Paragraph(
* "Amount ________________________",
* fontTimes);
*/
cell = new PdfPCell();
cell.addElement(parahWorkStartDateTime);
cell.addElement(parahWorkEndDateTime);
cell.addElement(parahWorkTotalDays);
cell.addElement(parahCheckNo_Date);
cell.addElement(parahDrawnOn);
cell.addElement(parahBankName_Branch);
// cell.addElement(parahAmount);
cell.setColspan(5);
table.addCell(cell);
Paragraph parahTotalCharge = new Paragraph("Amount : "
+ strAmount, fontTimes);
/*
* Paragraph parahFareCharges = new Paragraph(
* "Fare Charges ________________________", fontTimes);
*/
Paragraph parahLocalConveyance = new Paragraph(
"Local Conveyance : " + strlocalConvenc, fontTimes);
Paragraph parahOtherCharges = null;
/*if(FlagChargable==1){
parahOtherCharges = new Paragraph(
"GST Type : "+ strServiceTypeTax, fontTimes);
}else{
// Paragraph parahOtherCharges = new Paragraph("Service Tax Type :"+ strServiceTypeTax, fontTimes);
}*/
Paragraph parahSeviceTax = new Paragraph(
"Service Tax Amount : "+ strGSTServiceTax, fontTimes);
Paragraph parahTotalCharges = new Paragraph(
"Total Charges : " + strTotalCharges, fontTimes);
cell = new PdfPCell();
cell.addElement(parahChargePerDay);
cell.addElement(parahTotalCharge);
// cell.addElement(parahFareCharges);
cell.addElement(parahLocalConveyance);
cell.addElement(parahOtherCharges);
cell.addElement(parahSeviceTax);
cell.addElement(parahTotalCharges);
cell.setColspan(5);
table.addCell(cell);
cell = new PdfPCell();
Paragraph parahJobIs = new Paragraph("Job is: " + "Completed :"
+ strcompleted + " Awaiting for parts :"
+ strawaitingparts + " Pending :" + strPending
+ " On Contract :" + strOncontract, fontTimes);
cell.setColspan(10);
cell.addElement(parahJobIs);
table.addCell(cell);
cell = new PdfPCell(new Phrase("Accepted By Customer: "
+ acceptedByCustomer + "\n\n", fontTimes));
cell.setColspan(10);
table.addCell(cell);
cell = new PdfPCell(new Phrase("Customer Remarks: " + remark + "\n\n",
fontTimes));
cell.setColspan(10);
table.addCell(cell);
cell = new PdfPCell(new Phrase("Customer Feedback:\n\n",
fontTimes));
cell.setColspan(10);
table.addCell(cell);
cell = new PdfPCell(new Phrase("Other Engineers : "+multipleEnggName+"\n\n",
fontTimes));
cell.setColspan(10);
table.addCell(cell);
//By Kiran 31/5/2017
cell = new PdfPCell(new Phrase("Customer Contact Name : "+strCustName+"\n\n",fontTimes));
cell.setColspan(10);
table.addCell(cell);
cell = new PdfPCell(new Phrase("Customer Contact Email Id: "+ strCustEmailid+"\n\n", fontTimes));
cell.setColspan(5);
table.addCell(cell);
cell = new PdfPCell(new Phrase("Customer Contact Mobile Number: " + strCustMobile+"\n", fontTimes));
cell.setColspan(5);
table.addCell(cell);
cell = new PdfPCell();
Paragraph parahSign1 = new Paragraph(
"Name & Sign. of Engineer", fontTimes);
cell.addElement(parahSign1);
cell.addElement(imgSignEngineer);
Paragraph paraNAME1 = new Paragraph(service_eng_name, fontTimes);
cell.addElement(paraNAME1);
cell.setColspan(5);
table.addCell(cell);
cell = new PdfPCell();
Paragraph parahSign2 = new Paragraph(
"Name & Sign. of Customer", fontTimes);
cell.addElement(parahSign2);
cell.addElement(imgSignCustomer);
Paragraph paraNAME2 = new Paragraph(cust_name, fontTimes);// addd
// signh
// to
// pdf
cell.addElement(paraNAME2);// addd signh to pdf
cell.setColspan(5);
table.addCell(cell);
document.add(table);
document.add(paraDate);
document.close();
Toast.makeText(getApplicationContext(), "PDF Generated",
Toast.LENGTH_SHORT).show();
Intent target = new Intent(Intent.ACTION_VIEW);
target.setDataAndType(Uri.fromFile(gpxfile), "application/pdf");
target.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
Intent intent = Intent.createChooser(target, "Open File");
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
// Instruct the user to install a PDF reader here,
// or
// something
}
} catch (IOException e1) {
// TODO Auto-generated catch block
Log.e("Pdf exception", e1.getMessage());
} catch (BadElementException e) {
// TODO Auto-generated catch block
Log.e("Pdf exception", e.getMessage());
} catch (DocumentException e) {
// TODO Auto-generated catch block
Log.e("Pdf exception", e.getMessage());
}
}
else {
Toast.makeText(ServiceReportActivity.this,
"Unable to store the signature", Toast.LENGTH_SHORT).show();
}
}
我也尝试过使用ttf
个文件但没有结果。我应该使用Times-New Roman
这样的字体,以便生成中文字符