[Solved] Delete paragraph recursively

Java, C++, C#, Delphi... - Using the UNO bridges
Post Reply
pog
Posts: 1
Joined: Mon Mar 09, 2009 11:11 am

[Solved] Delete paragraph recursively

Post by pog »

Editing this post : I've found a way to delete recursively a paragraph. Some Java code is shown at the end of the post to illustrate how to do this.
While this is certainly not the best way to do it, it seems to work :-)
It does not work however in all cases : to remove a paragraph and all its 'children', the paragraph must be of Outline style (that is, be part of indexes like the table of content), and have a numbering attached. And paragraphs to be removed should have unique name (it's like a key); you can still have paragraphs of the same name if they are not removed directly but rather like a child of removed paragraph.
You'll need some code to load your document (I just give an operation here), but you can find lots of examples in the OOO API documentation.


There're still some problems:
  • The very last paragraph of a document (a file like an odt) is not removed even though I call dispose() on it.
  • Indexes must be refreshed once you have removed paragraphs. I have code (not shown here) that does this, but while paragraph titles are updated, page numbering in the table of content is not refreshed
Have fun,

--POG
Hi all,

I need to delete specific parts of a document using the Java OOO API.
For example:

1 section 1
2 section 2
2.1 foo
2.2 boo
2.2.1 bla
3 section 3

I want to delete section 2, so all child sections (like 2.1, 2.2, 2.2.1) should be recursively destroyed, including any content like images or tables.

Currently I know how to find the top level paragraph and delete it by calling dispose(), but I don't see how to delete children as there's no
real hierarchy linking paragraphs (unlike something like Latex for example). I suppose I could just delete all elements until I find one with a depth <= to the root element I want to destroy.
But I can't find in the API how to get this.

Any help is welcome.

-- POG

Code: Select all

/**
	 * Remove a single paragraph from a document. This operation must be called
	 * several times if more than one paragraph is to be removed.
	 * 
	 * @param xTextDocument
	 *            document to process
	 * @param filter
	 *            title of the paragraph to remove. This must exactly match the
	 *            title in the document (but not the numbering : it is ignored)
	 * @throws NoSuchElementException
	 * @throws WrappedTargetException
	 * @throws UnknownPropertyException
	 */
	private static void removeDocumentParagraph(XTextDocument xTextDocument,
			String filter) throws NoSuchElementException,
			WrappedTargetException, UnknownPropertyException {
		XText xText = xTextDocument.getText();

		XEnumeration xParagraphEnumeration = null;
		XTextContent xTextElement = null;

		// create an enumeration access of all paragraphs of a document
		XEnumerationAccess xEnumerationAccess = (XEnumerationAccess) UnoRuntime
				.queryInterface(XEnumerationAccess.class, xText);
		xParagraphEnumeration = xEnumerationAccess.createEnumeration();

		boolean removing = false;
		short removingLevel = 0;

		// Loop through all objects of the document
		while (xParagraphEnumeration.hasMoreElements()) {
			Object o = xParagraphEnumeration.nextElement();
			XServiceInfo xInfo = (XServiceInfo) UnoRuntime.queryInterface(
					XServiceInfo.class, o);

			if (xInfo.supportsService("com.sun.star.text.Paragraph")) {

				// get the text and compare to the filter
				xTextElement = (XTextContent) UnoRuntime.queryInterface(
						XTextContent.class, o);
				XTextRange xTextRange = xTextElement.getAnchor();

				String elementContent = xTextRange.getString().trim();
				System.out.println("Element text : [" + elementContent
						+ "]");

				if (removing == false) {
					if (elementContent.endsWith(filter)) {
						System.out
								.println("Section (maybe) to be removed ");
						XPropertySet xSet = (XPropertySet) UnoRuntime
								.queryInterface(XPropertySet.class, xInfo);

						short slevel = 0;
						Object level = xSet.getPropertyValue("NumberingLevel");
						if (level != null) {
							slevel = (Short) level;
						}

						boolean bIsNumber = false;
						Object isNumber = xSet
								.getPropertyValue("NumberingIsNumber");
						if (level != null) {
							bIsNumber = (Boolean) isNumber;
						}

						String style = "";
						Object styleo = xSet
								.getPropertyValue("NumberingStyleName");
						if (styleo != null) {
							style = (String) styleo;
						}

						if ((style.equals("Outline")) && (bIsNumber == true)) {
							removing = true;
							removingLevel = slevel;
							System.out
									.println("Starting deletion with element "
											+ elementContent);
							xTextElement.dispose();
						}
					}
				}

				// I'm removing paragraphs.
				// Stop only when it's the end of the document or when I get
				// a paragraph with a nesting lesser or equal to the nesting of
				// the startup paragraph.
				else if (removing == true) {
					XPropertySet xSet = (XPropertySet) UnoRuntime
							.queryInterface(XPropertySet.class, xInfo);

					short slevel = 0;
					Object level = xSet.getPropertyValue("NumberingLevel");
					if (level != null) {
						slevel = (Short) level;
					}

					boolean bIsNumber = false;
					Object isNumber = xSet
							.getPropertyValue("NumberingIsNumber");
					if (level != null) {
						bIsNumber = (Boolean) isNumber;
					}

					String style = "";
					Object styleo = xSet.getPropertyValue("NumberingStyleName");
					if (styleo != null) {
						style = (String) styleo;
					}

					System.out.println("del=true, level=" + slevel
							+ ", rmLevel=" + removingLevel + "NiN=" + bIsNumber
							+ ", style=" + style);

					if (slevel > removingLevel) {
						System.out
								.println("Deleting paragraph with nesting > startup nesting");
						xTextElement.dispose();
					} else if ((slevel <= removingLevel)
							&& (!style.equals("Outline"))) {
						System.out
								.println("Deleting paragraph with nesting <= startup nesting, but not Outline.");
						xTextElement.dispose();
					} else if ((slevel == removingLevel)
							&& (style.equals("Outline")) && (bIsNumber)) {
						System.out
								.println("Stopping deletion, found paragraph with startup nesting, outline, numbering.");
						removing = false;
					}
				}
			}
		}
	}
OOo 3.0.X on Debian + Windows
Selvanayagam
Posts: 5
Joined: Thu Aug 13, 2009 6:12 am

Re: [Solved] Delete paragraph recursively

Post by Selvanayagam »

Hi,

When i delete the page the last paragraph is not deleted tell me how to use index refresh.
OpenOffice 3.0 on Ubuntu 9.04
Post Reply