【问题标题】:Generating pdf file using Itext [duplicate]使用 Itext 生成 pdf 文件 [重复]
【发布时间】:2011-12-05 14:12:43
【问题描述】:

我正在尝试使用 itext jar 从 jsp 生成 pdf 文件。你能告诉我如何保留字符串之间的空格或制表符吗?

<%
response.setContentType("application/pdf");
String disHeader = "Attachment; Filename=\"example.pdf\"";
response.setHeader("Content-Disposition", disHeader);
Document document = new Document();
try{
    PdfWriter.getInstance(document, response.getOutputStream());

            document.open();
        document.add(new Paragraph("Hello    World"));
        document.add(new Paragraph("Hello World")); 
        document.close();
        }catch(DocumentException e){
e.printStackTrace();
}
%>

生成的 pdf 中没有“Hello”和“World”之间的空格。我怎样才能得到输入的行。

谢谢 MRK

【问题讨论】:

    标签: pdf itext


    【解决方案1】:

    将您的空格字符转换为non-breaking 空格:

    document.add(new Paragraph("Hello    World".replace(' ', '\u00a0')));
    

    【讨论】:

    • 感谢您的意见。我有标签分隔的数据。我想保留标签原样。即假设如果 hello 和 world 是制表符分隔的,那么即使在写入 pdf 之后,我也希望看到制表符分隔。能否请您告诉我是否有任何方法可以保留制表符。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-17
    • 1970-01-01
    • 2011-08-04
    • 2014-09-01
    • 2012-01-22
    相关资源
    最近更新 更多