C#代码在C#中的fileupload Microsoft world doc中更改word文档中存在的数据的字体大小 [英] C# code to change the font size of data present in word document in fileupload Microsoft world doc in C#

查看:122
本文介绍了C#代码在C#中的fileupload Microsoft world doc中更改word文档中存在的数据的字体大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

c#代码更改word文档中word文档中存在的数据的字体大小在asp.net或c#中的Microsoft World文档可能与否。我尝试下面的代码不能正常工作告诉我



我尝试了什么:



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:

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);
        }

推荐答案

是的,它可能,如果你想改变文件内的东西,你必须使用代码打开它并开始处理它,OpenXML SDK将帮助您实现这一目标。



步骤1:安装nuget包 - DocumentFormat.OpenXml 2.5.0。

步骤2:修改你的代码

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
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();
    }


这篇关于C#代码在C#中的fileupload Microsoft world doc中更改word文档中存在的数据的字体大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆