Android:如何创建PDF发票系统[关闭]

问题描述 投票:-6回答:1

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

java android invoice
1个回答
-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文件。

© www.soinside.com 2019 - 2024. All rights reserved.