回答
我想你是使用iText庫將文本轉換爲pdf。使用它將圖像轉換爲pdf。從這裏
import java.io.*;
import com.lowagie.text.*;
import com.lowagie.text.pdf.*;
public class imagesPDF
{
public static void main(String arg[])throws Exception
{
Document document=new Document();
PdfWriter.getInstance(document,new FileOutputStream("YourPDFHere.pdf"));
document.open();
Image image = Image.getInstance ("yourImageHere.jpg");
document.add(new Paragraph("Your Heading for the Image Goes Here"));
document.add(image);
document.close();
}
}
嗨Ashok,進口com.lowagie.text.pdf。*在android中不可用。我們可以在Android外部添加這個庫嗎?我在哪裏可以得到這個圖書館? – 2011-03-22 13:14:05
是的,你必須添加一個Lowagie iText庫到Android。您可以從http://sourceforge.net/projects/itext/files/下載最新版本的iText。只需將其包含在項目的lib中,將其添加到您的構建路徑中,並將其全部設置爲可供您破解。 – 2011-03-22 15:19:59
嗨阿肖克,我已經解決了我的問題。非常感謝你... – 2011-03-28 07:03:50
下載源代碼(Convert Image to pdf in android programattically)
MainActivity.java
package com.deepshikha.convertbitmap;
import android.Manifest;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.pdf.PdfDocument;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
public static final int GALLERY_PICTURE = 1;
Button btn_select, btn_convert;
ImageView iv_image;
boolean boolean_permission;
boolean boolean_save;
Bitmap bitmap;
public static final int REQUEST_PERMISSIONS = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
listener();
fn_permission();
}
private void init() {
btn_select = (Button) findViewById(R.id.btn_select);
btn_convert = (Button) findViewById(R.id.btn_convert);
iv_image = (ImageView) findViewById(R.id.iv_image);
}
private void listener() {
btn_select.setOnClickListener(this);
btn_convert.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn_select:
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, GALLERY_PICTURE);
break;
case R.id.btn_convert:
if (boolean_save){
Intent intent1=new Intent(getApplicationContext(),PDFViewActivity.class);
startActivity(intent1);
}else {
createPdf();
}
break;
}
}
private void createPdf(){
WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
DisplayMetrics displaymetrics = new DisplayMetrics();
this.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
float hight = displaymetrics.heightPixels ;
float width = displaymetrics.widthPixels ;
int convertHighet = (int) hight, convertWidth = (int) width;
// Resources mResources = getResources();
// Bitmap bitmap = BitmapFactory.decodeResource(mResources, R.drawable.screenshot);
PdfDocument document = new PdfDocument();
PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(bitmap.getWidth(), bitmap.getHeight(), 1).create();
PdfDocument.Page page = document.startPage(pageInfo);
Canvas canvas = page.getCanvas();
Paint paint = new Paint();
paint.setColor(Color.parseColor("#ffffff"));
canvas.drawPaint(paint);
bitmap = Bitmap.createScaledBitmap(bitmap, bitmap.getWidth(), bitmap.getHeight(), true);
paint.setColor(Color.BLUE);
canvas.drawBitmap(bitmap, 0, 0 , null);
document.finishPage(page);
// write the document content
String targetPdf = "/sdcard/test.pdf";
File filePath = new File(targetPdf);
try {
document.writeTo(new FileOutputStream(filePath));
btn_convert.setText("Check PDF");
boolean_save=true;
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(this, "Something wrong: " + e.toString(), Toast.LENGTH_LONG).show();
}
// close the document
document.close();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == GALLERY_PICTURE && resultCode == RESULT_OK) {
if (resultCode == RESULT_OK) {
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(
selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
cursor.close();
bitmap = BitmapFactory.decodeFile(filePath);
iv_image.setImageBitmap(bitmap);
btn_convert.setClickable(true);
}
}
}
private void fn_permission() {
if ((ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)||
(ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)) {
if ((ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, android.Manifest.permission.READ_EXTERNAL_STORAGE))) {
} else {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{android.Manifest.permission.READ_EXTERNAL_STORAGE},
REQUEST_PERMISSIONS);
}
if ((ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE))) {
} else {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
REQUEST_PERMISSIONS);
}
} else {
boolean_permission = true;
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REQUEST_PERMISSIONS) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
boolean_permission = true;
} else {
Toast.makeText(getApplicationContext(), "Please allow the permission", Toast.LENGTH_LONG).show();
}
}
}
}
謝謝!
- 1. android-將pdf轉換爲圖像
- 2. 將PDF轉換爲圖像
- 3. 將PDF轉換爲php中的圖像
- 4. 如何在Android中將圖像轉換爲pdf?
- 5. 是否有可能將圖像轉換爲PDF在android中
- 6. 如何在Android中將圖像轉換爲PDF
- 7. 將閃存圖轉換爲圖像/ pdf
- 8. 做android PDF查看器將PDF轉換爲圖像嗎?
- 9. pdfBox將pdf轉換爲透明圖像
- 10. pdfkit不將圖像轉換爲pdf
- 11. 將PDF轉換爲圖像序列C#
- 12. 將掃描PDF轉換爲圖像
- 13. 庫將PDF轉換爲圖像
- 14. 將兩個圖像轉換爲pdf
- 15. 安卓:將PDF轉換爲圖像
- 16. 將多頁PDF轉換爲多圖像
- 17. 使用ImageMagick將PDF轉換爲圖像
- 18. pdfbox將圖像轉換爲pdf文件
- 19. 將pdf和word轉換爲圖像
- 20. 如何將PDF轉換爲圖像?
- 21. Pdfbox1.8.12將PDF轉換爲白頁圖像
- 22. 將PDF轉換爲TBitmap圖像
- 23. 將網頁轉換爲PDF或圖像
- 24. 將PDF轉換爲圖像批量
- 25. 將EPL圖像轉換爲PDF?
- 26. PHP將PDF轉換爲圖像-dUseCropBox
- 27. 將圖像轉換爲PDF php
- 28. pdfbox將pdf轉換爲圖像byte []
- 29. 如何將PDF轉換爲圖像?
- 30. 如何將圖像文件轉換爲Android中的pdf文件
[如何使用android繪圖命令生成PDF?](http://stackoverflow.com/questions/5292590/how-to-generate-a-pdf-using-android-drawing-commands) – CommonsWare 2011-03-22 11:16:29