嗨,我正尝试使用iText库从Recyclerview创建PDF输出文件。经过数小时 的努力,我能够从recylerview创建PDF。
以下是我用来创建PDF的类
Codes from Main Class
private void getPrint() {
ArrayList<View> 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<T, VM extends ViewDataBinding> extends RecyclerView.Adapter<RecyclerAdapter.RecyclerViewHolder> {
private final Context context;
private ArrayList<T> items;
private int layoutId;
private RecyclerCallback<VM, T> bindingInterface;
private static ArrayList<View> mPrintView = new ArrayList<>();
public RecyclerAdapter(Context context, ArrayList<T> items, int layoutId, RecyclerCallback<VM, T> 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<View> getPrintView() {
return mPrintView;
}
}
我正在使用一个名为mPrintView的Arraylist将视图保存在RecylerView中。当用户多次向上滚动recylerview和DOWM时
,出现问题,即ArrayList中的数据重复
任何帮助表示赞赏
经过数小时的反复无常,我找到了答案。我正在分享代码段,因为这可能对其他人有所帮助
public void generatePDF(RecyclerView view) {
RecyclerView.Adapter adapter = view.getAdapter();
Bitmap bigBitmap = null;
if (adapter != null) {
int size = adapter.getItemCount();
int height = 0;
Paint paint = new Paint();
int iHeight = 0;
final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
// Use 1/8th of the available memory for this memory cache.
final int cacheSize = maxMemory / 8;
LruCache<String, Bitmap> bitmaCache = new LruCache<>(cacheSize);
for (int i = 0; i < size; i++) {
RecyclerView.ViewHolder holder = adapter.createViewHolder(view, adapter.getItemViewType(i));
adapter.onBindViewHolder(holder, i);
holder.itemView.measure(View.MeasureSpec.makeMeasureSpec(view.getWidth(), View.MeasureSpec.EXACTLY),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
holder.itemView.layout(0, 0, holder.itemView.getMeasuredWidth(), holder.itemView.getMeasuredHeight());
holder.itemView.setDrawingCacheEnabled(true);
holder.itemView.buildDrawingCache();
Bitmap drawingCache = holder.itemView.getDrawingCache();
if (drawingCache != null) {
bitmaCache.put(String.valueOf(i), drawingCache);
}
height += holder.itemView.getMeasuredHeight();
}
bigBitmap = Bitmap.createBitmap(view.getMeasuredWidth(), height, Bitmap.Config.ARGB_8888);
Canvas bigCanvas = new Canvas(bigBitmap);
bigCanvas.drawColor(Color.WHITE);
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 i = 0; i < size; i++) {
try {
//Adding the content to the document
Bitmap bmp = bitmaCache.get(String.valueOf(i));
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
Image image = Image.getInstance(stream.toByteArray());
float scaler = ((document.getPageSize().getWidth() - document.leftMargin()
- document.rightMargin() - 0) / image.getWidth()) * 100; // 0 means you have no indentation. If you have any, change it.
image.scalePercent(scaler);
image.setAlignment(com.itextpdf.text.Image.ALIGN_CENTER | com.itextpdf.text.Image.ALIGN_TOP);
if (!document.isOpen()) {
document.open();
}
document.add(image);
} catch (Exception ex) {
Log.e("TAG-ORDER PRINT ERROR", ex.getMessage());
}
}
if (document.isOpen()) {
document.close();
}
// Set on UI Thread
runOnUiThread(new Runnable() {
@Override
public void run() {
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();
}
});
}
}
我在Android日食中使用 itext 创建 pdf 时遇到问题 如果给定了引导入口,我无法创建pdf,在我的情况下是Android 5.0.1。如果我删除了bootstrap条目,我可以创建pdf,但不能启动活动,因为android 5.0.1包含android jar。你能告诉我如何解决这个问题吗?同样,如果我把它变成一个非活动类,我希望能够在另一个活动类中创建一个非活动类的对象,这样我就可
null 有人能解释一下,当我使用PdfReader阅读模板后,我如何制作模板的副本吗?有没有办法把表格写到模板副本上,而不是一个新文档上? 为了将来的参考,我做了以下工作:
首先,打开Android Studio并选择Create new Project,然后它会让你输入一个名字,你可以任意取一个名字,比如:Weather App。然后你需要输入公司域名。如果你不会真正发布这个app,这个字段就不是特别重要了,但是如果你有的话可以使用自己的域名。然后任意选择一个目录作为这个项目的保存地址。 下一步,它会让你选择最小的API版本。我们选择API 15,因为我们有一个库需
我正在开发一个web应用程序,并试图使用它的文本创建一个pdf文档。 当我得到一个路径(本地和服务器上)时,它工作得很好,但我知道我需要在不实际创建它的情况下完成它。 我不想要的:PdfCopy copy=newpdfcopy(documentPDF,newfileoutputstream(mypath)) 我想要的是://PdfCopy copy=new PdfCopy(documentPDF,
问题内容: 我们正在寻找替代方法,以替代当前通过小程序在Java Web应用程序中打印支票的方式。似乎共识是使用PDF进行打印,而itext提供了使用Java进行打印的功能。 但是 ,在我们的特殊情况下,支票是“仅打印”的,这一点很重要- 用户在应用程序中不应该具有保存支票的任何能力(我知道精明的用户可以做一个PrintScreen,但我们想遮盖住后盖,在应用程序中不进行任何本机功能来保存支票)。
问题内容: 我使用itext将pdf转换为文本文件,它实际上工作良好,但是对于某些词它可以完成以下操作:例如,在pdf中,有诸如“ present the main idea”之类的短语,但是itext创建了诸如“ presentthemainideas”之类的输出。无论如何,有没有纠正这种行为? 问题答案: 缺少空格字符的原因是,您在渲染的PDF中看到的空格不一定与PDF页面内容描述中的空格相对