Ejemplo n.º 1
0
        public static string ConvertDocx2Html(string fileInput, string fileOutput, int margin)
        {
            var    fileInfo     = new FileInfo(fileInput);
            string fullFilePath = fileInfo.FullName;
            string htmlText     = string.Empty;

            try
            {
                htmlText = ParseDOCX(fileInfo, margin);
            }
            catch (OpenXmlPackageException e)
            {
                if (e.ToString().Contains("Invalid Hyperlink"))
                {
                    using (FileStream fs = new FileStream(fullFilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite))
                    {
                        UriFixer.FixInvalidUri(fs, brokenUri => FixUri(brokenUri));
                    }
                    htmlText = ParseDOCX(fileInfo, margin);
                }
            }

            var writer = File.CreateText(fileOutput + ".html");

            writer.WriteLine(htmlText.ToString());
            writer.Dispose();
            return(fileOutput);
        }
        private async Task <IActionResult> ConvertDocxToHtmlWithImageAsync(string fileName, byte[] fileBytes)
        {
            try
            {
                var docFilePath = Path.Combine(_tempPath, fileName);
                System.IO.File.WriteAllBytes(docFilePath, fileBytes);
                string htmlText = string.Empty;
                try
                {
                    htmlText = ParseDOCX(docFilePath);
                }
                catch (OpenXmlPackageException e)
                {
                    if (e.ToString().Contains("Invalid Hyperlink"))
                    {
                        using (FileStream fs = new FileStream(docFilePath, FileMode.Open, FileAccess.Read))
                        {
                            UriFixer.FixInvalidUri(fs, brokenUri => FixUri(brokenUri));
                        }
                        htmlText = ParseDOCX(docFilePath);
                    }
                }

                var outfile = Path.Combine(_tempPath, ("htmldata" + ".html"));
                var writer  = System.IO.File.CreateText(outfile);
                writer.WriteLine(htmlText.ToString());
                writer.Dispose();
                System.IO.File.Delete(docFilePath);
                return(File(await System.IO.File.ReadAllBytesAsync(outfile), "application/octet-stream", (Path.GetFileNameWithoutExtension(fileName) + ".html")));
            }
            catch (Exception ex)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError));
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Code obtained from CodeProject on GitHub
        /// https://github.com/zaagan/Docx-Html-Docx/blob/master/DocxToHTML.Converter/HTMLConverter.cs
        /// </summary>
        /// <param name="fullFilePath"></param>
        /// <returns>html of document</returns>
        public string ConvertToHtml(string fullFilePath)
        {
            if (string.IsNullOrEmpty(fullFilePath) || Path.GetExtension(fullFilePath) != ".docx")
            {
                return("Unsupported format");
            }

            FileInfo fileInfo = new FileInfo(fullFilePath);

            string htmlText = string.Empty;

            try
            {
                htmlText = ParseDOCX(fileInfo);
            }
            catch (OpenXmlPackageException e)
            {
                if (e.ToString().Contains("Invalid Hyperlink"))
                {
                    using (FileStream fs = new FileStream(fullFilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite))
                    {
                        UriFixer.FixInvalidUri(fs, brokenUri => FixUri(brokenUri));
                    }
                    htmlText = ParseDOCX(fileInfo);
                }
            }


            return(htmlText);
        }
Ejemplo n.º 4
0
        public static Dictionary <uint, string[]> GetTextOnSlides(string presentationFile)
        {
            var slides = new Dictionary <uint, string[]>();

            try
            {
                using (PresentationDocument presentationDocument = PresentationDocument.Open(presentationFile, false))
                {
                    PresentationPart presentationPart = presentationDocument.PresentationPart;

                    // Verify that the presentation part and presentation exist.
                    if (presentationPart != null && presentationPart.Presentation != null)
                    {
                        // Get the Presentation object from the presentation part.
                        Presentation presentation = presentationPart.Presentation;

                        // Verify that the slide ID list exists.
                        if (presentation.SlideIdList != null)
                        {
                            // Get the collection of slide IDs from the slide ID list.
                            var slideIds = presentation.SlideIdList.ChildElements;

                            foreach (SlideId sld in slideIds)
                            {
                                string slidePartRelationshipId = sld.RelationshipId;

                                // Get the specified slide part from the relationship ID.
                                SlidePart slidePart = (SlidePart)presentationPart.GetPartById(slidePartRelationshipId);

                                // Pass the slide part to the next method, and
                                // then return the array of strings that method
                                // returns to the previous method.
                                slides.Add(sld.Id, GetAllTextInSlide(slidePart) ?? new string[] { });
                            }
                        }
                    }
                }
                return(slides);
            }
            catch (OpenXmlPackageException e)
            {
                if (e.ToString().Contains("Invalid Hyperlink"))
                {
                    using (FileStream fs = new FileStream(presentationFile, FileMode.OpenOrCreate, FileAccess.ReadWrite))
                    {
                        UriFixer.FixInvalidUri(fs, brokenUri => FixUri(brokenUri));
                    }
                    return(GetTextOnSlides(presentationFile));
                }
                throw e;
            }
        }
Ejemplo n.º 5
0
        public DataTable techBaseTable = new DataTable(); // Cюда записана техническая база
        public bool Optimize()
        {
            try // код помещён в блок try, потому что возможно исключение типа OpenXmlPackageException - Invalid Hyperlink
            {
                using (SpreadsheetDocument spreadSheetDocument = SpreadsheetDocument.Open(techBasePath, true))
                {
                    techBaseTable = DataTableFromExcel.GetDataTableFromSpreadSheet(spreadSheetDocument);
                }
            }
            catch (OpenXmlPackageException e)
            {
                if (e.ToString().Contains("Invalid Hyperlink")) // если исключение сожержит "Invalid Hyperlink"
                {
                    using (FileStream fs = new FileStream(techBasePath, FileMode.OpenOrCreate, FileAccess.ReadWrite))
                    {
                        UriFixer.FixInvalidUri(fs, brokenUri => UriFixer.FixUri(brokenUri)); // чиним Uri
                    }
                    using (SpreadsheetDocument spreadSheetDocument = SpreadsheetDocument.Open(techBasePath, true))
                    {
                        techBaseTable = DataTableFromExcel.GetDataTableFromSpreadSheet(spreadSheetDocument);
                    }
                }
            }
            catch (Exception)
            {
                return(false);
            }

            foreach (DataRow row in techBaseTable.Rows)           // Оптимизация тех. базы - убираем цифры
            {
                string product = System.Convert.ToString(row[1]); // Название продукта
                string productWithoutDigits = "";
                for (int i = 0; i < product.Length; i++)          // Убираем числа
                {
                    if (!char.IsDigit(product[i]))
                    {
                        productWithoutDigits += product[i];
                    }
                }
                row[1] = productWithoutDigits;
            }
            return(true);
        }
Ejemplo n.º 6
0
        static string[] stringSeparators = new string[] { ", " }; // разделитель строк, содержащих интересуюие продукты

        public static string Fill(ref List <ProductsOfInterest> products, List <string> sheets_names, List <ExcelFile> files, Report report)
        {
            products = new List <ProductsOfInterest>();
            foreach (string sheetFromListBox in sheets_names)
            {
                foreach (ExcelFile file in files)
                {
                    foreach (string sheetFromFile in file.Sheets)
                    {
                        if (sheetFromListBox.Equals(sheetFromFile))
                        {
                            try // код помещён в блок try, потому что возможно исключение типа OpenXmlPackageException - Invalid Hyperlink
                            {
                                using (SpreadsheetDocument spreadSheetDocument = SpreadsheetDocument.Open(file.FilePath, true))
                                {
                                    HelpFill(spreadSheetDocument, sheetFromFile, file, ref products, report);
                                }
                            }
                            catch (OpenXmlPackageException e)
                            {
                                if (e.ToString().Contains("Invalid Hyperlink")) // если исключение сожержит "Invalid Hyperlink"
                                {
                                    using (FileStream fs = new FileStream(file.FilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite))
                                    {
                                        UriFixer.FixInvalidUri(fs, brokenUri => UriFixer.FixUri(brokenUri)); // чиним Uri
                                    }
                                    using (SpreadsheetDocument spreadSheetDocument = SpreadsheetDocument.Open(file.FilePath, true))
                                    {
                                        HelpFill(spreadSheetDocument, sheetFromFile, file, ref products, report);
                                    }
                                }
                            }
                            catch (Exception e)
                            {
                                return(file.FilePath);
                            }
                        }
                    }
                }
            }
            return(null);
        }
Ejemplo n.º 7
0
        public string ConvertToHTML(byte[] wordBytes, string fileName)
        {
            string htmlText = string.Empty;

            try
            {
                htmlText = ParseDOCX(wordBytes, fileName);
            }
            catch (OpenXmlPackageException e)
            {
                if (e.ToString().Contains("Invalid Hyperlink"))
                {
                    using (MemoryStream ms = new MemoryStream(wordBytes))
                    {
                        UriFixer.FixInvalidUri(ms, brokenUri => FixUri(brokenUri));
                    }
                    htmlText = ParseDOCX(wordBytes, fileName);
                }
            }

            return(htmlText);
        }
Ejemplo n.º 8
0
        public static string GetHtmlfromWord(string fileInput, int margin)
        {
            var    fileInfo     = new FileInfo(fileInput);
            string fullFilePath = fileInfo.FullName;
            string htmlText     = string.Empty;

            try
            {
                htmlText = ParseDOCX(fileInfo, margin);
            }
            catch (OpenXmlPackageException e)
            {
                if (e.ToString().Contains("Invalid Hyperlink"))
                {
                    using (FileStream fs = new FileStream(fullFilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite))
                    {
                        UriFixer.FixInvalidUri(fs, brokenUri => FixUri(brokenUri));
                    }
                    htmlText = ParseDOCX(fileInfo, margin);
                }
            }
            return(htmlText);
        }
Ejemplo n.º 9
0
        public static string ConvertDocx2Pdf(string fileInput, string fileOutput, int margin)
        {
            //Convert docx to html
            var    fileInfo     = new FileInfo(fileInput);
            string fullFilePath = fileInfo.FullName;
            string htmlText     = string.Empty;

            try
            {
                htmlText = ParseDOCX(fileInfo, margin);
            }
            catch (OpenXmlPackageException e)
            {
                if (e.ToString().Contains("Invalid Hyperlink"))
                {
                    using (FileStream fs = new FileStream(fullFilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite))
                    {
                        UriFixer.FixInvalidUri(fs, brokenUri => FixUri(brokenUri));
                    }
                    htmlText = ParseDOCX(fileInfo, margin);
                }
            }

            var writer = File.CreateText(fileOutput + ".html");

            writer.WriteLine(htmlText.ToString());
            writer.Dispose();

            // Convert html to pdf
            var doc = new HtmlToPdfDocument()
            {
                GlobalSettings =
                {
                    DocumentTitle = "Business Document",
                    ColorMode     = DinkToPdf.ColorMode.Color,
                    Orientation   = DinkToPdf.Orientation.Portrait,
                    PaperSize     = PaperKind.A4,
                    Out           = fileOutput + ".pdf",
                    Margins       =
                    {
                        Top    =                              2,
                        Bottom =                              2,
                        Right  =                            1.5,
                        Left   =                              1,
                        Unit   = Unit.Centimeters
                    }
                },
                Objects =
                {
                    new ObjectSettings()
                    {
                        PagesCount     = true,
                        HtmlContent    = File.ReadAllText(fileOutput + ".html"),
                        WebSettings    = { DefaultEncoding = "utf-8" },
                        FooterSettings ={ FontSize                             =9, Center = "Trang [page] / [toPage]", Line = true }
                    }
                }
            };
            var converter = new BasicConverter(new PdfTools());

            converter.Convert(doc);
            return(fileOutput);
        }
Ejemplo n.º 10
0
        /// <summary>
        /// プロパティを読み込みます
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="propertiesTable"></param>
        /// <returns>取得したプロパティ一覧</returns>
        public Dictionary <string, string> ReadProperties(ClsFilePropertyList list, string fileType, string str_exception)  // 20171011 修正(ファイル読込時のエラー理由を保存)
        {
            Dictionary <string, string> retTable = new Dictionary <string, string>();

            System.IO.FileInfo fi = new System.IO.FileInfo(list.filePath);
            if (fi.Length == 0)
            {
                // ファイルサイズがゼロの場合プロパティが存在しないのでエラー
                // 20171011 追加 (読取専用、ファイ存在しない以外のエラー)
                //error_reason = ListForm.LIST_VIEW_NA;
                return(retTable);
            }

            SpreadsheetDocument    excel = null;
            WordprocessingDocument word  = null;
            PresentationDocument   ppt   = null;

            CoreFilePropertiesPart coreFileProperties;

            // 20171011 修正(xlsxとdocxのみファイルを開いている状態でファイルアクセスするとエラーになる為の回避対応)
            try
            {
                // ファイルのプロパティ領域を開く
                switch (fileType)
                {
                case "Excel":     // エクセルの場合
                    excel = SpreadsheetDocument.Open(list.filePath, false);
                    coreFileProperties = excel.CoreFilePropertiesPart;
                    break;

                case "Word":     // ワードの場合
                    word = WordprocessingDocument.Open(list.filePath, false);
                    coreFileProperties = word.CoreFilePropertiesPart;
                    break;

                case "PowerPoint":     // パワポの場合
                    ppt = PresentationDocument.Open(list.filePath, false);
                    coreFileProperties = ppt.CoreFilePropertiesPart;
                    break;

                default:
                    // 異常なファイル
                    // 20171228 追加(エラー理由保存)
                    //error_reason = ListForm.LIST_VIEW_NA;
                    return(retTable);
                }
                NameTable           nt        = new NameTable();
                XmlNamespaceManager nsManager = new XmlNamespaceManager(nt);
                nsManager.AddNamespace(PropertiesKeyList.STR_TAG_CP, PropertiesSchemaList.CorePropertiesSchema);
                nsManager.AddNamespace(PropertiesKeyList.STR_TAG_DC, PropertiesSchemaList.DcPropertiesSchema);
                nsManager.AddNamespace(PropertiesKeyList.STR_TAG_DCTRMS, PropertiesSchemaList.DctermsPropertiesSchema);
                nsManager.AddNamespace(PropertiesKeyList.STR_TAG_DCMITYPE, PropertiesSchemaList.DcmitypePropertiesSchema);
                nsManager.AddNamespace(PropertiesKeyList.STR_TAG_XSI, PropertiesSchemaList.XsiPropertiesSchema);

                XmlDocument xdoc = new XmlDocument();
                xdoc.Load(coreFileProperties.GetStream());

                // プロパティのキーリストを作成
                List <string> propertieslist = PropertiesKeyList.getPropertiesKeyList();

                // 全キーリストを見て存在するデータを取得
                foreach (string key in propertieslist)
                {
                    // 書き込み先のキーワードを指定
                    string searchString = string.Format(PropertiesKeyList.STR_CORE_PROPERTIES + "{0}", key);
                    // 書き込み先を検索
                    XmlNode xNode = xdoc.SelectSingleNode(searchString, nsManager);

                    if (xNode != null)
                    {
                        // 読み込む
                        retTable.Add(key, xNode.InnerText);
                    }
                }

                // ファイルのプロパティ領域を閉じる
                if (excel != null)
                {
                    excel.Close();
                }
                if (word != null)
                {
                    word.Close();
                }
                if (ppt != null)
                {
                    ppt.Close();
                }

                // プロパティ取得
                string[] propertyData = null;
                if (retTable.ContainsKey(PropertiesKeyList.STR_KEYWORDS) != false)
                {
                    propertyData = retTable[PropertiesKeyList.STR_KEYWORDS].Split(';');
                }

                if (propertyData != null)
                {
                    list.fileSecrecy = propertyData[0].TrimEnd();

                    // 機密区分が登録されているか判定
                    if (!string.IsNullOrEmpty(list.fileSecrecy))
                    {
                        // 登録あり
                        if (propertyData.Count() >= 1)
                        {
                            list.fileClassification = propertyData[1].TrimEnd();
                        }
                        if (propertyData.Count() >= 2)
                        {
                            list.fileOfficeCode = propertyData[2].TrimEnd();
                        }

                        // 他事務所ファイル判定
                        if (Globals.ThisAddIn.clsCommonSettings.strOfficeCode != list.fileOfficeCode.Trim())
                        {
                            // 他事務所ファイルの場合、登録なし扱にする
                            list.fileSecrecy = "";
                        }
                    }
                }
                else
                {
                    list.fileSecrecy = "Notting";
                }
            }
#if false
            #region HyperLink修復

            // ■ ADD TSE Kitada
            // HyperLinkが破損している場合に、そのリンクを書き直して正常にOPEN出来るようにする。
            // 但し、ドキュメントの中身を直接書き換える処理のため、見送る。
            catch (OpenXmlPackageException ope)
            {
                if (ope.ToString().Contains("Invalid Hyperlink"))
                {
                    // HyperLinkの破損が原因なので、内部のリンクを修正する
                    using (FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite))
                    {
                        UriFixer.FixInvalidUri(fs, brokenUri => FixUri(brokenUri));
                    }

                    if (count >= 1)
                    {
                        // 2回実行してダメだったので終了
                        error_reason = ListForm.LIST_VIEW_NA;
                    }
                    else
                    {
                        // もう一度トライ
                        retTable = ReadProperties(filePath, filetype, ref error_reason, ref str_exception, 1);
                    }
                }

                if (excel != null)
                {
                    excel.Close();
                }
                if (word != null)
                {
                    word.Close();
                }
                if (ppt != null)
                {
                    ppt.Close();
                }
            }
            #endregion
#endif
            catch (Exception e)
            {
                str_exception += "file : " + list.filePath + "\r\n\r\n";
                str_exception += "error : " + e.ToString();
                // xlsxとdocxのみファイルを開いている状態でファイルアクセスした場合
                // ファイルが開かれている場合のエラー
                //error_reason = ListForm.LIST_VIEW_NA;

                if (excel != null)
                {
                    excel.Close();
                }
                if (word != null)
                {
                    word.Close();
                }
                if (ppt != null)
                {
                    ppt.Close();
                }

                return(retTable);
            }
            return(retTable);
        }