[prev in list] [next in list] [prev in thread] [next in thread] 

List:       poi-user
Subject:    XWPF update table cell in header only appending, not overwriting
From:       Kent Johnson <kentoj () gmail ! com>
Date:       2015-02-19 16:30:59
Message-ID: FBB9B309-91A7-4CF1-A4DE-ACAC6C321CEB () gmail ! com
[Download RAW message or body]


Hi,
Can anyone here point me to a resource for updating a cell in a table in the header \
of a DOCX file?

I posted this question to stackoverflow here: \
http://stackoverflow.com/questions/28572615/apache-poi-how-to-update-header-table-cell-in-docx-file \
<http://stackoverflow.com/questions/28572615/apache-poi-how-to-update-header-table-cell-in-docx-file>


Every time I run the following unit test I get a document in which the text I add to \
a cell with a run only appends to the content already in the cell. I am starting with \
at template file that has a 4 rows by 3 column table in the header. I am trying to \
destructively update a cell in that table to no avail.  @Test
	public void thatWordHeaderTableWriteWorks() throws IOException, \
InvalidFormatException {  FileInputStream docxInput = null;
		XWPFDocument docxFile = null;
		docxInput = new FileInputStream("src/test/resources/02 Blank Docx.docx");
		docxFile = new XWPFDocument(OPCPackage.open(docxInput));

		// write something to the header
		XWPFDocument newDocument = docxFile;
		XWPFHeaderFooterPolicy policy = newDocument.getHeaderFooterPolicy();
		XWPFHeader header = policy.getDefaultHeader();

		XWPFTable headerTable = header.getTables().get(0);
		XWPFTableRow tr1 = headerTable.getRow(0);
		XWPFTableCell cell1 = tr1.getCell(2);

		tr1.removeCell(2);
		System.out.println("Cell data: " + cell1.getText());
		tr1.createCell();

		cell1.getParagraphs().get(0).setAlignment(ParagraphAlignment.RIGHT);
		XWPFRun textRun = cell1.getParagraphs().get(0).createRun();
		textRun.setText("Packet Ref # ");
		XWPFRun nextRun = cell1.getParagraphs().get(0).createRun();
		nextRun.setColor("FF0000");
		nextRun.setText("1000.01.01.01");

		System.out.println("row one cell one " + tr1.getCell(2).getText());

		// create new file with the same file name except for a '04.u' in the
		// reference
		File newWordFile = new File("src/test/resources/04.n New Table Docx.docx");
		FileOutputStream out = new FileOutputStream(newWordFile);
		newDocument.write(out);
		out.close();
		System.out.println(newWordFile.getName()
				+ " written successfully on disk.");
		assertTrue(newWordFile.exists());
	}

I checked the unit tests here \
(https://svn.apache.org/repos/asf/poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/ \
<https://svn.apache.org/repos/asf/poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/>)
 and found nothing that shows what I am trying to do.

Thanks,
Kent



[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic