user3367817 user3367817 - 6 months ago 23
Java Question

PDF size not showing 100%

Am sharing image as PDF through email, but when I share as PDF the PDF size does not show fully, only some portion of the PDF shows. This is the code am using to share as PDF, please kindly help I have been stuck here for a while, am willing to accept answer that works immediately.

private void sharePdf() {
try {
Bitmap bitmap;
View v1;
// File path;
File att = null;
root = new File(Environment.getExternalStorageDirectory(), getString(R.string.app_name));
if (!root.exists())
root.mkdir();
{
menu_layout.setVisibility(View.GONE);
bottom_layout.setVisibility(View.GONE);
menus.setVisibility(View.GONE);
v1 = rl_layout.getRootView();
v1.setDrawingCacheEnabled(true);

bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, bytes);
time = new SimpleDateFormat("yyyy-MM-dd_HHmmss", Locale.ENGLISH).format(new Date());
path1 = new File(root.getAbsolutePath() + File.separator + time + ".png");
path1.createNewFile();
FileOutputStream fo = new FileOutputStream(path1);
fo.write(bytes.toByteArray());
fo.close();

Bitmap bgBit = null;
try {
Uri uri = Uri.fromFile(new File(custombackground));
if (uri != null && custombackground != null && !custombackground.trim().equalsIgnoreCase("")) {
bgBit = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
}
} catch (Exception ex) {

}
if (bgBit != null) {
bitmap = overlay(bgBit, drawingPanelView.getBitmap());
bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, bytes);
}

att = new File(root.getAbsolutePath() + File.separator
+ "note.png");
if (att.exists()) {
att.delete();
}
att.createNewFile();
FileOutputStream fo_att = new FileOutputStream(att);
fo_att.write(bytes.toByteArray());
fo_att.close();

menu_layout.setVisibility(View.VISIBLE);
bottom_layout.setVisibility(View.VISIBLE);
menus.setVisibility(View.VISIBLE);
}

File pdfFile = new File(root.getAbsolutePath() + File.separator + "share_temp.pdf");
if (!pdfFile.exists()) {
pdfFile.createNewFile();
}
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(pdfFile.getAbsoluteFile()));
document.open();
document.add(new Paragraph(content.getText().toString()));

Image image = Image.getInstance(att.getAbsolutePath());
document.add(image);
for (HashMap<String, String> item : list) {
List<String> vales = new ArrayList<>(item.values());
for (String path : vales) {
File fileIn = new File(path);
if (fileIn.exists()) {
image = Image.getInstance(fileIn.getAbsolutePath());
document.add(image);
}
}
}

document.close();


final Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setType("text/plain");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, "");
emailIntent.putExtra(android.content.Intent.EXTRA_CC, "");
emailIntent.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.app_name));
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(pdfFile));
startActivity(Intent.createChooser(emailIntent, "Send mail..."));

} catch (Exception e) {
}

}

Answer

Actually you have to scale image to document width and height to fit into document to do this just add following lines to your code.

  float scaler = ((document.getPageSize().getWidth() - document.leftMargin()
               - document.rightMargin() - indentation) / image.getWidth()) * 100;

and before you add image,first scale that image

image.scalePercent(scaler);

then add this image to document

document.add(image);