Unable to replace title of .docx using apache poi in java

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Unable to replace title of .docx using apache poi in java

Bhavya Kothapally
I have a document with title and contents.  
I have used XWPFParagraph for paragraph, XWPFTable for table, XWPFSdt fro
content control.
I am unable to replace the title with other string. The title is not coming
under paragraph/table/content control.
The contents are coming under XWPFSdt but unable to replace them with other
string.
Please suggest how to proceed.



--
Sent from: http://apache-poi.1045710.n5.nabble.com/POI-User-f2280730.html

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Unable to replace title of .docx using apache poi in java

Mark Murphy
What have you tried?

On Wed, Sep 18, 2019 at 4:35 AM Bhavya Kothapally <
[hidden email]> wrote:

> I have a document with title and contents.
> I have used XWPFParagraph for paragraph, XWPFTable for table, XWPFSdt fro
> content control.
> I am unable to replace the title with other string. The title is not coming
> under paragraph/table/content control.
> The contents are coming under XWPFSdt but unable to replace them with other
> string.
> Please suggest how to proceed.
>
>
>
> --
> Sent from: http://apache-poi.1045710.n5.nabble.com/POI-User-f2280730.html
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>
Reply | Threaded
Open this post in threaded view
|

Re: Unable to replace title of .docx using apache poi in java

Bhavya Kothapally
This post was updated on .
1. I have tried in the below way for getting the content. Here I need to know how
to replace the text in the content with the same styles as in the source file.

import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.xwpf.usermodel.ISDTContent;
import org.apache.poi.xwpf.usermodel.XWPFDefaultParagraphStyle;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFSDT;
import org.apache.poi.xwpf.usermodel.XWPFStyle;
import org.apache.poi.xwpf.usermodel.XWPFStyles;
import org.apache.xmlbeans.XmlCursor;
import org.apache.xmlbeans.XmlException;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSdtBlock;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSdtRun;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

public class XWPFSDTEg {

        static String SOURCE_FILE = "Test.docx";
        static String OUTPUT_FILE = "TestOP.docx";

        public static void main(String[] args) throws Exception {

                Resource resource = new ClassPathResource(SOURCE_FILE);
                XWPFDocument document = new XWPFDocument(resource.getInputStream());

               
                List<XWPFSDT> xwpfsdts = extractSDTsFromBody(document);
                for (XWPFSDT xwpfsdt : xwpfsdts) {
                        ISDTContent isdtContent = xwpfsdt.getContent();
                        System.out.println("aa"+isdtContent.getText());

                        System.out.println(xwpfsdt.getTitle().toString());
                       
                        String text = isdtContent.getText();
                        text = text.replaceAll("Contents", "Contents-Contents");

                        XWPFDocument opdoc = new XWPFDocument();
                        FileOutputStream out = new FileOutputStream(new File(OUTPUT_FILE));
                        XWPFParagraph paragraph = opdoc.createParagraph();
                        XWPFRun run = paragraph.createRun();
                        run.setText(text);
                       
                        opdoc.write(out);
                        out.close();
                        System.out.println("Completed");
                }

        }

        private static List<XWPFSDT> extractSDTsFromBody(XWPFDocument document)
throws XmlException {

                XWPFSDT sdt;
                XmlCursor xmlcursor = document.getDocument().getBody().newCursor();
                List<XWPFSDT> allsdts = new ArrayList<XWPFSDT>();
                while (xmlcursor.hasNextToken()) {
                        XmlCursor.TokenType tokentype = xmlcursor.toNextToken();
                        if (tokentype.isStart()) {
                                if (xmlcursor.getObject() instanceof CTSdtRun) {
                                        sdt = new XWPFSDT((CTSdtRun) xmlcursor.getObject(), document);
                                        allsdts.add(sdt);
                                } else if (xmlcursor.getObject() instanceof CTSdtBlock) {
                                        sdt = new XWPFSDT((CTSdtBlock) xmlcursor.getObject(), document);
                                        allsdts.add(sdt);
                                }
                        }
                }
                return allsdts;
        }

}

2. How to get the title in the document and how to replace it?



--
Sent from: http://apache-poi.1045710.n5.nabble.com/POI-User-f2280730.html

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org

Reply | Threaded
Open this post in threaded view
|

Re: Unable to replace title of .docx using apache poi in java

Mark Murphy
So for each SDT in Test.docx you are creating a new document, adding some
text, and writing it to TestOP.docx. What is happening? Are you just
overwriting TestOPdocx for each SDT, and you only get the last one? Is
there only one SDT in Test.docx? Where is the Title in the document? Did
you know that you can inspect the raw contents of a docx by changing the
extension to .zip and unzipping it? If you know where in the document the
text you want to modify is, then it will be easier to determine how to do
that.

On Thu, Sep 19, 2019 at 2:56 AM Bhavya Kothapally <
[hidden email]> wrote:

> 1. I have tried in the below way for getting the content. I need to know
> how
> to replace the text in the content.
>
> import java.io.File;
> import java.io.FileOutputStream;
> import java.util.ArrayList;
> import java.util.List;
>
> import org.apache.poi.xwpf.usermodel.ISDTContent;
> import org.apache.poi.xwpf.usermodel.XWPFDefaultParagraphStyle;
> import org.apache.poi.xwpf.usermodel.XWPFDocument;
> import org.apache.poi.xwpf.usermodel.XWPFParagraph;
> import org.apache.poi.xwpf.usermodel.XWPFRun;
> import org.apache.poi.xwpf.usermodel.XWPFSDT;
> import org.apache.poi.xwpf.usermodel.XWPFStyle;
> import org.apache.poi.xwpf.usermodel.XWPFStyles;
> import org.apache.xmlbeans.XmlCursor;
> import org.apache.xmlbeans.XmlException;
> import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSdtBlock;
> import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSdtRun;
> import org.springframework.core.io.ClassPathResource;
> import org.springframework.core.io.Resource;
>
> public class XWPFSDTEg {
>
>         static String SOURCE_FILE = "Test.docx";
>         static String OUTPUT_FILE = "TestOP.docx";
>
>         public static void main(String[] args) throws Exception {
>
>                 Resource resource = new ClassPathResource(SOURCE_FILE);
>                 XWPFDocument document = new
> XWPFDocument(resource.getInputStream());
>
>
>                 List<XWPFSDT> xwpfsdts = extractSDTsFromBody(document);
>                 for (XWPFSDT xwpfsdt : xwpfsdts) {
>                         ISDTContent isdtContent = xwpfsdt.getContent();
>                         System.out.println("aa"+isdtContent.getText());
>
>                         System.out.println(xwpfsdt.getTitle().toString());
>
>                         String text = isdtContent.getText();
>                         text = text.replaceAll("Contents",
> "Contents-Contents");
>
>                         XWPFDocument opdoc = new XWPFDocument();
>                         FileOutputStream out = new FileOutputStream(new
> File(OUTPUT_FILE));
>                         XWPFParagraph paragraph = opdoc.createParagraph();
>                         XWPFRun run = paragraph.createRun();
>                         run.setText(text);
>
>                         opdoc.write(out);
>                         out.close();
>                         System.out.println("Completed");
>                 }
>
>         }
>
>         private static List<XWPFSDT> extractSDTsFromBody(XWPFDocument
> document)
> throws XmlException {
>
>                 XWPFSDT sdt;
>                 XmlCursor xmlcursor =
> document.getDocument().getBody().newCursor();
>                 List<XWPFSDT> allsdts = new ArrayList<XWPFSDT>();
>                 while (xmlcursor.hasNextToken()) {
>                         XmlCursor.TokenType tokentype =
> xmlcursor.toNextToken();
>                         if (tokentype.isStart()) {
>                                 if (xmlcursor.getObject() instanceof
> CTSdtRun) {
>                                         sdt = new XWPFSDT((CTSdtRun)
> xmlcursor.getObject(), document);
>                                         allsdts.add(sdt);
>                                 } else if (xmlcursor.getObject()
> instanceof CTSdtBlock) {
>                                         sdt = new XWPFSDT((CTSdtBlock)
> xmlcursor.getObject(), document);
>                                         allsdts.add(sdt);
>                                 }
>                         }
>                 }
>                 return allsdts;
>         }
>
> }
>
> 2. How to get the title in the document and how to replace it?
>
>
>
> --
> Sent from: http://apache-poi.1045710.n5.nabble.com/POI-User-f2280730.html
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>
Reply | Threaded
Open this post in threaded view
|

Re: Unable to replace title of .docx using apache poi in java

Bhavya Kothapally
Actually I want to modify the contents page in the document.

I tried to modify the same document but it is not possible using sdt, just
for trial I have modified the text and saved it as a string and copied that
to a new document.



--
Sent from: http://apache-poi.1045710.n5.nabble.com/POI-User-f2280730.html

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Unable to replace title of .docx using apache poi in java

Mark Murphy
Ok, the table of contents is not a page, instead it is a field. Support is
somewhat limited, but I have found that the easy way to create one is by
following my Stack Overflow answer here:
https://stackoverflow.com/a/40264237/2296441. This creates an automatic
table of contents that uses Heading styles to create the table of contents.
It finds the page numbers based on the way Microsoft Word renders the
pages. Note that the values in the table of  contents field are just cached
values, but if the document changes, it is re-generated, so you can't
change the table of contents by adjusting the cached values. You have to
adjust the Heading text (the text that is styled by one of the Heading
styles). The page numbers are generated automatically as the document
changes.

On Mon, Sep 23, 2019 at 2:37 AM Bhavya Kothapally <
[hidden email]> wrote:

> Actually I want to modify the contents page in the document.
>
> I tried to modify the same document but it is not possible using sdt, just
> for trial I have modified the text and saved it as a string and copied that
> to a new document.
>
>
>
> --
> Sent from: http://apache-poi.1045710.n5.nabble.com/POI-User-f2280730.html
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>