Issue 120625

Summary: [UNO API]can't get the text of "TextField PageNumber" by XTextDocument.getText()
Product: App Dev Reporter: zong dong jun <zongdj001>
Component: apiAssignee: AOO issues mailing list <issues>
Status: UNCONFIRMED --- QA Contact:
Severity: Normal    
Priority: P2 CC: arielch, issues, oliver.brinzing, orw
Version: 3.3.0 or older (OOo)Keywords: automation_bug
Target Milestone: ---   
Hardware: All   
OS: All   
Issue Type: DEFECT Latest Confirmation in: ---
Developer Difficulty: ---
Attachments:
Description Flags
Text Document with 5 pages and a test macro
none
test sample
none
Textfield Demo none

Description zong dong jun 2012-08-21 01:56:08 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);.
Comment 1 Ariel Constenla-Haile 2012-08-21 02:17:10 UTC
(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
Comment 2 zong dong jun 2012-08-21 07:26:40 UTC
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.
Comment 3 zong dong jun 2012-08-21 09:01:35 UTC
For only I page, it works well, if page is greater than 2, it has problem.
Comment 4 Oliver Brinzing 2012-08-21 11:49:55 UTC
.
Comment 5 Ariel Constenla-Haile 2012-08-21 12:31:06 UTC
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.
Comment 6 Ariel Constenla-Haile 2012-08-21 12:34:17 UTC
(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?
Comment 7 zong dong jun 2012-08-22 01:38:58 UTC
Created attachment 79042 [details]
test sample
Comment 8 zong dong jun 2012-08-22 07:16:27 UTC
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();
		}
Comment 9 Oliver Brinzing 2012-08-22 13:38:20 UTC
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?