private static CoreFilePropertiesPart MakeValidCoreFilePropertiesPart(CoreFilePropertiesPart coreFilePropertiesPart) { byte[] blob = System.Text.Encoding.UTF8.GetBytes("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><cp:coreProperties xmlns:cp=\"http://schemas.openxmlformats.org/package/2006/metadata/core-properties\" xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:dcterms=\"http://purl.org/dc/terms/\" xmlns:dcmitype=\"http://purl.org/dc/dcmitype/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><cp:lastModifiedBy>Niklas Karl</cp:lastModifiedBy><cp:revision>1</cp:revision><dcterms:modified xsi:type=\"dcterms:W3CDTF\">2021-02-18T15:56:56Z</dcterms:modified></cp:coreProperties>"); using (MemoryStream stream = new MemoryStream(blob)) { coreFilePropertiesPart.FeedData(stream); } return(coreFilePropertiesPart); }
/// <summary> /// Create instance from OpenXML part /// </summary> /// <param name="part">OpenXML part</param> /// <returns>Instance</returns> public static CoreProperties FromCoreFileProperties(CoreFilePropertiesPart part) { // Temporary variable CoreProperties coreProperties = new CoreProperties(); // Read XML using (XmlReader reader = new XmlTextReader(part.GetStream())) { while (reader.Read()) { if (reader.NodeType == XmlNodeType.Element) { switch (reader.Name.ToLower()) { case "dc:title": coreProperties.Title = reader.ReadElementContentAsString(); break; case "dc:subject": coreProperties.Creator = reader.ReadElementContentAsString(); break; case "dc:creator": coreProperties.Keywords = reader.ReadElementContentAsString(); break; case "cp:keywords": coreProperties.Description = reader.ReadElementContentAsString(); break; case "dc:description": coreProperties.LastModifiedBy = reader.ReadElementContentAsString(); break; case "cp:lastmodifiedby": coreProperties.Revision = reader.ReadElementContentAsString(); break; case "dcterms:created": coreProperties.Created = DateTime.Parse(reader.ReadElementContentAsString()); break; case "dcterms:modified": coreProperties.Modified = DateTime.Parse(reader.ReadElementContentAsString()); break; } } } } return(coreProperties); }
public static void ConvertToHtml(string file, string outputDirectory) { var fi = new FileInfo(file); Console.WriteLine(fi.Name); byte[] byteArray = File.ReadAllBytes(fi.FullName); using (var memoryStream = new MemoryStream()) { memoryStream.Write(byteArray, 0, byteArray.Length); using (WordprocessingDocument wDoc = WordprocessingDocument.Open(memoryStream, true)) { var destFileName = new FileInfo(fi.Name.Replace(".docx", ".html")); if (outputDirectory != null && outputDirectory != string.Empty) { var di = new DirectoryInfo(outputDirectory); if (!di.Exists) { throw new OpenXmlPowerToolsException("Output directory does not exist"); } destFileName = new FileInfo(Path.Combine(di.FullName, destFileName.Name)); } string imageDirectoryName = destFileName.FullName.Substring(0, destFileName.FullName.Length - 5) + "_files"; var imageCounter = 0; string pageTitle = fi.FullName; CoreFilePropertiesPart part = wDoc.CoreFilePropertiesPart; if (part != null) { pageTitle = (string)part.GetXDocument().Descendants(DC.title).FirstOrDefault() ?? fi.FullName; } // TODO: Determine max-width from size of content area. var settings = new HtmlConverterSettings { AdditionalCss = "body { margin: 1cm auto; max-width: 20cm; padding: 0; }", PageTitle = pageTitle, FabricateCssClasses = true, CssClassPrefix = "pt-", RestrictToSupportedLanguages = false, RestrictToSupportedNumberingFormats = false, ImageHandler = imageInfo => { var localDirInfo = new DirectoryInfo(imageDirectoryName); if (!localDirInfo.Exists) { localDirInfo.Create(); } ++imageCounter; string extension = imageInfo.ContentType.Split('/')[1].ToLower(); ImageFormat imageFormat = null; if (extension == "png") { imageFormat = ImageFormat.Png; } else if (extension == "gif") { imageFormat = ImageFormat.Gif; } else if (extension == "bmp") { imageFormat = ImageFormat.Bmp; } else if (extension == "jpeg") { imageFormat = ImageFormat.Jpeg; } else if (extension == "tiff") { // Convert tiff to gif. extension = "gif"; imageFormat = ImageFormat.Gif; } else if (extension == "x-wmf") { extension = "wmf"; imageFormat = ImageFormat.Wmf; } // If the image format isn't one that we expect, ignore it, // and don't return markup for the link. if (imageFormat == null) { return(null); } string imageFileName = imageDirectoryName + "/image" + imageCounter + "." + extension; try { imageInfo.Bitmap.Save(imageFileName, imageFormat); } catch (ExternalException) { return(null); } string imageSource = localDirInfo.Name + "/image" + imageCounter + "." + extension; var img = new XElement(Xhtml.img, new XAttribute(NoNamespace.src, imageSource), imageInfo.ImgStyleAttribute, imageInfo.AltText != null ? new XAttribute(NoNamespace.alt, imageInfo.AltText) : null); return(img); } }; XElement htmlElement = HtmlConverter.ConvertToHtml(wDoc, settings); // Produce HTML document with <!DOCTYPE html > declaration to tell the browser // we are using HTML5. var html = new XDocument( new XDocumentType("html", null, null, null), htmlElement); // Note: the xhtml returned by ConvertToHtmlTransform contains objects of type // XEntity. PtOpenXmlUtil.cs define the XEntity class. See // http://blogs.msdn.com/ericwhite/archive/2010/01/21/writing-entity-references-using-linq-to-xml.aspx // for detailed explanation. // // If you further transform the XML tree returned by ConvertToHtmlTransform, you // must do it correctly, or entities will not be serialized properly. var htmlString = html.ToString(SaveOptions.DisableFormatting); File.WriteAllText(destFileName.FullName, htmlString, Encoding.UTF8); } } }
private static void ChangeCoreFilePropertiesPart1(CoreFilePropertiesPart coreFilePropertiesPart1) { var package = coreFilePropertiesPart1.OpenXmlPackage; package.PackageProperties.Modified = System.Xml.XmlConvert.ToDateTime("2015-07-30T03:03:22Z", System.Xml.XmlDateTimeSerializationMode.RoundtripKind); }