-1

I am trying to convert an activity to pdf format.I have taken a screenshot of the same as a bitmap.PLease help me to convert that sreenshot to the required pdf file.Thank you. Please provide the code.Thank you.

3 Answers3

0

This is the code I used...

1) MainActivity

public class MainActivity extends Activity { //Button btn_getpdf;

      private LinearLayout linearLayout;
      private Bitmap myBitmap;

      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          // btn_getpdf = (Button) findViewById(R.id.button_pdf);
          linearLayout = (LinearLayout) findViewById(R.id.linear1);
          linearLayout.post(new Runnable() {
              public void run() {

                  //take screenshot
                  myBitmap = captureScreen(linearLayout);

                  Toast.makeText(getApplicationContext(), "Screenshot captured..!", Toast.LENGTH_LONG).show();

                  try {
                      if (myBitmap != null) {
                          //save image to SD card
                          saveImage(myBitmap);
                      }
                      Toast.makeText(getApplicationContext(), "Screenshot saved..!", Toast.LENGTH_LONG).show();
                  } catch (IOException e) {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
                  }

              }
          });

      }

      public Bitmap captureScreen(View v) {

          Bitmap screenshot = null;
          try {

              if (v != null) {

                  screenshot = Bitmap.createBitmap(v.getMeasuredWidth(), v.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
                  Canvas canvas = new Canvas(screenshot);
                  v.draw(canvas);
              }

          } catch (Exception e) {
              // Log.d("ScreenShotActivity", "Failed to capture screenshot because:" + e.getMessage());
          }

          return screenshot;
      }

      public void saveImage(Bitmap bitmap) throws IOException {

          ByteArrayOutputStream bytes = new ByteArrayOutputStream();
          bitmap.compress(Bitmap.CompressFormat.PNG, 40, bytes);
          File f = new File(root.getAbsolutePath() + "/DCIM/Camera/bitmap.jpg");
          f.createNewFile();
          FileOutputStream fo = new FileOutputStream(f);
          fo.write(bytes.toByteArray());
          fo.close();
      }
  }

2)WritePdfActivity

public class WritePdfActivity extends Activity { private static String FILE ="DCIM/Camera/GenPdf.pdf";

static Image image;
static ImageView img;
Bitmap bmp;
static Bitmap bt;
static byte[] bArray;

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    img=(ImageView)findViewById(R.id.imageView1);

    try
    {
        Document document = new Document();

        PdfWriter.getInstance(document, new FileOutputStream(FILE));
        document.open();

        addImage(document);
        document.close();
    }

    catch (Exception e)
    {
        e.printStackTrace();
    }

}
private static void addImage(DocumentsContract.Document document)
{

    try
    {
        image = Image.getInstance(bArray);  ///Here i set byte array..you can do bitmap to byte array and set in image...
    }
    catch (BadElementException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    catch (MalformedURLException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    catch (IOException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    // image.scaleAbsolute(150f, 150f);
    try
    {
        document.add(image);
    } catch (DocumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

}

-1

Plz try this So question:

How I can convert a bitmap into PDF format in android

pdf-format-in-android/14393561#14393561

or also include itextpdf-5.3.2.jar in your project

Community
  • 1
  • 1
Damini Mehra
  • 3,257
  • 3
  • 13
  • 24
-2

@Swagatam Dutta use File f = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/DCIM/Camera/bitmap.jpg");

  • 1
    This looks more like a response to [this comment](https://stackoverflow.com/questions/39158777/screenshot-conversion-to-pdf-file-in-android/74810299#comment65670540_39158826) rather than a new answer. – Eric Aya Dec 15 '22 at 11:13