Java PDF文档中的中心图像
我有一份A4格式的PDF文件。 我想将图像垂直和水平居中。我试过这个:Java PDF文档中的中心图像,java,android,Java,Android,我有一份A4格式的PDF文件。 我想将图像垂直和水平居中。我试过这个: PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(595, 842, 1).create(); PdfDocument.Page page = document.startPage(pageInfo);
PdfDocument.PageInfo pageInfo = new
PdfDocument.PageInfo.Builder(595, 842, 1).create();
PdfDocument.Page page = document.startPage(pageInfo);
Canvas canvas = page.getCanvas();
Rect rectangle = new Rect(0,0,500,500);
canvas.drawBitmap(bitmapArray.get(i), null, rectangle, null);
document.finishPage(page);
但是图像没有居中。有没有办法解决这个问题?在从各个网站寻找一些解决方案后,我成功地制作了一个pdf生成器应用程序。我将整个视图转换为pdf格式。它对我有用。希望能对你有所帮助
private fun savePdf() {
val printAttributes = PrintAttributes.Builder()
.setMediaSize(PrintAttributes.MediaSize.ISO_A4)
.setResolution(Resolution("id", Context.PRINT_SERVICE, 400, 600))
.setColorMode(PrintAttributes.COLOR_MODE_COLOR)
.setMinMargins(PrintAttributes.Margins.NO_MARGINS)
.build()
val pdfDocument = PrintedPdfDocument(this, printAttributes)
val content = findViewById<View>(R.id.linearLayoutId)
val pageInfo = PageInfo.Builder(content.width, content.height, 1).create()
val page = pdfDocument.startPage(pageInfo)
content.draw(page.canvas)
pdfDocument.finishPage(page)
val file = File(Environment.getExternalStorageDirectory(), "/hello9.pdf")
try {
pdfDocument.writeTo(FileOutputStream(file))
} catch (e: IOException) {
e.printStackTrace()
}
pdfDocument.close()
}
private fun savePdf(){
val printtributes=printtributes.Builder()
.setMediaSize(PrintAttributes.MediaSize.ISO_A4)
.setResolution(分辨率(“id”,Context.PRINT_服务,400600))
.setColorMode(打印属性.COLOR\u MODE\u COLOR)
.setMinMargins(printtributes.Margins.NO_Margins)
.build()
val pdfDocument=PrintedPdfDocument(这是printAttributes)
val内容=findViewById(R.id.linearLayoutId)
val pageInfo=pageInfo.Builder(content.width,content.height,1).create()
val页面=pdfDocument.startPage(页面信息)
content.draw(page.canvas)
PDF文档完成页(第页)
val file=file(Environment.getExternalStorageDirectory(),“/hello9.pdf”)
试一试{
pdfDocument.writeTo(FileOutputStream(文件))
}捕获(e:IOException){
e、 printStackTrace()
}
pdfDocument.close()
}
这似乎不完整,请提供完整的代码片段。还有,有没有办法通过缩放将全分辨率图像添加到pdf中?