2017-02-10 62 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); 
    } 
}