none
Change font and background MS Word 2007 generated using Altchunks (html) RRS feed

  • Question

  • Hello,

    I have requirement to have entire worddocument that is generated from the template to be of Arial 10 with black font and white background.

    The document generated with have some altchunks with html text.

    Is there a way i can convert the font and remove background of altchunks along with entire document?
    I am using open xml sdk 2.0.


    Regards
    Monday, April 5, 2010 3:14 PM

All replies

  • Hi mac73,

    Thanks for your question.

    When inserting an AltChunk element, you could choose whether to keep Source Formatting or not by Element MatchSource under AltChunk\AltChunkProperties. You could set this value as false and the format of the AltChunk content will be the same as that of the document it is imported into. Below is the code snippet on setting this:

    AltChunk

     

     

    altChunk = new AltChunk();

    AltChunkProperties altChunkProperties = new AltChunkProperties();

    altChunkProperties.MatchSource =

    new MatchSource() { Val = new OnOffValue(false)};

    altChunk.AppendChild<

    AltChunkProperties>(altChunkProperties);

    Another point is that the backgroud of the HTML page will not be applied no matter what the value of MatchSource is.

    Hope this helps. If you have any question, please let me know.

    Thanks,

    Lu

    • Edited by Lu Zhang Thursday, April 8, 2010 5:25 AM
    Wednesday, April 7, 2010 9:54 AM
  • Lu

    Thanks for your reply. Unfortunately this doesnt work. Even the background shading of html text appears in word document. Can i set the properties of entire word document progammatically and than use your code to have match source?

    Here is the scenario

    1) I have word template in docx with content controls mapped with xml.

    2) I loop through the collection of content controls and get to the point where i know that i have to insert html.

    3) Html text is from rich textbox

    4) I use below code to insert html chunks in word document.

    protected void ReplaceCustomXML(string fileName, string customXML)
      {
       using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(fileName, true))
       {
        MainDocumentPart mainPart = wordDoc.MainDocumentPart;
        mainPart.DeleteParts<CustomXmlPart>(mainPart.CustomXmlParts);
        //Add a new customXML part and then add the content.
        CustomXmlPart customXmlPart = mainPart.AddCustomXmlPart(CustomXmlPartType.CustomXml);
        var sdtHtmlText = mainPart.Document.Descendants<SdtBlock>().Where(s => s.SdtProperties.InnerXml.Contains("SmartHtmlText")).FirstOrDefault();
        var sdtHtmlText2 = mainPart.Document.Descendants<SdtBlock>().Where(s => s.SdtProperties.InnerXml.Contains("SmartHtmlText2")).FirstOrDefault();
        var sdtList = new List<SdtBlock>();
        sdtList.Add(sdtHtmlText);
        sdtList.Add(sdtHtmlText2);

        int id = 1;
        if (sdtList.Count != 0)
        {
         foreach (var sdt in sdtList)
         {
          if (sdt != null && sdt.Parent != null)
          {
           string altChunkId = "AltChunkId" + id;
           id++;
           AlternativeFormatImportPart chunk = mainPart.AddAlternativeFormatImportPart(AlternativeFormatImportPartType.Html, altChunkId);

           using (Stream chunkStream = chunk.GetStream(FileMode.Create, FileAccess.Write))
           using (StreamWriter stringStream = new StreamWriter(chunkStream))
            stringStream.Write(
               String.Format("<html xmlns:v=\"urn:schemas-microsoft-com:vml\" xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:w=\"urn:schemas-microsoft-com:office:word\" xmlns=\"http://www.w3.org/TR/REC-html40\"><head><style>body,span,p,div{{font-family: arial; font-size: 9pt;}}</style> <meta http-equiv=\"Content-Type\" value=\"text/html; charset=utf-8\"></head><body>{0}</span></body></html>",
                       Editor1.Content));

           AltChunk altChunk = new AltChunk();
           altChunk.Id = altChunkId;
           AltChunkProperties altChunkProperties = new AltChunkProperties();
           altChunkProperties.MatchSource = new MatchSource() { Val = new OnOffValue(false) };
           altChunk.AppendChild<AltChunkProperties>(altChunkProperties);

           OpenXmlElement parent = sdt.Parent;
           parent.InsertAfter(altChunk, sdt);
           sdt.Remove();
          }
         }
           mainPart.Document.Save();

        }
        //Copy the XML into the new part.
        using (StreamWriter ts = new StreamWriter(customXmlPart.GetStream())) ts.Write(customXML);
       }
      }

     

     

    Wednesday, April 7, 2010 2:18 PM
  • Hi mac73,

    Firstly, I have one question regarding your code provided:

    What's the relationship between adding a new CustomXML part and replacing the Content Controls with AltChunks? I didn't find any direct relation between them and does it mean that we should only focus on the AltChunk related code?

    As to the MatchSource class, a detailed definition is from here. It only decides whether to remain or not those styles appearing in the imported document same as the host document. So I think it cannot be done by the SDK to remove those text shadings if they are inserted as AltChunk (AltChunk is imported as binary data and to modify it takes much more effort). I'm sorry I have some misunderstanding about it in my previous post.

    One suggestion for your scenario is that you may consider saving this document in Word if possible. Then all the AltChunk elements will be replaced with the content of the imported file (which may have some mappings between tags). After that, you could change the style of the whole document as normal ones.

    Hope this helps. If you have any question, please let me know.

    Thanks,

    Lu

    Friday, April 9, 2010 7:00 AM
  • Lu

    Thanks for your reply. So do you suggest that i should first add altchunks to blank word document and than merge the newly created word document to parent word document with matchsource of destination?

    Monday, April 12, 2010 7:15 PM
  • Hi mac73,

    I don't mean you should first add altchunks to blank word document and then merge it to the parent word document. The MatchSource class couldn't help achieve the goal of keeping the style of imported altchunk completely the same as the word document. It only specifies if any style definitions present in the imported content shall be overridden by identical styles present in the host WordprocessingML document. But if you open a document with altchunk, then save it in word, it will replace the altchunk element with the content of the imported file (no matter it is HTML or word file), saving it as an Open XML file (similar to normal word files, without any altchunk). If the file you imported is HTML, the Word will do some mapping between those tags in HTML and Open XML. After that you could apply the styles you need on the document without any concern about the altchunk.

    Hope this helps. If you have any question, please let me know.

    Thanks,

    Lu

    Tuesday, April 13, 2010 2:09 AM
  • Lu,

    Thanks for your response. Can you direct me to any sample code available so that i can be more clear.

    Regards

    Mac

    Tuesday, April 13, 2010 2:58 PM
  • Hi Mac,

    Thanks for your reply. To save the file in Word, you just need to open the file with altchunk in Word and click "Save". Word will automatically do all the transformation issues. Then you just need to deal with the newly saved document with Open XML SDK to apply the style. Here is a how-to article which tells you how to apply a style to a paragraph. If you need to change the font of the whole document, you could also try to change the content of the "/word/styles.xml" and "/word/stylesWithEffects.xml"

    <w:docDefaults>

    <w:rPrDefault>

    <w:rPr>

    <w:rFonts w:asciiTheme="minorHAnsi" w:eastAsiaTheme="minorEastAsia" w:hAnsiTheme="minorHAnsi" w:cstheme="minorBidi"/>

    <w:sz w:val="32"/>

    <w:szCs w:val="22"/>

    <w:lang w:val="en-US" w:eastAsia="zh-CN" w:bidi="ar-SA"/>

    </w:rPr>

    </w:rPrDefault>

    <w:pPrDefault>

    <w:pPr>

    <w:spacing w:after="200" w:line="276" w:lineRule="auto"/>

    </w:pPr>

    </w:pPrDefault>

    </w:docDefaults>

    in which the element under w:rPpr defines the default normal font style of the text in document. This presentation may help you better understand the style in WordprocessingML.

    Hope this helps. If you have any question, please let me know.

    Thanks,

    Lu

    Wednesday, April 14, 2010 5:21 AM
  • Another approach is instead of automating Word client application you can take advantage of Word Automation Services. Check out the following post: http://blogs.msdn.com/microsoft_office_word/archive/2009/12/16/Word-Automation-Services_3A00_-What-It-Does.aspx

    Word Automation Services will merge the altChunk into the document. At which point you can then use the Open XML SDK to modify the document appropriately.


    Zeyad Rajabi (MS)
    Thursday, April 29, 2010 9:47 PM