Problem On My Extension Text To Pdf

Today I was making an extension to convert text to Pdf file and went through this video guide then wrote code like this:

package com.oseamiya.TextToPdf;

import android.content.Context;
import android.util.Log;




import android.os.Environment;
import android.util.DisplayMetrics;

import android.os.Bundle;
import android.view.View;

    version = 1,
    iconName = "",
    description = "It will help to convert any text to a simple pdf file. The extension is launced by Sneha Sharma",
    category = ComponentCategory.EXTENSION,
    nonVisible = true

@SimpleObject(external = true)
@UsesPermissions(permissionNames = "android.permission.READ_EXTERNAL_STORAGE, android.permission.WRITE_EXTERNAL_STORAGE")

public class TextToPdf extends AndroidNonvisibleComponent{
    private ComponentContainer container;
    private Activity activity;
    private Context context;
    private int pageWidth = 360;
    private int pageHeight = 600;
    private int pageNumber = 1;
    private float textSize = 12;
    private int fontcolor = -16777216;
    private int backgroundcolor = 1;
    private float x = 14;
    private float y = 25;
    private String filePath = "";

    public TextToPdf(ComponentContainer container){
        this.container = container;
        this.context = container.$context();
        this.activity = container.$context();

    @SimpleFunction(description = "Convert any text to pdf")
    public void Convert(String text, String fileName){
        PdfDocument myPdfDocument = new PdfDocument();
        PdfDocument.PageInfo myPageInfo = new PdfDocument.PageInfo.Builder(this.pageWidth,this.pageHeight,this.pageNumber).create();
        PdfDocument.Page myPage = myPdfDocument.startPage(myPageInfo);

        Paint myPaint = new Paint();
        Paint backPaint = new Paint();

        //To find size of screen so that line is perfectly fit
        DisplayMetrics dm = this.context.getResources().getDisplayMetrics();
        int widthOfScreen = dm.widthPixels;
        int periodForSplittingText = widthOfScreen / Math.round(this.textSize);

        // To make line split in width of the screen here;
        String insert = "\n";
        StringBuilder builder = new StringBuilder(
         text.length() + insert.length() * (text.length()/periodForSplittingText)+1);

        int index = 0;
        String prefix = "";
        while (index < text.length()){
            prefix = insert;
            builder.append(text.substring(index, Math.min(index + periodForSplittingText, text.length())));
            index += periodForSplittingText;

        String myString = builder.toString();

        for (String line:myString.split("\n")){
            myPage.getCanvas().drawText(line, this.x, this.y, myPaint);

        String myFilePath = Environment.getExternalStorageDirectory().getPath() + this.filePath + "/" + fileName;
        File myFile = new File (myFilePath);
        try {
            myPdfDocument.writeTo(new FileOutputStream(myFile));

        } catch (Exception e) {

    @SimpleProperty(description = "Set Height of the document page. It accepts integer value and page height will be in pt")
    public void Height(int height){
        this.pageHeight = height;

    @SimpleProperty(description = "Set Width of the document page. It accepts integer value and page width will be in pt")
    public void Width(int width){
        this.pageWidth = width;
    @SimpleProperty(description = "Set number of pages in document")
    public void NumberOfPage(int numberOfPages){
        this.pageNumber = numberOfPages;
    @SimpleProperty(description = " The x-coordinate of the origin of the text being drawn. It accepts float value")
    public void X(float x){
        this.x = x;
    @SimpleProperty(description = "The y-coordinate of the baseline of the text being drawn. It accepts float value")
    public void Y(float y){
        this.y = y;
    @SimpleProperty(description = "To set filepath. If you want to save content in Download then filepath should be /Download")
    public void FilePath(String filePath){
        this.filePath = filePath;
    @SimpleProperty(description = "To set size of text. It accepts float value")
    public void TextSize(float textSize){
        this.textSize = textSize;
    @SimpleProperty(description = "To set the font color. It accepts int color value")
    public void FontColor(int fontColor){
        this.fontcolor = fontColor;
    @SimpleProperty(description = "To set the background color of the document. It accepts int value")
    public void BackgroundColor(int backColor){
        this.backgroundcolor = backColor;


The extension successfully built but I was facing a problem that if I use long text then it is not parsing text to another page (I mean only one page is forming, I am not able to create 2 pages of pdf)
I've tried to search on google about the problem but can't find it out.
If anyone tried this then please let me know my mistakes in the code.

References :

Extension :
com.oseamiya.TextToPdf.aix (9.2 KB)

I have recategorized topic to Extension-Development :+1:

Now it is fixed .
As I didn't see this.
This class enables generating a PDF document from native Android content. You create a new document and then for every page you want to add you start a page, write content to the page, and finish the page. After you are done with all pages, you write the document to an output stream and close the document. After a document is closed you should not use it anymore. Note that pages are created one by one, i.e. you can have only a single page to which you are writing at any given time. This class is not thread safe.

Glad to know :+1:

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.