android指定位置print,在Android中生成并打印具有特定大小的PDF

我正在使用

Android应用程序,我想生成并打印PDF.但我遇到了一些麻烦.我需要生成宽度为80毫米的PDF,高度可能会有所不同.

我正在尝试这个:public class PDFGenerator implements Runnable {

private Context ctx;

private View view;

private Intent mShareIntent;

private OutputStream os;

public PDFGenerator(Context ctx, View view) {

this.ctx = ctx;

this.view = view;

makeAndSharePDF();

}

public void makeAndSharePDF() {

new Thread(this).start();

}

@TargetApi(Build.VERSION_CODES.KITKAT)

public void run() {

PrintAttributes printAttrs = new PrintAttributes.Builder().

setColorMode(PrintAttributes.COLOR_MODE_MONOCHROME).

setMediaSize(PrintAttributes.MediaSize.ISO_C7).

setResolution(new PrintAttributes.Resolution("zooey", ctx.PRINT_SERVICE, 500, 500)).

setMinMargins(PrintAttributes.Margins.NO_MARGINS).

build();

PdfDocument document = new PrintedPdfDocument(ctx, printAttrs);

PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(view.getWidth(), view.getHeight(), 1).create();

PdfDocument.Page page = document.startPage(pageInfo);

view.draw(page.getCanvas());

document.finishPage(page);

try {

File pdfDirPath = new File(ctx.getFilesDir(), "pdfs");

pdfDirPath.mkdirs();

File file = new File(pdfDirPath, "danfe.pdf");

Uri contentUri = FileProvider.getUriForFile(ctx, "br.com.rdscodes.nfcelular", file);

os = new FileOutputStream(file);

document.writeTo(os);

document.close();

os.close();

shareDocument(contentUri);

} catch (IOException e) {

throw new RuntimeException("Error generating file", e);

}

}

private void shareDocument(Uri uri) {

mShareIntent = new Intent();

mShareIntent.setAction(Intent.ACTION_SEND);

mShareIntent.setType("application/pdf");

mShareIntent.putExtra(Intent.EXTRA_SUBJECT, "NFC-e");

mShareIntent.putExtra(Intent.EXTRA_STREAM, uri);

ctx.startActivity(mShareIntent);

}

}

此代码生成PDF,但带有字母大小.

我在Android Developers media size documentation中看到“ISO_C7”媒体大小是我几乎想要的大小,但我可以更改所有“printAttrs”,并且PDF的最终结果没有任何变化.

有更好的方法来生成和打印PDF吗?如何生成宽度为80mm的PDF?

这部分应用程序是由一位老员工制作的.

谢谢.

最佳答案 只是这样你就不要像我一样打破它,看看这个错误报告:

考虑到这个错误报告,我会说这是Android打印界面的一个问题,你不能使用PrintAttributes设置任何默认值,至少在Android N之前.

如果您找到/找到了解决方法,请告诉我:)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值