Android生成pdf的问题
我在创建 PDF 文件并将其保存在 SD 上时遇到问题,希望有人建议我该怎么做,按照我开始做的操作,如果他们有任何示例,我很感激您发送给我!
我的类尝试生成 PDF:
public class TestActivity extends Activity {
private static String FILE = "sdcard/exaple.pdf";
private static final String TAG = "documentPDF";
/**
* @see android.app.Activity#onCreate(Bundle)
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tela_relatorio);
File Directory = new File(FILE);
try {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(Directory));
document.open();
document.addAuthor("Name Author");
addTitlePage(document);
document.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private void addTitlePage(Document document)throws DocumentException {
Paragraph preface = new Paragraph();
preface.add(new Paragraph("Document Title", null));
document.add(preface);
document.newPage();
}
}
I'm having trouble creating a PDF file and save it on SD, and would like someone suggested to me what to do, follow what I started doing, if they have any examples, I am grateful for sending me!
My class that tries to generate the PDF:
public class TestActivity extends Activity {
private static String FILE = "sdcard/exaple.pdf";
private static final String TAG = "documentPDF";
/**
* @see android.app.Activity#onCreate(Bundle)
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tela_relatorio);
File Directory = new File(FILE);
try {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(Directory));
document.open();
document.addAuthor("Name Author");
addTitlePage(document);
document.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private void addTitlePage(Document document)throws DocumentException {
Paragraph preface = new Paragraph();
preface.add(new Paragraph("Document Title", null));
document.add(preface);
document.newPage();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
编辑:另外,您是否使用此库来实现 pdf 功能? http://sourceforge.net/projects/apwlibrary/
我要尝试一下这个尽力回答。不过,真正可以帮助您的应用程序开发的一些注意事项是首先使用正确的代码间距。由于这是一个 Android 问题,我想您正在 Eclipse 中进行开发,但一个鲜为人知但很棒的快捷方式是输入 Ctrl+A 来选择所有代码,然后使用 Ctrl+I 这将正确对齐您的代码,使其更具可读性。在这种情况下,我发现了一些可能不是你想要的东西
所以,我对所涉及的特定库还不够了解,无法提供比这更多的建议,但是再说一次,里面有一些你可能不想要的东西。另一个问题是您的
try
语句没有包含足够的代码。 :) 仔细看看你想要做什么,然后思考应该如何完成。无论如何,我认为这至少应该消除任何编译时错误,只要您有适当的导入。哦,如果您使用 Eclipse,另一个方便的快捷方式是 Ctrl+Shift+I 它将管理您的导入语句并帮助您找到需要导入的包。从那里,您将需要找到与编译错误无关的任何错误
Edit: Also, are you using this library for your pdf functionality? http://sourceforge.net/projects/apwlibrary/
I'm gonna take a shot at this and try to answer the best I can. A few notes though that could really help your application development is to first use proper code spacing. Since this is an Android question, I imagine that you are developing in Eclipse, but a little known, yet awesome shortcut is to type
Ctrl+A
to select all your code then useCtrl+I
which will properly align your code so that it is more readable. In this case, I was able to find out a few things that MIGHT not be what you wantedSo, I don't know enough about the specific libraries involved to offer much more advice than that yet, but once again, there are somethings in there that you might not want. The other problem was your
try
statement wasn't surrounding enough code there. :) Look closely at what you are trying to do, and then think about how that should be done.Anyway, I think this should at minimum get rid of any compile-time errors, provided that you have the appropriate imports. Oh, another handy shortcut if you are using Eclipse is
Ctrl+Shift+I
It will manage your import statements and help you find that packages you need to import. From there, you will need to find any bugs that aren't related to compile errors工作人员能够在 android 中生成 PDF,我的代码已经从数据库生成了 pdf 报告!
再见!
Staff was able to generate a PDF in android, there is my code already generating a pdf report from the database!
bye!