简体   繁体   English

我无法使用 Android 的 PdfDocument 创建两页 pdf

[英]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 :我正在使用 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();
}

But if I entered the long text then it is not able to create it on two pages.但是,如果我输入了长文本,则无法在两页上创建它。 According to Android Developer's根据 Android 开发者的

This class enables generating a PDF document from native Android content.此 class 允许从原生 Android 内容生成 PDF 文档。 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.完成所有页面后,将文档写入 output stream 并关闭文档。 After a document is closed you should not use it anymore.文档关闭后,您不应再使用它。 Note that pages are created one by one, ie you can have only a single page to which you are writing at any given time.请注意,页面是一页一页地创建的,也就是说,在任何给定时间,您只能拥有一个要写入的页面。 This class is not thread-safe.这个 class 不是线程安全的。

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

When you start a new page you are not assigning it to a variable当您开始一个新页面时,您没有将其分配给变量

Change to改成

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Android上使用PdfDocument设置PDF文件大小 - How to set PDF file size using PdfDocument on Android 我正在使用Parse构建Android应用程序,但无法创建对象 - I am building an Android app using Parse, but I am not able to create an object 使用 PdfDocument 在 android 中生成自定义大小的 PDF - Generating a PDF with custom size in android with PdfDocument 无法使用appium-android创建新会话,但是我给了正确的包名称 - Not able to create a new session using appium-android, however I am giving the right package name PDF 尺寸过大通过 Android PDFDocument 生成。 在使用 pdfbox 时,它正在 output 中切割图像 - PDF size too large generating through Android PDFDocument. And while using pdfbox it is cutting image in output 如何在 android 中使用具有适当多行和多页的长字符串使用 PdfDocument 生成 Pdf? - How to generate a Pdf using PdfDocument from a long string with proper multiline and multipage in android? 我正在尝试使用 maven 创建一个 OSGI 包。 我无法正确捆绑 JAR - I am trying to create a OSGI bundle using maven. I am not able to bundle the JARs properly 我正在尝试使用Java中的svg libraray画一条线,但无法创建OMSVGPathSegList - I am trying to draw a line using an svg libraray in Java , but I am not able to create OMSVGPathSegList 为什么我无法下载任何网站的任何 html 页面?(我使用的是 android studio) - why i am not able to download any html page of any website?(i am using android studio) 我无法使用itext(xmlWorker)读取h1标签pdf - i am not able to read h1 tag pdf using itext (xmlWorker)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM