none
Having a problem with italics RRS feed

  • Question

  • Hello,

    I'm facing a problem adding italics to a paragraph, in the creation of a word document,

    This is my code(similar to the msdn example for adding bold/italics):


    var tr = new TableRow(); var tcTime = new TableCell();

    var textRun = new Run();
    textRun.Append(new Text("Test"));

    var runProperties = new RunProperties(new Italic()); tcText.Append(new Paragraph(runProperties, textRun)); tcText.Append(new TableCellProperties(new TableCellWidth { Type = TableWidthUnitValues.Pct, Width = "45%" })); tr.Append(tcText); table.Append(tr);

    What am i doing wrong here?


    thank you very much in advance!

    jony.

    Monday, June 10, 2013 8:18 AM

Answers

  • Hi jony

    You need to be very careful about the order in which you append things. It helps to create a document with what's giving you problems and compare that XML with what your code is generating. In this case, you need to be sure that the paragraph is built as: <w:p><w:r><w:rPr><w:t>

    Your code is associating the run properties with the paragraph, not the run. Word is being generous when it opens the document and is ignoring the invalid XML.

    Here's one way to accomplish what you want to do

            Document doc = partDoc.Document;
            Table t = new Table( new TableRow ( new TableCell( 
                new TableCellProperties ( new TableCellWidth() {Type = TableWidthUnitValues.Pct, Width = "45%"}),
                new Paragraph(new Run ( new RunProperties (new Italic()), new Text("test"))))));
            doc.Body.Append(t);
    


    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by jony feldman Thursday, June 13, 2013 7:55 AM
    Wednesday, June 12, 2013 3:56 PM
    Moderator

All replies

  • What data type is the object tcText? I see no declaration or instantiation for it in your code.

    Please give us a link to the msdn example you're using.

    Please describe the result you're getting, as opposed to the result you expect. If you're getting any error messages, please specify the message and the line the triggers it.


    Cindy Meister, VSTO/Word MVP, my blog

    Tuesday, June 11, 2013 7:33 PM
    Moderator
  • Thanks for the replay,

    I made a mistake in the code example, insteed of:

    var tcTime = new TableCell();

    i meant:

    var tcText = new TableCell();

    the msdn article is: MSDN

    And the result i get is the text as i expect but without italic, just regular text

    as in "text" instead of "text"

    thanks again for the replay

    jony

    Tuesday, June 11, 2013 9:28 PM
  • Hi jony

    You need to be very careful about the order in which you append things. It helps to create a document with what's giving you problems and compare that XML with what your code is generating. In this case, you need to be sure that the paragraph is built as: <w:p><w:r><w:rPr><w:t>

    Your code is associating the run properties with the paragraph, not the run. Word is being generous when it opens the document and is ignoring the invalid XML.

    Here's one way to accomplish what you want to do

            Document doc = partDoc.Document;
            Table t = new Table( new TableRow ( new TableCell( 
                new TableCellProperties ( new TableCellWidth() {Type = TableWidthUnitValues.Pct, Width = "45%"}),
                new Paragraph(new Run ( new RunProperties (new Italic()), new Text("test"))))));
            doc.Body.Append(t);
    


    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by jony feldman Thursday, June 13, 2013 7:55 AM
    Wednesday, June 12, 2013 3:56 PM
    Moderator
  • thank you very much!

    it works [=

    Thursday, June 13, 2013 7:55 AM