如何在内存中创建pdf文件

编程入门 行业动态 更新时间:2024-10-24 22:19:38
本文介绍了如何在内存中创建pdf文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_pdftest); Button Btgerar = (Button) findViewById(R.id.gerarpdf); Btgerar.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { CriarPDF(); } }); } public void CriarPDF() { String FILE = Environment.getExternalStorageDirectory().toString() + "/PDF/" + "Ficha.pdf"; EditText txt = (EditText) findViewById(R.id.editText); // Create New Blank Document Document document = new Document(PageSize.A4); // Create Directory in External Storage String root = Environment.getExternalStorageDirectory().toString(); File myDir = new File(root + "/PDF"); myDir.mkdirs(); // Create Pdf Writer for Writting into New Created Document try { PdfWriter.getInstance(document, new FileOutputStream(FILE)); // Open Document for Writting into document document.open(); document.add(new Paragraph(txt.getText().toString())); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (DocumentException e) { e.printStackTrace(); } // Close Document after writting all content document.close(); Toast.makeText(this, "PDF File is Created. Location : " + FILE, Toast.LENGTH_LONG).show(); // Create new Page in PDF document.newPage(); } }

我已宣布在android清单中

I have declared in android manifest

使用权限android:name =android.permission.WRITE_EXTERNAL_STORAGE

和build gradle版本是:

and build gradle version is :

compileSdkVersion 24 buildToolsVersion '24.0.0' minSdkVersion 8 targetSdkVersion 24

应用程序可以正常运行,但是可以不创建pdf文件,我找不到。

The application can run normally, but can't create pdf file and I can't find.

推荐答案

如果我看一下你问题的主题,那么答案就是答案很简单。你正在使用 FileOutputStream 在文件系统上创建一个PDF。在主题中你问:如何在内存中创建pdf文件?这很简单.iText接受任何 OutputStream ,所以答案是:使用 ByteArrayOutputStream 而不是 FileOutputStream 。

If I look at the subject of your question, then the answer is simple. You are creating a PDF on the file system using a FileOutputStream. In the subject you ask: "How to create pdf files in memory?" That is simple. iText accepts any OutputStream, so the answer is: use a ByteArrayOutputStream instead of a FileOutputStream.

参见使用iText,在内存上生成一个在磁盘上生成的PDF而不是

Document document = new Document(); ByteArrayOutputStream out = new ByteArrayOutputStream(); PdfWriter writer = PdfWriter.getInstance(documento, out); (...) byte[] pdf = out.getBytes();

但是,为什么要在Android设备的内存中创建PDF?你打算用这些字节做什么?如果您只有 byte [] ,则无法显示PDF。在我对这个问题的回答中解释了这一点:显示存储在ByteArrayOutputStream中的PDF文档(不在文件中)

However, why would you want to create a PDF in memory on an Android device? What are you going to do with those bytes? You can't show the PDF if you only have a byte[]. That is explained in my answer to this question: Display PDF document stored in a ByteArrayOutputStream (not in a file)

您还会注意到以下问题仍未得到答复(因为没有答案): 如何在Android上将ByteArrayOutputStream可视化为PDF?

You will also notice that the following question remained unanswered (because there is no answer): How to visualize a ByteArrayOutputStream as PDF on Android?

您的问题在最后更改主题,您写道:应用程序可以正常运行,但无法创建pdf文件,我找不到。这听起来好像你不知道如何在Android上的文件系统中编写PDF。这与你在这个主题中提到的问题不同。之前已经多次回答过这个问题:

Your question changes subject towards the end, where you write: "The application can run normally, but can't create pdf file and I can't find." This sounds as if you don't know how to write a PDF to the file system on Android. That's a different question than what you mention in the subject. That question has been answered many times before:

  • 创建Android中的PDF
  • 如何使用eclipse解决android编程中的只读文件系统错误
  • 如何在使用iText创建的Sdcard上保存PDF文件?
  • 无法在Android中运行iText库的简单程序
  • 将iText创建的PDF文件保存为内部存储器为MODE_WORLD_READABLE
  • Creating PDFs in android
  • how to solve read only file system error in android programming with eclipse
  • How to save PDF file on Sdcard created using iText?
  • Not able to run simple program of iText library in android
  • Save iText created PDF to internal memory as MODE_WORLD_READABLE

更多推荐

如何在内存中创建pdf文件

本文发布于:2023-05-26 19:32:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/265262.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:内存   文件   如何在   pdf

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!