将文本字段添加到pdfpcell的问题 [英] Issue with adding textfield to pdfpcell
问题描述
我正在使用itext生成可编辑的日历pdf。
我正在尝试使用此代码将TextField添加到PdfPCell ,
//为特定日期创建PdfPCell
public PdfPCell getDayCell(日历日历,区域设置区域设置){
PdfPCell cell = new PdfPCell() ;
cell.setPadding( 3 );
// 根据日期类型设置背景颜色
< span class =code-keyword> if (isSunday(calendar))
cell.setBackgroundColor(BaseColor.GRAY);
else if (isSpecialDay(calendar))
cell.setBackgroundColor(BaseColor。浅灰);
else
cell.setBackgroundColor(BaseColor.WHITE);
// 以语言环境的语言设置内容
Chunk chunk = new 块( String .format(locale, %1 $ ta,calendar),small);
chunk.setTextRise( 5 );
// 带有日期的段落
段落p = new 段落(chunk);
// 分隔符
p.add( new 块( new VerticalPositionMark()));
// 和当天的数量
p.add( new 块( String .format(locale, %1 $ te,calendar),normal));
cell.addElement(p);
cell.setCellEvent( new MyCellField(locale + 跨度> +日历));
cell.setFixedHeight( 80 );
返回单元格;
}
//将TextField添加到cellEvent
class MyCellField implements PdfPCellEvent {
protected String fieldname;
public MyCellField( String fieldname){
此 .fieldname = fieldname;
}
public void cellLayout(PdfPCell单元格,矩形矩形,PdfContentByte []画布){
final PdfWriter writer = canvases [ 0 ]。getPdfWriter( );
final TextField textField = new TextField(writer,rectangle) ,fieldname);
textField.setAlignment(Element.ALIGN_TOP);
textField.setOptions(TextField.MULTILINE);
尝试 {
final PdfFormField field = textField.getTextField();
writer.addAnnotation(field);
} catch ( final IOException ioe){
throw new ExceptionConverter(ioe);
} catch ( final DocumentException de){
throw new ExceptionConverter(de);
}
}
}
当我渲染日历pdf时,Cell焦点是垂直的,不是水平的。请帮助我找出我所缺少的东西。
参考
注意:我使用iText5
我尝试过:
我试过了,
float textboxheight = 12f;
矩形矩形=矩形;
rect.Bottom = rect.Top - textboxheight;
rect.Bottom显示错误最后一个字段Rectangle.BOTTOM无法分配。
ta,日历),小);
chunk.setTextRise( 5 );
// 带有日期的段落
段落p = new 段落(块);
// 分隔符
p.add( new 块( new VerticalPositionMark()));
// 和当天的数量
p.add( new 块( String .format(locale, %1
te,calendar),normal));
cell.addElement(p);
cell.setCellEvent( new MyCellField(locale + 跨度> +日历));
cell.setFixedHeight( 80 );
返回单元格;
}
//将TextField添加到cellEvent
class MyCellField implements PdfPCellEvent {
protected String fieldname;
public MyCellField( String fieldname){
此 .fieldname = fieldname;
}
public void cellLayout(PdfPCell单元格,矩形矩形,PdfContentByte []画布){
final PdfWriter writer = canvases [ 0 ]。getPdfWriter( );
final TextField textField = new TextField(writer,rectangle) ,fieldname);
textField.setAlignment(Element.ALIGN_TOP);
textField.setOptions(TextField.MULTILINE);
尝试 {
final PdfFormField field = textField.getTextField();
writer.addAnnotation(field);
} catch ( final IOException ioe){
throw new ExceptionConverter(ioe);
} catch ( final DocumentException de){
throw new ExceptionConverter(de);
}
}
}
当我渲染日历pdf时,Cell焦点是垂直的,不是水平的。请帮助我找出我所缺少的东西。
参考
注意:我使用iText5
我尝试过:
我试过了,
float textboxheight = 12f;
矩形矩形=矩形;
rect.Bottom = rect.Top - textboxheight;
rect.Bottom显示错误最后一个字段Rectangle.BOTTOM不能分配。
I'm using itext to generate editable Calendar pdf.
I'm trying to add TextField to the PdfPCell using this code,
//To create PdfPCell for a specific day
public PdfPCell getDayCell(Calendar calendar, Locale locale) {
PdfPCell cell = new PdfPCell();
cell.setPadding(3);
// set the background color, based on the type of day
if (isSunday(calendar))
cell.setBackgroundColor(BaseColor.GRAY);
else if (isSpecialDay(calendar))
cell.setBackgroundColor(BaseColor.LIGHT_GRAY);
else
cell.setBackgroundColor(BaseColor.WHITE);
// set the content in the language of the locale
Chunk chunk = new Chunk(String.format(locale, "%1$ta", calendar), small);
chunk.setTextRise(5);
// a paragraph with the day
Paragraph p = new Paragraph(chunk);
// a separator
p.add(new Chunk(new VerticalPositionMark()));
// and the number of the day
p.add(new Chunk(String.format(locale, "%1$te", calendar), normal));
cell.addElement(p);
cell.setCellEvent(new MyCellField(locale+""+calendar));
cell.setFixedHeight(80);
return cell;
}
// Adding TextField to the cellEvent
class MyCellField implements PdfPCellEvent {
protected String fieldname;
public MyCellField(String fieldname) {
this.fieldname = fieldname;
}
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_TOP);
textField.setOptions(TextField.MULTILINE);
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);
}
}
}
When I render the calendar pdf, the Cell focus is vertical, not horizontal. Kindly help me to find out what I'm missing.
Refer
NOTE: Im using iText5
What I have tried:
I have tried,
float textboxheight = 12f;
Rectangle rect = rectangle;
rect.Bottom = rect.Top - textboxheight;
rect.Bottom is showing error "The final field Rectangle.BOTTOM cannot be assigned".
ta", calendar), small); chunk.setTextRise(5); // a paragraph with the day Paragraph p = new Paragraph(chunk); // a separator p.add(new Chunk(new VerticalPositionMark())); // and the number of the day p.add(new Chunk(String.format(locale, "%1
te", calendar), normal)); cell.addElement(p); cell.setCellEvent(new MyCellField(locale+""+calendar)); cell.setFixedHeight(80); return cell; }
// Adding TextField to the cellEvent
class MyCellField implements PdfPCellEvent { protected String fieldname; public MyCellField(String fieldname) { this.fieldname = fieldname; } 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_TOP); textField.setOptions(TextField.MULTILINE); 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); } } }
When I render the calendar pdf, the Cell focus is vertical, not horizontal. Kindly help me to find out what I'm missing.
Refer
NOTE: Im using iText5
What I have tried:
I have tried,
float textboxheight = 12f; Rectangle rect = rectangle; rect.Bottom = rect.Top - textboxheight;
rect.Bottom is showing error "The final field Rectangle.BOTTOM cannot be assigned".
这篇关于将文本字段添加到pdfpcell的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!