我有一个带有滚动视图的几个相对布局的布局,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
android:id="@+id/root"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<RelativeLayout
android:id="@+id/relativeRoot"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/mainr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAlignment="center">
<RelativeLayout
android:gravity="center"
android:id="@+id/business_details"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/business_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_gravity="center"
android:layout_marginLeft="88dp"
android:layout_marginStart="88dp"
android:padding="2dp"
android:textStyle="bold"
android:text=" Business Name "
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="16sp" />
<TextView
android:id="@+id/business_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/business_name"
android:layout_alignStart="@+id/business_name"
android:layout_below="@+id/business_name"
android:layout_gravity="center"
android:padding="2dp"
android:text=" Business Address "
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="16sp" />
<TextView
android:id="@+id/business_contact"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/business_name"
android:layout_alignStart="@+id/business_name"
android:layout_below="@+id/business_add"
android:layout_gravity="center"
android:padding="2dp"
android:text=" Business contactNo "
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="16sp" />
<View
android:layout_below="@+id/business_contact"
android:layout_width="match_parent"
android:layout_height="3dp"
android:background="@android:color/black"/>
</RelativeLayout>
<RelativeLayout
android:id="@+id/customer_details"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/business_details"
android:gravity="center">
<TextView
android:id="@+id/gstuin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="GSTIN :"
android:textStyle="bold" />
<TextView
android:id="@+id/gstinvalue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="@+id/datev"
android:layout_alignParentTop="true"
android:layout_alignRight="@+id/datev"
android:text="xyz1234566" />
<TextView
<TextView
android:id="@+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/bill"
android:text="Bill date:"
android:textStyle="bold" />
<TextView
android:id="@+id/datev"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/date"
android:layout_alignBottom="@+id/date"
android:layout_alignLeft="@+id/customerNamev"
android:layout_alignStart="@+id/customerNamev"
android:text="20-12-2017" />
<View
android:id="@+id/view"
android:layout_width="match_parent"
android:layout_height="3dp"
android:layout_below="@+id/customerName"
android:background="@android:color/black" />
</RelativeLayout>
<LinearLayout
android:layout_below="@+id/customer_details"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/linearLayout">
<TextView
android:id="@+id/product_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="2dp"
android:text=" Product Name "
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="@+id/gst"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:padding="2dp"
android:textStyle="bold"
android:text=" gst% "
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="16sp" />
<TextView
android:id="@+id/total"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:padding="2dp"
android:text=" Total "
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="16sp"
android:textStyle="bold" />
</LinearLayout>
<View
android:id="@+id/v2"
android:layout_below="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="3dp"
android:background="@android:color/black"/>
<RelativeLayout
android:id="@+id/r2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/v2"
android:background="@color/colorAccent"
android:textAlignment="center">
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="1dp"
android:background="@color/white">
</ListView>
<View
android:id="@+id/v3"
android:layout_below="@+id/list"
android:layout_width="match_parent"
android:layout_height="3dp"
android:background="@android:color/black"/>
</RelativeLayout>
<RelativeLayout
android:id="@+id/r3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/r2">
<TextView
android:id="@+id/netTotal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="Net Total :" />
<View
android:id="@+id/v4"
android:layout_below="@+id/netTotalv"
android:layout_width="match_parent"
android:layout_height="3dp"
</RelativeLayout>
</RelativeLayout>
<Button
android:id="@+id/print"
android:background="@null"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="12dp"
android:layout_marginStart="12dp"
android:text="Print" />
</RelativeLayout>
</ScrollView>
基本上这是一张发票。 现在我正在尝试将整个布局转换为位图 图像,然后尝试将此图像转换为pdf。
但是我的pdf结果只显示了列表视图的某些部分。
这是我的java代码:
public class InvoicePreview extends AppCompatActivity {
String str,customerName,email,phone,zipcode,comment,invoiceTotal,loginId;
List<ItemModel> itemsList = new ArrayList<ItemModel>();
ItemAdapter aAdpt;
ListView listView;
Button print,download,cancel;
RelativeLayout main,relativeroot;
WebView mWebView;
ScrollView root;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.invoice_preview);
listView=(ListView)findViewById(R.id.list);
print = (Button)findViewById(R.id.print);
download = (Button)findViewById(R.id.down);
cancel = (Button)findViewById(R.id.cancel);
main = (RelativeLayout)findViewById(R.id.relativeRoot);
aAdpt = new ItemAdapter(itemsList, this);
listView.setAdapter(aAdpt);
Intent intent = getIntent();
str = intent.getExtras().getString("json");
Log.d("json preview",str);
Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
try {
jsonparse();
} catch (JSONException e) {
e.printStackTrace();
}
print.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
Toast.makeText(InvoicePreview.this, "clicked", Toast.LENGTH_SHORT).show();
createPdf();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
private void createPdf() throws IOException, DocumentException {
File pdfFolder = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOCUMENTS), "pdfdemo");
if (!pdfFolder.exists()) {
pdfFolder.mkdir();
// Log.i(LOG_TAG, "Pdf Directory created");
}
//Create time stamp
Date date = new Date() ;
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(date);
File myFile = new File(pdfFolder + timeStamp + ".pdf");
OutputStream output = new FileOutputStream(myFile);
//Step 1
Rectangle pagesize = new Rectangle(4000f, 14000f);
Document document = new Document(pagesize, 36f, 7f, 10f, 18f);
//Step 2
PdfWriter.getInstance(document, output);
//Step 3
document.open();
// for bitmap
main.setDrawingCacheEnabled(true);
main.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
main.layout(0, 0, main.getMeasuredWidth(), main.getMeasuredHeight());
main.buildDrawingCache(true);
Bitmap b = Bitmap.createBitmap(main.getDrawingCache());
main.setDrawingCacheEnabled(false);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
Image myImg = Image.getInstance(bytes.toByteArray());
// document.setPageSize(myImg);
// document.newPage();
// myImg.scaleToFit(PageSize.A4.getWidth(), PageSize.A4.getHeight());
document.add(myImg);
//Step 4 Add content
// document.add(new Paragraph("test doc"));
// document.add(new Paragraph(mBodyEditText.getText().toString()));
//Step 5: Close the document
document.close();
// Image img = Image.getInstance(IMAGES[0]);
// Document document = new Document(img);
// PdfWriter.getInstance(document, new FileOutputStream(dest));
// document.open();
//
// document.close();
// Intent intent = new Intent(Intent.ACTION_VIEW);
// intent.setDataAndType(Uri.fromFile(myFile), "application/pdf");
// intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
// startActivity(intent);
}
}
我正在网上尝试各种方法,但仍面临整个布局的相同问题 没有以pdf格式显示。如何解决这个问题?(如果缺少任何必要的信息,请询问)。
答案 0 :(得分:0)
尝试获取视图的完整高度,以便可以将完整的滚动视图包含到pdf转换中。 为此,请添加以下代码
Image myImg = Image.getInstance(bytes.toByteArray());
float scaler = ((document.getPageSize().getHeight() - document.topMargin() - document.bottomMargin() - 0) / myImg.getHeight()) * 100; // getting height of your scroll view
myImg.scalePercent(scaler);
myImg.setAlignment(Image.ALIGN_TOP | Image.ALIGN_CENTER | Image.ALIGN_BOTTOM);
document.add(myImg);
这将使您完全看到A4尺寸的pdf文件。
答案 1 :(得分:0)
您可以使用此 library 使其在几行代码内轻松完成 -
PdfGenerator.getBuilder()
.setContext(context)
.fromViewIDSource()
.fromViewID(activity,R.id.fulldoc) //The specifi view id you want to print
.setDefaultPageSize(PdfGenerator.PageSize.A4)
.setFileName("Allotment_PaperPDF")
.setFolderName("TCBMobileApp")
.build(new PdfGeneratorListener() {
@Override
public void onFailure(FailureResponse failureResponse) {
super.onFailure(failureResponse);
}
@Override
public void onStartPDFGeneration() {
/*When PDF generation begins to start*/
}
@Override
public void onFinishPDFGeneration() {
/*When PDF generation is finished*/
}
@Override
public void showLog(String log) {
super.showLog(log);
}
@Override
public void onSuccess(SuccessResponse response) {
super.onSuccess(response);
}
});