我需要生成一个pdf文件并给该文件打印,我该如何在android中使用依赖项
implementation 'org.apache.pdfbox:pdfbox:2.0.0-RC3'
我将pdf适配器编码如下
public class PdfCreateAdapter extends RecyclerView.Adapter<PdfCreateAdapter.MyViewHolder> {
private List<PDFModel> pdfModels;
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_pdf_creation, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
PDFModel model = pdfModels.get(position);
if (model != null) {
if (model.isReceived()) {
holder.mReceivedTV.setVisibility(View.VISIBLE);
} else {
holder.mReceivedTV.setVisibility(View.GONE);
}
holder.mPriceTV.setText(model.getPrice());
holder.mNameTV.setText(model.getName());
int ratingDrawable = getRatingImage(model.getRating());
holder.mRateIM.setImageResource(ratingDrawable);
}
}
@Override
public int getItemCount() {
return pdfModels.size();
}
/**
* This is set from PDFCreateByXML class
* This is my own model. This model have to set data from api
*
* @param pdfModels
*/
public void setListData(List<PDFModel> pdfModels) {
this.pdfModels = pdfModels;
notifyDataSetChanged();
}
/**
* Set rating image
*
* @param rating this is getting from api and set to image by rate point
* @return
*/
private int getRatingImage(float rating) {
int image = 0;
if (rating == 0f) {
image = R.drawable.pdf_star_1;
} else if (rating == 0.5f) {
image = R.drawable.pdf_half_star_2;
} else if (rating == 1f) {
image = R.drawable.pdf_star_2;
} else if (rating == 1.5f) {
image = R.drawable.pdf_half_star_3;
} else if (rating == 2f) {
image = R.drawable.pdf_star_3;
} else if (rating == 2.5f) {
image = R.drawable.pdf_half_star_4;
} else if (rating == 3f) {
image = R.drawable.pdf_star_4;
} else if (rating == 3.5f) {
image = R.drawable.pdf_half_star_5;
} else if (rating == 4f) {
image = R.drawable.pdf_star_5;
} else if (rating == 4.5f) {
image = R.drawable.pdf_half_star_6;
} else if (rating == 5f) {
image = R.drawable.pdf_star_6;
}
return image;
}
public class MyViewHolder extends RecyclerView.ViewHolder {
private final TextView mReceivedTV;
private final TextView mNameTV;
private final ImageView mRateIM;
private final TextView mPriceTV;
public MyViewHolder(View view) {
super(view);
mPriceTV = (TextView) view.findViewById(R.id.tv_price);
mReceivedTV = (TextView) view.findViewById(R.id.tv_received);
mNameTV = (TextView) view.findViewById(R.id.tv_name);
mRateIM = (ImageView) view.findViewById(R.id.iv_rate);
}
}
}
我的pdf模型从xml获取数据
public class PDFModel {
private boolean isPending;
private boolean isReceived;
private String price;
private String name;
private float rating;
public boolean isPending() {
return isPending;
}
public void setPending(boolean pending) {
isPending = pending;
}
public boolean isReceived() {
return isReceived;
}
public void setReceived(boolean received) {
isReceived = received;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getRating() {
return rating;
}
public void setRating(float rating) {
this.rating = rating;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
/**
* Create dummy PDF model
*
* @return PDF Models
*/
public static List<PDFModel> createDummyPdfModel() {
PDFCreationUtils.filePath.clear();
PDFCreationUtils.progressCount = 1;
boolean isFirstReceivedItem = false;
List<PDFModel> pdfModels = new ArrayList<>();
for (int i = 0; i < 110; i++) {
Random rand = new Random();
int price = rand.nextInt((1000 - 200) + 1) + 200;
PDFModel model = new PDFModel();
if (!isFirstReceivedItem) {
model.setReceived(true);
isFirstReceivedItem = true;
} else {
model.setReceived(false);
}
model.setPrice(String.valueOf(price) + String.valueOf(".0 Rs."));
if (i % 4 == 0) {
model.setName("Umesh Kumar " + i);
} else {
model.setName("Ram Kumar " + i);
}
model.setRating(i % 3);
pdfModels.add(model);
}
return pdfModels;
}
}
正在执行的代码适用于板条箱pdf
这是pdfcrattionacivity.java
public class PdfCreationActivity extends AppCompatActivity {
private boolean IS_MANY_PDF_FILE;
/**
* This is identify to number of pdf file. If pdf model list size > sector so we have create many file. After that we have merge all pdf file into one pdf file
*/
private int SECTOR = 100; // Default value for one pdf file.
private int START;
private int END = SECTOR;
private int NO_OF_PDF_FILE = 1;
private int NO_OF_FILE;
private int LIST_SIZE;
private ProgressDialog progressDialog;
/**
* Store all dummy PDF models
*/
private List<PDFModel> pdfModels;
private TextView btnPdfPath;
/**
* Share PDF file
*/
private Button btnSharePdfFile;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pdf_creation);
btnSharePdfFile = (Button) findViewById(R.id.btn_share_pdf);
btnPdfPath = (TextView) findViewById(R.id.btn_pdf_path);
findViewById(R.id.btn_create_pdf).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
requestPermission();
}
});
pdfModels = PDFModel.createDummyPdfModel();
RecyclerView rvShowDemo = (RecyclerView) findViewById(R.id.rv_show_demo);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(this);
rvShowDemo.setLayoutManager(mLayoutManager);
PdfCreateAdapter pdfRootAdapter = new PdfCreateAdapter();
pdfRootAdapter.setListData(pdfModels);
rvShowDemo.setAdapter(pdfRootAdapter);
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (requestCode == 111 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
generatePdfReport();
}
}
private void requestPermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, 111);
} else {
generatePdfReport();
}
}
/**
* This is manage to all model
*/
private void generatePdfReport() {
// NO_OF_FILE : This is identify to one file or many file have to created
LIST_SIZE = pdfModels.size();
NO_OF_FILE = LIST_SIZE / SECTOR;
if (LIST_SIZE % SECTOR != 0) {
NO_OF_FILE++;
}
if (LIST_SIZE > SECTOR) {
IS_MANY_PDF_FILE = true;
} else {
END = LIST_SIZE;
}
createPDFFile();
}
private void createProgressBarForPDFCreation(int maxProgress) {
progressDialog = new ProgressDialog(this);
progressDialog.setMessage(String.format(getString(R.string.msg_progress_pdf), String.valueOf(maxProgress)));
progressDialog.setCancelable(false);
progressDialog.setIndeterminate(false);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setMax(maxProgress);
progressDialog.show();
}
private void createProgressBarForMergePDF() {
progressDialog = new ProgressDialog(this);
progressDialog.setMessage(getString(R.string.msg_progress_merger_pdf));
progressDialog.setCancelable(false);
progressDialog.show();
}
/**
* This function call with recursion
* This recursion depend on number of file (NO_OF_PDF_FILE)
*/
private void createPDFFile() {
// Find sub list for per pdf file data
List<PDFModel> pdfDataList = pdfModels.subList(START, END);
PdfBitmapCache.clearMemory();
PdfBitmapCache.initBitmapCache(getApplicationContext());
final PDFCreationUtils pdfCreationUtils = new PDFCreationUtils(PdfCreationActivity.this, pdfDataList, LIST_SIZE, NO_OF_PDF_FILE);
if (NO_OF_PDF_FILE == 1) {
createProgressBarForPDFCreation(PDFCreationUtils.TOTAL_PROGRESS_BAR);
}
pdfCreationUtils.createPDF(new PDFCreationUtils.PDFCallback() {
@Override
public void onProgress(final int i) {
progressDialog.setProgress(i);
}
@Override
public void onCreateEveryPdfFile() {
// Execute may pdf files and this is depend on NO_OF_FILE
if (IS_MANY_PDF_FILE) {
NO_OF_PDF_FILE++;
if (NO_OF_FILE == NO_OF_PDF_FILE - 1) {
progressDialog.dismiss();
createProgressBarForMergePDF();
pdfCreationUtils.downloadAndCombinePDFs();
} else {
// This is identify to manage sub list of current pdf model list data with START and END
START = END;
if (LIST_SIZE % SECTOR != 0) {
if (NO_OF_FILE == NO_OF_PDF_FILE) {
END = (START - SECTOR) + LIST_SIZE % SECTOR;
}
}
END = SECTOR + END;
createPDFFile();
}
} else {
// Merge one pdf file when all file is downloaded
progressDialog.dismiss();
createProgressBarForMergePDF();
pdfCreationUtils.downloadAndCombinePDFs();
}
}
@Override
public void onComplete(final String filePath) {
progressDialog.dismiss();
if (filePath != null) {
btnPdfPath.setVisibility(View.VISIBLE);
btnPdfPath.setText("PDF path : " + filePath);
Toast.makeText(PdfCreationActivity.this, "pdf file " + filePath, Toast.LENGTH_LONG).show();
btnSharePdfFile.setVisibility(View.VISIBLE);
btnSharePdfFile.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sharePdf(filePath);
}
});
}
}
@Override
public void onError(Exception e) {
Toast.makeText(PdfCreationActivity.this, "Error " + e.getMessage(), Toast.LENGTH_LONG).show();
}
});
}
private void sharePdf(String fileName) {
final Intent emailIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
emailIntent.setType("text/plain");
emailIntent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
ArrayList<Uri> uris = new ArrayList<Uri>();
File fileIn = new File(fileName);
Uri u = FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID, fileIn);
uris.add(u);
emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
try {
startActivity(Intent.createChooser(emailIntent, getString(R.string.send_to)));
} catch (ActivityNotFoundException e) {
Toast.makeText(this, getString(R.string.error_file), Toast.LENGTH_SHORT).show();
}
}
}
我无法理解我该如何发出命令以将生成的PDF打印到打印机
答案 0 :(得分:1)
您需要连接到打印机,然后将PDDocument
发送到连接的打印机以进行打印。遵循以下内容应该可以。
public static PrintService choosePrinter() {
PrinterJob printJob = PrinterJob.getPrinterJob();
if(printJob.printDialog()) {
return printJob.getPrintService();
}
else {
return null;
}
}
public static void printPDF(String fileName, PrintService printer)
throws IOException, PrinterException {
PrinterJob job = PrinterJob.getPrinterJob();
job.setPrintService(printer);
PDDocument doc = PDDocument.load(fileName);
doc.silentPrint(job);
}
此SO answer引用的代码。