Click here to Skip to main content
15,921,941 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
c# Code to change the font size of data present in word document in fileUpload Microsoft World doc in asp.net or c# its possible or not.Iam trying below code its not working Tell me

What I have tried:

C#
protected void btnUpload_Click(object sender, EventArgs e)
        {
            string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
            FontFamily family = new FontFamily("Times New Roman");
            Font font = new Font(family, 16.0f,
            FontStyle.Bold | FontStyle.Italic | FontStyle.Underline);
            
            FileUpload1.PostedFile.SaveAs(Server.MapPath("~/Uploads/") + fileName);
          //  Response.Redirect(Request.Url.AbsoluteUri);
        }
Posted
Updated 25-Oct-16 11:35am
v2
Comments
Suvendu Shekhar Giri 25-Oct-16 3:12am    
But where have you changed font of the content in the file?
Have you even opened the file for reading/writting via c#?
jpr chaitanya 25-Oct-16 3:26am    
that's what i need .i have to change the font family and font-size present in the file upload control's Word Document. i mean posted file in the file upload control.
Karthik_Mahalingam 27-Oct-16 5:46am    
Always use  Reply  button, to post Comments/query to the user, so that the user gets notified and responds to your text.

1 solution

Yes, its possible, if you want to change something inside file, you have to open it using code and start working on it, OpenXML SDK will help you to achieve this.

Step1: Install nuget package - DocumentFormat.OpenXml 2.5.0.
Step2: Modify your code
C#
protected void btnUpload_Click(object sender, EventArgs e)
{
    string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);

    FileUpload1.PostedFile.SaveAs(Server.MapPath("~/") + fileName);

    fileName = Server.MapPath("~/") + fileName;
    using (var document = WordprocessingDocument.Open(fileName, true))
    {
        RunProperties runProp = new RunProperties();

        RunFonts runFont = new RunFonts();           // Create font
        runFont.Ascii = "Arial";                     // Specify font family

        DocumentFormat.OpenXml.Wordprocessing.FontSize size = new DocumentFormat.OpenXml.Wordprocessing.FontSize();
        size.Val = new StringValue("48");  // 48 half-point font size
        runProp.Append(runFont);
        runProp.Append(size);

        Run r = document.MainDocumentPart.Document.Descendants<Run>().First();
        r.PrependChild<RunProperties>(runProp);
        document.MainDocumentPart.Document.Save();
    }
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900