1

我制作了一个带有多行文本框的 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);
    }
}
4

3 回答 3

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();

现在您的文本将被正确包装而不会破坏连字。

于 2014-06-18T10:51:57.323 回答
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();
            }
        }

    }
}
于 2014-06-18T11:58:15.027 回答
0

我有同样的问题。您必须通过 adobe acrobat pro 重新创建 template.pdf,并将文本框的字体设置为操作系统中已知的字体之一,例如 arial。

祝你好运。

于 2016-07-09T13:55:37.343 回答