gpt4 book ai didi

Android:PdfDocument 生成空 pdf

转载 作者:可可西里 更新时间:2023-11-01 19:03:14 26 4
gpt4 key购买 nike

        PdfDocument document = new PdfDocument();
// crate a page description
PageInfo pageInfo = new PageInfo.Builder(300, 300, 1).create();
// create a new page from the PageInfo
Page page = document.startPage(pageInfo);
// repaint the user's text into the page
View content = findViewById(R.id.textarea);
content.draw(page.getCanvas());
// do final processing of the page
document.finishPage(page);
try {
File f = getPDFPath();
FileOutputStream fos = new FileOutputStream(f);
document.writeTo(fos);
document.close();
fos.close();

} catch (IOException e) {
throw new RuntimeException("Error generating file", e);
}

其中 findViewById(R.id.textarea); 指的是带有一些文本的 TextView,但上面的代码只生成空的 pdf。可能是什么问题?

是否有任何链接包含使用 Android 原生 API 生成 pdf 的工作示例?

最佳答案

我有,但经过大量测试后,我意识到我的 View 的高度和宽度为 0,因为我使用的是 TextView。所以我设法等到 View (TextView)加载并开始创建文档后,看看代码,希望你能修复它:

    @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);

final TextView tv = (TextView) findViewById(R.id.textView1);
ViewTreeObserver vto = tv.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Toast.makeText(MainActivity.this, tv.getWidth() + " x " + tv.getHeight(), Toast.LENGTH_LONG).show();

try {
File file1 = new File("/mnt/sdcard/test/");
if(!file1.exists()){
file1.mkdirs();
}

File file = new File("/mnt/sdcard/test", "filename"+System.currentTimeMillis()+".pdf");
PrintAttributes printAttrs = new PrintAttributes.Builder().
setColorMode(PrintAttributes.COLOR_MODE_COLOR).
setMediaSize(PrintAttributes.MediaSize.ISO_A4).
setResolution(new Resolution("zooey", PRINT_SERVICE, 450, 700)).
setMinMargins(Margins.NO_MARGINS).
build();
PdfDocument document = new PrintedPdfDocument(MainActivity.this, printAttrs);
PageInfo pageInfo = new PageInfo.Builder(450, 700, 1).create();
Page page = document.startPage(pageInfo);

if (page != null) {

View view = findViewById(R.id.textView1);//getContentView();
view.layout(0, 0, view.getWidth(),
view.getHeight());
Log.i("draw view", " content size: "+view.getWidth()+" / "+view.getHeight());
view.draw(page.getCanvas());
// Move the canvas for the next view.
page.getCanvas().translate(0, view.getHeight());
}

document.finishPage(page);
os = new FileOutputStream(file);
document.writeTo(os);
document.close();
os.close();
Log.i("done", file.getAbsolutePath().toString());

} catch (IOException e) {
throw new RuntimeException("Error generating file", e);
}

tv.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
});

}

里面的魔法:

 final TextView tv = (TextView) findViewById(R.id.textView1);
ViewTreeObserver vto = tv.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
// create document here
}
});

关于Android:PdfDocument 生成空 pdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22959502/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com