LibrePDF / OpenPDF

OpenPDF is a free Java library for creating and editing PDF files, with a LGPL and MPL open source license. OpenPDF is based on a fork of iText. We welcome contributions from other developers. Please feel free to submit pull-requests and bugreports to this GitHub repository.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

font can't display Complete when text length greater than pdf edit box length

qiuhuanhen opened this issue · comments

when text length is longger, same text can't display ,
such as my text is "这是一段非常长的文字这是一段非常长的文字这是一段非常长的文字",
but pdf may be only show "这是一段非常长的文字这是一段" ,
I don't find api or code how to slove it , in AcroFields#setFieldProperty , can't find "fontsize" .
may I have some other way to slove it ? I hope can display complete ,although the font size may be small.

there is my demo code:

`

// load pdf file
ClassPathResource resource = new ClassPathResource("my.pdf");
InputStream inputStream = resource.getInputStream();
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);

    com.lowagie.text.pdf.PdfReader reader = null;
    ByteArrayOutputStream bos = null;
    try {
        reader = new com.lowagie.text.pdf.PdfReader(bufferedInputStream);
        bos = new ByteArrayOutputStream();
        com.lowagie.text.pdf.PdfStamper pdfStamper = new com.lowagie.text.pdf.PdfStamper(reader, bos);
        com.lowagie.text.pdf.AcroFields acroFields = pdfStamper.getAcroFields();

        // chinese font
        com.lowagie.text.pdf.BaseFont font = com.lowagie.text.pdf.BaseFont.createFont("simsun.ttc,0", com.lowagie.text.pdf.BaseFont.IDENTITY_H, com.lowagie.text.pdf.BaseFont.EMBEDDED);

        for (Map.Entry<String, String> param : params1.entrySet()) {
            // set text font 
            acroFields.setFieldProperty(param.getKey().trim(), "textfont", font, null);

            // replace map content to pdf (write content from map value, map defined by self)
           //  and the problems occur here
            acroFields.setField(param.getKey().trim(), param.getValue());
        }

        //  set pdf edit box can't edit after replacing map value
        pdfStamper.setFormFlattening(true);
        pdfStamper.close();
        // return
        PdfServiceController.ServletUtils.writeAttachment(response, "简单劳动合同模板.pdf", bos.toByteArray());
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            assert bos != null;
            bos.close();
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    `

sorry ,the title may be : text can't display Complete not font

I find it ,
acroFields.setFieldProperty(param.getKey().trim(), "textsize",9f, null);
but the pdf may have many different edit box , some text may short ,so needn't 9f (font size) ; and I wonder if it counld auto make the font of long text smaller (don't change global font size) or change line