Filipe Costa Filipe Costa - 2 years ago 115
ASP.NET (C#) Question

OpenXML strange behaviour with font-size

I am using a dll to create methods that generates me the logic to create a paragraph based on the parameters that are passed:

for example on my C# code i have this:

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


i have my method on my dll that does the logic:

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;

}

}
}


after i return the run, i can do the same with the images nd other paragraph and just append them to the doc like this:

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

}


my problem is that the document generated font size are always half of the real size i defined on the openXML dll that i created.

I debuged the fontSize that i pass as the parameter and the fontsize received on the dll is the correct one, what is going on?

Thanks guys,

Answer Source

The FontSize is specified with a value which measures in half-points. So if you need a 11 point font, you need to specify the value to be 22.

This is also documented on page 13 in the e-book "Open XML Explained" by Wouter Van Vugt.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download