Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can I reaplce text in word document include image? #1169

Open
tonyqus opened this issue Aug 23, 2023 Discussed in #767 · 0 comments
Open

How can I reaplce text in word document include image? #1169

tonyqus opened this issue Aug 23, 2023 Discussed in #767 · 0 comments

Comments

@tonyqus
Copy link
Member

tonyqus commented Aug 23, 2023

Discussed in #767

Originally posted by david810376 February 25, 2022
Hi,

I am trying to replace some text in the word document, but in the document there are some image in the header, so when I read the document and replace the text, then I open the new word document that finish replace, the word show out the document is borken so it can't open it. Thus I did not have any idea how can I replace the document that include images, I did not do anything to the image, I just replace the text. I'm sure about the replace text is fine because in the beginning just want to replace the text it work great, but when the document have image in the header it doesn't work anymore. Thus is there have any example about edit the word document include image?

Here are the part of code

`

        string strSourcePath = System.Environment.CurrentDirectory + "\\Sample.docx";
        string filepath = selectfolder +"\\" +TXT_FileName.Text +".docx";
        try
        {
            System.IO.File.Copy(strSourcePath, filepath, true);
            FileStream fsFile = new FileStream(filepath, FileMode.Open, FileAccess.Read); 
            XWPFDocument document = new XWPFDocument(fsFile);
            document.CreateParagraph();
            //into the header
            foreach (XWPFHeader header in document.HeaderList)
            {
                //create header
                document.CreateParagraph().CreateRun();
            }
            //body part
            foreach(var para in document.Paragraphs)
            {
                string strOldText = para.ParagraphText;
                if (strOldText == "")
                    continue;
                string strTempText = para.ParagraphText;
                if (strTempText.Contains("Test"))
                   strTempText = strTempText.Replace("Test", "Hello");
                para.ReplaceText(strOldText, strTempText);
            }
            FileStream out1 = new FileStream(filepath, FileMode.Create);
            document.Write(out1);
            out1.Close();

`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant