private Structures.Core.ExportPackage ReadExportPackage(string xmlFilePath) { XmlSerializer serializer = null; FileStream stream = null; try { serializer = new XmlSerializer(typeof(Structures.Core.ExportPackage)); stream = new FileStream(xmlFilePath, FileMode.Open); Structures.Core.ExportPackage ep = (Structures.Core.ExportPackage)serializer.Deserialize(stream); return(ep); } catch (Exception ex) { throw ex; } finally { if (stream != null) { stream.Close(); } } }
internal bool Import(string xmlFilePath, string pubFilePath, string imagePath, SettingsCore settings) { bool success = false; pub2xml.api.Processor.ProcessingErrors = new List <Structures.Core.TextFrame>(); pub2xml.api.Processor.MaximumShapes = 0; pub2xml.api.Processor.CurrentIndex = 0; pub2xml.api.Processor.FramesDictoinary = null; string fileName = pubFilePath; if (settings.ImportCreateBakFile) { string fileNameBak = pubFilePath + Constants.pub2xmlBakExtension; if (_onChange_Progress != null) { _onChange_Progress(pub2xml.api.Processor.MaximumShapes, pub2xml.api.Processor.MaximumShapes, StringResources.CreatingBackupFile); } if (File.Exists(fileNameBak)) { File.Delete(fileNameBak); } File.Copy(fileName, fileNameBak, true); } if (_onChange_Progress != null) { _onChange_Progress(pub2xml.api.Processor.MaximumShapes, pub2xml.api.Processor.MaximumShapes, StringResources.LoadingXmlFile); } Structures.Core.ExportPackage ep = ReadExportPackage(xmlFilePath); pub2xml.api.Processor.FramesDictoinary = new Dictionary <long, List <Structures.Core.TextFrame> >(); foreach (Structures.Core.TextFrame tf in ep.textFrames) { if (pub2xml.api.Processor.FramesDictoinary.ContainsKey(tf.shapeId)) { pub2xml.api.Processor.FramesDictoinary[tf.shapeId].Add(tf); } else { pub2xml.api.Processor.FramesDictoinary.Add(tf.shapeId, new List <Structures.Core.TextFrame> { tf }); } } Microsoft.Office.Interop.Publisher.Application pbApp = new Microsoft.Office.Interop.Publisher.Application(); pbApp.Options.AutoHyphenate = false; try { if (_onChange_Progress != null) { _onChange_Progress(pub2xml.api.Processor.MaximumShapes, pub2xml.api.Processor.MaximumShapes, StringResources.LoadingPublisherFile); } Microsoft.Office.Interop.Publisher.Document pbDoc = pbApp.Open(pubFilePath, false, false, PbSaveOptions.pbDoNotSaveChanges); #region | get total shapes count | foreach (Microsoft.Office.Interop.Publisher.Page page in pbDoc.MasterPages) { foreach (Microsoft.Office.Interop.Publisher.Shape shape in page.Shapes) { pub2xml.api.Processor.MaximumShapes++; } } foreach (Microsoft.Office.Interop.Publisher.Page page in pbDoc.Pages) { foreach (Microsoft.Office.Interop.Publisher.Shape shape in page.Shapes) { pub2xml.api.Processor.MaximumShapes++; } } #endregion try { #region | MasterPages | foreach (Microsoft.Office.Interop.Publisher.Page page in pbDoc.MasterPages) { foreach (Microsoft.Office.Interop.Publisher.Shape shape in page.Shapes) { pub2xml.api.Processor.CurrentIndex++; if (_onChange_Progress != null) { _onChange_Progress(pub2xml.api.Processor.MaximumShapes, pub2xml.api.Processor.CurrentIndex, StringResources.BuildingShape + ": " + shape.ID + ", " + shape.Name + "..."); } try { List <Structures.Core.TextFrame> tfs = setTextFramesFromShape(shape, imagePath, settings); if (tfs.Count > 0) { ep.textFrames.AddRange(tfs); } } catch (Exception ex) { throw ex; } } } #endregion #region | Pages | foreach (Microsoft.Office.Interop.Publisher.Page page in pbDoc.Pages) { foreach (Microsoft.Office.Interop.Publisher.Shape shape in page.Shapes) { pub2xml.api.Processor.CurrentIndex++; if (_onChange_Progress != null) { _onChange_Progress(pub2xml.api.Processor.MaximumShapes, pub2xml.api.Processor.CurrentIndex, StringResources.BuildingShape + ": " + shape.ID + ", " + shape.Name + "..."); } try { List <Structures.Core.TextFrame> tfs = setTextFramesFromShape(shape, imagePath, settings); if (tfs.Count > 0) { ep.textFrames.AddRange(tfs); } } catch (Exception ex) { throw ex; } if (pub2xml.api.Processor.CurrentIndex % 100 == 0) { if (_onChange_Progress != null) { _onChange_Progress(pub2xml.api.Processor.MaximumShapes, pub2xml.api.Processor.CurrentIndex, StringResources.SavingChanges); } pbDoc.Save(); } } } #endregion if (_onChange_Progress != null) { _onChange_Progress(pub2xml.api.Processor.MaximumShapes, pub2xml.api.Processor.MaximumShapes, StringResources.ApplyingInternalFontStyles); } #region | apply internal font information | Cache.settings.FontFormatTags.ForEach(a => { pbDoc.Find.FindText = "<" + a.name + ">"; pbDoc.Find.ReplaceScope = PbReplaceScope.pbReplaceScopeAll; pbDoc.Find.ReplaceWithText = ""; pbDoc.Find.Execute(); pbDoc.Find.FindText = "</" + a.name + ">"; pbDoc.Find.ReplaceScope = PbReplaceScope.pbReplaceScopeAll; pbDoc.Find.ReplaceWithText = ""; pbDoc.Find.Execute(); }); #endregion success = true; } catch (Exception ex) { throw ex; } finally { if (success) { if (_onChange_Progress != null) { _onChange_Progress(pub2xml.api.Processor.MaximumShapes, pub2xml.api.Processor.MaximumShapes, StringResources.SavingPublisherFile); } pbDoc.Save(); #region | create pdf | if (settings.ImportCreatePdfFile) { if (_onChange_Progress != null) { _onChange_Progress(pub2xml.api.Processor.MaximumShapes, pub2xml.api.Processor.MaximumShapes, StringResources.SavingPDFFile); } pbDoc.ExportAsFixedFormat(PbFixedFormatType.pbFixedFormatTypePDF, pubFilePath + Constants.pub2xmlPDFAfterImportExtension); } #endregion } pbDoc.Close(); pbDoc = null; } } catch (Exception ex) { throw ex; } finally { pbApp = null; GC.Collect(); Process[] Processes = Process.GetProcessesByName("MSPUB"); foreach (Process p in Processes) { if (p.MainWindowTitle.Trim() == "") { p.Kill(); } } } return(success); }
internal ExportPackage export(string pubFilePath, string xmlFilePath, string imagePath, SettingsCore settings) { pub2xml.api.Processor.ProcessingErrors = new List <Structures.Core.TextFrame>(); pub2xml.api.Processor.MaximumShapes = 0; pub2xml.api.Processor.CurrentIndex = 0; pub2xml.api.Processor.FramesDictoinary = null; Structures.Core.ExportPackage ep = new Structures.Core.ExportPackage(); ep.pubFilePath = pubFilePath; ep.xmlFilePath = xmlFilePath; ep.imagePath = imagePath; Application pbApp = new Application(); try { if (_onChange_Progress != null) { _onChange_Progress(pub2xml.api.Processor.MaximumShapes, pub2xml.api.Processor.MaximumShapes, StringResources.LoadingPublisherFile); } Document pbDoc = pbApp.Open(pubFilePath, false, false, PbSaveOptions.pbDoNotSaveChanges); pbDoc.ActiveWindow.Visible = false; object fileName_temp = pubFilePath + Constants.pub2xmlTempPubExtension; pbDoc.SaveAs(fileName_temp, PbFileFormat.pbFilePublication, false); #region | get total shapes count | foreach (Page page in pbDoc.MasterPages) { foreach (Shape shape in page.Shapes) { pub2xml.api.Processor.MaximumShapes++; } } foreach (Page page in pbDoc.Pages) { foreach (Shape shape in page.Shapes) { pub2xml.api.Processor.MaximumShapes++; } } #endregion pub2xml.api.Processor.CurrentIndex = 0; try { #region | MasterPages | foreach (Page page in pbDoc.MasterPages) { foreach (Shape shape in page.Shapes) { pub2xml.api.Processor.CurrentIndex++; if (_onChange_Progress != null) { _onChange_Progress(pub2xml.api.Processor.MaximumShapes, pub2xml.api.Processor.CurrentIndex, StringResources.ParsingShape + ": " + shape.ID + ", " + shape.Name + "..."); } try { List <Structures.Core.TextFrame> tfs = getTextFramesFromShape(shape, settings, ep.imagePath, Path.GetFileName(pubFilePath)); if (tfs.Count > 0) { ep.textFrames.AddRange(tfs); } } catch (Exception ex) { throw ex; } } } #endregion #region | Pages | foreach (Page page in pbDoc.Pages) { foreach (Shape shape in page.Shapes) { pub2xml.api.Processor.CurrentIndex++; if (_onChange_Progress != null) { _onChange_Progress(pub2xml.api.Processor.MaximumShapes, pub2xml.api.Processor.CurrentIndex, StringResources.ParsingShape + ": " + shape.ID + ", " + shape.Name + "..."); } try { List <Structures.Core.TextFrame> tfs = getTextFramesFromShape(shape, settings, ep.imagePath, Path.GetFileName(pubFilePath)); if (tfs.Count > 0) { ep.textFrames.AddRange(tfs); } } catch (Exception ex) { throw ex; } if (pub2xml.api.Processor.CurrentIndex % 10 == 0) { if (_onChange_Progress != null) { _onChange_Progress(pub2xml.api.Processor.MaximumShapes, pub2xml.api.Processor.CurrentIndex, StringResources.SavingChanges); } pbDoc.Save(); } } } #endregion ep.success = true; } catch (Exception ex) { ep.success = false; ep.message = ex.Message; throw ex; } finally { if (ep.success) { if (settings.ExportPseudoTranslateFile) { object fileName_vts = pubFilePath + Constants.pub2xmlPseudoTranslationExtension; pbDoc.SaveAs(fileName_vts, PbFileFormat.pbFilePublication, false); } } pbDoc.Close(); pbDoc = null; } } catch (Exception ex) { ep.success = false; ep.message = ex.Message; throw ex; } finally { if (ep.success) { if (_onChange_Progress != null) { _onChange_Progress(pub2xml.api.Processor.MaximumShapes, pub2xml.api.Processor.MaximumShapes, StringResources.SavingOutputToXmlFile); } saveExportPackage(ep); #region | create pdf | if (settings.ExportCreatePdfFile) { if (_onChange_Progress != null) { _onChange_Progress(pub2xml.api.Processor.MaximumShapes, pub2xml.api.Processor.MaximumShapes, StringResources.LoadingPublisherFile); } Document pbDoc2 = pbApp.Open(pubFilePath, false, false, PbSaveOptions.pbDoNotSaveChanges); try { if (_onChange_Progress != null) { _onChange_Progress(pub2xml.api.Processor.MaximumShapes, pub2xml.api.Processor.MaximumShapes, StringResources.SavingPDFFile); } pbDoc2.ExportAsFixedFormat(PbFixedFormatType.pbFixedFormatTypePDF, pubFilePath + Constants.pub2xmlPDFAfterExportExtension); } finally { pbDoc2.Close(); pbDoc2 = null; } } #endregion #region | remove temp file | try { if (File.Exists(pubFilePath + Constants.pub2xmlTempPubExtension)) { File.Delete(pubFilePath + Constants.pub2xmlTempPubExtension); } } catch (Exception ex) { Trace.WriteLine(ex.Message); } #endregion } pbApp = null; GC.Collect(); Process[] Processes = Process.GetProcessesByName("MSPUB"); foreach (Process p in Processes) { if (p.MainWindowTitle.Trim() == string.Empty) { p.Kill(); } } } return(ep); }