I can set hyperlink for cell in Apache POI, but I don't know how to put the hyperlink into an image (I'm using XSSF)
Here is the function of putting cell hyperlink :
/**
* Helper function for putting hyperlink into specified cell
* #param label
* #param value
* #param col
* #param row
* #param sheet
*/
private static void putImageHyperlink(Cell cell, CellStyle hyperlinkStyle, String value, Workbook wb) {
try {
CreationHelper createHelper = wb.getCreationHelper();
Hyperlink link = createHelper.createHyperlink(Hyperlink.LINK_URL);
link.setAddress(value);
cell.setHyperlink(link);
cell.setCellStyle(hyperlinkStyle);
// Put hyperlink value
cell.setCellValue(value);
} catch (Exception e) {
log.severe("Can't create hyperlink : " + Utils.exceptionToString(e));
}
}
And here is the function for putting image into specified cell :
/**
* Put image into sheet at position [row,col]
* #param sheet
* #param col
* #param row
* #param imgData
* #throws Exception
*/
private static void putImage(Workbook wb, Sheet sheet, int col, int row, byte[] imgData) throws Exception {
try {
Drawing drawing = sheet.createDrawingPatriarch();
int pictureIdx = wb.addPicture(imgData, Workbook.PICTURE_TYPE_PICT);
CreationHelper helper = wb.getCreationHelper();
ClientAnchor anchor4 = helper.createClientAnchor();
//set top-left corner of the picture,
//subsequent call of Picture#resize() will operate relative to it
anchor4.setCol1(col);
anchor4.setRow1(row);
anchor4.setCol2(col+1);
anchor4.setRow2(row+1);
drawing.createPicture(anchor4, pictureIdx);
} catch (Exception ex) {
log.severe("Exception : " + Utils.exceptionToString(ex));
}
}
=> How to put hyperlink into picture created by command drawing.createPicture(anchor4, pictureIdx); ?
Thanks in advance!
When looking at how Excel stores this it seems to be stored differently for Images in the xl\drawings\_rels\drawing1.xml.rels and xl\drawings\drawing1.xml part of the XLSX file:
<Relationship Id="rId1" Target="http://poi.apache.org" TargetMode="External" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink"/>
<xdr:cNvPr descr="Picture" id="2" name="Picture 1">
<a:hlinkClick xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" r:id="rId1"/>
</xdr:cNvPr>
POI does not support adding such hyperlinks via it's API yet, but you can use the underlying lowlevel-API as follows to first create the relationship for the hyperlink and then set the relation to the hyperlink in the Picture-object:
PackageRelationship rel = ((XSSFDrawing)patriarch).getPackagePart().addRelationship(
new URI("http://poi.apache.org"),
TargetMode.EXTERNAL, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink");
((XSSFDrawing)patriarch).addRelation(rel.getId(),new POIXMLDocumentPart());
CTPictureNonVisual nvPicPr = ((XSSFPicture)picture).getCTPicture().getNvPicPr();
CTHyperlink hLinkClick = nvPicPr.getCNvPr().addNewHlinkClick();
hLinkClick.setId(rel.getId());
Related
How can I strikethrough diagonally a cell in excel using Apache poi?
I want to get something like this:
I have tried:
cell.getCellStyle().setFillPattern(CellStyle.THIN_BACKWARD_DIAG)
but then the cell is having many diagonal lines instead of one from top right to bottom left.
Edit:
This is how I create the workbook:
Workbook workbook = new SXSSFWorkbook(SXSSFWorkbook.DEFAULT_WINDOW_SIZE);
Sheet sheet = workbook.createSheet("Test");
Then creating the columns and the rows, I don't see there any special to show.
Any idea what can I do?
Sorry for my previous answer, currently there is no diagonal border available readily in apache poi, though we can write our own custom method. You can use below method which will add a diagonal border in a cell.
public static void setDiagonal(StylesTable stylesSource, CTXf cellXtraFormating, ThemesTable theme) {
CTBorder ct = CTBorder.Factory.newInstance();
CTBorderPr pr = ct.addNewDiagonal();
ct.setDiagonalUp(true);
pr.setStyle(STBorderStyle.Enum.forInt(BorderFormatting.BORDER_THIN + 1));
int idx = stylesSource.putBorder(new XSSFCellBorder(ct, theme));
cellXtraFormating.setBorderId(idx);
cellXtraFormating.setApplyBorder(true);
}
call the above method in your class like this
CellStyle cs = workbook.createCellStyle();
StylesTable styleSource = ((XSSFWorkbook) workbook).getStylesSource();
ThemesTable theme = styleSource.getTheme();
CTXf cellXtraFormating = ((XSSFCellStyle) cs).getCoreXf();
-------your custom code---------
setDiagonal(styleSource, cellXtraFormating, theme);
cell.setCellStyle(cs);
I hope this answers your question
I'm trying to read data from a docx file using java. The data is is tables. Is there a way to iterate through the table cells and extract the cell data?
You have two choices:
Write a library that can open, read, and manipulate Word documents, for some definitions of "open", "read", and "manipulate", and some definition of "Word document".
Find a third-party library that can do some or all of what is defined in (1).
Search "java docx library" in your favourite web search and see what comes up.
This should help you.
http://svn.apache.org/repos/asf/poi/trunk/src/examples/src/org/apache/poi/xwpf/usermodel/SimpleTable.java
The above was achieved using Apache POI
Here is the code from the link:
package org.apache.poi.xwpf.usermodel;
import java.io.FileOutputStream;
import java.math.BigInteger;
import java.util.List;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHeight;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTShd;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTString;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTcPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTrPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTVerticalJc;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STShd;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STVerticalJc;
/**
* This program creates a simple WordprocessingML table using POI XWPF API, and
* a more complex, styled table using both XWPF and ooxml-schema. It's possible
* that not all referenced wordprocessingml classes are defined in
* poi-ooxml-schemas-3.8-beta4. If this is the case, you'll need to use the full
* ooxml-schemas.jar library.
*
* #author gisella bronzetti (original)
* #author Gregg Morris (styled table)
*/
public class SimpleTable {
public static void main(String[] args) throws Exception {
try {
createSimpleTable();
}
catch(Exception e) {
System.out.println("Error trying to create simple table.");
throw(e);
}
try {
createStyledTable();
}
catch(Exception e) {
System.out.println("Error trying to create styled table.");
throw(e);
}
}
public static void createSimpleTable() throws Exception {
XWPFDocument doc = new XWPFDocument();
XWPFTable table = doc.createTable(3, 3);
table.getRow(1).getCell(1).setText("EXAMPLE OF TABLE");
// table cells have a list of paragraphs; there is an initial
// paragraph created when the cell is created. If you create a
// paragraph in the document to put in the cell, it will also
// appear in the document following the table, which is probably
// not the desired result.
XWPFParagraph p1 = table.getRow(0).getCell(0).getParagraphs().get(0);
XWPFRun r1 = p1.createRun();
r1.setBold(true);
r1.setText("The quick brown fox");
r1.setItalic(true);
r1.setFontFamily("Courier");
r1.setUnderline(UnderlinePatterns.DOT_DOT_DASH);
r1.setTextPosition(100);
table.getRow(2).getCell(2).setText("only text");
FileOutputStream out = new FileOutputStream("simpleTable.docx");
doc.write(out);
out.close();
}
/**
* Create a table with some row and column styling. I "manually" add the
* style name to the table, but don't check to see if the style actually
* exists in the document. Since I'm creating it from scratch, it obviously
* won't exist. When opened in MS Word, the table style becomes "Normal".
* I manually set alternating row colors. This could be done using Themes,
* but that's left as an exercise for the reader. The cells in the last
* column of the table have 10pt. "Courier" font.
* I make no claims that this is the "right" way to do it, but it worked
* for me. Given the scarcity of XWPF examples, I thought this may prove
* instructive and give you ideas for your own solutions.
* #throws Exception
*/
public static void createStyledTable() throws Exception {
// Create a new document from scratch
XWPFDocument doc = new XWPFDocument();
// -- OR --
// open an existing empty document with styles already defined
//XWPFDocument doc = new XWPFDocument(new FileInputStream("base_document.docx"));
// Create a new table with 6 rows and 3 columns
int nRows = 6;
int nCols = 3;
XWPFTable table = doc.createTable(nRows, nCols);
// Set the table style. If the style is not defined, the table style
// will become "Normal".
CTTblPr tblPr = table.getCTTbl().getTblPr();
CTString styleStr = tblPr.addNewTblStyle();
styleStr.setVal("StyledTable");
// Get a list of the rows in the table
List<XWPFTableRow> rows = table.getRows();
int rowCt = 0;
int colCt = 0;
for (XWPFTableRow row : rows) {
// get table row properties (trPr)
CTTrPr trPr = row.getCtRow().addNewTrPr();
// set row height; units = twentieth of a point, 360 = 0.25"
CTHeight ht = trPr.addNewTrHeight();
ht.setVal(BigInteger.valueOf(360));
// get the cells in this row
List<XWPFTableCell> cells = row.getTableCells();
// add content to each cell
for (XWPFTableCell cell : cells) {
// get a table cell properties element (tcPr)
CTTcPr tcpr = cell.getCTTc().addNewTcPr();
// set vertical alignment to "center"
CTVerticalJc va = tcpr.addNewVAlign();
va.setVal(STVerticalJc.CENTER);
// create cell color element
CTShd ctshd = tcpr.addNewShd();
ctshd.setColor("auto");
ctshd.setVal(STShd.CLEAR);
if (rowCt == 0) {
// header row
ctshd.setFill("A7BFDE");
}
else if (rowCt % 2 == 0) {
// even row
ctshd.setFill("D3DFEE");
}
else {
// odd row
ctshd.setFill("EDF2F8");
}
// get 1st paragraph in cell's paragraph list
XWPFParagraph para = cell.getParagraphs().get(0);
// create a run to contain the content
XWPFRun rh = para.createRun();
// style cell as desired
if (colCt == nCols - 1) {
// last column is 10pt Courier
rh.setFontSize(10);
rh.setFontFamily("Courier");
}
if (rowCt == 0) {
// header row
rh.setText("header row, col " + colCt);
rh.setBold(true);
para.setAlignment(ParagraphAlignment.CENTER);
}
else if (rowCt % 2 == 0) {
// even row
rh.setText("row " + rowCt + ", col " + colCt);
para.setAlignment(ParagraphAlignment.LEFT);
}
else {
// odd row
rh.setText("row " + rowCt + ", col " + colCt);
para.setAlignment(ParagraphAlignment.LEFT);
}
colCt++;
} // for cell
colCt = 0;
rowCt++;
} // for row
// write the file
FileOutputStream out = new FileOutputStream("styledTable.docx");
doc.write(out);
out.close();
}
}
I want to export data to excel using Apache poi. Now the problem that I am facing is that I am unable to merge rows and align them in the center.
Code for export data is:
List<LinkedHashMap<String,Object>> lstReportHeader = null;
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet();
//Set Header Font
HSSFFont headerFont = wb.createFont();
headerFont.setBoldweight(headerFont.BOLDWEIGHT_BOLD);
headerFont.setFontHeightInPoints((short) 12);
//Set Header Style
CellStyle headerStyle = wb.createCellStyle();
headerStyle.setFillBackgroundColor(IndexedColors.BLACK.getIndex());
headerStyle.setAlignment(headerStyle.ALIGN_CENTER);
headerStyle.setFont(headerFont);
headerStyle.setBorderBottom(HSSFCellStyle.BORDER_MEDIUM);
int rowCount= 0;
Row header;
header = sheet.createRow(0);//its for header
Cell cell ;//= header.createCell(0);
for(int j = 0;j < 4; j++) {
cell = header.createCell(j);
if(j == 0) {
cell.setCellValue("ItemWise List");
}
cell.setCellStyle(headerStyle);
}
sheet.addMergedRegion(new CellRangeAddress(rowCount, rowCount, 0, lstReportFormHeader.size()-1));
header = sheet.createRow(0);
cell = header.createCell(0);
cell.setCellValue("Sr. No");
cell = header.createCell(1);
cell.setCellValue("Item Name");
cell = header.createCell(2);
cell.setCellValue("Qty");
cell = header.createCell(3);
cell.setCellValue("Rate");
Now I want to ItemWise List merge and make it align center.
My solution was to merge the cells by their positions, then created a cell (reference to the first block of the merged cells) to assign a value and then set the alignment throught the CellUtil
// Merges the cells
CellRangeAddress cellRangeAddress = new CellRangeAddress(start, start, j, j + 1);
sheet.addMergedRegion(cellRangeAddress);
// Creates the cell
Cell cell = CellUtil.createCell(row, j, entry.getKey());
// Sets the allignment to the created cell
CellUtil.setAlignment(cell, workbook, CellStyle.ALIGN_CENTER);
Merge like:::
Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet("new sheet");
Row row = sheet.createRow((short) 1);
Cell cell = row.createCell((short) 1);
cell.setCellValue("This is a test of merging");
sheet.addMergedRegion(new CellRangeAddress(
1, //first row (0-based)
1, //last row (0-based)
1, //first column (0-based)
2 //last column (0-based)
));
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
For aligning also check the below official link of Apache poi:::
http://poi.apache.org/spreadsheet/quick-guide.html#Alignment
After study I found that after merging 7 cells, merged cell id will be 0 so I applied following style to cell id 0 using following style.
headerStyle.setAlignment(headerStyle.ALIGN_CENTER);
This worked for me and I think it's cleaner:
/**
* Merge and center the cells specified by range
* #param startCell the first cell in the cells to be merged
* #param range the range of the cells to be merged
*/
private static void mergeAndCenter(Cell startCell, CellRangeAddress range) {
startCell.getSheet().addMergedRegion(range);
CellStyle style = startCell.getSheet().getWorkbook().createCellStyle();
style.setAlignment(CellStyle.ALIGN_CENTER);
style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
startCell.setCellStyle(style);
}
As per my understanding, you have start and end cells for merging and you want to merge the cell ranges and align the cell content. If I am right, you can use the following method:
/**
* #param startCell: first cell of merging area
* #param endCell: last cell of merging area
*/
public static void mergeAndAlignCenter(HSSFCell startCell, HSSFCell endCell){
//finding reference of start and end cell; will result like $A$1
CellReference startCellRef= new CellReference(startCell.getRowIndex(),startCell.getColumnIndex());
CellReference endCellRef = new CellReference(endCell.getRowIndex(),endCell.getColumnIndex());
// forming string of references; will result like $A$1:$B$5
String cellRefernce = startCellRef.formatAsString()+":"+endCellRef.formatAsString();
//removing $ to make cellRefernce like A1:B5
cellRefernce = cellRefernce.replace("$","");
//passing cellRefernce to make a region
CellRangeAddress region = CellRangeAddress.valueOf(cellRefernce);
//use region to merge; though other method like sheet.addMergedRegion(new CellRangeAddress(1,1,4,1));
// is also available, but facing some problem right now.
startCell.getRow().getSheet().addMergedRegion( region );
//setting alignment to center
CellUtil.setAlignment(startCell, wb, CellStyle.ALIGN_CENTER);
}
Well what worked for me is to set all the merged cells' Cellstyle to CENTER ALIGN. Whether you put the XSSFSheet.addMergedRegion() method before or after setting the cellstyle values to center don't matter.
private void insertXlsHeader(XSSFSheet sheet){
....
//first cell for row1
cell = row1.createCell(colstart);
cell.setCellType(org.apache.poi.ss.usermodel.Cell.CELL_TYPE_STRING);
cell.setCellValue("COURSES");
setHeaderCellStyle(sheet,cell);
//first cell for row2
cell = row2.createCell(colstart);
setHeaderCellStyle(sheet,cell);
//first cell for row3
cell = row3.createCell(colstart);
setHeaderCellStyle(sheet,cell);
//merged the first cells of rows 1 to 3
sheet.addMergedRegion(new CellRangeAddress(ROW1, ROW3, colstart, colstart));
...
}
private void setHeaderCellStyle(XSSFSheet sheet,org.apache.poi.ss.usermodel.Cell cell) {
CellStyle s = null;
s = sheet.getWorkbook().createCellStyle();
cell.setCellStyle(s);
Font f = sheet.getWorkbook().createFont();
f.setBoldweight(Font.BOLDWEIGHT_BOLD);
s.setBorderBottom(CellStyle.BORDER_THIN);
s.setBorderLeft(CellStyle.BORDER_THIN);
s.setBorderRight(CellStyle.BORDER_THIN);
s.setBorderTop(CellStyle.BORDER_THIN);
s.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
s.setAlignment(CellStyle.ALIGN_CENTER);
s.setFont(f);
}
As answered above, merging cells can be achieved using
sheet.addMergedRegion(new CellRangeAddress(frstRow, lastRow, firstColumnIndex, lastColumnIndex));
But for aligning cells vertically,recently I faced similar issue and I tried above answer, but using
CellUtil.setAlignment(dataCell, workbook, CellStyle.VERTICAL_CENTER);
aligned Date formatted cells to Horizontal Left aligned. So I used following method to set only Vertical Alignment of Cell content.
CellUtil.setCellStyleProperty(dataCell, workbook,CellUtil.VERTICAL_ALIGNMENT,CellStyle.VERTICAL_CENTER);
I hope this helps!!
Happy Coding
Use
style.setVerticalAlignment()
to set the vertical alignments instead of
style.setAlignment().
We can merge the column along with we can vertically and horizontally align too.
I had the rows 2 to 10 of column A having the same values.
I used the below code to merge the data where the variable sheet is XSSFSheet. The parameters of CellRangeAddress have the parameters are start row, last row, start column and last column. In my example, the value USA starts from 2nd row (index is 1) and the last value of USA is in 10th row and column is the 1st column.
CellRangeAddress ca = new CellRangeAddress(1,9,0,0);
sheet.addMergedRegion(ca);
When I executed the above code, the cell was merged but the text was not aligned to center.
To overcome this issue, I utilized the class CellStyle and Cell. Get the 2nd row of 1st column text to cell variable. Now set the vertical and horizontal alignment to the cellStyle and set this style to the cell which will align the text to the center.
Cell cell = sheet.getRow(1).getCell(0);
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
cellStyle.setAlignment(HorizontalAlignment.CENTER);
cell.setCellStyle(cellStyle);
Below is the final result
Additional references :
Jar files used
Import statements
I'm trying to create an PDF document using iText. I followed to THIS nice tutorial and tried to create single page pdf document which has a table. In the tutorial the author keeps table creation of table on separate method such as addMetaData, addTitlePage and addContent. I also would keep them separately, but I'm new to iText and currently I'm stuck. The current code is:
public static void main(String args[]) {
try {
Document document = new Document(PageSize.A4);
PdfWriter.getInstance(document, new FileOutputStream(FILE));
document.open();
addMetaData(document);
addTitlePage(document);
addContent(document);
document.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private static void addTitlePage(Document document) throws DocumentException {
Paragraph preface = new Paragraph();
// Add one empty line
addEmptyLine(preface, 1);
// Header of the document
preface.add(new Paragraph("Title here", capFont));
addEmptyLine(preface, 1);
// Report generated by: _name, _date
preface.add(new Paragraph("Report generated by: " + System.getProperty("user.name") + ", " + new Date(), //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
normFont));
addEmptyLine(preface, 2);
preface.add(new Paragraph("This document describes some kind of price list which is unknown to me.", normFont));
document.add(preface);
}
private static void addContent(Document document) throws DocumentException {
Paragraph content = new Paragraph();
// Add one empty line
addEmptyLine(content, 1);
// Content of the document
content.add(new Paragraph(createTable(subPart), normFont)); // not working line
addEmptyLine(content, 5);
content.add(new Paragraph("This document is a preliminary version and not subject to the license agreement.", redFont));
document.add(content);
}
private static void createTable(Section subPart) throws BadElementException {
PdfPTable table = new PdfPTable(3);
table.setHorizontalAlignment(Element.ALIGN_CENTER);
// Data
table.addCell("1");
table.addCell("2");
table.addCell("3");
subPart.add(table);
}
Any help would be appreciated.
So, after one day literature reading and api homepage visiting I came to my solution:
Instead of: content.add(new Paragraph(createTable(subPart), normFont));
I have now: createTable(content);
and of course I changed the type of variable in createTable method to the paragraph in order to get it working.
package src.AutosysPolicyWriter.Utility;
import java.util.StringTokenizer;
import com.lowagie.text.Cell;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.Phrase;
import com.lowagie.text.Rectangle;
import com.lowagie.text.Table;
/**
* Modification/itext 1.4 - <Modified : September 26, 2013>
* #author Oliver Lundag
* #date 2013-09-06
* Can handle table creation in the PDF
*/
public class PdfTableUtility {
/***
*
* #param titleFont - font of the title of the table
* #param fontHeader - font of the headers
* #param fontData - font of the data
* #param thisReport - Document
* #param tableTitle - Name of the table
* #param headerStrings - headers
* #param data - data
*
* How to use this function;
*
* Example:
*
* // Initialize PdfTableUtility object
* 1. PdfTableUtility tableUtility = new PdfTableUtility();
*
* // Declare the value for headers.
* // Note: Number of columns will depends on how many headers has been declared
* 2. String[] headers = {"Customer Name","Age","Plan","Amount"};
*
* // Declare the data that will be put inside
* // Note that the arrangement of the strings are the actual display in the pdf
* // ; - separator
* 3. String data = "1.Oliver Lundag;26;Plan A;250,000;"+
* "2.Oliver Lundag;26;Plan A;250,000;"+
* "3.Oliver Lundag;26;Plan A;250,000";
*
* //call the function with specified arguments
* //arguments will depend on developers perspective
* 4. tableUtility.displaytable(FontBold11, FontBold9, FontNormal9, thisReport, "Information", 4, headers, data);
*/
public void displaytable(Font titleFont, Font fontHeader, Font fontData, Document thisReport,String tableTitle, String[] headerStrings, String data) {
try{
//START-(Modification/itext 1.4) SR-CS-13035 - OLUND <Modified : September 26, 2013> - title
//1.create table
Table title = new Table(1);
title.setLastHeaderRow(1);
title.setOffset(12f);
title.setSpaceInsideCell(1f);
title.setBorder(Rectangle.NO_BORDER);
//2.create table
Cell celltitle = new Cell(new Phrase(tableTitle, titleFont));
celltitle.setLeading(12);
celltitle.setBorder(Rectangle.NO_BORDER);
celltitle.setHorizontalAlignment(Element.ALIGN_CENTER);
title.addCell(celltitle);
//3.add the title in document
thisReport.add(title);
//END -(Modification/itext 1.4) SR-CS-13035 - OLUND <Modified : September 26, 2013> - title
//START-(Modification/itext 1.4) SR-CS-13035 - OLUND <Modified : September 26, 2013> - data
//1. get the max number of columns
int numColumns = headerStrings.length;
//2. create a table
Table table = new Table(numColumns);
table.setOffset(12f);
table.setLastHeaderRow(1);
table.setSpaceInsideCell(1f);
table.setTableFitsPage(true);
table.setAutoFillEmptyCells(true);
//3.get headers and add it into cells
for (String header : headerStrings) {
Cell headerCell = new Cell(new Phrase(header, fontHeader));
headerCell.setLeading(12);
headerCell.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(headerCell);
}
//4.get data and add it into cells
String strToken;
if (data.length() > 0) {
StringTokenizer stStr = new StringTokenizer(data,";",false);
while(stStr.hasMoreTokens()){
strToken = stStr.nextToken().toString();
Cell cell = new Cell(new Phrase(strToken,fontData));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setLeading(12);
table.addCell(cell);
}
}
//5. add the table in the document
thisReport.add(table);
//END - (Modification/itext 1.4) SR-CS-13035 - OLUND <Modified : September 26, 2013> - data
}catch (Exception e) {
e.printStackTrace();
}
}
}
I'm generating a PDF using iText and wanted to include an iText Form and TextFields so that users can fill in the PDF Form electronically, rather than printing it out.
I've followed the Examples here: http://itextpdf.com/examples/iia.php?id=161
and here: http://itextpdf.com/examples/iia.php?id=162
So far, I have a TextField on my Landscape PDF, and I can click and enter text.
I have two issues:
TextField alignment. To click on the text field, I have to position my mouse to the right of the cell that I added the TextField too. How can I align the TextField with the cell?
Entered Text Rotation. Once I've entered text, it's displayed rotated and 90 degrees to the rest of the page. How can I set the rotation of this displayed text?
The rotation of the entered text is the thing that bugs me th most. I've tried setting the rotation on the cell and the TextField but that seems to have no effect.
Any suggestions?
Thanks
Here is my code:
int rotation = document.getPageSize().getRotation();
PdfFormField pdfForm = PdfFormField.createEmpty(docWriter);
coverSheetPdfForm.setFieldName("Form");
coverSheetPdfForm.setRotate(rotation);
...
cell = new PdfPCell(borderedTable("Cell Title:", tblHeadingFont, "", borderColor));
cell.setColspan(1);
cell.setPadding(5f);
cell.setBorderWidth(0f);
cell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
cell.setMinimumHeight(100f);
TextField textField = new TextField(docWriter, new Rectangle(50, 50,rotation), "cell_text_field");
textField.setFontSize(12);
textField.setRotation(rotation);
textField.setVisibility(TextField.VISIBLE);
textField.setBorderColor(borderColor);
textField.setBorderWidth(BORDER_WIDTH);
cell.setCellEvent(new ChildFieldEvent(pdfForm, textField.getTextField(), 1, rotation));
.....
docWriter.addAnnotation(pdfForm);
and the ChildFieldEvent code that I've borrowed from the example page, and added a extra parameter for rotation:
/**
* Creates a ChildFieldEvent.
* #param parent the parent field
* #param kid the child field
* #param padding a padding
* #param rotation
*/
public ChildFieldEvent(PdfFormField parent, PdfFormField kid, float padding, int rotation) {
this.parent = parent;
this.kid = kid;
this.padding = padding;
this.rotation = rotation;
}
/**
* Add the child field to the parent, and sets the coordinates of the child field.
* #param cell
* #param rect
* #param cb
* #see com.lowagie.text.pdf.PdfPCellEvent#cellLayout(com.lowagie.text.pdf.PdfPCell,
* com.lowagie.text.Rectangle, com.lowagie.text.pdf.PdfContentByte[])
*/
#Override
public void cellLayout(PdfPCell cell, Rectangle rect, PdfContentByte[] cb) {
try {
parent.addKid(kid);
kid.setWidget(new Rectangle(rect.getLeft(padding), rect.getBottom(padding),
rect.getRight(padding), rect.getTop(padding),rotation),
PdfAnnotation.HIGHLIGHT_INVERT);
} catch (Exception e) {
throw new ExceptionConverter(e);
}
}
I've done some more testing and it looks like the Ubuntu PDF fiewer (Document Viewer) is not rendering the PDF correctly as viewing the same file in Windows Adobe PDF Viewer, the form looks fine.
I'll do more testing and report back.