c# - 带有字体大小的 OpenXML 奇怪行为

标签 c# asp.net xml asp.net-web-api openxml

我正在使用一个 dll 来创建方法,这些方法会根据传递的参数为我生成创建段落的逻辑:

例如在我的 C# 代码中我有这个:

    // document permission title
    DocRun accessTypeTitle  = new DocRun();
    Run permissionTitle = accessTypeTitle.createParagraph("DOCUMENT ACCESS", PARAGRAPHCOLOR,FONTSIZETEXT,DEFAULTFONT);

我的 dll 上有我的方法来执行逻辑:

 public class DocRun
    {
        public Run createParagraph(String text, String colorVal, String fontSize,String font)
        {
            Run run = new Run() { RsidRunProperties = "00C53974" };

            RunProperties runProperties = new RunProperties();
            RunFonts runFonts = new RunFonts() { Ascii = font, HighAnsi = font, EastAsia = "Segoe UI", ComplexScript = font };
            Color color = new Color() { Val = colorVal };
            //Kern kern = new Kern() { Val = (UInt32Value)24U };
            FontSize fontSize11 = new FontSize() { Val = fontSize };
            FontSizeComplexScript fontSizeComplexScript11 = new FontSizeComplexScript() { Val = fontSize };

            runProperties.Append(runFonts);
            runProperties.Append(color);
            //runProperties.Append(kern);
            runProperties.Append(fontSize11);
            runProperties.Append(fontSizeComplexScript11);
            Text t = new Text(text)
            {
                Text = text,
                Space = SpaceProcessingModeValues.Preserve
            };

            run.Append(runProperties);
            run.Append(t);

            return run;

        }

    }
}

在我返回运行后,我可以对图像和其他段落执行相同的操作,并将它们附加到文档中,如下所示:

   var stream = new MemoryStream();
    using (WordprocessingDocument doc = WordprocessingDocument.Create(stream, WordprocessingDocumentType.Document, true))
    {
        MainDocumentPart mainPart = doc.AddMainDocumentPart();

        // Logo company construction
        DocImage companyLogo = new DocImage();
        Run imageLogo = companyLogo.imageCreator(mainPart,COMPANYLOGOPATH,COMPANYIMAGENAME,COMPANYLOGOWIDTH,COMPANYLOGOHEIGHT,COMPANYIMAGEALING);

        DocImage titleShape = new DocImage();
        Run imageShape = titleShape.imageCreator(mainPart, SHAPEIMAGEPATH, TITLESHAPEIMAGENAME, TITLESHAPEWIDTH, TITLESHAPEHEIGHT,SHAPEIMAGEALING);

        DocImage clientImage = new DocImage();
        Run clientLogo = titleShape.imageCreatorUrl(mainPart, SHAPEIMAGEPATH, TITLESHAPEIMAGENAME, TITLESHAPEWIDTHCLIENTLOGO, TITLESHAPEHEIGHTCLIENTLOGO, CLIENTIMAGEALIGN,clientLogoPath);

        new Document(new Body()).Save(mainPart);

        Body body = mainPart.Document.Body;

        body.Append(new Paragraph(
                 new Run(imageLogo)));

        body.Append(new Paragraph(
                 new Run(imageShape)));

        body.Append(new Paragraph(
               new Run(projectNameTxt)));

        body.Append(new Paragraph(
                new Run(clientLogo)));

        body.Append(new Paragraph(
                new Run(dateTxt)));

        body.Append(new Paragraph(
                new Run(permissionTitle)));

        body.Append(new Paragraph(
                new Run(permission)));

        body.Append(new Paragraph(
                new Run(disclaimerTitleTxt)));

        body.Append(new Paragraph(
                new Run(disclaimerDescriptionTxt)));

        mainPart.Document.Save();



    }
    stream.Seek(0, SeekOrigin.Begin);
    Directory.CreateDirectory(HostingEnvironment.MapPath(DOCUMENTSLOCATION));
    System.IO.File.WriteAllBytes(HostingEnvironment.MapPath("~/Files/test5.docx"), stream.ToArray());

}

我的问题是文档生成的字体大小总是我在创建的 openXML dll 上定义的实际大小的一半。

我调试了作为参数传递的 fontSize,并且在 dll 上接收到的 fontsize 是正确的,这是怎么回事?

谢谢大家,

最佳答案

FontSize指定一个以半点为单位的值。所以如果需要11点的字体,需要指定值为22。

这也记录在电子书的第 13 页 "Open XML Explained" by Wouter Van Vugt .

关于c# - 带有字体大小的 OpenXML 奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45302588/

相关文章:

C# 如何处理 "check on"、 "cancel"并等待异步 Web 请求?

c# - 如何使用 id 代码更新?

c# - 在 C#/Asp.Net 中确定 IIS 服务器上的 "user logout"的最佳方法是什么?

asp.net - 为什么嵌套的 ASP.NET 4.0 web.config 有时会停止继承?

具有多个字段的 XML 架构键

xml - 从Powershell中的xml节点中删除内部文本

c# - 如果我在代码中设置一些值,我仍然可以使用 UpdateModel() 吗?

c# - 我可以将设置存储在 ASP.NET MVC 的 settings.config 文件中吗?

asp.net - 通过 HTTP GET 访问 ASMX 脚本服务

xml - JAXBException 发生 : class javax. jdo.identity.LongIdentity 及其任何父类(super class)在此上下文中均未知