Exemple #1
0
        /// <summary>
        /// 保存OPF文件。
        /// </summary>
        private void SaveOPF()
        {
            //生成OPF文件
            var filePath = Path.Combine(_directoryName, "content.opf").DeleteFile();

            using (var writer = new XmlTextWriter(filePath, Encoding.UTF8))
            {
                writer.WriteStartDocument();
                writer.WriteStartElement("package", "http://www.idpf.org/2007/opf");
                writer.WriteAttributeString("xmlns:dc", "http://purl.org/dc/elements/1.1/");
                writer.WriteAttributeString("unique-identifier", BookId);
                writer.WriteAttributeString("version", "2.0");
                //metadata
                writer.WriteStartElement("metadata");
                writer.WriteAttributeString("xmlns:opf", "http://www.idpf.org/2007/opf");
                writer.WriteAttributeString("xmlns:dc", "http://purl.org/dc/elements/1.1/");
                foreach (var property in typeof(DublinCore).GetEntityType().GetProperties())
                {
                    var value = property.Get(DC)?.ToString();
                    if (!string.IsNullOrEmpty(value))
                    {
                        writer.WriteElementText($"dc:{property.Name.ToLower()}", value);
                    }
                }
                //默认首页和图片
                var coverImage = Manifest.FirstOrDefault(x => x.IsCover && !x.IsSpine);
                if (coverImage != null)
                {
                    Metadata["cover"] = coverImage.Id;
                }
                foreach (var data in Metadata)
                {
                    writer.WriteMetaElement(data.Key, data.Value);
                }
                writer.WriteEndElement();
                //manifest
                writer.WriteStartElement("manifest");
                foreach (var manifest in Manifest)
                {
                    writer.WriteStartElement("item");
                    writer.WriteAttributeString("id", manifest.Id);
                    writer.WriteAttributeString("href", manifest.Href);
                    writer.WriteAttributeString("media-type", manifest.MediaType);
                    writer.WriteEndElement();
                }
                writer.WriteEndElement();
                //spine
                writer.WriteStartElement("spine");
                writer.WriteAttributeString("toc", "ncx");//NCX文件Id
                var manifests = LoadSortManifest();
                foreach (var manifest in manifests)
                {
                    writer.WriteStartElement("itemref");
                    writer.WriteAttributeString("idref", manifest.Id);
                    if (manifest.IsCover)
                    {
                        writer.WriteAttributeString("linear", "no");
                    }
                    writer.WriteEndElement();
                }
                writer.WriteEndElement();
                //guide
                writer.WriteStartElement("guide");
                //var guide = manifests.FirstOrDefault(x => x.IsCover);
                //writer.WriteReferenceElement(guide, "cover");
                var guide = manifests.FirstOrDefault(x => x.IsToc);
                writer.WriteReferenceElement(guide, "toc");
                writer.WriteEndElement();
                //end
                writer.WriteEndElement();
                writer.WriteEndDocument();
            }
        }