使用 Itext 7.1.7 时,文本区域中的连续文本会剪切溢出文本的 PDF

Continuous text in text-area cuts the PDF overflowing the text when using Itext 7.1.7

我看到有很多问题与同一场景相关,但这个问题略有不同,无法找到解决方案。我在一个单元格的表格中有一个。当我给出像 "cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc" 这样的连续文本时,当我下载打印文件时它会溢出文本,当我用 breaks.Here 给出普通文本时它是正常的是我的代码

.generaltable {
 background-color : #5C7FBF;
 border:thin;
 width : 100%;
 font-family: Arial, Helvetica, sans-serif;
 font-size: 14px;
 }
  
  
 .column {
 background-color : #DEEDFF;
 font-weight : bold;
 padding-bottom : 1px;
 padding-left : 1px;
 padding-right : 1px;
 padding-top : 1px;
 text-align : center;
 font-family: Arial, Helvetica, sans-serif;
 font-size: 14px;
 border: none;
 }
  
 .edit {
 background-color : #DEEDFF;
 border-width: 1px;
 border-style:solid;
 border-color:#DEEDFF;
 border: 1px solid #DEEDFF;
 color: black;
 text-align : left;
 font-weight : bold;
 font-family: Arial, Helvetica, sans-serif;
 font-size: 14px;
 word-break: break-all;
 }
  
  .iedit2 {
 background-color : white;
 text-align: left;
 color: black;
 font-family: Arial, Helvetica, sans-serif;
 font-weight: bold;
 border-top: 1px solid #999999;
 border-right: 1px solid #333333;
 border-bottom: 1px solid #333333;
 border-left: 1px solid #999999;
 word-break: break-all;
 }
  
  
  
  
  
<table border="1" width="100%" align="center" cellpadding="2" cellspacing="1"  class="generaltable">  <tbody><tr id="Row35494#0">
    <th id="04" class="column" width="39%"><a href="javascript:alert('Self Explanatory')">Brief                 description of the issue *</a></th>
        <td id="1 04" width="39%" class="edit">
            <textarea id="269494_0" class="iedit2" cols="35" rows="5" wrap="virtual" maxlength="4000"                   name="fiy">ssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss
             </textarea>
        </td>
      </tr>
  </tbody>
 </table>

请找到图片以清楚概览

我有一个像 Go 一样的点击,一旦我这样做它就会使用 JAVA 代码在此处下载整个文档并且代码部分工作正常但问题出在 itext 7.1.7 上。这种情况正在发生,我已将其更改为飞碟,它工作正常但会导致其他问题。我想继续使用 Itext 7.1.7 并解决这个连续修复问题。

您可以检测文本何时到达输入框中的新行,并插入'\n'强制换行,这样当您下载图片时,它应该有换行符hard-coded。希望这对您有所帮助!

pdfHTML 允许您将与表单相关的元素(输入、文本区域)直接转换为纯 PDF 内容,或使用 AcroForm 创建 PDF(以便这些元素可编辑,因为它们应该在 HTML).

要启用该行为,您应该在传递给 HtmlConverterConverterProperties 中使用 setCreateAcroForm(true)

如果您不想让这些字段可编辑,您可以在将 HTML 转换为 PDF 后将这些字段拼合为第二步。

话虽如此,您描述的行为看起来像是 iText 中的错误。但是创建 AcroForm 和展平的模式是以稍微不同的方式实现的,看起来文本区域会按照您的情况进行转换。您没有附上整个示例,因此很难确定地验证,但是对于您附上的小片段,一切都很好。这是您可以使用的代码:

ByteArrayOutputStream baos = new ByteArrayOutputStream();
HtmlConverter.convertToPdf(new FileInputStream("C:\file.html"), baos,
        new ConverterProperties().setCreateAcroForm(true));

PdfDocument document = new PdfDocument(new PdfReader(new ByteArrayInputStream(baos.toByteArray())), 
        new PdfWriter(new File("C:\out.pdf")));
PdfAcroForm acroForm = PdfAcroForm.getAcroForm(document, false);
acroForm.flattenFields();
document.close();

我想将以下 css 添加到您的内容元素(即 .edit.iedit2)应该可以解决问题。

overflow-wrap: break-word;
word-wrap: break-word;
word-break: break-word;

如果可行请告诉我:)

另外,为了让代码看起来更好看,您可以添加以下内容 ;)

-ms-hyphens: auto;
-moz-hyphens: auto;
-webkit-hyphens: auto;
hyphens: auto;

这些将添加“-”来中断单词:)