I generate a simple PDF with an image (banner) and a title in code as follows:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Bitmap headerBmp = BitmapFactory.decodeResource(getResources(), R.drawable.headerimage);
PdfDocument pdfDocument = new PdfDocument();
PdfDocument.PageInfo mypageInfo = new PdfDocument.PageInfo.Builder(2480, 3508, 1).create();
PdfDocument.Page myPage = pdfDocument.startPage(mypageInfo);
Canvas canvas = myPage.getCanvas();
canvas.setDensity(300);
canvas.drawBitmap(headerBmp, 50, 50, new Paint());
Paint titlePaint = new Paint();
titlePaint.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.BOLD));
titlePaint.setTextSize(75);
canvas.drawText("Title", 50, 250, titlePaint);
pdfDocument.finishPage(myPage);
File pdfPath = new File(getFilesDir(), "pdf");
if (!pdfPath.exists()) {
pdfPath.mkdir();
}
File pdfFile = new File(pdfPath, "document.pdf");
try {
pdfDocument.writeTo(new FileOutputStream(pdfFile, false));
} catch (IOException e) {
// below line is used
// to handle error
e.printStackTrace();
} finally {
// after storing our pdf to that
// location we are closing our PDF file.
pdfDocument.close();
}
}
}
The resulting Pdf document is too big (page size). It should fit A4 page size. The document size seems correct when I change this line
PdfDocument.PageInfo mypageInfo = new PdfDocument.PageInfo.Builder(2480, 3508, 1).create();
to this:
PdfDocument.PageInfo mypageInfo = new PdfDocument.PageInfo.Builder(595, 842, 1).create();
But now the image (about 1200 x 300) does not fit the page. If resized to 595 px width the image looks very bad.
2480 x 3508 is the correct size for A4 with 300 dpi 595 x 842 is the correct size for A4 with 72 dpi
But images normally have 300dpi.
How do I make the page fit without losing image quality?