Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8256277
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T01:48:38+00:00 2026-06-08T01:48:38+00:00

I am working on prototypes to replace an existing word-automation based system for template

  • 0

I am working on prototypes to replace an existing word-automation based system for template rendering, and currently evaluation the OpenXML SDK. The template library is quite extensive (150-200 templates, maintained by non-technical resources) so I am hoping to avoid any template changes other than upgrading from the 1997-2003 word format.

Tags currently embedded need to be replaced sometimes with Text, and sometimes with Images/Charts/etc… (assume for now that all charts will be rendered to images prior to insertion).

I am able to do the straight text replacement using a technique similar to the one described in this MSDN article. My scenario is slightly more complex but looks something like this:

    public void ReplaceFirstOccurrenceWithText(string tagBody, string replacement)
    {
        var modifiedText = GetCurrentText();
        modifiedText = modifiedText.ReplaceFirst(tagBody, XmlEncoder.Encode(replacement));
        using (var sw = new StreamWriter(document.MainDocumentPart.GetStream(FileMode.Create)))
        {
            sw.Write(modifiedText);
        }
    }

    public string GetCurrentText()
    {
        using(var reader = new StreamReader(document.MainDocumentPart.GetStream()))
        {
            return reader.ReadToEnd();
        }
    }

The reason I don’t save the string is because I want the underlying document to stay up to date so I can add images through the normal API. Using the technique described in another MSDN article:

    public void ReplaceFirstOccurrenceWithImage(string tagBody, byte[] replacement)
    {
        ReplaceFirstOccurrenceWithText(tagBody, "IMAGE TAG WAS HERE!");
        var main = document.MainDocumentPart;
        var imagePart = main.AddImagePart(ImagePartType.Gif);//sniff this by loading bytes into a bitmap
        using(var imageStream = new MemoryStream(replacement))
        {
            imagePart.FeedData(imageStream);
        }

        ImageInserter.AddImageToBody(document, main.GetIdOfPart(imagePart));
    }

Where ImageInserter is literally a copy/paste of the code in that article (I realize these abstractions are not the best but I’m just trying to get anything to work at this point).

Now is where it gets hairy – the document APPEARS to be staying in sync. The image is the first tag getting replaced, and the text replacement for the tag works, as does adding the image at the bottom of the document. My problem is that subsequent text replacement does not seem to work at all after this point – all the other tags remain in the document. However, If I set a break point in the text replacement function, each call to .GetCurrentText() returns the correct result (text with the tags up to that point having been replaced). But when I save the document, it is saved with only the first replacement having been done.

Has anyone run into anything like this? Next step is going to be to try a phased approach (resolve all tags, run straight text replacement first, then do all image replacements) but I feel like whatever is wrong currently will remain a problem regardless of order.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T01:48:41+00:00Added an answer on June 8, 2026 at 1:48 am

    If I where you I would look into

    http://docx.codeplex.com

    its a lot more straight forward for general stuff .. maybe you are doing something more complex then the library can handle but I would def take a look at it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a Javascript class based on the Prototype library. This class needs
I am currently working on some prototypes for a web site I am wanting
Im thinking of making a custom datatypes / prototypes for a project im working
I am working on a display/control utility to replace an ancient dedicated hardware controller
I'm working on an assignment, and my professor has given us the function prototypes
I'm working with some C code that does not contain function prototypes for a
I am working only in C regarding this issue. I have two function prototypes
On the system I am working with, we have a Password class that validates
I am working on the prototype for a scheduling application on an intranet system.
Hi! I have a working prototype of a game engine and right now I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.