Apache OpenOffice (AOO) Bugzilla – Issue 120625
[UNO API]can't get the text of "TextField PageNumber" by XTextDocument.getText()
Last modified: 2013-02-24 21:10:11 UTC
Build: AOO350m1(Build:9610) Steps: Using UNO API do below operation. 1.Launch a odt document, 2.Create a page number field and insert into this docment 3.Get the document text But in the step3, page number is not contained in the get text. But if I launch a doc document, I can get the page number in step 3. Below is code pieces. XMultiServiceFactory sevriceFactory = (XMultiServiceFactory) UnoRuntime.queryInterface(XMultiServiceFactory.class, document); XTextField pageNumberFiled = (XTextField)UnoRuntime.queryInterface(XTextField.class, sevriceFactory.createInstance("com.sun.star.text.textfield.PageNumber")); XPropertySet props = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, pageNumberFiled); props.setPropertyValue("NumberingType", 4);//Set page number display as Arabic XTextCursor xTextCursor = document.getText().createTextCursor(); xTextCursor.gotoEnd(false); document.getText().insertTextContent(document.getText().getEnd(), pageNumberFiled, true); String documentString = document.getText().getString(); System.out.println(documentString);.
(In reply to comment #0) > Build: > AOO350m1(Build:9610) > > Steps: > Using UNO API do below operation. > 1.Launch a odt document, > 2.Create a page number field and insert into this docment > 3.Get the document text > > But in the step3, page number is not contained in the get text. But if I > launch a doc document, I can get the page number in step 3. Below is code > pieces. > > XMultiServiceFactory sevriceFactory = (XMultiServiceFactory) > UnoRuntime.queryInterface(XMultiServiceFactory.class, document); > XTextField pageNumberFiled = > (XTextField)UnoRuntime.queryInterface(XTextField.class, > sevriceFactory.createInstance("com.sun.star.text.textfield.PageNumber")); > > XPropertySet props = > (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, pageNumberFiled); > props.setPropertyValue("NumberingType", 4);//Set page number display > as Arabic > > XTextCursor xTextCursor = document.getText().createTextCursor(); > xTextCursor.gotoEnd(false); > > document.getText().insertTextContent(document.getText().getEnd(), > pageNumberFiled, true); > > > String documentString = document.getText().getString(); > System.out.println(documentString);. You might be missing to set the property SubType to com.sun.star.text.PageNumberType.CURRENT http://www.openoffice.org/api/docs/common/ref/com/sun/star/text/textfield/PageNumber.html#SubType http://www.openoffice.org/api/docs/common/ref/com/sun/star/text/PageNumberType.html#CURRENT If you only have one page, and it default to com.sun.star.text.PageNumberType.PREV, the field will always be empty. Sample AOO Basic code: Sub Main Dim oDoc as Object oDoc = ThisComponent Dim oField as Object oField = oDoc.createInstance("com.sun.star.text.textfield.PageNumber") oField.setPropertyValue("SubType", com.sun.star.text.PageNumberType.CURRENT) oField.setPropertyValue("NumberingType", com.sun.star.style.NumberingType.ARABIC) Dim oText as Object oText = oDoc.getText() oText.insertTextContent(oText.getEnd(), oField, True) oDoc.getTextFields().refresh() Dim sText as String sText = oText.getString() End Sub
I have 3 pages in my sample. The strange thing is if I use a doc sample there is not problem. So I think it's a bug of uno API.
For only I page, it works well, if page is greater than 2, it has problem.
.
Created attachment 79038 [details] Text Document with 5 pages and a test macro The macro inserts a page text field in every page. The inserts the document text string in a new blank document.
(In reply to comment #2) > I have 3 pages in my sample. The strange thing is if I use a doc sample > there is not problem. So I think it's a bug of uno API. I test with any number of pages, and it works as expected. See the test macro in attachment 79038 [details] The document text string is written in a new text document, in this new document you should find the page fields text representation ("1", "2", "3", "4", "5"). Can you attach the code you are using?
Created attachment 79042 [details] test sample
Java Code as below. I have found the reason of this problem, if I add a sleep after inert page number field, I can get the page number value. public void createPageNumber(XComponent component) { XMultiServiceFactory sevriceFactory = (XMultiServiceFactory) UnoRuntime.queryInterface(XMultiServiceFactory.class, component); XTextField pageNumberFiled; try { pageNumberFiled = (XTextField)UnoRuntime.queryInterface(XTextField.class, sevriceFactory.createInstance("com.sun.star.text.textfield.PageNumber")); XPropertySet props = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, pageNumberFiled); props.setPropertyValue("NumberingType", NumberingType.ARABIC);//Set page number display as Arabic props.setPropertyValue("SubType", PageNumberType.CURRENT); document.getText().insertTextContent(document.getText().getEnd(), pageNumberFiled, true); // try { // Thread.sleep(1000); // } catch (InterruptedException e) { // e.printStackTrace(); // } System.out.println(document.getText().getString()); } catch (com.sun.star.uno.Exception e) { e.printStackTrace(); }
Created attachment 79070 [details] Textfield Demo attached demo "TextfieldProblem.java" gives me Page # 1 from 3 Page # 2 from 3 Page # 3 from 3 with aoo3.4.1rc2 as long as i do not use the "refreshTextFields()" method or a Thread.sleep(). with refreshTextfield() output is like: Page # 3 from 4 Page # 2 from 4 Page # 3 from 4 strange, isn't it?