java 将文本转换为 PDF
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4696463/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert text into PDF
提问by Shaded
I have a huge string of text that is apparently raw data for a PDF file, and I need to make it back into a PDF.
我有一大串文本,显然是 PDF 文件的原始数据,我需要将其重新转换为 PDF。
Currently I'm reading the string into a StringBuffer but if I need to I can change that. From there I have tried just writing it out to a file and changing the extension (I really hoped that worked, but I kinda knew it wouldn't), I've tried taking it to a String then getting a byte[] out of it and writing that to the file or using a DataOutputStream to put the bytes into the file. None of these has seemed to work.
目前我正在将字符串读入 StringBuffer,但如果需要,我可以更改它。从那里我尝试将其写入文件并更改扩展名(我真的希望它起作用,但我有点知道它不会),我尝试将它带入一个 String 然后从中获取一个字节 []它并将其写入文件或使用 DataOutputStream 将字节放入文件中。这些似乎都没有奏效。
I've also tried using the iText plugin, I tried just writing it to a pdf through that and I also tried reading the text as a pdf and then copying it page by page to a new pdf. Neither of these have returned very good results.
我也尝试过使用 iText 插件,我试着通过它把它写成 pdf,我也尝试将文本作为 pdf 阅读,然后将它逐页复制到新的 pdf。这些都没有返回非常好的结果。
It's Friday afternoon, I'm tapped, any suggestions will be a huge help!
现在是星期五下午,我被选中了,任何建议都会有很大帮助!
回答by mark stephens
A PDF is a binary object. You need to write the bytes directly to a file.
PDF 是一个二进制对象。您需要将字节直接写入文件。
Turning into text will probably break it. Does it start with %%PDF-
and end with %%EOF
?
变成文本可能会破坏它。它以 开头%%PDF-
和结尾%%EOF
吗?
回答by RealHowTo
The iText approach is the right one. You can do something like this :
iText 方法是正确的方法。你可以这样做:
import java.io.*;
import com.lowagie.text.*;
import com.lowagie.text.pdf.*;
public class TextFileToPDF {
/*
ex. java TextFileToPDF c:\temp\text.txt c:\temp\text.pdf
*/
public static void main (String [] args){
BufferedReader input = null;
Document output = null;
System.out.println("Convert text file to pdf");
System.out.println("input : " + args[0]);
System.out.println("output : " + args[1]);
try {
// text file to convert to pdf as args[0]
input =
new BufferedReader (new FileReader(args[0]));
// letter 8.5x11
// see com.lowagie.text.PageSize for a complete list of page-size constants.
output = new Document(PageSize.LETTER, 40, 40, 40, 40);
// pdf file as args[1]
PdfWriter.getInstance(output, new FileOutputStream (args[1]));
output.open();
output.addAuthor("RealHowTo");
output.addSubject(args[0]);
output.addTitle(args[0]);
String line = "";
while(null != (line = input.readLine())) {
System.out.println(line);
Paragraph p = new Paragraph(line);
p.setAlignment(Element.ALIGN_JUSTIFIED);
output.add(p);
}
System.out.println("Done.");
output.close();
input.close();
System.exit(0);
}
catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
}
回答by Shaded
Okay, well after a lot of research I found out that to preserve the binary data in the string that typically you convert it to Base64 encoding. On a complete guess I decoded the string out of Base64 and dropped the bytes into the pdf file, and lo and behold I had a pdf that could be opened!
好的,经过大量研究,我发现要保留字符串中的二进制数据,通常会将其转换为 Base64 编码。完全猜测我从 Base64 中解码出字符串并将字节放入 pdf 文件中,瞧,我有一个可以打开的 pdf!
Thanks for the answers and I hope this helps someone in the future!
感谢您的回答,我希望这对未来的人有所帮助!
回答by Shane
How did you come across this string? If it is a raw ASCII string, you will be missing a large amount of binary data that is embedded within the PDF.
你是怎么找到这个字符串的?如果它是原始 ASCII 字符串,您将丢失大量嵌入在 PDF 中的二进制数据。
If you have a unicode string, you may be able to write it to a file directly using an OutputStream (not a Writer as you don't actually want to write character data).
如果您有一个 unicode 字符串,您可以直接使用 OutputStream 将其写入文件(不是 Writer,因为您实际上并不想写入字符数据)。