javaandroidandroid-layoutpdfpdfdocument

I am not able to create two pages of pdf using PdfDocument of Android


Devs! I am using PdfDocument to try to save the text as a pdf file. So I wrote this code :

 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();
    backPaint.setColor(this.backgroundcolor);
    myPaint.setTextSize(this.textSize);
    myPaint.setColor(this.fontcolor);

    //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()){
        builder.append(prefix);
        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().drawPaint(backPaint);
        myPage.getCanvas().drawText(line, this.x, this.y, myPaint);
        y+=myPaint.descent()-myPaint.ascent();
        
    }
    myPdfDocument.finishPage(myPage);

// Started another page 
myPdfDocument.startPage(myPageInfo);
myPage.getCanvas().drawText("Second Page Text", 14, 25, myPaint);
myPdfDocument.finishPage(myPage);



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

    } catch (Exception e) {
        e.printStackTrace();
    }
    
    myPdfDocument.close();
}

But if I entered the long text then it is not able to create it on two pages. According to Android Developer's

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.

But I am not understanding what does it mean? How can I solve this? Help

Edit: Now adding this

// Started another page 
myPdfDocument.startPage(myPageInfo);
myPage.getCanvas().drawText("Second Page Text", 14, 25, myPaint);
myPdfDocument.finishPage(myPage);

results this error : Attempt to invoke virtual method 'void android.graphics.Canvas.drawText(java.lang.String, float, float, android.graphics.Paint)' on a null object reference


Solution

  • When you start a new page you are not assigning it to a variable

    Change to

    // Started another page 
    myPage = myPdfDocument.startPage(myPageInfo);