# Getting Started

* [x] First thing first, Look at [PdfCreatorExampleActivity](https://github.com/tejpratap46/PDFCreatorAndroid/blob/master/app/src/main/java/com/tejpratapsingh/pdfcreatorandroid/PdfCreatorExampleActivity.java) of app.

## 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](https://github.com/tejpratap46/PDFCreatorAndroid/issues/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

```java
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);
    }
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://notes.tejpratapsingh.com/pdf-creator-android/getting-started.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
