2017-02-10 53 views
0

我编程方式创建PDF,设置文档方向是这样的:iText的PDF格式字段没有填写文档方向

public Document createStableFordPDF(Context ctx){ 
    Document document = new Document(PageSize.A4.rotate()); 
    try { 
     PdfWriter writer = PdfWriter.getInstance(document, 
       new FileOutputStream(ctx.getExternalCacheDir().getAbsolutePath()+"/StableFord.pdf")); 

     document.open(); 
... 

创建的文档看起来不错: enter image description here

当我尝试填补空白领域,他们出现在错误的方向

public void fillStablefordPDF(Context ctx) { 
    OutputStream output = null; 
    try { 
     output = new FileOutputStream(ctx.getExternalCacheDir().getAbsolutePath() + "/StableFord_filled.pdf"); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } 

    try { 
     PdfReader reader = new PdfReader(new FileInputStream(ctx.getExternalCacheDir().getAbsolutePath() + "/StableFord.pdf")); 
     PdfStamper stamper = new PdfStamper(reader, output); 
     AcroFields acroFields = stamper.getAcroFields(); 
     ... 
     acroFields.setField("Name", "TOM"); 

见创建的文档: enter image description here

我应该怎么做,以正确的方向填补这些领域?

+1

你需要旋转值添加到字段:你的填充PDF是输出如预期的结构参数,我会看看我是否可以挖掘一个例子 –

+1

或者简单地用'new RectangleReadOnly(842,595)'替换'PageSize.A4.rotate()'。 – mkl

回答

0

塞缪尔说 我PdfCellEvent I类由roration设置为我的专业领域取得: 公共类MyCellField实现PdfPCellEvent {

public MyCellField(String fieldname) { 
    this.fieldname = fieldname; 
    this.color = BaseColor.BLACK; 
} 

@Override 
public void cellLayout(PdfPCell cell, Rectangle rectangle, PdfContentByte[] canvases) { 
    final PdfWriter writer = canvases[0].getPdfWriter(); 
    final TextField textField = new TextField(writer, rectangle, fieldname); 
    textField.setAlignment(Element.ALIGN_CENTER); 
    textField.setTextColor(color); 
    textField.setFontSize(10); 
    textField.setRotationFromPage(PageSize.A4.rotate()); 
try { 
     final PdfFormField field = textField.getTextField(); 
     writer.addAnnotation(field); 
    } catch (final IOException ioe) { 
     throw new ExceptionConverter(ioe); 
    } catch (final DocumentException de) { 
     throw new ExceptionConverter(de); 
    } 
}