【发布时间】:2021-08-20 18:09:44
【问题描述】:
我正在使用 iText 生成 PDF。生成 PDF 是为了显示学校的一些记录,所以我需要在第一页顶部标题的中心显示学校标志和学校名称。我有可能是任何大小的图像文件的字符串 HTTP URL。我想在不损失质量的情况下调整图像大小并在标题部分进行调整(因此有意义),并且需要在下面显示学校名称。我的问题是我不知道如何添加图像,所以它显示在“标题框”中。
这里有一些代码sn-ps ...
private void createPdf() {
try {
String imgURL = "http URL";
String schoolName = "School Name";
File filePath = new File(Environment.getExternalStoragePublicDirectory( Environment.DIRECTORY_DOCUMENTS), "PROJECT");
if (! filePath.exists()){
if (! filePath.mkdirs()){
}else{
}
}
// Create a file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
fileExp = new File(filePath+"/Report_"+ timeStamp + ".pdf");
photoURI = FileProvider.getUriForFile(this, this.getApplicationContext().getPackageName() + ".fileprovider", fileExp);
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(fileExp));
document.open();
addMetaData(document);
Paragraph preface = new Paragraph();
// We add one empty line
addEmptyLine(preface, 1);
// Lets write a big header
Paragraph para1 = new Paragraph(repHeader, catFont);
para1.setAlignment(Element.ALIGN_CENTER);
preface.add(para1);
addEmptyLine(preface, 2);
Paragraph para3 = new Paragraph("Period : ", textBold);
Chunk perChunk3 = new Chunk("from "+edtFromDate.getText().toString()+" to "+edtToDate.getText().toString(),textNormal);
para3.add(perChunk3);
preface.add(para3);
addEmptyLine(preface, 1);
document.close();
//insertDocument("Report_"+ timeStamp + ".pdf");
Intent emailIn = new Intent(Intent.ACTION_SEND);
emailIn.setType("application/pdf");
emailIn.putExtra(Intent.EXTRA_STREAM, photoURI);
emailIn.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
emailIn.putExtra(Intent.EXTRA_EMAIL, new String[] { selEmailId });
emailIn.putExtra(Intent.EXTRA_SUBJECT, "Report : "+repHeader);
emailIn.putExtra(Intent.EXTRA_TEXT, repHeader);
startActivityForResult(Intent.createChooser(emailIn, "E-mail"),15);
} catch (Exception e) {
e.printStackTrace();
}
}
下面是依赖:
implementation 'com.itextpdf:itextg:5.5.10'
【问题讨论】:
标签: android pdf itext pdf-generation