我制作了一个带有多行文本框的pdf模板,并且必须使用PDFStamper在Acrofields中设置一些阿拉伯数据。 文本的运行方向对于第一行是正确的,但是当文本换行发生时它会改变。
请指导。
package test;
import java.io.FileOutputStream;
import java.io.IOException;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.pdf.AcroFields;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
public class TextFields{
public static final String RESULT1 = "D:/template.pdf";
public static final String RESULT2 = "D:/pdf/result.pdf";
protected int tf;
public TextFields(int tf) {
this.tf = tf;
}
public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
PdfReader reader = new PdfReader(src);
FontFactory.registerDirectories();
BaseFont unicode = null;
unicode = BaseFont.createFont("D:/arialuni.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
AcroFields form = stamper.getAcroFields();
stamper.getAcroFields().addSubstitutionFont(unicode);
form.setField("TextBox","اب اب اب اب اب اب اب اب اب اب اب اب اب اب اب اب اب");
stamper.close();
reader.close();
}
public static void main(String[] args) throws DocumentException, IOException {
TextFields example = new TextFields(0);
example.manipulatePdf(RESULT1, RESULT2);
}
}
答案 0 :(得分:1)
我看到你没有展平文件。如果您的意图是文档保持交互,则应指示iText不要生成任何外观。这可以这样做:
form.setGenerateAppearances(false);
通过这样做,您可以告诉PDF查看器创建外观。如果问题仍然存在,则问题出在PDF查看器中,而不是iText级别。
如果您打算压扁表单,我担心您在当前版本的iText中遇到了限制。您需要使用变通方法。请查看FillFlattenMerge3示例。在这个例子中,我们得到每个字段的位置:
AcroFields form = reader.getAcroFields();
positions = new HashMap<String, Rectangle>();
Rectangle rectangle;
Map<String, AcroFields.Item> fields = form.getFields();
for (String name : fields.keySet()) {
rectangle = form.getFieldPositions(name).get(0).position;
positions.put(name, rectangle);
}
我们使用这些位置使用ColumnText
对象在适当的位置绘制字段的内容,而不是使用setField()
方法:
Rectangle rect = positions.get(name);
Phrase p = new Phrase(value, FONT);
ColumnText.showTextAligned(cb, Element.ALIGN_LEFT,
p, rect.getLeft() + 2, rect.getBottom() + 2, 0);
这还没有解决您的问题,因为showTextAligned()
方法不会包装文本。您需要使用showTextAligned()
,setRunDirection()
,addElement()
和setSimpleColumn()
方法,而不是使用go()
方法:
ColumnText column = new ColumnText(writer.getDirectContent());
column.setSimpleColumn(rect);
column.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
column.addElement(new Paragraph("اب اب اب اب اب اب اب اب اب اب اب اب اب اب اب اب اب", unicode));
column.go();
现在,您的文字将被正确包裹而不会破坏连字。
答案 1 :(得分:0)
以下代码适用于我。
我在test.css中使用了p {font-family:“arial”;}。
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Font.FontFamily;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.AcroFields;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfImportedPage;
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.ElementList;
import com.itextpdf.tool.xml.XMLWorker;
import com.itextpdf.tool.xml.XMLWorkerHelper;
import com.itextpdf.tool.xml.css.CssFile;
import com.itextpdf.tool.xml.html.Tags;
import com.itextpdf.tool.xml.parser.XMLParser;
import com.itextpdf.tool.xml.pipeline.css.CSSResolver;
import com.itextpdf.tool.xml.pipeline.css.CssResolverPipeline;
import com.itextpdf.tool.xml.pipeline.end.ElementHandlerPipeline;
import com.itextpdf.tool.xml.pipeline.html.HtmlPipeline;
import com.itextpdf.tool.xml.pipeline.html.HtmlPipelineContext;
public class FillFlattenMerge3 {
public static final String SRC = "C:/templatestnew.pdf";
public static final String DEST = "C:/NewFile.pdf";
public static final Font FONT = new Font(FontFamily.HELVETICA, 10);
protected Map<String, Rectangle> positions;
public static void main(String[] args) throws IOException,
DocumentException {
File file = new File(DEST);
file.getParentFile().mkdirs();
new FillFlattenMerge3().manipulatePdf(SRC, DEST);
}
public class Background extends PdfPageEventHelper {
PdfImportedPage background;
public Background(PdfImportedPage background) {
this.background = background;
}
@Override
public void onEndPage(PdfWriter writer, Document document) {
PdfContentByte cb = writer.getDirectContentUnder();
cb.addTemplate(background, 0, 0);
}
}
public void manipulatePdf(String src, String dest)
throws DocumentException, IOException {
PdfReader reader = new PdfReader(src);
AcroFields form = reader.getAcroFields();
positions = new HashMap<String, Rectangle>();
Rectangle rectangle;
Map<String, AcroFields.Item> fields = form.getFields();
for (String name : fields.keySet()) {
rectangle = form.getFieldPositions(name).get(0).position;
positions.put(name, rectangle);
}
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document,
new FileOutputStream(dest));
writer.setPageEvent(new Background(writer.getImportedPage(reader, 1)));
document.open();
PdfContentByte cb = writer.getDirectContent();
process(cb, "TextBox", "<p> اب اب اب اب اب اب اب اب اب اب اب اب اب اب اب اب اب اب اب اب اب اب اب اب اب اب اب اب اب اب اب اب اب اب اب اب اب اب اب اب اب اب اب اب اب اب اب اب اب اب اب اب اب اب اب اب اب اب اب اب اب اب اب اب اب اب اب اب اب اب </p>", writer, document);
document.close();
reader.close();
}
protected void process(PdfContentByte cb, String name, String value,
PdfWriter writer, Document document) throws DocumentException,
IOException {
Rectangle rect = positions.get(name);
// mycode starts
XMLWorkerHelper helper = XMLWorkerHelper.getInstance();
// CSS
CSSResolver cssResolver = helper.getDefaultCssResolver(true);
CssFile cssFile = helper.getCSS(new FileInputStream(
"D:\\Itext_Test\\Test\\src\\test.css"));
cssResolver.addCss(cssFile);
// HTML
HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);
htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());
htmlContext.autoBookmark(false);
// Pipelines
ElementList elements = new ElementList();
ElementHandlerPipeline end = new ElementHandlerPipeline(elements, null);
HtmlPipeline html = new HtmlPipeline(htmlContext, end);
CssResolverPipeline css = new CssResolverPipeline(cssResolver, html);
// XML Worker
XMLWorker worker = new XMLWorker(css, true);
XMLParser parser = new XMLParser(worker);
// mycode ends
ColumnText column = new ColumnText(writer.getDirectContent());
column.setSimpleColumn(rect);
column.setRunDirection(PdfWriter.RUN_DIRECTION_LTR);
ByteArrayInputStream is = new ByteArrayInputStream(
value.getBytes("UTF-8"));
parser.parse(is);
int status = ColumnText.START_COLUMN;
for (Element e : elements) {
if (ColumnText.isAllowedElement(e)) {
column.addElement(e);
status = column.go();
}
}
}
}
答案 2 :(得分:0)