我想创建一个简单的PDF文档使用Android本机PdfDocument类(从API 19)。我想要的是有一个XML布局文件,例如pdf_doc.xml,然后在创建PDF时对其进行充气。在pdf_doc.xml里面,我会看到一堆可以在代码中获取的视图,然后单独绘制到PdfDocuments页面上。问题是,这会创建一个损坏的PDF文件。另一方面,如果我只是在我的main_activity.xml(我在创建PDF时使用的活动的xml)中创建一个简单的TextView,并使用该TextView,它可以正常工作。Android PdfDocument损坏时,保存到外部存储
为什么TextView来自膨胀的布局或活动的布局有什么区别?我是否试图以错误的方式做到这一点?
仅供参考:以编程方式创建TextView时也失败。
以下是我的源代码。这两个功能在彼此之后立即被调用。第一个创建PDF,另一个保存。问题是在我看来,被称为内容,即时通讯膨胀的布局。如果我将该TextView放在Activity的XML中,然后从活动中获取它,就像act.findViewById(R.id.pdf_text);
一样,然后按预期工作。
代码:
public static PdfDocument createPdf(Activity act){
PrintAttributes printAttrs = new PrintAttributes.Builder().
setColorMode(PrintAttributes.COLOR_MODE_COLOR).
setMediaSize(PrintAttributes.MediaSize.ISO_A4).
setMinMargins(PrintAttributes.Margins.NO_MARGINS).
build();
ViewGroup mainLayout = (ViewGroup) View.inflate(act, R.layout.pdf_doc, null);
int pageHeight = printAttrs.getMediaSize().getHeightMils()/1000 * 72;
int pageWidth = printAttrs.getMediaSize().getWidthMils()/1000 * 72;
PdfDocument document = new PrintedPdfDocument(act, printAttrs);
PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(pageWidth, pageHeight, 1).create();
PdfDocument.Page page = document.startPage(pageInfo);
View content = mainLayout.findViewById(R.id.pdf_text);
content.draw(page.getCanvas());
document.finishPage(page);
return document;
}
public static void saveFile(PdfDocument doc){
String root = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString();
File myDir = new File(root + "/pdf_test");
myDir.mkdirs();
File file = new File(myDir, "test.pdf");
if (file.exists()) {
file.delete();
}
try {
FileOutputStream out = new FileOutputStream(file);
doc.writeTo(out);
doc.close();
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
String path = file.getAbsolutePath();
Log.d("pdftest", "path: " + path);
}
@ FrankN.Stein也许我还不够清楚,但我不打算一下子画出整个布局。我只是从布局中取出一个TextView并试图绘制它。我看不出这样做的区别,相比之下,从活动使用的布局来看...... –
根本没有。正如我所说,如果我从活动的XML中取出一个TextView,我可以将它绘制到PdfDocument页面画布上,但是我无法使用编程式膨胀布局中的TextView执行此操作? –