簡體   English   中英

我無法使用 Android 的 PdfDocument 創建兩頁 pdf

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

開發者。 我正在使用 PdfDocument 嘗試將文本保存為 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();
    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();
}

但是,如果我輸入了長文本,則無法在兩頁上創建它。 根據 Android 開發者的

此 class 允許從原生 Android 內容生成 PDF 文檔。 您創建一個新文檔,然后為要添加的每個頁面開始一個頁面,將內容寫入頁面,然后完成頁面。 完成所有頁面后,將文檔寫入 output stream 並關閉文檔。 文檔關閉后,您不應再使用它。 請注意,頁面是一頁一頁地創建的,也就是說,在任何給定時間,您只能擁有一個要寫入的頁面。 這個 class 不是線程安全的。

但我不明白這是什么意思? 我該如何解決這個問題? 幫助

編輯:現在添加這個

// 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

當您開始一個新頁面時,您沒有將其分配給變量

改成

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM