简体   繁体   中英

add character spacing for range of character in open xml c#

how to add character spacing for range of characters

like i want to give character spacing In word for 2 characters of with value of 2个字符值为


在此输入图像描述

Open XML has not only SDK, but also a tool for converting any document to C# code .

Best way to find out how to use some word feature is to make 2 short documnets - one with this feature used and the other - without. Then convert both documnets into C# code and compare generated code (you can use WinMerge, for example).

As you wish to apply a style to the middle of a paragraph you will need to have (at least) 3 separate Run elements; the first for the start of the unstyled text, the second for the text that has the spacing and the third for the rest of the unstyled text.

To add the character spacing you need to add a Spacing element to the Run . The Spacing element has a Value property that sets the spacing you want in twentieths of a point (so to get 4pt you need to set the Value to 80).

The following code will create a document with spacing on the gg in the word toggling

public static void CreateDoc(string fileName)
{
    // Create a Wordprocessing document. 
    using (WordprocessingDocument package = 
             WordprocessingDocument.Create(fileName, WordprocessingDocumentType.Document))
    {
        // Add a new main document part. 
        package.AddMainDocumentPart();

        //create a body and a paragraph
        Body body = new Body();
        Paragraph paragraph = new Paragraph();

        //add the first part of the text to the paragraph in a Run
        paragraph.AppendChild(new Run(new Text("This sentence has spacing between the gg in to")));

        //create another run to hold the text with spacing
        Run secondRun = new Run();
        //create a RunProperties with a Spacing child.
        RunProperties runProps = new RunProperties();
        runProps.AppendChild(new Spacing() { Val = 80 });
        //append the run properties to the Run we wish to assign spacing to
        secondRun.AppendChild(runProps);
        //add the text to the Run
        secondRun.AppendChild(new Text("gg"));
        //add the spaced Run to the paragraph
        paragraph.AppendChild(secondRun);
        //add the final text as a third Run
        paragraph.AppendChild(new Run(new Text("ling")));

        //add the paragraph to the body
        body.AppendChild(paragraph);

        package.MainDocumentPart.Document = new Document(body);

        // Save changes to the main document part. 
        package.MainDocumentPart.Document.Save();
    }
}

The above produces the following: 在此输入图像描述

Note that you can set the Value of the Spacing to a negative number and the text will be condensed rather than expanded.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM