public AnnotationForm() { InitializeComponent(); // screen size Size screenSize = new Size(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height); // get screen cap Bitmap bmp = new Bitmap(screenSize.Width, screenSize.Height); Graphics g = Graphics.FromImage(bmp); g.CopyFromScreen(0, 0, 0, 0, screenSize); g.Dispose(); // create dv & de dv = new WFViewer(wfViewerControl1); dv.Preview = true; dv.EditFigures = true; dv.AntiAlias = true; de = new DEngine(null); de.AddedFigure += new AddedFigureHandler(de_AddedFigure); de.AddViewer(dv); WorkBookUtils.SetupDEngine(de); // setup undo/redo sensitive stuff de.UndoRedo.Start("initial setup"); de.PageSize = new DPoint(screenSize.Width, screenSize.Height); BackgroundFigure bf = new BackgroundFigure(); // background figure bf.ImageData = WFHelper.ToImageData(bmp); bf.FileName = "screen_capture.png"; bf.BitmapPosition = DBitmapPosition.Normal; de.SetBackgroundFigure(bf, true); de.UndoRedo.Commit(); de.UndoRedo.ClearHistory(); // set form to full screen Location = new Point(0, 0); Size = screenSize; }
bool OpenFile(string fileName) { bool result = false; CheckState(); // create progress form ProgressForm pf = new ProgressForm(); pf.Text = WbLocale.OpeningFile; pf.Shown += delegate(object s, EventArgs e) { Application.DoEvents(); // start undo/redo undoRedoArea.Start(WbLocale.OpenDocument); try { // load new engines ClearDocument(); DPoint pageSize; BackgroundFigure bf; Dictionary<string, byte[]> extraEntries; List<DEngine> engines = FileHelper.Load(fileName, undoRedoArea, out pageSize, out bf, new string[] { AttachmentView.ATTACHMENTS_DIR }, out extraEntries); if (pageSize != null) NewPageSize = pageSize; else NewPageSize = PageTools.FormatToSize(PageFormat.Default); if (bf != null) BackgroundFigure = bf; else BackgroundFigure = new BackgroundFigure(); // init new dengines foreach (DEngine newDe in engines) { InitDEngine(newDe, false); this.engines.Add(newDe); Application.DoEvents(); // update progress form } // set attachments attachmentView1.ClearAttachments(); if (extraEntries != null) foreach (string name in extraEntries.Keys) if (name.IndexOf(AttachmentView.ATTACHMENTS_DIR) == 0) attachmentView1.AddAttachment(name, extraEntries[name]); // commit undo/redo undoRedoArea.Commit(); Dirty = false; // update vars this.fileName = fileName; beenSaved = true; needSave = false; UpdateTitleBar(); // show first page ShowFirstPage(); // add to recent documents AddRecentDocument(fileName); // start autosaving StartAutoSaveTimer(); // result true! result = true; } catch (Exception e2) { undoRedoArea.Cancel(); MessageBox.Show(e2.Message, WbLocale.ErrorReadingFile, MessageBoxButtons.OK, MessageBoxIcon.Error); } pf.Close(); }; pf.ShowDialog(); return result; }
void ClearDocument() { engines.Clear(); BackgroundFigure = new BackgroundFigure(); }
public void SetBackgroundFigure(BackgroundFigure f, bool isCustom) { f.Rect = new DRect(0, 0, PageSize.X, PageSize.Y); figureHandler.BackgroundFigure = f; viewerHandler.Update(); figureHandler.CustomBackgroundFigure = isCustom; }
public static List<DEngine> Load(string fileName, UndoRedoArea undoRedoArea, out DPoint pageSize, out BackgroundFigure bf, string[] extraEntryDirs, out Dictionary<string, byte[]> extraEntries) { System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding(); pageSize = null; bf = null; extraEntries = null; List<DEngine> res = new List<DEngine>(); // load zipfile using (ZipFile zf = new ZipFile(fileName)) { // find pages ini file entry byte[] data = Read(zf, PAGES_INI); if (data != null) { // read general background figure byte[] genBkgndFigureData = Read(zf, GENBKGNDFIGURE); if (genBkgndFigureData != null) { List<Figure> figs = FigureSerialize.FromXml(encoding.GetString(genBkgndFigureData)); if (figs.Count == 1 && figs[0] is BackgroundFigure) { LoadImage(zf, figs[0]); bf = (BackgroundFigure)figs[0]; // read page size from background figure if (bf.Width > 0 && bf.Height > 0) pageSize = new DPoint(bf.Width, bf.Height); } } // create Nini config source from pages ini entry stream IniConfigSource source = new IniConfigSource(new MemoryStream(data)); // load each page info mentioned in ini entry foreach (IConfig config in source.Configs) { // create new DEngine for page DEngine de = new DEngine(undoRedoArea); // set page size if (config.Contains(PAGESIZE)) de.PageSize = DPoint.FromString(config.Get(PAGESIZE)); if (config.Contains(PAGENAME)) de.PageName = config.Get(PAGENAME); // set the figures if (config.Contains(FIGURELIST)) { string figureListEntryName = config.Get(FIGURELIST); data = Read(zf, figureListEntryName); if (data != null) { List<Figure> figs = FigureSerialize.FromXml(encoding.GetString(data)); foreach (Figure f in figs) { de.AddFigure(f); LoadImage(zf, f); } } } // set the background figure if (config.Contains(BACKGROUNDFIGURE)) { string backgroundFigureEntryName = config.Get(BACKGROUNDFIGURE); data = Read(zf, backgroundFigureEntryName); if (data != null) { List<Figure> figs = FigureSerialize.FromXml(encoding.GetString(data)); if (figs.Count == 1 && figs[0] is BackgroundFigure) { LoadImage(zf, figs[0]); de.SetBackgroundFigure((BackgroundFigure)figs[0], true); } } } else if (bf != null) de.SetBackgroundFigure(bf, false); // add to list of DEngines res.Add(de); } // read extra entries if (extraEntryDirs != null) { extraEntries = new Dictionary<string, byte[]>(); foreach (string dir in extraEntryDirs) { IEnumerator en = zf.GetEnumerator(); en.Reset(); while (en.MoveNext()) { ZipEntry entry = (ZipEntry)en.Current; if (entry.Name.IndexOf(dir) == 0) extraEntries.Add(entry.Name, Read(zf, entry.Name)); } } } } } return res; }
public static void Save(string fileName, IList<DEngine> engines, DPoint pageSize, BackgroundFigure bf, Dictionary<string, byte[]> extraEntries) { System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding(); using (ZipOutputStream zipOut = new ZipOutputStream(File.Create(fileName))) { IniConfigSource source = new IniConfigSource(); // write each page int i = 0; Dictionary<string, byte[]> images = new Dictionary<string, byte[]>(); foreach (DEngine de in engines) { IConfig config = source.AddConfig(string.Format("page{0}", i)); config.Set(PAGESIZE, DPoint.FormatToString(de.PageSize)); if (de.PageName != null) config.Set(PAGENAME, de.PageName); string figureListName = string.Format("figureList{0}.xml", i); byte[] data = encoding.GetBytes(FigureSerialize.FormatToXml(de.Figures, images)); config.Set(FIGURELIST, figureListName); Write(zipOut, figureListName, data); if (de.CustomBackgroundFigure) { string backgroundFigureName = string.Format("backgroundFigure{0}.xml", i); config.Set(BACKGROUNDFIGURE, backgroundFigureName); data = encoding.GetBytes(FigureSerialize.FormatToXml(de.BackgroundFigure, images)); Write(zipOut, backgroundFigureName, data); } i += 1; } // write background figure if (bf != null) { // store page size to background figure if (pageSize != null) { bf.Width = pageSize.X; bf.Height = pageSize.Y; } else { DPoint sz = PageTools.FormatToSize(PageFormat.Default); bf.Width = sz.X; bf.Height = sz.Y; } byte[] data = encoding.GetBytes(FigureSerialize.FormatToXml(bf, images)); Write(zipOut, GENBKGNDFIGURE, data); } // write images foreach (KeyValuePair<string, byte[]> kvp in images) if (kvp.Key != null && kvp.Key.Length > 0) Write(zipOut, IMAGES_DIR + Path.DirectorySeparatorChar + Path.GetFileName(kvp.Key), kvp.Value); // write extra entries if (extraEntries != null) foreach (string name in extraEntries.Keys) Write(zipOut, name, extraEntries[name]); // write pages ini Write(zipOut, PAGES_INI, encoding.GetBytes(source.ToString())); } }
public void AddPageToEngine(XmlDocument page, DEngine de) { // svg namespace XmlNamespaceManager nsmgr = new XmlNamespaceManager(page.NameTable); nsmgr.AddNamespace("svg", svgNs); bool hasSvgNs = HasSvgNamespace(page); // set de page size int width; int height; int.TryParse(page.DocumentElement.Attributes.GetNamedItem("width").Value, out width); int.TryParse(page.DocumentElement.Attributes.GetNamedItem("height").Value, out height); if (width == 0 || height == 0) de.PageSize = new DPoint(800, 600); else de.PageSize = new DPoint(width, height); // node list XmlNodeList nl; // find page name if (hasSvgNs) nl = page.SelectNodes("/svg:svg/svg:title", nsmgr); else nl = page.SelectNodes("/svg/title"); if (nl.Count == 1 && nl[0].InnerText != null) de.PageName = nl[0].InnerText; // find background if (hasSvgNs) nl = page.SelectNodes("/svg:svg/svg:rect", nsmgr); else nl = page.SelectNodes("/svg/rect"); if (nl.Count == 1) { SvgElement rect = SvgFactory.LoadFromXML(page, (XmlElement)nl[0]); Figure f = SvgElementToFigure(rect); if (f is RectFigure) { BackgroundFigure bf = new BackgroundFigure(); bf.Fill = ((RectFigure)f).Fill; de.SetBackgroundFigure(bf, true); } } // try to find foreground group if (hasSvgNs) nl = page.SelectNodes("/svg:svg/svg:g[@class='foreground']", nsmgr); else nl = page.SelectNodes("/svg/g[@class='foreground']"); // convert svg elements to figures and add them to DEngine if (nl.Count == 1) { SvgElement root = SvgFactory.LoadFromXML(page, (XmlElement)nl[0]); foreach (SvgElement e in root.Children) { Figure f = SvgElementToFigure(e); if (f != null) de.AddFigure(f); } } }