使用字符串值创建pdf文件

时间:2018-11-07 12:53:42

标签: java android file pdf

我正在android应用程序中生成以下字符串,我想知道是否有一种简单的方法可以从中创建pdf或doc(x)文件?我尝试过PDFDocument类,但对我来说不起作用。抱歉,如果有已知的解决方案,我是android和java的新手。

String s = "build:              some build name\r\n" + 
                "Version:            25\r\n" + 
                "Specification:      wtx 26.1\r\n" + 
                "\r\n" + 
                "Files to edit:\r\n" + 
                "doc1.doc\r\n" + 
                "doc2.doc\r\n" + 
                "\r\n" + 
                "Notes:\r\n" + 
                "some notes ...";

2 个答案:

答案 0 :(得分:0)

有一个非常简单的代码块here,它执行类似的操作。选中library page,以查看全部功能和示例用法。

您只需要用编写的内容替换 Hello World 部分。请注意,您需要在应用程序中授予 STORAGE 权限,才能将其保存将该文件保存到手机存储中,如下所示:

<manifest ...>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    ...
</manifest>

然后您可以使用以下代码:

import com.cete.dynamicpdf.*;
import com.cete.dynamicpdf.pageelements.Label;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.widget.Toast;

public class DynamicPDFHelloWorld extends Activity {
    private static String FILE = Environment.getExternalStorageDirectory()
            + "/HelloWorld.pdf";

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // Create a document and set it's properties
        Document objDocument = new Document();
        objDocument.setCreator("DynamicPDFHelloWorld.java");
        objDocument.setAuthor("Your Name");
        objDocument.setTitle("Hello World");

        // Create a page to add to the document
        Page objPage = new Page(PageSize.LETTER, PageOrientation.PORTRAIT,
                54.0f);

        // Create a Label to add to the page
        String strText = "Hello World...\nFrom DynamicPDF Generator "
                + "for Java\nDynamicPDF.com";
        Label objLabel = new Label(strText, 0, 0, 504, 100,
                Font.getHelvetica(), 18, TextAlign.CENTER);

        // Add label to page
        objPage.getElements().add(objLabel);

        // Add page to document
        objDocument.getPages().add(objPage);

        try {
            // Outputs the document to file
            objDocument.draw(FILE);
            Toast.makeText(this, "File has been written to :" + FILE,
                    Toast.LENGTH_LONG).show();
        } catch (Exception e) {
            Toast.makeText(this,
                    "Error, unable to write to file\n" + e.getMessage(),
                    Toast.LENGTH_LONG).show();
        }
    }
}

答案 1 :(得分:0)

在这里您可以轻松创建pdf文件

private void createALlPdf(String str){
    PdfDocument document = new PdfDocument();
    // crate a page description
    PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(600, 1000, 1).create();
    // start a page
    PdfDocument.Page page = document.startPage(pageInfo);
    Canvas canvas = page.getCanvas();
    Paint paint = new Paint();
    //  paint.setColor(Color.RED);
    // canvas.drawCircle(50, 50, 30, paint);
    Date currentTime = Calendar.getInstance().getTime();

    paint.setColor(Color.BLACK);
    // canvas.drawText(wise, 60, 50, paint);
    int y=50;


    canvas.drawText(str, 80, 50, paint);

    canvas = page.getCanvas();
    paint = new Paint();
    // paint.setColor(Color.BLUE);
    // canvas.drawCircle(100, 100, 100, paint);
    document.finishPage(page);
    // write the document content
    String directory_path = Environment.getExternalStorageDirectory().getPath() + "/P-ALLPDF/";
    File file = new File(directory_path);
    if (!file.exists()) {
        file.mkdirs();
    }
    String targetPdf = directory_path+"All record"+currentTime+".pdf";
    File filePath = new File(targetPdf);
    try {
        document.writeTo(new FileOutputStream(filePath));
        Toast.makeText(this, "Pdf file generated in your internal storage under P-ALLPDF directory.Please check!!", Toast.LENGTH_LONG).show();
    } catch (IOException e) {
        Log.e("main", "error "+e.toString());
        Toast.makeText(this, "Something wrong: " + e.toString(),  Toast.LENGTH_LONG).show();
    }
    // close the document
    document.close();
}

通过传递字符串来调用函数

createALlPdf(s);