Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
qiuhuanhen opened this issue May 28, 2024 · 2 comments
Labels

Comments

@qiuhuanhen
Copy link

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();
        }
    }
    `
@qiuhuanhen qiuhuanhen added the bug label May 28, 2024
@qiuhuanhen
Copy link
Author

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

@qiuhuanhen
Copy link
Author

qiuhuanhen commented May 28, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant