예제 #1
1
        public static new Boolean Convert(String inputFile, String outputFile) {
            Microsoft.Office.Interop.Publisher.Application app;
            String tmpFile = null;
            try {
                app = new Microsoft.Office.Interop.Publisher.Application();
                app.Open(inputFile, false, false, PbSaveOptions.pbDoNotSaveChanges);

                // Try and avoid dialogs about versions
                tmpFile = System.IO.Path.GetTempPath() + Guid.NewGuid().ToString() + ".pub";
                app.ActiveDocument.SaveAs(tmpFile, PbFileFormat.pbFilePublication, false);
                app.ActiveDocument.ExportAsFixedFormat(PbFixedFormatType.pbFixedFormatTypePDF, outputFile, PbFixedFormatIntent.pbIntentStandard, true);
                app.ActiveDocument.Close();
                ((Microsoft.Office.Interop.Publisher._Application)app).Quit();
                return true;
            } catch (Exception e) {
                Console.WriteLine(e.Message);
                return false;
            } finally {
                if (tmpFile != null) {
                    System.IO.File.Delete(tmpFile);
                }
                app = null;
            }
        }
예제 #2
0
        public static new Boolean Convert(String inputFile, String outputFile)
        {
            Microsoft.Office.Interop.Publisher.Application app;
            String tmpFile = null;

            try {
                app = new Microsoft.Office.Interop.Publisher.Application();
                app.Open(inputFile, false, false, PbSaveOptions.pbDoNotSaveChanges);

                // Try and avoid dialogs about versions
                tmpFile = System.IO.Path.GetTempPath() + Guid.NewGuid().ToString() + ".pub";
                app.ActiveDocument.SaveAs(tmpFile, PbFileFormat.pbFilePublication, false);
                app.ActiveDocument.ExportAsFixedFormat(PbFixedFormatType.pbFixedFormatTypePDF, outputFile, PbFixedFormatIntent.pbIntentStandard, true);
                app.ActiveDocument.Close();
                ((Microsoft.Office.Interop.Publisher._Application)app).Quit();
                return(true);
            } catch (Exception e) {
                Console.WriteLine(e.Message);
                return(false);
            } finally {
                if (tmpFile != null)
                {
                    System.IO.File.Delete(tmpFile);
                }
                app = null;
            }
        }
예제 #3
0
        public void Convert(string sourceFilename, string destinationFilename)
        {
            Trace.TraceInformation("Convert Pub file {0} to pdf.", sourceFilename);
            var publisherFile = _appPub.Open(sourceFilename, ReadOnly: true);

            publisherFile.ExportAsFixedFormat(PbFixedFormatType.pbFixedFormatTypePDF, destinationFilename);
            publisherFile.Close();

            Trace.TraceInformation("Convert Pub file {0} complete.", sourceFilename);
        }
예제 #4
0
        public static new int Convert(String inputFile, String outputFile, Hashtable options)
        {
            Boolean running = (Boolean)options["noquit"];

            Microsoft.Office.Interop.Publisher.Application app = null;
            String tmpFile = null;

            try
            {
                try
                {
                    app = (Microsoft.Office.Interop.Publisher.Application)Marshal.GetActiveObject("Publisher.Application");
                }
                catch (System.Exception)
                {
                    app     = new Microsoft.Office.Interop.Publisher.Application();
                    running = false;
                }
                Boolean nowrite = (Boolean)options["readonly"];
                bool    pdfa    = (Boolean)options["pdfa"] ? true : false;
                if ((Boolean)options["hidden"])
                {
                    var activeWin = app.ActiveWindow;
                    activeWin.Visible = false;
                    Converter.ReleaseCOMObject(activeWin);
                }
                app.Open(inputFile, nowrite, false, PbSaveOptions.pbDoNotSaveChanges);
                PbFixedFormatIntent quality = PbFixedFormatIntent.pbIntentStandard;
                if ((Boolean)options["print"])
                {
                    quality = PbFixedFormatIntent.pbIntentPrinting;
                }
                if ((Boolean)options["screen"])
                {
                    quality = PbFixedFormatIntent.pbIntentMinimum;
                }
                Boolean includeProps = !(Boolean)options["excludeprops"];
                Boolean includeTags  = !(Boolean)options["excludetags"];

                // Try and avoid dialogs about versions
                tmpFile = System.IO.Path.GetTempPath() + Guid.NewGuid().ToString() + ".pub";
                var activeDocument = app.ActiveDocument;
                activeDocument.SaveAs(tmpFile, PbFileFormat.pbFilePublication, false);
                activeDocument.ExportAsFixedFormat(PbFixedFormatType.pbFixedFormatTypePDF, outputFile, quality, includeProps, -1, -1, -1, -1, -1, -1, -1, true, PbPrintStyle.pbPrintStyleDefault, includeTags, true, pdfa);
                activeDocument.Close();

                Converter.ReleaseCOMObject(activeDocument);
                return((int)ExitCode.Success);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return((int)ExitCode.UnknownError);
            }
            finally
            {
                if (tmpFile != null)
                {
                    System.IO.File.Delete(tmpFile);
                }
                if (app != null)
                {
                    ((Microsoft.Office.Interop.Publisher._Application)app).Quit();
                }
                Converter.ReleaseCOMObject(app);
            }
        }
예제 #5
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);
        }
예제 #6
0
        public void runInForm()
        {
            ed.addDebug("Exporting " + file + "...");
            String tempDir = getTempDir();
            ed.addDebug("Tempoary Directory: " + tempDir);

            if (this.method == "handouts")
            {
                Package zipPresentation = null;
                if (options.Contains("exportNotes"))
                {
                    String tmpFile = Path.Combine(tempDir, "pres.zip");
                    File.Copy(file, tmpFile);
                    zipPresentation = ZipPackage.Open(new FileStream(tmpFile, FileMode.Open));
                }

                Microsoft.Office.Interop.PowerPoint.Application powerPoint = openPowerPoint();
                ed.addDebug("PowerPoint should have loaded document");
                ed.addDebug("Exporting as handouts...");
                Presentation toExport = powerPoint.Presentations[1];

                ed.addDebug("Opening Publisher Template");
                Microsoft.Office.Interop.Publisher.Application publisher = new Microsoft.Office.Interop.Publisher.Application();

                String pubFile = Path.Combine(tempDir, "out.pub");
                File.Copy(Path.Combine(
                    Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath),
                    "Templates",
                    "Handout-3P.pub" // TODO: Add option for different templates
                ), pubFile);
                Document publisherDoc = publisher.Open( pubFile );
                publisher.ActiveWindow.Visible = true;

                int slideWidth = (int) toExport.PageSetup.SlideWidth;
                int slideHeight = (int)toExport.PageSetup.SlideHeight;
                int slideCount = 1, onPage = 1;
                publisherDoc.Pages[onPage].Duplicate();

                foreach(_Slide slide in toExport.Slides){
                    if (slideCount == 4)  // End of Page but only create a new one, if there is more!
                    {
                        publisherDoc.Pages[onPage + 1].Duplicate();
                        onPage += 1;
                        slideCount = 1;
                    }

                    ed.addDebug("Exporting slide #" + slide.SlideNumber);
                    String fname = Path.Combine(tempDir, slide.SlideNumber + "-ppt.png");
                    slide.Export(fname, "png");

                    String notes = "";
                    if (options.Contains("exportNotes"))
                    {
                        // I HATE YOU POWERPOINT!
                        Uri partUriResource = PackUriHelper.CreatePartUri(
                              new Uri(@"ppt/notesSlides/notesSlide" + slide.SlideNumber + ".xml", UriKind.Relative));
                        if (zipPresentation.PartExists(partUriResource))
                        {
                            Stream noteStream = zipPresentation.GetPart(partUriResource).GetStream(FileMode.Open, FileAccess.Read);
                            StreamReader nSR = new StreamReader(noteStream);

                            // Strip crap
                            notes = nSR.ReadToEnd();
                            // Chop out slide numbers. I don't have a clue really :')
                            int s = notes.IndexOf("<p:txBody>");
                            if (s != -1)
                            {
                                notes = notes.Substring(s, notes.IndexOf("</p:txBody>") - s);
                                notes = (new Regex("<[^>]*>")).Replace(notes, "");
                            }

                            nSR.Close();
                            noteStream.Close();
                        }
                    }

                    foreach (Microsoft.Office.Interop.Publisher.Shape shape in publisherDoc.Pages[onPage].Shapes)
                    {
                        if (shapeHasTag(shape, "Picture "+ slideCount))
                        {
                            shape.Fill.UserPicture(fname);
                        }
                        else if (shapeHasTag(shape, "Text " + slideCount))
                        {
                            shape.TextFrame.AutoFitText = PbTextAutoFitType.pbTextAutoFitBestFit;
                            shape.TextFrame.TextRange.Text = notes;
                        }
                    }

                    slideCount += 1;
                }

                publisherDoc.Pages[publisherDoc.Pages.Count].Delete();
                toExport.Close();
                ed.addDebug("Exported as handouts :)");
            }
            ed.finishJob("");
        }
        public static new int Convert(String inputFile, String outputFile, Hashtable options)
        {
            Boolean running = (Boolean)options["noquit"];
            Microsoft.Office.Interop.Publisher.Application app = null;
            String tmpFile = null;
            try
            {
                try
                {
                    app = (Microsoft.Office.Interop.Publisher.Application)Marshal.GetActiveObject("Publisher.Application");
                }
                catch (System.Exception)
                {
                    app = new Microsoft.Office.Interop.Publisher.Application();
                    running = false;
                }
                Boolean nowrite = (Boolean)options["readonly"];
                bool pdfa = (Boolean)options["pdfa"] ? true : false;
                if ((Boolean)options["hidden"])
                {
                    var activeWin = app.ActiveWindow;
                    activeWin.Visible = false;
                    Converter.releaseCOMObject(activeWin);
                }
                app.Open(inputFile, nowrite, false, PbSaveOptions.pbDoNotSaveChanges);
                PbFixedFormatIntent quality = PbFixedFormatIntent.pbIntentStandard;
                if ((Boolean)options["print"])
                {
                    quality = PbFixedFormatIntent.pbIntentPrinting;
                }
                if ((Boolean)options["screen"])
                {
                    quality = PbFixedFormatIntent.pbIntentMinimum;
                }
                Boolean includeProps = !(Boolean)options["excludeprops"];
                Boolean includeTags = !(Boolean)options["excludetags"];

                // Try and avoid dialogs about versions
                tmpFile = System.IO.Path.GetTempPath() + Guid.NewGuid().ToString() + ".pub";
                var activeDocument = app.ActiveDocument;
                activeDocument.SaveAs(tmpFile, PbFileFormat.pbFilePublication, false);
                activeDocument.ExportAsFixedFormat(PbFixedFormatType.pbFixedFormatTypePDF, outputFile, quality, includeProps, -1, -1, -1, -1, -1, -1, -1, true, PbPrintStyle.pbPrintStyleDefault, includeTags, true, pdfa);
                activeDocument.Close();

                Converter.releaseCOMObject(activeDocument);
                return (int)ExitCode.Success;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return (int)ExitCode.UnknownError;
            }
            finally
            {
                if (tmpFile != null)
                {
                    System.IO.File.Delete(tmpFile);
                }
                if (app != null)
                {
                    ((Microsoft.Office.Interop.Publisher._Application)app).Quit();
                }
                Converter.releaseCOMObject(app);
            }
        }
예제 #8
0
        public void runInForm()
        {
            ed.addDebug("Exporting " + file + "...");
            String tempDir = getTempDir();

            ed.addDebug("Tempoary Directory: " + tempDir);

            if (this.method == "handouts")
            {
                Package zipPresentation = null;
                if (options.Contains("exportNotes"))
                {
                    String tmpFile = Path.Combine(tempDir, "pres.zip");
                    File.Copy(file, tmpFile);
                    zipPresentation = ZipPackage.Open(new FileStream(tmpFile, FileMode.Open));
                }

                Microsoft.Office.Interop.PowerPoint.Application powerPoint = openPowerPoint();
                ed.addDebug("PowerPoint should have loaded document");
                ed.addDebug("Exporting as handouts...");
                Presentation toExport = powerPoint.Presentations[1];

                ed.addDebug("Opening Publisher Template");
                Microsoft.Office.Interop.Publisher.Application publisher = new Microsoft.Office.Interop.Publisher.Application();

                String pubFile = Path.Combine(tempDir, "out.pub");
                File.Copy(Path.Combine(
                              Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath),
                              "Templates",
                              "Handout-3P.pub" // TODO: Add option for different templates
                              ), pubFile);
                Document publisherDoc = publisher.Open(pubFile);
                publisher.ActiveWindow.Visible = true;


                int slideWidth = (int)toExport.PageSetup.SlideWidth;
                int slideHeight = (int)toExport.PageSetup.SlideHeight;
                int slideCount = 1, onPage = 1;
                publisherDoc.Pages[onPage].Duplicate();

                foreach (_Slide slide in toExport.Slides)
                {
                    if (slideCount == 4)  // End of Page but only create a new one, if there is more!
                    {
                        publisherDoc.Pages[onPage + 1].Duplicate();
                        onPage    += 1;
                        slideCount = 1;
                    }

                    ed.addDebug("Exporting slide #" + slide.SlideNumber);
                    String fname = Path.Combine(tempDir, slide.SlideNumber + "-ppt.png");
                    slide.Export(fname, "png");

                    String notes = "";
                    if (options.Contains("exportNotes"))
                    {
                        // I HATE YOU POWERPOINT!
                        Uri partUriResource = PackUriHelper.CreatePartUri(
                            new Uri(@"ppt/notesSlides/notesSlide" + slide.SlideNumber + ".xml", UriKind.Relative));
                        if (zipPresentation.PartExists(partUriResource))
                        {
                            Stream       noteStream = zipPresentation.GetPart(partUriResource).GetStream(FileMode.Open, FileAccess.Read);
                            StreamReader nSR        = new StreamReader(noteStream);

                            // Strip crap
                            notes = nSR.ReadToEnd();
                            // Chop out slide numbers. I don't have a clue really :')
                            int s = notes.IndexOf("<p:txBody>");
                            if (s != -1)
                            {
                                notes = notes.Substring(s, notes.IndexOf("</p:txBody>") - s);
                                notes = (new Regex("<[^>]*>")).Replace(notes, "");
                            }

                            nSR.Close();
                            noteStream.Close();
                        }
                    }


                    foreach (Microsoft.Office.Interop.Publisher.Shape shape in publisherDoc.Pages[onPage].Shapes)
                    {
                        if (shapeHasTag(shape, "Picture " + slideCount))
                        {
                            shape.Fill.UserPicture(fname);
                        }
                        else if (shapeHasTag(shape, "Text " + slideCount))
                        {
                            shape.TextFrame.AutoFitText    = PbTextAutoFitType.pbTextAutoFitBestFit;
                            shape.TextFrame.TextRange.Text = notes;
                        }
                    }

                    slideCount += 1;
                }

                publisherDoc.Pages[publisherDoc.Pages.Count].Delete();
                toExport.Close();
                ed.addDebug("Exported as handouts :)");
            }
            ed.finishJob("");
        }