Android:如何创建PDF发票系统

时间:2018-01-04 12:30:01

标签: java android invoice

如何为我的Android应用程序创建发票系统并将其另存为pdf?是否有用于创建发票的库?

1 个答案:

答案 0 :(得分:-2)

请在下面找到创建pdf文件的代码。

public void createPDF(String Data)
    {
        Document doc = new Document();


         try {
                String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/droidText";

                File dir = new File(path);
                    if(!dir.exists())
                        dir.mkdirs();

                Log.d("PDFCreator", "PDF Path: " + path);


                File file = new File(dir, "sample.pdf");
                FileOutputStream fOut = new FileOutputStream(file);

                PdfWriter.getInstance(doc, fOut);

                //open the document
                doc.open();


                Paragraph p1 = new Paragraph(Data);
                Font paraFont= new Font(Font.COURIER);
                p1.setAlignment(Paragraph.ALIGN_CENTER);
                p1.setFont(paraFont);

                 //add paragraph to document    
                 doc.add(p1);

                 ByteArrayOutputStream stream = new ByteArrayOutputStream();
                 Bitmap bitmap = BitmapFactory.decodeResource(getBaseContext().getResources(), R.drawable.android);
                 bitmap.compress(Bitmap.CompressFormat.JPEG, 100 , stream);
                 Image myImg = Image.getInstance(stream.toByteArray());
                 myImg.setAlignment(Image.MIDDLE);

                 //add image to document
                 doc.add(myImg);

                 //set footer
                 Phrase footerText = new Phrase("This is an example of a footer");
                 HeaderFooter pdfFooter = new HeaderFooter(footerText, false);
                 doc.setFooter(pdfFooter);



         } catch (DocumentException de) {
                 Log.e("PDFCreator", "DocumentException:" + de);
         } catch (IOException e) {
                 Log.e("PDFCreator", "ioException:" + e);
         } 
         finally
         {
                 doc.close();
         }

    }  

您可以传递数据并存储pdf文件。