poi-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Mark Murphy <jmarkmur...@gmail.com>
Subject Re: Unable to replace title of .docx using apache poi in java
Date Thu, 19 Sep 2019 15:31:04 GMT
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 <
bhavya.kothapally@verizon.com> 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: user-unsubscribe@poi.apache.org
> For additional commands, e-mail: user-help@poi.apache.org
>
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message