安卓实现文本以pdf格式保存,导出时中文出现的空白问题,以及打印pdf文档

1.实现以pdf格式保存时,需要下载iTextpdf.jar,导入项目中;

下载链接:http://www.pc6.com/softview/SoftView_438379.html

2.在AndroidManifest.xml文件中添加读写权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

3.pdf文档打印满足条件(第一、支持手机打印;第二、打开手机wiff与打印机的wiff相连)

4.以下提供中文不会出现空白demo

第一种方式:

Document doc = new Document();//页面布局
File foder = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/pdf");//声明存储位置
if (!foder.exists()) {//判断文件夹是否存在,如不存在就重新创建
    foder.mkdirs();
}
File myCaptureFile = new File(foder, "test4.pdf");//声明文件名称
try {
    //设置中文显示问题
    //pdf文件导出时中文不就出现乱码或者空白
    BaseFont baseFont = BaseFont.createFont("assets/fonts/STSong.ttf",BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
    //pdf文件导出时中文出现乱码或者空白
    //BaseFont baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
    Font font = new Font(baseFont, 32);//设置字体格式及大小

    FileOutputStream fOut = new FileOutputStream(myCaptureFile);//输出流
    PdfWriter writer = PdfWriter.getInstance(doc, fOut);

    doc.open();
    if (!myCaptureFile.exists()) {//判断文件是否存在,如不存在重新创建
        myCaptureFile.createNewFile();
    }
    Paragraph p = new Paragraph("这是一个中国文化", font);
    p.setAlignment(Element.ALIGN_CENTER);
    doc.add(p);
    doc.close();
} catch (Exception e) {
    e.printStackTrace();
}

第二种方式:

try {
    File file = new File(Environment.getExternalStorageDirectory() + File.separator + "pdf");
    if (!file.exists()) {
        file.mkdirs();
    }
    PdfDocument document = new PdfDocument();
    /***宽 高 页数******/
    PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(210, 297, 1).create();
    PdfDocument.Page page = document.startPage(pageInfo);

    TextPaint textPaint = new TextPaint();
    textPaint.setColor(Color.BLACK);
    textPaint.setTextSize(16);
    textPaint.setTextAlign(Paint.Align.LEFT);

    Typeface textTypeface = Typeface.create(Typeface.MONOSPACE, Typeface.NORMAL);
    textPaint.setTypeface(textTypeface);

    String text = "这是一个中文测试";
    /*StaticLayout参数的意义
     * 1.字符串子资源
     * 2.画笔对象
     * 3.layout的宽度,字符串超出宽度时自动换行
     * 4.layout的样式,有ALIGN_CENTER, ALIGN_NORMAL, ALIGN_OPPOSITE 三种
     * 5.相对行间距,相对字体大小,1.5f表示行间距为1.5倍的字体高度
     * 6.相对行间距,0表示0个像素
     * 实际行间距等于两者的和
     * 7.还不知道是什么意思,参数名是boolean includepad*/
    StaticLayout mTextLayout = new StaticLayout(text, textPaint, page.getCanvas().getWidth(),
            Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
    mTextLayout.draw(page.getCanvas());
    document.finishPage(page);
    File file1 = new File(file, "pdf_android.pdf");

    try {
        FileOutputStream mFileOutStream = new FileOutputStream(file1);

        document.writeTo(mFileOutStream);
        mFileOutStream.flush();
        mFileOutStream.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
    document.close();

} catch (Exception e) {
    e.printStackTrace();
}

5.pdf打印就不做详情介绍了

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值