Getting Started

Implementation

Implementation

  1. PDF creator uses views which can be rendered on screen, So we need to extend an Activity in order to create pdf.

  2. Create a Empty Activity without any layout and extend it with PDFCreatorActivity. Do not set use setContentView(int resourceId) inside your created activity.

  3. There are 3 abstract methods you have to override.

    1. getHeaderView()

      • This will be header for PDF and will be added to each page. (Accepts PDFHeaderView)

    2. getBodyViews()

      • This will return a PDFBody which consist of list of views which can be broken between pages.

    3. getFooterView()

      • This will be footer for PDF and will be added to each page. (Accepts PDFFooterView)

    4. getWatermarkView()

      • [OPTIONAL] This add a watermark image to each page. (Accepts PDFImageView), see issue #14

    5. onNextClicked()

      • This is a handler method to get callback when user taps on Next.

  4. In onCreate of you activity, you have to call createPDF(String fileName, PDFUtilListener listener). It will generate PDF and give you a PDF file in callback (if success). After receiving callback you can close activity and do whatever you need to do with PDF.

  5. This library also provides PDFUtil.pdfToBitmap(File pdfFile) method to get image preview of all pages of specified PDF file.

Here is an example to create your own pdf

public class MyPdfCreatorActivity extends PDFCreatorActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (getSupportActionBar() != null) {
            getSupportActionBar().hide();
        }

        createPDF("test", new PDFUtil.PDFUtilListener() {
            @Override
            public void pdfGenerationSuccess(File savedPDFFile) {
                Toast.makeText(PdfCreatorActivity.this, "PDF Created", Toast.LENGTH_SHORT).show();
                // Share your pdf OR Do whatever you want.
                // You do not have to wait for user to interact.
            }

            @Override
            public void pdfGenerationFailure(Exception exception) {
                Toast.makeText(PdfCreatorActivity.this, "PDF NOT Created", Toast.LENGTH_SHORT).show();
            }
        });
    }
    
    @Override
    protected PDFHeaderView getHeaderView(int pageIndex) {
        PDFHeaderView headerView = new PDFHeaderView(getApplicationContext());
        // ...
        // Customize your header here
        // ...
        return headerView;
    }
    
    @Override
    protected PDFBody getBodyViews() {
        PDFBody pdfBody = new PDFBody();
        // ...
        // Customise your Paginating body here
        // ...
        return pdfBody;
    }

    @Override
    protected PDFFooterView getFooterView(int pageIndex) {
        PDFFooterView footerView = new PDFFooterView(getApplicationContext());
        // ...
        // Customise your footer here
        // ...
        return footerView;
    }
    
    @Nullable
    @Override
    protected PDFImageView getWatermarkView(int forPage) {
        // OPTIONAL, Add custom watermark to every page.
        PDFImageView pdfImageView = new PDFImageView(getApplicationContext());
        FrameLayout.LayoutParams childLayoutParams = new FrameLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                200, Gravity.CENTER);
        pdfImageView.setLayout(childLayoutParams);

        pdfImageView.setImageResource(R.drawable.ic_pdf);
        pdfImageView.setImageScale(ImageView.ScaleType.FIT_CENTER);
        pdfImageView.getView().setAlpha(0.3F);

        return pdfImageView;
    }

    @Override
    protected void onNextClicked(final File savedPDFFile) {
    
        // Share your pdf OR Do whatever you want
        Uri pdfUri = Uri.fromFile(savedPDFFile);

        Intent intentPdfViewer = new Intent(PdfCreatorActivity.this, PdfViewerActivity.class);
        intentPdfViewer.putExtra(PdfViewerActivity.PDF_FILE_URI, pdfUri);

        startActivity(intentPdfViewer);
    }
}

Last updated