【问题标题】:Screenshot conversion to pdf file in android在android中将屏幕截图转换为pdf文件
【发布时间】:2016-08-26 05:21:11
【问题描述】:

我正在尝试将活动转换为 pdf 格式。我已经截取了与位图相同的屏幕截图。请帮助我将该屏幕截图转换为所需的 pdf 文件。谢谢。 请提供代码。谢谢。

【问题讨论】:

标签: android


【解决方案1】:

请试试这个 所以问题:

How I can convert a bitmap into PDF format in android

pdf-format-in-android/14393561#14393561

或者在你的项目中包含 itextpdf-5.3.2.jar

【讨论】:

  • 我添加了 doidtext.. 可以吗??
  • 是的,没关系@SwagatamDutta
  • 文件 f = new File(root.getAbsolutePath() + "/DCIM/Camera/bitmap.jpg");出现无法解析符号根..这里的根是什么?!
【解决方案2】:

这是我使用的代码...

1) 主活动

公共类 MainActivity 扩展 Activity { //按钮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

公共类 WritePdfActivity 扩展 Activity { 私有静态字符串文件="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();
    }
}

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-04-18
    • 1970-01-01
    • 1970-01-01
    • 2020-01-23
    • 1970-01-01
    • 2014-06-12
    • 2019-12-03
    • 2019-05-19
    相关资源
    最近更新 更多