java为什么在云服务器上,带有多行文本框结果的generatedwithPDFBox PDF表单看起来不同?
我有一个web应用程序,可以使用PDF框填充PDF表单
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.24</version>
</dependency>
我在多行文本字段中遇到空格字符问题
空表格可从以下网址获取:
https://www.taxdataexchange.org/pdf-template
填写的表格可在以下网址查看:
https://www.taxdataexchange.org/pdf-template-filled
这是我的代码:
package processors;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDDocumentCatalog;
import org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm;
import org.apache.pdfbox.pdmodel.interactive.form.PDField;
import utils.BytesToFile;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
public class TemplateFiller {
public static final String LF = "\n";
public TemplateFiller( ) {
}
public ByteArrayOutputStream fill( ) throws IOException {
ClassLoader classLoader = TemplateFiller.class.getClassLoader( );
String templateFilePath = "pdf/form.template.pdf";
InputStream templateStream = classLoader.getResourceAsStream( templateFilePath );
String singleLine = "Single line";
StringBuilder stringBuilder = new StringBuilder( );
for ( int i = 1; i < 16; i++ ) {
stringBuilder.append( String.format( "This is line number %d.", i ) ).append( LF );
}
String multiLine = stringBuilder.toString( );
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream( );
try (
PDDocument doc = PDDocument.load( templateStream )
) {
PDDocumentCatalog pdDocCatalog = doc.getDocumentCatalog( );
PDAcroForm acroForm = pdDocCatalog.getAcroForm( );
// ===============================================
// Fill in the AcroFields
// ===============================================
PDField field1 = acroForm.getField( "Title" );
field1.setValue( singleLine );
PDField field2 = acroForm.getField( "Contents" );
field2.setValue( multiLine );
doc.save( byteArrayOutputStream );
}
return byteArrayOutputStream;
}
public static void main( String[] args ) throws IOException {
TemplateFiller templateFiller = new TemplateFiller( );
BytesToFile.writeToDirFile(
templateFiller.fill().toByteArray(),
"/Users/xxxxxxxxx/Downloads",
"filled.pdf"
);
}
}
@WebServlet( "/pdf-template-filled" )
public class PdfTemplateFilled extends BaseServlet {
@Override
protected void doGet(
HttpServletRequest request,
HttpServletResponse response
) throws IOException, ServletException {
TemplateFiller templateFiller = new TemplateFiller( );
ByteArrayOutputStream byteArrayOutputStream = templateFiller.fill( );
byte[] pdfBytes = byteArrayOutputStream.toByteArray( );
String fileName = "filled-form.pdf";
downloadPdfFile( request, response, fileName, pdfBytes );
}
}
谢谢你的帮助
共 (0) 个答案