예제 #1
0
파일: Store.cs 프로젝트: kpocza/docx2tex
 public Store(Styling stylingFn, Taging tagingFn, IStatusInformation statusInfo)
 {
     _stylingFn  = stylingFn;
     _tagingFn   = tagingFn;
     _runs       = new List <Run>();
     _statusInfo = statusInfo;
 }
예제 #2
0
        /// <summary>
        /// Setup helpers and namespaces
        /// </summary>
        /// <param name="documentXmlStream"></param>
        /// <param name="dotnetFn"></param>
        public Engine(Stream documentXmlStream, Numbering numberingFn, Imaging imagingFn, IStatusInformation statusInfo)
        {
            _statusInfo = statusInfo;
            _doc        = new XmlDocument();
            _doc.Load(documentXmlStream);
            _texingFn  = new TeXing();
            _stylingFn = new Styling();
            var tagingFn = new Taging();

            _tex = new Store(_stylingFn, tagingFn, statusInfo);

            _nsmgr = new XmlNamespaceManager(_doc.NameTable);
            _nsmgr.AddNamespace("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main");
            _nsmgr.AddNamespace("wp", "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing");
            _nsmgr.AddNamespace("a", "http://schemas.openxmlformats.org/drawingml/2006/main");
            _nsmgr.AddNamespace("pic", "http://schemas.openxmlformats.org/drawingml/2006/picture");
            _nsmgr.AddNamespace("r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships");
            _nsmgr.AddNamespace("m", "http://schemas.openxmlformats.org/officeDocument/2006/math");
            _nsmgr.AddNamespace("v", "urn:schemas-microsoft-com:vml");

            _numberingFn = numberingFn;
            _imagingFn   = imagingFn;

            InitMathTables();
            CacheResolvedStyles();
            CacheBookmarks();
        }
예제 #3
0
        public string ResolveImage(string imageId, IStatusInformation statusInfo)
        {
            PackageRelationship rs = _docRelPart.GetRelationship(imageId);

            string imageUrl         = rs.TargetUri.OriginalString;
            string orginalImagePath = Path.Combine(_latexDirectory, imageUrl);
            string newImagePath     = Path.Combine(_latexDirectory, imageUrl);

            ZipPackagePart imagePackagePart = (ZipPackagePart)rs.Package.GetPart(new Uri("/word/" + imageUrl, UriKind.Relative));

            using (Stream contentStream = imagePackagePart.GetStream())
            {
                byte[] content = new byte[contentStream.Length];
                contentStream.Read(content, 0, (int)contentStream.Length);

                using (FileStream fs = new FileStream(orginalImagePath, FileMode.Create))
                {
                    using (BinaryWriter bwImage = new BinaryWriter(fs))
                    {
                        bwImage.Write(content, 0, (int)contentStream.Length);
                    }
                }
            }

            ConvertImageToEPS(orginalImagePath, newImagePath, statusInfo);

            return(Path.ChangeExtension(imageUrl, "eps"));;
        }
예제 #4
0
        private static void ConvertImageToEPS(string orginalImagePath, string newImagePath, IStatusInformation statusInfo)
        {
            string epsImagePath    = Path.ChangeExtension(newImagePath, "eps");
            string imageMagickPath = Config.Instance.Infra.ImageMagickPath;

            if (string.IsNullOrEmpty(imageMagickPath))
            {
                statusInfo.WriteLine("ERROR: Unable to read configuration setting of ImageMagick's path");
                return;
            }
            try
            {
                Process proc = Process.Start(imageMagickPath, string.Format("\"{0}\" \"{1}\"", orginalImagePath, epsImagePath));
                proc.WaitForExit(60 * 1000); // wait one minute
            }
            catch
            {
                statusInfo.WriteLine("ERROR: Unable to start ImageMagicK");
            }
        }
예제 #5
0
        public bool Process(string inputDocxPath, string outputTexPath, IStatusInformation statusInfo)
        {
            string documentPath = Path.GetDirectoryName(outputTexPath);

            if (documentPath == null)
            {
                documentPath = Path.GetPathRoot(outputTexPath);
            }

            EnsureMediaPath(documentPath);

            statusInfo.WriteCR("Opening document...");
            Package pkg = null;

            try
            {
                pkg = Package.Open(inputDocxPath, FileMode.Open, FileAccess.Read, FileShare.Read);
            }
            catch (Exception ex)
            {
                // this happens mostly when the user leaves the Word file open
                statusInfo.WriteLine(ex.Message);
                return(false);
            }

            ZipPackagePart documentPart = (ZipPackagePart)pkg.GetPart(new Uri("/word/document.xml", UriKind.Relative));

            //numbering part may not exist for simple documents
            ZipPackagePart numberingPart = null;

            if (pkg.PartExists(new Uri("/word/numbering.xml", UriKind.Relative)))
            {
                numberingPart = (ZipPackagePart)pkg.GetPart(new Uri("/word/numbering.xml", UriKind.Relative));
            }

            Numbering numbering = new Numbering(numberingPart);
            Imaging   imaging   = new Imaging(documentPart, inputDocxPath, outputTexPath);

            using (Stream documentXmlStream = documentPart.GetStream())
            {
                Engine engine = new Engine(documentXmlStream, numbering, imaging, statusInfo);
                statusInfo.WriteLine("Document opened.        ");

                string outputString = engine.Process();
                string latexSource  = ReplaceSomeCharacters(outputString);

                Encoding encoding = Encoding.Default;

                var enc = docx2tex.Library.Data.InputEnc.Instance.CurrentEncoding;
                if (enc != null)
                {
                    encoding = Encoding.GetEncoding(enc.DotNetEncoding);
                }

                byte[] data = encoding.GetBytes(latexSource);
                using (FileStream fs = new FileStream(outputTexPath, FileMode.Create, FileAccess.Write))
                {
                    using (BinaryWriter outputTexStream = new BinaryWriter(fs))
                    {
                        outputTexStream.Write(data);
                    }
                }
            }
            pkg.Close();
            return(true);
        }