我能够使用pdf
生成iText
文件,但是当我打开它时,我总是会收到一个错误,该文件似乎已损坏。我已经添加了库itextg-5.5.8.jar
任何人都知道如何解决这个问题?
这是我的代码
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.ActionBarActivity;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
String FILE = Environment.getExternalStorageDirectory().toString() +"/Report.pdf";
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(FILE));
document.open();
Paragraph paragraph1 = new Paragraph();
paragraph1.add("Hello World!");
document.add(paragraph1);
document.close();
//Log.d("OK", "done");
//Toast.makeText(getApplicationContext(), "Generated Report!", Toast.LENGTH_SHORT).show();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
}
}
}
我上传了 PDF ,这是link。如您所见,它只包含以下字节:
%PDF-1.4
%âãÏÓ