以编程方式在android中创建一个pdf文件并在其中写入 [英] Creating a pdf file in android programmatically and writing in it
问题描述
我正在尝试在我的应用程序中创建一个 pdf 文件,将其保存在外部存储中并打开它.保存文件对我来说不是问题,打开文件也不是问题,我的问题是创建一个文件并在其中写入.因此,在网上进行了一些研究后,我发现了以下方法:
I'm trying to create a pdf file inside my app, save it on the external storage the open it. Saving a file isn't an issue for me, nor is opening one, my issue is with creating one and writing in it. So after some research online I found the following way of doing it:
File file = new File(directoryName, fileName);
// Creating output stream to write in the newly created file
FileOutputStream fOut = null;
try {
fOut = new FileOutputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
// Creating a new document
Document document = new Document(PageSize.A4, 50, 50, 50, 50);
try {
PdfWriter.getInstance(document, fOut);
// Open the document for writing
document.open();
// Write in the document
document.add(new Paragraph("Hello world"));
document.close();
} catch (DocumentException de) {
System.err.println(de.getMessage());
}
运行我的应用程序并执行上面的代码后,出现以下错误:
Upon running my app and executing the code above, I get the following error:
java.lang.NoClassDefFoundError: Failed resolution of: Ljava/awt/Color;
有人知道我的代码有什么问题吗,或者知道另一种肯定适用于创建和编写 pdf 文件的方法吗?
Would someone know what's the issue with my code, or of another way that is sure to work for creating and writing a pdf file ?
谢谢!
推荐答案
很明显我使用的代码与 android 不兼容,因此我得到了错误.您会在下面找到实际运行的正确代码(用于创建 pdf 文件、在其中放入一些内容、保存并打开新创建的文件):
So apparently the code I was using wasn't compatible with android, hence the error I was getting. Below you'll find the correct code that actually works right (for creating a pdf file, putting some content in it, saving in and the opening the newly created file):
PS:为此,您需要将 iTextG 的 jar 添加到您的项目中:
PS: For this you'll need to add the jar of iTextG to your project:
// Method for creating a pdf file from text, saving it then opening it for display
public void createandDisplayPdf(String text) {
Document doc = new Document();
try {
String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Dir";
File dir = new File(path);
if(!dir.exists())
dir.mkdirs();
File file = new File(dir, "newFile.pdf");
FileOutputStream fOut = new FileOutputStream(file);
PdfWriter.getInstance(doc, fOut);
//open the document
doc.open();
Paragraph p1 = new Paragraph(text);
Font paraFont= new Font(Font.COURIER);
p1.setAlignment(Paragraph.ALIGN_CENTER);
p1.setFont(paraFont);
//add paragraph to document
doc.add(p1);
} catch (DocumentException de) {
Log.e("PDFCreator", "DocumentException:" + de);
} catch (IOException e) {
Log.e("PDFCreator", "ioException:" + e);
}
finally {
doc.close();
}
viewPdf("newFile.pdf", "Dir");
}
// Method for opening a pdf file
private void viewPdf(String file, String directory) {
File pdfFile = new File(Environment.getExternalStorageDirectory() + "/" + directory + "/" + file);
Uri path = Uri.fromFile(pdfFile);
// Setting the intent for pdf reader
Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
pdfIntent.setDataAndType(path, "application/pdf");
pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
startActivity(pdfIntent);
} catch (ActivityNotFoundException e) {
Toast.makeText(TableActivity.this, "Can't read pdf file", Toast.LENGTH_SHORT).show();
}
}
这篇关于以编程方式在android中创建一个pdf文件并在其中写入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!