截图转换为pdf文件在android [复制]

pxy2qtax  于 2022-12-16  发布在  Android
关注(0)|答案(3)|浏览(129)

此问题在此处已有答案

How I can convert a bitmap into PDF format in android [closed](3个答案)
19小时前关门了。
我正在尝试将一个活动转换为pdf格式。我已经采取了截图一样的位图。请帮助我转换为所需的pdf文件的截图。谢谢。请提供代码。谢谢。

cfh9epnr

cfh9epnr1#

这是我用的密码
1)主要活动
公共类MainActivity扩展活动{ //按钮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)写入Pdf活动
公共类WritePdfActivity扩展活动{私有静态字符串文件=“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();
    }
}

}

6vl6ewon

6vl6ewon2#

请尝试以下问题:
How I can convert a bitmap into PDF format in android

pdf-format-in-android/14393561#14393561

也可以在项目中包含itextpdf-5.3.2.jar

i2byvkas

i2byvkas3#

@世伟达使用文件f =新文件(环境.getExternalStorageDirectory().getAbsolutePath()+“/DCIM/Camera/bitmap.jpg”);

相关问题