Example #1
0
 /// <summary>
 /// Creates a PDF of teh MS Publisher document
 /// </summary>
 /// <param name="pdfFilePath">The path where you will save the PDF file</param>
 /// <param name="pbDoc">The path to the MS Publisher document that you will export to PDF</param>
 private void ExportToPDF(string pdfFilePath, Microsoft.Office.Interop.Publisher.Document pbDoc)
 {
     pbDoc.ExportAsFixedFormat(
         PbFixedFormatType.pbFixedFormatTypePDF,
         pdfFilePath, PbFixedFormatIntent.pbIntentPrinting,
         true, 300, 450, 1200, 1800, 1, pbDoc.Pages.Count, 1,
         false, PbPrintStyle.pbPrintStyleDefault,
         false, false, false, System.Reflection.Missing.Value);
 }
Example #2
0
        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);
        }