我正在尝试在我的应用程序中使用APW library,但无法使其正常运行。另外,我找不到任何演示,操作指南或完整代码示例来指导我使用此库。到目前为止,这是我所做的:

XML文件

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<TextView
        android:id="@+id/text"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent">
</TextView>




PDFWriterDemo.java

public class PDFWriterDemo extends Activity {

    TextView mText;

    private String generateHelloWorldPDF() {
        PDFWriter mPDFWriter = new PDFWriter(PaperSize.FOLIO_WIDTH, PaperSize.FOLIO_HEIGHT);
        mPDFWriter.newPage();
        mPDFWriter.setFont(StandardFonts.SUBTYPE, StandardFonts.COURIER_BOLD);
        mPDFWriter.addText(150, 150, 14, "http://stackoverflow.com");
        mPDFWriter.addLine(150, 140, 270, 140);

        int pageCount = mPDFWriter.getPageCount();
        for (int i = 0; i < pageCount; i++) {
            mPDFWriter.setCurrentPage(i);
            mPDFWriter.addText(10, 10, 8, Integer.toString(i + 1) + " / " + Integer.toString(pageCount));
        }

        String s = mPDFWriter.asString();
        return s;
    }

    private void outputToScreen(int viewID, String pdfContent) {
        mText = (TextView) this.findViewById(viewID);
        mText.setText(pdfContent);
    }

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pdf);
        String pdfcontent = generateHelloWorldPDF();
        outputToScreen(R.id.text, pdfcontent);
    }
}


我从我的PDFWriterDemo呼叫MainActivity像这样:

Intent pdf= new Intent(MainActivity.this, PDFwriterDemo.class);
pdf.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(pdf);


这是输出:

最佳答案

我正在尝试在我的应用程序中使用APW库,但无法使其正常运行


那是因为您试图在TextView中显示PDF文件。 TextView无法显示PDF。如果要显示PDF,请使用PDF查看器应用程序。

09-27 18:47