How to create a PDF file with multiple pages from an image file in Android? I created one PDF file from an image. There is one page in this PDF file. This is half this image. In the right part of the search, the part is cut in the PDF file. I am using itext-5.3.4.jar to create a PDF.
wbviewnews.loadUrl("http://developer.android.com/about/index.html"); // button for create wbpage to image than image to PDF file Button btnclick =(Button)findViewById(R.id.btnclick); btnclick.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Picture p = wbviewnews.capturePicture(); bitmap=null; PictureDrawable pictureDrawable = new PictureDrawable(p); bitmap = Bitmap.createBitmap(pictureDrawable.getIntrinsicWidth(),pictureDrawable.getIntrinsicHeight(), Config.ARGB_8888); //Bitmap bitmap = Bitmap.createBitmap(200,200, Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); canvas.drawPicture(pictureDrawable.getPicture()); ImageView imgdata=(ImageView)findViewById(R.id.imgdata); imgdata.setImageBitmap(bitmap); String filename = "pippo.png"; File sd = Environment.getExternalStorageDirectory(); File dest = new File(sd, filename); String pdffilename = "pippo.pdf"; File pdffilepath = new File(sd, pdffilename); try { FileOutputStream out = new FileOutputStream(dest); bitmap.compress(Bitmap.CompressFormat.PNG, 100, out); out.flush(); out.close(); } catch (Exception e) { e.printStackTrace(); Log.e("Exception", e.toString()); } Document document=new Document(); try { Log.e("pdffilepath", pdffilepath.toString()); PdfWriter writer = PdfWriter.getInstance(document,new FileOutputStream(pdffilepath)); document.open(); // URL url = new URL (Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+filename); // Log.e("url", url.toString()); Image image = Image.getInstance(Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+filename) ; document.add(image); document.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); Log.e("FileNotFoundException", e.toString()); } catch (DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); Log.e("DocumentException", e.toString()); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); Log.e("MalformedURLException", e.toString()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); Log.e("IOException", e.toString()); } } });
source share