1

I want to, using the DocX library [https://docx.codeplex.com/], convert a .docx document to use a different font. Does anybody know how to do that? The samples projects are very spare, and the documentation is nonexistent.

I find, too, that often there are extraneous spaces in documents, and I want to iterate over all these until there are never two contiguous spaces. I can do this in a loop, I guess, replacing " " (2 spaces) with " " (1 space) until " " (2 spaces) is no longer found.

However, I also want to remove superfluous line breaks that sometimes occur when copying-and-pasting text into a document. I can do it "manually" (in Libre Office, not sure how it's done in MS Word), as I got an answer to this question: (select "Regular Expressions" and then replace "$" (without the quotes) with a space)

...but how programmatically, with DocX?

Additionally, in some cases I want to ADD line breaks/"paragraph returns" where there are legitimate line breaks between the end of one paragraph and the start of another, but no extra line to separate them visually. According to this:

...I can add a paragraph/line break to a legitimate line break by searching for "$" and replacing that with "\n\n"

This does work, too (manually, in Libre Office); but again...how to do this with the DocX library?

Community
  • 1
  • 1
B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862
  • As far as removing extra spaces, I've answered that here: http://stackoverflow.com/questions/20910408/how-can-i-remove-all-extraneous-spaces-from-a-docx-file. I'm still interested in the rest, though, and am looking forward to bountifying somebody's answer. – B. Clay Shannon-B. Crow Raven Jan 03 '14 at 19:08

1 Answers1

1

It appears that not all of this is possible with the current version of the DocX library you are using. If it is not exposed in documentation, the functions might as well not exist, and you should not be using undocumented features.

There is a much more mature library available, however, called the "Open XML SDK", that can do everything you need.

The correct way to change a font, regardless of whether you are doing it with the document editor, or you are writing a program to manipulate these files, is to change the appropriate text's style attribute, or changing the definition of style in use.

You should never, ever, ever, ever directly change the font of any text. Personally, I think that the 'font type' and 'font size' menus should be removed entirely from word/libreoffice/etc, and only be accessible inside a 'change style properties' dialog; the only reason to directly apply a font is if you are actually providing an example of particular typeface under discussion!

See How to: Replace the styles parts in a word processing document (Open XML SDK) from the MSDN documentation for a description of the way that works.

To search and replace text, the applicable MSDN page is How to: Search and replace text in a document part (Open XML SDK). For specifically replacing multiple spaces with a single space, there are numerous results on Google that should all work to at least some degree.

AJMansfield
  • 4,039
  • 3
  • 29
  • 50
  • 1
    I don't understand/agree with this comment: "You should never, ever, ever, ever directly change the font of any text. Personally, I think that the 'font type' and 'font size' menus should be removed entirely from word/libreoffice/etc, and only be accessible inside a 'change style properties' dialog; the only reason to directly apply a font is if you are actually providing an example of particular typeface under discussion!" – B. Clay Shannon-B. Crow Raven Jan 08 '14 at 17:38
  • 1
    If I copy-and-paste an ugly (such as Courier or Times New Roman) document into a new doc, the first thing I do is change the font. The default font should be something other than the 9X-uglier-than-a-bag-of-butts Times New Roman. Verdana would be okay (easy to read, not so ugly). – B. Clay Shannon-B. Crow Raven Jan 08 '14 at 17:39
  • 2
    @B.ClayShannon my point was, if you want to change how the text looks, you should do it, not by selecting a font from the font dropdown, but by going into the style editor and changing the fontface for the relevant style, similar to how one should edit the `.css` file to change the fontface of text on a webpage, rather than forcing it by using the `` tag, (which has, not coincidentally, been removed entirely from html5). – AJMansfield Jan 11 '14 at 00:34
  • Fair enough; I'm enough of a neanderthal, word-processor-finery-wise, that I had never even thought of (or was aware of) changing a style. – B. Clay Shannon-B. Crow Raven Jan 11 '14 at 00:36