Esempio n. 1
0
        public static void UpdateTitlePage(string srcFile, EpubMetaData dat)
        {
            var doc = XDocument.Load(srcFile);
            var idNodes = doc.Descendants().Where(e => e.Attribute("id") != null);    //idを持つノードを取得する

            if (idNodes != null)
            {
                //タイトルを書き込む
                var titleNode = idNodes.Where(e => e.Attribute("id").Value == "title").FirstOrDefault();
                if (titleNode != null)
                {
                    titleNode.Value = dat.title;

                }

                //著者を書き込む
                var authorNode = idNodes.Where(e => e.Attribute("id").Value == "author").FirstOrDefault();
                if (authorNode != null)
                {
                    authorNode.Value = dat.author;
                }

                //出版社を書き込む
                var publisherNode = idNodes.Where(e => e.Attribute("id").Value == "publisher").FirstOrDefault();
                if (publisherNode != null)
                {
                    publisherNode.Value = dat.publisher;
                }
            }
            doc.Save(srcFile);
        }
        //メタデータを書き込む
        public static void WriteMetaData(string packFile, EpubMetaData ePubDat)
        {
            var doc = new XDocument();
            try
            {
                doc = XDocument.Load(packFile);     //ファイルを読み込む
            }
            catch (Exception ex)
            {
                throw (new Exception("パッケージ文書を読み込めませんでした" + ex.ToString()));
            }

 
            //DC名前空間のノードをまとめて取得しておく
            var dcNodes = doc.Descendants().Where(e => e.Name.Namespace == NS_DC);

            //書名
            var titleNode =dcNodes.Where(e => e.Name.LocalName == "title").FirstOrDefault();
            if (titleNode == null)
            {
                throw (new Exception("パッケージ文書にtitle要素がありません"));
            }
            titleNode.Value = ePubDat.title;

            //著者
            var authorNode = dcNodes.Where(e => e.Name.LocalName == "creator").FirstOrDefault();
            if (authorNode == null)
            {
                throw (new Exception("パッケージ文書にcreator要素がありません"));
            }
            authorNode.Value = ePubDat.author;

            //出版
            var publisherNode = dcNodes.Where(e => e.Name.LocalName == "publisher").FirstOrDefault();
            if (publisherNode == null)
            {
                throw (new Exception("パッケージ文書にcreator要素がありません"));
            }
            publisherNode.Value = ePubDat.publisher;

            //ID identifier
            var packNode = doc.Descendants().Where(e => (e.Name.LocalName == "package") && (e.Name.Namespace == NS_PACKAGE)).FirstOrDefault();
            if (packNode == null)   //パッケージ文書が存在しなければ
            {
                throw (new Exception("パッケージ文書にpackage要素がありません"));
            }
            //unique-identifierに対応する要素を取得する
            var uniqueIDName = "";  //unique-identifier属性の値
            var uniqueID = packNode.Attributes().Where(e => e.Name == "unique-identifier").FirstOrDefault();
            if (uniqueID != null)   //unique-identifier属性がある
            {
                uniqueIDName = uniqueID.Value;  //unique IDとして使用する要素のid
                if (uniqueIDName.Length == 0)  //unique-identifierが記入されていなければ
                {
                    uniqueIDName = "pub-id";    //pub-idとする
                    packNode.SetAttributeValue("unique-identifier", uniqueIDName);  //パッケージノードのunique-identifier属性を更新する
                }
            }
            else //unique-identifier属性がない
            {
                uniqueIDName = "pub-id";    //pub-idとする
                packNode.SetAttributeValue("unique-identifier", uniqueIDName);  //パッケージノードのunique-identifier属性を更新する
            }
            //identifierノードを更新する
            var idNodes = dcNodes.Where(e => e.Name.LocalName == "identifier")           //identifier要素
                                .Where(e => e.Attribute("id") != null);                  //id属性がある
            var idNode = idNodes.Where(e => e.Attribute("id").Value == uniqueIDName)    //id属性がunique ID
                                .FirstOrDefault();
            if (idNode != null)    //IDノードが見つかったのでIDを書き込む
            {
                WriteUniqueIdentifier(ePubDat.title, idNode);
            }
            else //IDノードが見つからなかったのでノードを追加する
            {
                var idElement = new XElement(NS_DC+"identifier");   //dc:identifier
                idElement.SetAttributeValue("id", uniqueIDName);    //id="pub-id"

                var metadataNode = doc.Descendants().Where(e => e.Name.LocalName == "metadata").FirstOrDefault();
                if (metadataNode != null)
                {
                    metadataNode.Add(idElement);                        //metadataにidentifier要素を追加
                    WriteUniqueIdentifier(ePubDat.title, idElement);    //IDを書き込む
                }
                else //metadataがなかった
                {
                    throw (new Exception("パッケージ文書にpackage要素がありません"));
                }
            }

            //ページ送り方向を書き込む
            XNamespace package = "http://www.idpf.org/2007/opf";
            var spinNode = doc.Descendants()
                .Where(e => (e.Name.LocalName == "spine")&&(e.Name.Namespace==package)).FirstOrDefault();
            if (spinNode == null)   //spineが見つからなかった
            {
                throw (new Exception("パッケージ文書にspine要素がありません"));
            }
            if (ePubDat.isRightToLeft == true) //右から左なら
            {
                spinNode.SetAttributeValue("page-progression-direction", "rtl");
            }
            else    //左から右なら
            {
                spinNode.SetAttributeValue("page-progression-direction", "ltr");
            }
            try
            {
                doc.Save(packFile);
            }
            catch (Exception ex)
            {
                throw (new Exception("パッケージ文書の上書きに失敗しました" + ex.ToString()));
            }
            UpdatePackageDocument(packFile);    //日付を更新する
        }
        //EPUB生成ボタンが押された
        private async void GenerateEPUB(object sender, RoutedEventArgs e)
        {
            var btn = sender as Button;     //ボタンをdisableにする
            btn.IsEnabled = false;
            try
            {
                //
                //EPUBを作成する
                //

                //テンプレートを確認する
                btn.Content = "テンプレートを確認しています。";
                await Task.Run(() => EpubDocument.CheckEpubTemplate());

                //メタデータを取得する
                var ePubDoc = new EpubDocument();
                var metaData = new EpubMetaData();
                metaData.title = title.Text;                            //タイトル
                metaData.author = author.Text;                          //著者
                metaData.publisher = publisher.Text;                    //出版社
                metaData.isRightToLeft = (bool)isVertical.IsChecked;    //縦書きであれば右→左
                metaData.isVertical = (bool)isVertical.IsChecked;       //縦書きか?
                ePubDoc.metaData = metaData;
                
                //ファイル情報を取得する
                ePubDoc.coverImageFileName = cover.Text;    //表紙画像
                ePubDoc.novelFileName = novel.Text;         //本文

                //テキストフォーマットを取得する
                var opt = new ConvertOptions();
                opt.hasTag = (bool)hasTag.IsChecked;                    //修飾タグの有無
                opt.isSpaceIndented = (bool)isSpaceIndented.IsChecked;  //インデントがスペースか?

                if (isPlaneText.IsChecked == true)                      //プレーンテキスト
                {
                    opt.format = TextFormat.PLAIN_TEXT;
                }
                else if (isHeaddedText.IsChecked == true)               //*でヘッダを示すテキスト
                {
                    opt.format = TextFormat.PLAIN_TEXT_WITH_HEADER;
                }
                else                                                    //XHTML
                {
                    opt.format = TextFormat.XHTML;
                }
                ePubDoc.opt = opt;

                //生成処理実行
                btn.Content = "EPUBを作成しています";

                //後処理設定を読み込む
                PostProcess.executeEpubCheck = useEpubCheck.IsChecked;              //EpubCheck
                PostProcess.executeKindePreViewer = execKindlePreviewer.IsChecked;  //KindlePreviewer

                var isEpubGen = ePubDoc.GenerateEpubDocument();

                //設定を保存する
                SaveDefaults();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            btn.IsEnabled = true;   //ボタンをEnableに戻す
            btn.Content = "EPUB3ファイルを生成する";
        }
Esempio n. 4
0
        //メタデータを書き込む
        public static void WriteMetaData(string packFile, EpubMetaData ePubDat)
        {
            var doc = new XDocument();

            try
            {
                doc = XDocument.Load(packFile);     //ファイルを読み込む
            }
            catch (Exception ex)
            {
                throw (new Exception("パッケージ文書を読み込めませんでした" + ex.ToString()));
            }


            //DC名前空間のノードをまとめて取得しておく
            var dcNodes = doc.Descendants().Where(e => e.Name.Namespace == NS_DC);

            //書名
            var titleNode = dcNodes.Where(e => e.Name.LocalName == "title").FirstOrDefault();

            if (titleNode == null)
            {
                throw (new Exception("パッケージ文書にtitle要素がありません"));
            }
            titleNode.Value = ePubDat.title;

            //著者
            var authorNode = dcNodes.Where(e => e.Name.LocalName == "creator").FirstOrDefault();

            if (authorNode == null)
            {
                throw (new Exception("パッケージ文書にcreator要素がありません"));
            }
            authorNode.Value = ePubDat.author;

            //出版
            var publisherNode = dcNodes.Where(e => e.Name.LocalName == "publisher").FirstOrDefault();

            if (publisherNode == null)
            {
                throw (new Exception("パッケージ文書にcreator要素がありません"));
            }
            publisherNode.Value = ePubDat.publisher;

            //ID identifier
            var packNode = doc.Descendants().Where(e => (e.Name.LocalName == "package") && (e.Name.Namespace == NS_PACKAGE)).FirstOrDefault();

            if (packNode == null)   //パッケージ文書が存在しなければ
            {
                throw (new Exception("パッケージ文書にpackage要素がありません"));
            }
            //unique-identifierに対応する要素を取得する
            var uniqueIDName = "";  //unique-identifier属性の値
            var uniqueID     = packNode.Attributes().Where(e => e.Name == "unique-identifier").FirstOrDefault();

            if (uniqueID != null)                                                  //unique-identifier属性がある
            {
                uniqueIDName = uniqueID.Value;                                     //unique IDとして使用する要素のid
                if (uniqueIDName.Length == 0)                                      //unique-identifierが記入されていなければ
                {
                    uniqueIDName = "pub-id";                                       //pub-idとする
                    packNode.SetAttributeValue("unique-identifier", uniqueIDName); //パッケージノードのunique-identifier属性を更新する
                }
            }
            else //unique-identifier属性がない
            {
                uniqueIDName = "pub-id";    //pub-idとする
                packNode.SetAttributeValue("unique-identifier", uniqueIDName);  //パッケージノードのunique-identifier属性を更新する
            }
            //identifierノードを更新する
            var idNodes = dcNodes.Where(e => e.Name.LocalName == "identifier")          //identifier要素
                          .Where(e => e.Attribute("id") != null);                       //id属性がある
            var idNode = idNodes.Where(e => e.Attribute("id").Value == uniqueIDName)    //id属性がunique ID
                         .FirstOrDefault();

            if (idNode != null)    //IDノードが見つかったのでIDを書き込む
            {
                WriteUniqueIdentifier(ePubDat.title, idNode);
            }
            else //IDノードが見つからなかったのでノードを追加する
            {
                var idElement = new XElement(NS_DC + "identifier");   //dc:identifier
                idElement.SetAttributeValue("id", uniqueIDName);    //id="pub-id"

                var metadataNode = doc.Descendants().Where(e => e.Name.LocalName == "metadata").FirstOrDefault();
                if (metadataNode != null)
                {
                    metadataNode.Add(idElement);                        //metadataにidentifier要素を追加
                    WriteUniqueIdentifier(ePubDat.title, idElement);    //IDを書き込む
                }
                else //metadataがなかった
                {
                    throw (new Exception("パッケージ文書にpackage要素がありません"));
                }
            }

            //ページ送り方向を書き込む
            XNamespace package  = "http://www.idpf.org/2007/opf";
            var        spinNode = doc.Descendants()
                                  .Where(e => (e.Name.LocalName == "spine") && (e.Name.Namespace == package)).FirstOrDefault();

            if (spinNode == null)   //spineが見つからなかった
            {
                throw (new Exception("パッケージ文書にspine要素がありません"));
            }
            if (ePubDat.isRightToLeft == true) //右から左なら
            {
                spinNode.SetAttributeValue("page-progression-direction", "rtl");
            }
            else    //左から右なら
            {
                spinNode.SetAttributeValue("page-progression-direction", "ltr");
            }
            try
            {
                doc.Save(packFile);
            }
            catch (Exception ex)
            {
                throw (new Exception("パッケージ文書の上書きに失敗しました" + ex.ToString()));
            }
            UpdatePackageDocument(packFile);    //日付を更新する
        }