如何解决我如何将android的textviews中的值导出为pdf?我已经尝试了一些方法,但是就像获取布局的屏幕截图并将其另存为pdf一样
我正在尝试使用android studio开发一个android应用,其中我需要将textview中的文本导出到PDF文档。如果文本较长,PDF文档应自动增加页数。我希望你能理解我...
实现此目标的最佳方法是什么?
有内置库吗? 或者我该怎么办?
这是我尝试过的:
activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:orientation="vertical">
<Button
android:id="@+id/btn_generate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Generate PDF" />
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:id="@+id/ll_pdflayout"
android:background="#ffffff"
android:layout_height="match_parent">
<ImageView
android:id="@+id/iv_image"
android:src="@drawable/image"
android:layout_width="300dp"
android:scaleType="fitXY"
android:layout_marginTop="10dp"
android:layout_gravity="center"
android:layout_height="250dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14dp"
android:text="Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries,but also the leap into electronic typesetting,remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages,and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."
android:layout_marginTop="10dp"
android:gravity="center"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textColor="#000000"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv_link"
android:textSize="10dp"
android:textColor="#000000"/>
</LinearLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
Button btn_generate;
TextView tv_link;
ImageView iv_image;
LinearLayout ll_pdflayout;
public static int REQUEST_PERMISSIONS = 1;
boolean boolean_permission;
boolean boolean_save;
Bitmap bitmap;
ProgressDialog progressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
fn_permission();
listener();
}
private void init(){
btn_generate = (Button)findViewById(R.id.btn_generate);
tv_link = (TextView)findViewById(R.id.tv_link);
iv_image = (ImageView) findViewById(R.id.iv_image);
ll_pdflayout = (LinearLayout) findViewById(R.id.ll_pdflayout);
}
private void listener(){
btn_generate.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn_generate:
if (boolean_save) {
Intent intent = new Intent(getApplicationContext(),PDFViewActivity.class);
startActivity(intent);
} else {
if (boolean_permission) {
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setMessage("Please wait");
bitmap = loadBitmapFromView(ll_pdflayout,ll_pdflayout.getWidth(),ll_pdflayout.getHeight());
createPdf();
} 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;
PdfDocument document = new PdfDocument();
PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(convertWidth,convertHighet,1).create();
PdfDocument.Page page = document.startPage(pageInfo);
Canvas canvas = page.getCanvas();
Paint paint = new Paint();
canvas.drawPaint(paint);
bitmap = Bitmap.createScaledBitmap(bitmap,convertWidth,true);
paint.setColor(Color.BLUE);
canvas.drawBitmap(bitmap,null);
document.finishPage(page);
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString() + "/PdfTett/";
File dir = new File(path);
if (!dir.exists())
dir.mkdirs();
File filePath = new File(dir,"Testtt.pdf");
try {
document.writeTo(new FileOutputStream(filePath));
btn_generate.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();
}
public static Bitmap loadBitmapFromView(View v,int width,int height) {
Bitmap b = Bitmap.createBitmap(width,height,Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
v.draw(c);
return b;
}
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();
}
}
}
}
这是@Deepshikha Puri在此post
中的回答解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com(将#修改为@)