public bool OpenFile(string filePath, bool addToJumpList = true) { if (string.IsNullOrEmpty(filePath) || !File.Exists(filePath)) { OpenFileDialog ofd = new OpenFileDialog(); if (!string.IsNullOrEmpty(filePath)) { if (!Directory.Exists(filePath)) { filePath = Path.GetDirectoryName(filePath); } if (Directory.Exists(filePath)) { ofd.InitialDirectory = filePath; } } ofd.Filter = "All C1 Documents (*.c1dx;*.c1d;*.c1mdx)|*.c1dx;*.c1d;*.c1mdx|C1 Open XML Documents (*.c1dx)|*.c1dx|C1 Documents (*.c1d)|*.c1d|C1 Open XML Multi Documents (*.c1mdx)|*.c1mdx|All Files (*.*)|*.*"; bool res = ofd.ShowDialog() == DialogResult.OK; filePath = ofd.FileName; ofd.Dispose(); if (!res) { return(false); } } object doc = null; string ext = Path.GetExtension(filePath).ToLowerInvariant(); try { if (ext == ".c1dx") { var dx = new C1PrintDocument(); dx.Load(filePath, C1DocumentFormatEnum.C1dx); doc = dx; } else if (ext == ".c1d") { var dx = new C1PrintDocument(); dx.Load(filePath, C1DocumentFormatEnum.C1d); doc = dx; } else if (ext == ".c1mdx") { var mdx = new C1MultiDocument(); mdx.Load(filePath); doc = mdx; } else { MessageBox.Show("Can't open file with \"" + ext + "\" extension.", "C1PrintDocument Viewer", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } } catch (Exception ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } if (doc != null) { Text = Path.GetFileNameWithoutExtension(filePath); rpc.Document = doc; if (addToJumpList && CheckFileRegistration()) { atb.JumpList.AddToRecentCategory(filePath); } return(true); } return(false); }
/// <summary> /// Fills a C1MultiDocument or a C1PrintDocument passed to it with listings of /// all files in the specified directory and its subdirectories matching the specified mask. /// </summary> /// <param name="theDoc">A C1MultiDocument or a C1PrintDocument.</param> /// <param name="dir">Directory containing the files to list.</param> /// <param name="mask">The files mask (e.g. "*.cs").</param> /// <param name="pf">Progress form.</param> public void MakeMultiDocument(object theDoc, string dir, string mask, ProgressForm pf) { // Find out what kind of document we're creating: _mdoc = theDoc as C1MultiDocument; _sdoc = theDoc as C1PrintDocument; if (_mdoc == null && _sdoc == null) { throw new Exception("Unsupported document type."); } _dir = Path.GetFullPath(dir); _mask = mask; // Set up the document: if (_mdoc != null) { _mdoc.Clear(); _mdoc.DoEvents = true; } else { _sdoc.Clear(); _sdoc.DoEvents = true; SetupDoc(_sdoc); } // Show initial progress: pf.SetProgress(string.Format("Reading {0}...", _dir), 0); // For progress indicator only - get the list of all subdirectories: // long allDirsCount = Directory.GetDirectories(_dir, "*", SearchOption.AllDirectories).LongLength; // long allDirsIdx = 0; // Create TOC render object that will list directories and files: RenderToc toc = new RenderToc(); // Add a TOC directory entry in "directory added" event: DirAdded += (doc, dirRo, dirName, level) => { C1Anchor aDir = new C1Anchor(string.Format("d{0}", dirName.GetHashCode())); dirRo.Anchors.Add(aDir); // Add a TOC item for the directory (full name for root dir, own name for subdirs): string tocName = dirName == _dir ? dirName : Path.GetFileName(dirName); RenderTocItem rti = toc.AddItem(tocName, new C1Hyperlink(new C1LinkTargetAnchor(aDir.Name)), level); // Bold directory TOC entries: rti.Style.FontBold = true; // duplicate TOC entry in OUTLINE: OutlineNode outlineNode = new OutlineNode(dirName, dirRo); doc.Outlines.Add(outlineNode); // in this mode, all directory nodes are top-level // update overall progress: pf.SetProgress(string.Format("Reading {0}...", dirName)); if (pf.Cancelled) { throw new Exception("Directory scan aborted."); } return(outlineNode); }; // Add a TOC file entry in "file added" event: FileAdded += (doc, dirOutlineNode, fileRo, fileName, level) => { C1Anchor aFile = new C1Anchor(string.Format("f{0}", fileName.GetHashCode())); fileRo.Anchors.Add(aFile); string tocItemName = Path.GetFileName(fileName); toc.AddItem(tocItemName, new C1Hyperlink(new C1LinkTargetAnchor(aFile.Name)), level); // duplicate TOC entry in OUTLINE: if (dirOutlineNode == null) { doc.Outlines.Add(tocItemName, fileRo); // top-level entry } else { dirOutlineNode.Children.Add(tocItemName, fileRo); // nested entry } }; // Create the common index: RenderIndex index = new RenderIndex(); // Init keywords: Dictionary <string, IndexEntry> indexEntries = new Dictionary <string, IndexEntry>(); // For this sample, we will build an index of all KnownColor names: string[] colorNames = Enum.GetNames(typeof(KnownColor)); foreach (string keyword in colorNames) { indexEntries.Add(keyword, new IndexEntry(keyword)); } // Add an index entry for each key word in line: LineAdded += (lineRo, fileName, line, lineNo) => { var words = line.Split(s_delims, StringSplitOptions.RemoveEmptyEntries); C1Anchor a = null; foreach (string word in words) { if (indexEntries.ContainsKey(word)) { if (a == null) { a = new C1Anchor(string.Format("k{0}{1}", fileName.GetHashCode(), lineNo)); lineRo.Anchors.Add(a); } indexEntries[word].Occurrences.Add(new IndexEntryOccurrence(new C1LinkTargetAnchor(a.Name))); } } }; // Add listings of files to the document: ListDir(_dir, 1); // insert TOC at the top: RenderText tocHeader = new RenderText("Table of Contents"); tocHeader.Style.Spacing.Bottom = "3mm"; tocHeader.Style.TextAlignHorz = AlignHorzEnum.Center; tocHeader.Style.FontSize = 12; tocHeader.Style.FontBold = true; if (_mdoc != null) { C1PrintDocument docToc = new C1PrintDocument(); docToc.Body.Children.Add(tocHeader); docToc.Body.Children.Add(toc); _mdoc.Items.Insert(0, docToc); docToc = null; GC.Collect(); GC.WaitForPendingFinalizers(); } else { toc.BreakAfter = BreakEnum.Page; _sdoc.Body.Children.Insert(0, toc); _sdoc.Body.Children.Insert(0, tocHeader); } // insert word index at the bottom: RenderText indexHeader = new RenderText("Index of Known Colors"); indexHeader.Style.Spacing.Bottom = "3mm"; indexHeader.Style.TextAlignHorz = AlignHorzEnum.Center; indexHeader.Style.FontSize = 12; indexHeader.Style.FontBold = true; index.HeadingStyle.FontSize += 1; index.HeadingStyle.FontBold = true; index.HeadingStyle.Padding.All = "2mm"; // add index entries that have some occurrences: foreach (IndexEntry ie in indexEntries.Values) { if (ie.HasOccurrences) { index.Entries.Add(ie); } } if (_mdoc != null) { C1PrintDocument docIndex = new C1PrintDocument(); docIndex.Body.Children.Add(indexHeader); docIndex.Body.Children.Add(index); _mdoc.Items.Add(docIndex); docIndex = null; GC.Collect(); GC.WaitForPendingFinalizers(); } else { indexHeader.BreakBefore = BreakEnum.Page; _sdoc.Body.Children.Add(indexHeader); _sdoc.Body.Children.Add(index); } // we're done! pf.SetProgress(string.Format("Reading {0}...", _dir), 1); }
private void MultiDocument_Click(object sender, EventArgs e) { C1MultiDocument mdoc = new C1MultiDocument(); // make sure we can cancel the generation: mdoc.DoEvents = true; // Attach a handler to ItemGenerated to make sure all memory used by a multi-doc // item's generation is released when that generation is complete: mdoc.ItemGenerated += (ss, ee) => { GC.Collect(); GC.WaitForPendingFinalizers(); }; this._pview.Document = mdoc; ProgressForm pf = new ProgressForm(); pf.Show(); // Build the multi-document: try { FilesPrinter fp = new FilesPrinter(); fp.MakeMultiDocument(mdoc, tbDir.Text, tbMask.Text, pf); } catch (Exception ex) { MessageBox.Show(ex.Message); } // Reset cancel state of the progress form: pf.DialogResult = System.Windows.Forms.DialogResult.None; // Set up event handlers for better UI: this.FormClosing += (ss, ee) => { if (mdoc.BusyState == BusyStateEnum.Generating) { mdoc.Cancel = true; } }; mdoc.LongOperation += (ss, ee) => { pf.SetProgress(string.Format("Generating C1MultiDocument, {0:P} complete...", ee.Complete), (float)ee.Complete); if (pf.Cancelled && mdoc.BusyState == BusyStateEnum.Generating) { mdoc.Cancel = true; } }; mdoc.DocumentEnded += (ss, ee) => { pf.Hide(); this.Activate(); }; // Generate the multi-document: mdoc.Generate(); }