【发布时间】:2019-05-11 07:17:29
【问题描述】:
我正在制作一个 Android 应用程序,我必须在其中制作 PDF 报告。这些报告的数据来自 SQLite 数据库。 PDF 页面正在正确创建,但我在显示它们时遇到问题。 PDF 未显示。我希望 PDF 页面在创建后显示出来。以下是创建 PDF 报告的代码:
public void savePDF(){
Document mDoc = new Document();
fileName = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss", Locale.getDefault()).format(System.currentTimeMillis());
filePath = Environment.getExternalStorageDirectory() + "/Project Manager/" + "Project Report/" ;
try{
File dir = new File(filePath);
if (!dir.exists())
dir.mkdirs();
file = new File(dir, nameforR + "_" +fileName+".pdf");
FileOutputStream fOut = new FileOutputStream(file);
PdfWriter.getInstance(mDoc,fOut);
mDoc.open();
mDoc.addTitle("Project Manager");
mDoc.addSubject("Project Report");
mDoc.addKeywords("Projects");
mDoc.addAuthor("Fahad");
mDoc.addCreator("Fahad");
Paragraph preface = new Paragraph();
Paragraph repTitle = new Paragraph("Project Manager", catFont);
repTitle.setAlignment(Element.ALIGN_CENTER);
preface.add(repTitle);
Paragraph repTitlepowered = new Paragraph("PDF Reports", smallBold);
repTitlepowered.setAlignment(Element.ALIGN_CENTER);
preface.add(repTitlepowered);
Paragraph emptyL = new Paragraph("\n");
preface.add(emptyL);
Paragraph reportTitle = new Paragraph(nameforR, subFont);
reportTitle.setAlignment(Element.ALIGN_CENTER);
preface.add(reportTitle);
mDoc.add(preface);
Paragraph emptyLine = new Paragraph("\n");
mDoc.add(emptyLine);
PdfPTable table = new PdfPTable(3);
table.setWidthPercentage(70);
table.setSpacingAfter(10);}
mDoc.add(Chunk.NEWLINE);
mDoc.add(tableDeductions);
mDoc.close();
showPDf();
}
显示报告的代码:
private void showPDF(){
AssetManager assetManager = getAssets();
InputStream in = null;
OutputStream out = null;
File file = new File(getFilesDir() ,fileName + ".pdf");
try
{
in = assetManager.open(fileName + ".pdf");
out = this.openFileOutput(file.getName(), Context.MODE_WORLD_READABLE);
copyFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
}
catch (Exception e)
{
Log.e("tag", e.getMessage());
}
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
}
private void copyFile(InputStream in, OutputStream out) throws IOException
{
byte[] buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1)
{
out.write(buffer, 0, read);
}
}
【问题讨论】:
-
PDF 未显示。我不知道为什么,但它没有显示
-
我没听懂你
-
我这里没有使用任何光标
标签: android itext android-fileprovider