public PdfStreamWindow(Tokeniser tokeniser, StringBuilder sb, ObjectId objectId, PdfSourceRichTextBox pdfSourceRichTextBox) { this.tokeniser = tokeniser; this.sb = sb; ObjectId = objectId; this.pdfSourceRichTextBox = pdfSourceRichTextBox; Owner = pdfSourceRichTextBox.MainWindow; Width = Owner.ActualWidth / 2; Height = Owner.ActualHeight * 0.9; //var location = Owner.PointToScreen(new Point(Owner.Width*0, Owner.Height*0)); Left = Owner.Left + Owner.ActualWidth * .49; Top = Owner.Top + Owner.ActualHeight * 0.05; InitializeComponent(); CharRadioButton.Click += charRadioButton_Click; HexRadioButton.Click += hexRadioButton_Click; FindButton.Click += findButton_Click; Closed += pdfStreamWindow_Closed; displayContent(); Show(); }
private void navigate(bool isNext) { pdfRefRunTrace.Clear(); BackStatusBarItem.Visibility = Visibility.Collapsed; //check if user has changed file or directory if (FileTextBox.Text != "" && fileString != FileTextBox.Text) { var fileInfo = new FileInfo(FileTextBox.Text); if (!fileInfo.Exists) { MessageBox.Show($"Could not find file '{FileTextBox.Text}'.", "Pdf file not found"); return; } fileString = FileTextBox.Text; directoryString = fileInfo.DirectoryName; DirectoryTextBox.Text = directoryString; files.Clear(); dirs.Clear(); allFiles.Clear(); currentFileIndex = 0; dirs.Push(new DirectoryInfo(directoryString)); isShowStartFile = true; } else if (DirectoryTextBox.Text != "" && directoryString != DirectoryTextBox.Text) { var directoryInfo = new DirectoryInfo(DirectoryTextBox.Text); if (!directoryInfo.Exists) { MessageBox.Show($"Could not find directory '{DirectoryTextBox.Text}'.", "Directory not found"); return; } directoryString = DirectoryTextBox.Text; files.Clear(); dirs.Clear(); allFiles.Clear(); currentFileIndex = 0; dirs.Push(directoryInfo); } var haveAllFilesBeenFound = false; if (currentFileIndex < allFiles.Count - 1 || (isNext == false) || (files.Count == 0 && dirs.Count == 0)) { //show already read files if (isNext) { currentFileIndex++; if (currentFileIndex >= allFiles.Count) { currentFileIndex = 0; } } else { currentFileIndex--; if (currentFileIndex < 0) { currentFileIndex = allFiles.Count - 1; } } } else { while (files.Count == 0 && !haveAllFilesBeenFound) { if (dirs.Count == 0) { if (allFiles.Count == 0) { MessageBox.Show($"There are no pdf files in '{directoryString}' and its subdirectories.", "No pdf file found"); return; } else { currentFileIndex = 0; haveAllFilesBeenFound = true; } } else { //read next directory var dir = dirs.Pop(); foreach (var subDir in dir.GetDirectories().OrderByDescending(d => d.Name)) { dirs.Push(subDir); } foreach (var subfile in dir.GetFiles("*.pdf")) { if (isShowStartFile) { if (subfile.FullName == fileString) { isShowStartFile = false; files.Enqueue(subfile); } else { allFiles.Add(subfile); } } else { files.Enqueue(subfile); } } } } if (!haveAllFilesBeenFound) { currentFileIndex = allFiles.Count; allFiles.Add(files.Dequeue()); } } var file = allFiles[currentFileIndex]; FileTextBox.Text = file.FullName; fileString = FileTextBox.Text; PagesTabControl.Items.Clear(); PdfParser pdfParser; try { pdfParser = new PdfParser(file.FullName, "|", streamBuffer, stringBuilder); } catch (Exception ex) { var pageTabItem = new TabItem { Header = "E_xception" }; var bytes = ""; if (ex is PdfException pdfException) { bytes = Environment.NewLine + Environment.NewLine + pdfException.Tokeniser.ShowBufferContent(); } var textBox = new TextBox { Text = ex.ToDetailString() + bytes, VerticalScrollBarVisibility = ScrollBarVisibility.Auto, IsReadOnly = true }; pageTabItem.Content = textBox; PagesTabControl.Items.Add(pageTabItem); PagesTabControl.SelectedIndex = 0; return; } try { PdfWebBrowser.Visibility = Visibility.Visible; PdfTextBox.Visibility = Visibility.Collapsed; var fileUri = new Uri(new Uri("file://"), file.FullName); PdfWebBrowser.Navigate(fileUri); //if (PdfWebBrowser.Source?.AbsolutePath!=file.FullName) { // //couldn't find file // PdfWebBrowser.Navigate(new Uri("about:blank")); //} } catch (Exception ex) { PdfWebBrowser.Visibility = Visibility.Collapsed; PdfTextBox.Visibility = Visibility.Visible; PdfTextBox.Text = ex.ToDetailString(); } var pageIndex = 0; foreach (var page in pdfParser.Pages) { //todo: How to deal with pdf documents having more than 20 pages ? if (pageIndex > 20) { break; } var hasException = false; var underline = ""; if (pageIndex < 10) { underline = "_"; } var pageTabItem = new TabItem { Header = underline + pageIndex++ }; stringBuilder.Clear(); var isFirstContent = true; foreach (var content in page.Contents) { if (isFirstContent) { isFirstContent = false; } else { stringBuilder.AppendLine(new string('-', 80)); } stringBuilder.AppendLine(content.Text); if (content.Exception != null) { hasException = true; stringBuilder.AppendLine(new string('+', 80)); stringBuilder.AppendLine(content.Exception); stringBuilder.AppendLine(new string('+', 80)); } if (content.Error != null) { hasException = true; stringBuilder.AppendLine(new string('+', 80)); stringBuilder.AppendLine(content.Error); stringBuilder.AppendLine(new string('+', 80)); } } if (page.Exception != null) { hasException = true; stringBuilder.AppendLine(new string('+', 80)); stringBuilder.AppendLine(page.Exception); stringBuilder.AppendLine(new string('+', 80)); } var textBox = new TextBox { Text = stringBuilder.ToString(), VerticalScrollBarVisibility = ScrollBarVisibility.Auto, HorizontalScrollBarVisibility = ScrollBarVisibility.Auto, IsReadOnly = true }; if (hasException) { pageTabItem.Background = Brushes.Khaki; } pageTabItem.Content = textBox; PagesTabControl.Items.Add(pageTabItem); } var infoTabItem = new TabItem { Header = "_Info" }; var tokeniser = pdfParser.Tokeniser; var infotext = "PDF Version: " + tokeniser.PdfVersion; if (tokeniser.DocumentInfo != null) { infotext += Environment.NewLine + Environment.NewLine + "Document Info: " + tokeniser.DocumentInfo; } if (tokeniser.DocumentID != null) { infotext += Environment.NewLine + Environment.NewLine + "Document ID: " + tokeniser.DocumentID; } infotext += Environment.NewLine + Environment.NewLine + "Pages: " + tokeniser.Pages.Count; infotext += Environment.NewLine + Environment.NewLine + "Fonts: "; foreach (var objectId_Token in tokeniser.Tokens) { if (objectId_Token.Value is DictionaryToken objectDictionaryToken) { if (objectDictionaryToken.Type == "Font") { var pdfFont = (PdfFont)objectDictionaryToken.PdfObject !; infotext += Environment.NewLine + Environment.NewLine + pdfFont.Name + objectDictionaryToken.ToString(); if (pdfFont.ToUnicodeHeader != null) { infotext += Environment.NewLine + "ToUnicodeHeader: " + pdfFont.ToUnicodeHeader; } if (pdfFont.CMap != null) { foreach (var code_char in pdfFont.CMap) { infotext += Environment.NewLine + $"{code_char.Key}: '{code_char.Value}'"; } } if (pdfFont.Exception != null) { infotext += Environment.NewLine + new string('+', 80); infotext += Environment.NewLine + pdfFont.Exception; infotext += Environment.NewLine + new string('+', 80); infoTabItem.Background = Brushes.Khaki; } infotext += Environment.NewLine; } } } if (tokeniser.Metadata != null) { infotext += Environment.NewLine + Environment.NewLine + "Meta data: " + tokeniser.Metadata; } var textBoxInfo = new TextBox { Text = infotext, VerticalScrollBarVisibility = ScrollBarVisibility.Auto, HorizontalScrollBarVisibility = ScrollBarVisibility.Auto, IsReadOnly = true }; infoTabItem.Content = textBoxInfo; PagesTabControl.Items.Add(infoTabItem); var bytesTabItem = new TabItem { Header = "_Bytes" }; //var bytesContextMenu = new ContextMenu(); //bytesContextMenu.Items.Add(new MenuItem { Command = System.Windows.Input.ApplicationCommands.SelectAll}); //bytesContextMenu.Items.Add(new MenuItem { Command = System.Windows.Input.ApplicationCommands.Copy}); //bytesContextMenu.Items.Add(new MenuItem { Command = System.Windows.Input.ApplicationCommands.Cut }); //bytesContextMenu.Items.Add(new MenuItem { Command = System.Windows.Input.ApplicationCommands.Paste }); //bytesContextMenu.Items.Add(new MenuItem { Command = System.Windows.Input.ApplicationCommands.Undo }); //bytesContextMenu.Items.Add(new MenuItem { Command = System.Windows.Input.ApplicationCommands.Redo }); ///* //(int)Shortcut.CtrlS, Show Stream //(int)Shortcut.CtrlA, Select all //(int)Shortcut.CtrlC, Copy //(int)Shortcut.CtrlX, Cut //(int)Shortcut.CtrlV, Paste //Shortcut.CtrlZ, Undo //(int)Shortcut.CtrlY, Redo //*/ //bytesTextBox = new TextBox { // Text = pdfParser.Tokeniser.ShowBufferContent(), // VerticalScrollBarVisibility = ScrollBarVisibility.Auto, // HorizontalScrollBarVisibility = ScrollBarVisibility.Auto, // ContextMenu = bytesContextMenu, // IsReadOnly = true // }; var pdfSourceRichTextBox = new PdfSourceRichTextBox(pdfParser.Tokeniser, stringBuilder, this); bytesTabItem.Content = pdfSourceRichTextBox; PagesTabControl.Items.Add(bytesTabItem); PagesTabControl.SelectedIndex = 0; }