gpt4 book ai didi

android - 在 Android 中将 ListView 转为 pdf

转载 作者:太空狗 更新时间:2023-10-29 16:26:41 25 4
gpt4 key购买 nike

我有一个自定义 ListView ,我想从整个 ListView 制作 pdf。我引用了很多帖子并实现了下面的代码,将我的 listview 转换为 pdf。但问题是它不包含整个 ListView 项目。 pdf 中只有前几项可用。

我将 listview 转换为 pdf 的函数是

fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

String state = Environment.getExternalStorageState();
if (!Environment.MEDIA_MOUNTED.equals(state)) {

}
File pdfDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOCUMENTS), "MyProject");
if (!pdfDir.exists()){
pdfDir.mkdir();
}
Bitmap screen = getWholeListViewItemsToBitmap();
File pdfFile = new File(pdfDir, "myPdfFile_new.pdf");

try {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(pdfFile));
document.open();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
screen.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
addImage(document,byteArray);
document.close();
}
catch (Exception e){
e.printStackTrace();
}
}
});

添加图片方法

private static void addImage(Document document,byte[] byteArray)
{
Image image = null;
try
{
image = Image.getInstance(byteArray);
}
catch (BadElementException e)
{
e.printStackTrace();
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
try
{
document.add(image);
} catch (DocumentException e) {
e.printStackTrace();
}
}

获取列表项的方法

public Bitmap getWholeListViewItemsToBitmap() {

ListView listview = StockReportActivity.category_list;
ListAdapter adapter = listview.getAdapter();
int itemscount = adapter.getCount();
int allitemsheight = 0;
List<Bitmap> bmps = new ArrayList<Bitmap>();

for (int i = 0; i < itemscount; i++) {

View childView = adapter.getView(i, null, listview);
childView.measure(View.MeasureSpec.makeMeasureSpec(listview.getWidth(), View.MeasureSpec.EXACTLY),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));

childView.layout(0, 0, childView.getMeasuredWidth(), childView.getMeasuredHeight());
childView.setDrawingCacheEnabled(true);
childView.buildDrawingCache();
bmps.add(childView.getDrawingCache());
allitemsheight+=childView.getMeasuredHeight();
}
Bitmap bigbitmap = Bitmap.createBitmap(listview.getMeasuredWidth(), allitemsheight, Bitmap.Config.ARGB_8888);
Canvas bigcanvas = new Canvas(bigbitmap);
Paint paint = new Paint();
int iHeight = 0;
for (int i = 0; i < bmps.size(); i++) {
Bitmap bmp = bmps.get(i);
bigcanvas.drawBitmap(bmp, 0, iHeight, paint);
iHeight+=bmp.getHeight();
bmp.recycle();
bmp=null;
}
return bigbitmap;
}

我的 ListView 中包含大量项目,因此如何将整个 ListView 转换为 pdf。感谢您的所有建议

最佳答案

您可以在 recyclerView 中获取屏幕上显示的项目数,因此为第一组 View 创建位图,之后您可以动态滚动到最后显示的项目下方的项目(即 noOfItemDisplayed+1),再次获取位图,同样将所有位图都获取到 ArrayList<Bitmap>您可以从中创建 pdf 文件,请参阅下面的代码以从位图的数组列表创建 pdf:

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private void createPdf() throws IOException {

PdfDocument document = new PdfDocument();
PdfDocument.Page page = null;
// crate a page description
for (int i = 0; i < bitmaps.size(); i++) {
Bitmap bitmap = bitmaps.get(i);
PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(1400, 1979, i).create();

// start a page
page = document.startPage(pageInfo);
if (page == null) {
return;
}
Canvas canvas = page.getCanvas();
canvas.drawBitmap(bitmap, 0, 0, null);
document.finishPage(page);
}

// finish the page


// write the document content
fileHandler = new FileHandler(this, getString(R.string.app_name));
File pdf = fileHandler.getNewFileToWrite(AppConstants.FileExtensions.PDF); //crete and get file

try {
document.writeTo(new FileOutputStream(pdf));
} catch (IOException e) {
logger.error(e);
}

// close the document
document.close();

}

关于android - 在 Android 中将 ListView 转为 pdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48924544/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com