You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于Print Document Adapter打印预览PDF异常:预览缺失且与输出不符

使用Print Document Adapter和Print Manager打印PDF文件时,遇到如下问题:

  • PDF预览显示不全且存在数据缺失
  • 最终输出的PDF文件内容完全正常

预览模式截图:PDF内容显示不完整,存在数据缺失区域
下载后完整PDF截图:内容完整无缺失

以下是使用的示例代码:

@Override
public void onLayout(PrintAttributes printAttributes, PrintAttributes printAttributes1, CancellationSignal cancellationSignal, LayoutResultCallback layoutResultCallback, Bundle bundle) {
    if (cancellationSignal.isCanceled()) {
        layoutResultCallback.onLayoutCancelled();
    }
    else {
        PrintDocumentInfo.Builder builder=
                new PrintDocumentInfo.Builder("Contract");
        builder.setContentType(PrintDocumentInfo.CONTENT_TYPE_DOCUMENT)
                .setPageCount(PrintDocumentInfo.PAGE_COUNT_UNKNOWN)
                .build();
        layoutResultCallback.onLayoutFinished(builder.build(),
                !printAttributes1.equals(printAttributes));
    }
}

@Override
public void onWrite(PageRange[] pageRanges, ParcelFileDescriptor parcelFileDescriptor, CancellationSignal cancellationSignal, WriteResultCallback writeResultCallback) {

    InputStream in=null;
    OutputStream out=null;
    try {
        File file = new File(pathName);
        in = new FileInputStream(file);
        out=new FileOutputStream(parcelFileDescriptor.getFileDescriptor());

        byte[] buf=new byte[16384];
        int size;

        while ((size=in.read(buf)) >= 0
                && !cancellationSignal.isCanceled()) {
            out.write(buf, 0, size);
        }

        if (cancellationSignal.isCanceled()) {
            writeResultCallback.onWriteCancelled();
        }
        else {
            writeResultCallback.onWriteFinished(new PageRange[] { PageRange.ALL_PAGES });
        }
    }
    catch (Exception e) {
        writeResultCallback.onWriteFailed(e.getMessage());
        Log.e("ErrorInWriting" , e.getMessage());
    }
    finally {
        try {
            in.close();
            out.close();
        }
        catch (IOException e) {
            Log.e("ErrorInFinally" , e.getMessage());
        }
    }
}

已尝试多种方法,仍未找到解决方案。

内容的提问来源于stack exchange,提问作者Mahmoud Nasr

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 01:55:12