Generating pdf file using Itext [duplicate]

2019-07-16 11:20发布

问题:

This question already has an answer here:

  • iText - generating files on the fly without needing a PDF file 3 answers

I am trying to generate pdf file from jsp using itext jar. Could you please tell me how to retain the space or tab between strings as it is.

<%
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();
}
%>

The space between "Hello" and "World" is not there in the generated pdf. How can i get the line as it is entered.

Thanks MRK

回答1:

Convert your space characters to non-breaking spaces:

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


标签: pdf itext