我正在使用一个 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/