在android中生成带有中文字符的PDF
我正在尝试用下面的代码在android上生成一些中文字符的PDF文件。它没有中文字符(只有英文)也可以正常工作,PDF大小约为40-50Kb。 但当我尝试生成带有中文字符的PDF时,它工作正常,PDF大小为3-4MB 两种情况下的字符数几乎相同。 你知道如何减少PDF的大小吗? 因为我需要把PDF上传到服务器上在android中生成带有中文字符的PDF,android,pdf,Android,Pdf,我正在尝试用下面的代码在android上生成一些中文字符的PDF文件。它没有中文字符(只有英文)也可以正常工作,PDF大小约为40-50Kb。 但当我尝试生成带有中文字符的PDF时,它工作正常,PDF大小为3-4MB 两种情况下的字符数几乎相同。 你知道如何减少PDF的大小吗? 因为我需要把PDF上传到服务器上 public void generatePDFDoc(Activity activity, View view, String pdfFileName){ {
public void generatePDFDoc(Activity activity, View view, String pdfFileName){
{
File pdfFile = null;
PrintAttributes printAttrs = new PrintAttributes.Builder().
setColorMode(PrintAttributes.COLOR_MODE_COLOR).
setMediaSize(PrintAttributes.MediaSize.NA_LETTER).
setResolution(new PrintAttributes.Resolution("print", activity.PRINT_SERVICE,
25, 25)).
setMinMargins(PrintAttributes.Margins.NO_MARGINS).
build();
PdfDocument document = new PrintedPdfDocument(activity, printAttrs);
View content = view;
int width = content.getWidth();
int height = content.getHeight();
PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(width, height, 1).create();
PdfDocument.Page page = document.startPage(pageInfo);
content.draw(page.getCanvas());
document.finishPage(page);
try {
pdfFile = new File(pdfFileName);
if (pdfFile != null) {
FileOutputStream os = new FileOutputStream(pdfFile);
document.writeTo(os);
document.close();
os.close();
}
} catch (Exception e) {
throw new RuntimeException("Error generating file", e);
} finally {
if (pdfFile != null) {
//Success Generate PDF file
}
}
}
}
你试过UTF-8编码吗?谢谢。我们如何将UTF-8添加到FileOutputStream?因为document.writeTo(操作系统);应将FileOutputstream作为参数。BufferedWriter out=new BufferedWriter(new OutputStreamWriter(new FileOutputstream(“jedis.txt”),“UTF-8”);谢谢。问题在这里,文档。写到(操作系统);。。我需要使用UTF编码的FileOutputStream。not Writer.writeTo方法只接受OutputStream not Writer。事实上,我发现了这个问题,汉字像图像一样打印而不像字符。这就是增加PDF文件大小的原因。但我仍然没有解决方法来解决这个问题。您尝试过UTF-8编码吗?谢谢。我们如何将UTF-8添加到FileOutputStream?因为document.writeTo(操作系统);应将FileOutputstream作为参数。BufferedWriter out=new BufferedWriter(new OutputStreamWriter(new FileOutputstream(“jedis.txt”),“UTF-8”);谢谢。问题在这里,文档。写到(操作系统);。。我需要使用UTF编码的FileOutputStream。not Writer.writeTo方法只接受OutputStream not Writer。事实上,我发现了这个问题,汉字像图像一样打印而不像字符。这就是增加PDF文件大小的原因。但我仍然没有解决方法来解决这个问题。