itext库下载android pdf,在Android中从Recyclerview(内部的整个项目)使用iText创建PDF文件?...

嗨,我正尝试使用iText库从Recyclerview创建PDF输出文件。经过数小时 的努力,我能够从recylerview创建PDF。

以下是我用来创建PDF的类

Codes from Main Class

private void getPrint() {

ArrayList viewArrayList = mAdapter.getPrintView(); // A function from Adapter class which returns ArrayList of VIEWS

Document document = new Document(PageSize.A4);

final File file = new File(getStorageDir("PDF"), "print.pdf");

try {

PdfWriter.getInstance(document, new FileOutputStream(file));

} catch (DocumentException | FileNotFoundException e) {

e.printStackTrace();

}

for (int im = 0; im < viewArrayList.size(); im++) {

// Iterate till the last of the array list and add each view individually to the document.

try {

viewArrayList.get(im).buildDrawingCache(); //Adding the content to the document

Bitmap bmp = viewArrayList.get(im).getDrawingCache();

ByteArrayOutputStream stream = new ByteArrayOutputStream();

bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);

Image image = Image.getInstance(stream.toByteArray());

image.scalePercent(70);

image.setAlignment(Image.MIDDLE);

if (!document.isOpen()) {

document.open();

}

document.add(image);

} catch (Exception ex) {

Log.e("TAG-ORDER PRINT ERROR", ex.getMessage());

}

}

if (document.isOpen()) {

document.close();

}

AlertDialog.Builder builder = new AlertDialog.Builder(Index.this);

builder.setTitle("Success")

.setMessage("PDF File Generated Successfully.")

.setIcon(android.R.drawable.ic_dialog_alert)

.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int whichButton) {

Intent intent = new Intent(Intent.ACTION_VIEW);

intent.setDataAndType(Uri.fromFile(file), "application/pdf");

intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

startActivity(intent);

}

}).show();

}

Common RecyclerView Adapter Class

public class RecyclerAdapter extends RecyclerView.Adapter {

private final Context context;

private ArrayList items;

private int layoutId;

private RecyclerCallback bindingInterface;

private static ArrayList mPrintView = new ArrayList<>();

public RecyclerAdapter(Context context, ArrayList items, int layoutId, RecyclerCallback bindingInterface) {

this.items = items;

this.context = context;

this.layoutId = layoutId;

this.bindingInterface = bindingInterface;

}

public class RecyclerViewHolder extends RecyclerView.ViewHolder {

VM binding;

public RecyclerViewHolder(View view) {

super(view);

binding = DataBindingUtil.bind(view);

}

public void bindData(T model) {

bindingInterface.bindData(binding, model);

binding.executePendingBindings();

}

}

@Override

public RecyclerViewHolder onCreateViewHolder(ViewGroup parent,

int viewType) {

View v = LayoutInflater.from(parent.getContext())

.inflate(layoutId, parent, false);

return new RecyclerViewHolder(v);

}

@Override

public void onBindViewHolder(RecyclerAdapter.RecyclerViewHolder holder, int position) {

T item = items.get(position);

Log.e("PRINT ", holder.binding.getRoot().getId() + "");

mPrintView.add(holder.binding.getRoot());

holder.bindData(item);

}

@Override

public int getItemCount() {

if (items == null) {

return 0;

}

return items.size();

}

public static ArrayList getPrintView() {

return mPrintView;

}

}

我正在使用一个名为mPrintView的Arraylist将视图保存在RecylerView中。当用户多次向上滚动recylerview和DOWM时

,出现问题,即ArrayList中的数据重复

任何帮助表示赞赏

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值