Exemplo n.º 1
0
        public static void Save(Dictionary <int, PdfiumPage> pages, string fileName)
        {
            var name = Path.GetFileNameWithoutExtension(fileName);
            var book = new PdfiumBook();

            book.Name  = name;
            book.Pages = new PdfiumPage[pages.Count];

            int no = 0;

            foreach (var p in pages.Values)
            {
                book.Pages[no++] = PdfiumPage.Encode(p);
            }

            if (File.Exists(fileName))
            {
                File.Delete(fileName);
            }

            using (var file = new StreamWriter(fileName))
            {
                Serializer.Serialize(file, book);
            }
        }
Exemplo n.º 2
0
        private void OnPdfiumPageTranslatedEvent(PdfiumPage p)
        {
            int page = pdfViewer.Renderer.Page;

            if (p.No != page)
            {
                return;
            }

            this.InvokeIfRequired(() =>
            {
                if (!string.Equals(p.SourceText, _sourceText))
                {
                    tbSourceText.TextChanged -= tbSourceText_TextChanged;
                    tbSourceText.Text         = p.SourceText;
                    tbSourceText.TextChanged += tbSourceText_TextChanged;
                    _sourceText = p.SourceText;
                }
                if (!string.Equals(p.TargetText, _targetText))
                {
                    tbTargetText.TextChanged -= tbTargetText_TextChanged;
                    tbTargetText.Text         = p.TargetText;
                    tbTargetText.TextChanged += tbTargetText_TextChanged;

                    var text = p.TargetText;
                    UpdateMarkdown(text);

                    _targetText = p.TargetText;
                }
            });
        }
Exemplo n.º 3
0
        private void TranslatePage(PdfiumPage p)
        {
            if (string.IsNullOrEmpty(p.SourceText))
            {
                p.Translated = true;
                p.TargetText = null;
                return;
            }

            var sourceCollection = SplitText(p.SourceText);
            var t             = new GoogleTranslate();
            var targetBuilder = new StringBuilder();

            int i = 0;

            foreach (var sourceLine in sourceCollection)
            {
                i++;
                if (string.IsNullOrEmpty(sourceLine))
                {
                    targetBuilder.AppendLine();
                    continue;
                }
                var source = sourceLine.Trim();
                if (string.IsNullOrEmpty(source))
                {
                    targetBuilder.AppendLine();
                    continue;
                }

                string[] words = source.Split(tochka);

                var target = new StringBuilder();
                var pos    = 0;
                for (int j = 0; j < words.Length; j++)
                {
                    var word = words[j];
                    var w    = t.Translate(word, "English", "Russian");
                    if (t.Error != null)
                    {
                        MessageBox.Show(t.Error.Message, nameof(GoogleTranslate), MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        break;
                    }
                    target.Append(w);

                    pos += word.Length;
                    if (pos < source.Length)
                    {
                        var e = source[pos];
                        pos++;
                        target.Append(e);
                    }
                }
                targetBuilder.AppendLine(target.ToString());
            }
            p.TargetText = targetBuilder.ToString();
            p.Translated = true;
        }
Exemplo n.º 4
0
        public static PdfiumPage Encode(PdfiumPage p)
        {
            var page = new PdfiumPage();

            page.No         = p.No;
            page.SourceText = Base64Encode(p.SourceText);
            page.Translated = p.Translated;
            page.TargetText = Base64Encode(p.TargetText);
            return(page);
        }
Exemplo n.º 5
0
        private PdfiumPage CreateOrGetPage(int page)
        {
            if (!_pages.ContainsKey(page))
            {
                var p = new PdfiumPage();
                p.No = page;
                var text = GetPDFText(page);
                if (OnlyAscii)
                {
                    text = Regex.Replace(text, @"[^\u0000-\u007F]", "");
                }
                p.SourceText = text;
                _pages[page] = p;

                HasSave = false;
            }
            return(_pages[page]);
        }
Exemplo n.º 6
0
        private void TranslateWorker()
        {
            int        page;
            PdfiumPage p = null;

            while (_queue.TryDequeue(out page))
            {
                HasSave = false;
                PdfiumPageTranslateBegin?.Invoke();

                p = _pages[page];
                TranslatePage(p);

                PdfiumPageEvent?.Invoke(p);
            }

            _translate = null;
            PdfiumPageTranslateEnd?.Invoke();
        }
Exemplo n.º 7
0
        public static Dictionary <int, PdfiumPage> Load(string fileName)
        {
            var pages = new Dictionary <int, PdfiumPage>();

            if (!File.Exists(fileName))
            {
                return(pages);
            }

            PdfiumBook book;

            using (var file = new StreamReader(fileName))
            {
                book = (PdfiumBook)Serializer.Deserialize(file);
            }
            foreach (var p in book.Pages)
            {
                var page = PdfiumPage.Decode(p);
                pages.Add(page.No, page);
            }
            return(pages);
        }