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
 bool HookDocumentClose(Pub.Document doc)
 {
     if (doc != null)
     {
         Pub.DocumentEvents_Event docEvents = (Pub.DocumentEvents_Event)doc;
         docEvents.BeforeClose += new Microsoft.Office.Interop.Publisher.DocumentEvents_BeforeCloseEventHandler(docEvents_BeforeClose);
         return(true);
     }
     return(false);
 }
Example #3
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);
        }
 public PubRangeDocument(Pub.Document doc, OfficeTextDocument.ProcessingType eType)
     : base(doc, eType)
 {
 }
 public PubDocument(Pub.Document doc, ProcessingType eType)
     : base(doc, eType)
 {
 }
Example #6
0
        /// <summary>
        /// Converts Расписашка (*.db) database file with schedule to Microsoft Office Publisher 2010 (*.pub) file
        /// </summary>
        /// <param name="inPath">full path to Расписашка (*.db) database file with schedule</param>
        /// <param name="outPath">full path to Публикация Publisher 2010 (*.pub) publication file with converted schedule</param>
        /// <param name="backWorker">BackgroundWorker for reporting progress</param>
        /// <returns>full path to converted Microsoft Office Publisher 2010 (*.pub) file</returns>
        private string convertDatabase(string inPath, string outPath, BackgroundWorker backWorker)
        {
            string savePath = inPath.Substring(0, inPath.Length - 3) + ".pub";

            SQLiteConnection dbConnection = new SQLiteConnection("Data Source=" + inPath + ";Version=3");

            dbConnection.Open();
            const string command = "SELECT names_1.name AS teacher_short, names_1.full_name AS teacher, lessons._id, lessons.day, lessons.number, names.name AS subject, names_2.name AS place, names_3.name AS type, lessons.weeks, lessons.kind_id AS type_id" +
                                   " FROM lessons, names, names names_1, names names_2, names names_3" +
                                   " WHERE lessons.name_id = names._id AND lessons.teacher_id = names_1._id AND lessons.place_id = names_2._id AND lessons.kind_id = names_3._id";
            SQLiteCommand    dbCommand = new SQLiteCommand(command, dbConnection);
            SQLiteDataReader dbReader  = dbCommand.ExecuteReader();

            Publisher.Application publisherApp = new Publisher.Application();
            publisherApp.ScreenUpdating = false;
            Publisher.Document document        = publisherApp.ActiveDocument;
            Publisher.Window   publisherWindow = publisherApp.ActiveWindow;
            //publisherWindow.Caption = "Schedule";
            //publisherWindow.WindowState = Publisher.PbWindowState.pbWindowStateMaximize;
            //publisherWindow.Visible = false;

            // Setup design
            document.PageSetup.PageWidth  = publisherApp.CentimetersToPoints(PAGE_A4_WIDTH);
            document.PageSetup.PageHeight = publisherApp.CentimetersToPoints(PAGE_A4_HEIGHT);


            //Publisher.Page page = document.Pages.Add(1, 0);
            Publisher.Page page = document.Pages[1];

            // Create tables
            Publisher.Shape[] shapes = new Publisher.Shape[10];
            Publisher.Table[] tables = new Publisher.Table[10];

            float[]  positionsX = { 0.5f, 10.25f, 20.0f, 0.5f, 10.25f, 0.5f, 10.25f, 20.0f, 0.5f, 10.25f };
            float[]  positionsY = { 0.5f, 0.5f, 0.5f, 5.6f, 5.6f, 10.75f, 10.75f, 10.75f, 15.85f, 15.85f };
            string[] weekDays   = { "Понедельник", "Вторник", "Среда", "Четверг", "Пятница" };

            for (int i = 0; i < 10; i++)
            {
                Publisher.Shape shape = page.Shapes.AddTable(6, 5, publisherApp.CentimetersToPoints(positionsX[i]), publisherApp.CentimetersToPoints(positionsY[i]), publisherApp.CentimetersToPoints(9.25f), publisherApp.CentimetersToPoints(4.6f));
                Publisher.Table table = shape.Table;
                shapes[i] = shape;
                tables[i] = table;
                table.Rows[1].Cells.Merge();

                // Setup rows/cols sizes
                table.Columns[1].Width = publisherApp.CentimetersToPoints(0.425f);
                table.Columns[2].Width = publisherApp.CentimetersToPoints(0.425f);
                table.Columns[3].Width = publisherApp.CentimetersToPoints(3.625f);
                table.Columns[4].Width = publisherApp.CentimetersToPoints(1.375f);
                table.Columns[5].Width = publisherApp.CentimetersToPoints(3.4f);

                table.Rows[1].Height = publisherApp.CentimetersToPoints(0.63f);
                table.Rows[2].Height = publisherApp.CentimetersToPoints(0.62f);
                table.Rows[3].Height = publisherApp.CentimetersToPoints(0.62f);
                table.Rows[4].Height = publisherApp.CentimetersToPoints(0.62f);
                table.Rows[5].Height = publisherApp.CentimetersToPoints(0.62f);
                table.Rows[6].Height = publisherApp.CentimetersToPoints(1.45f);

                table.GrowToFitText = false;

                // Setup borders
                foreach (Publisher.Cell tcell in table.Cells)
                {
                    tcell.BorderLeft.Weight   = 1.0f;
                    tcell.BorderTop.Weight    = 1.0f;
                    tcell.BorderRight.Weight  = 1.0f;
                    tcell.BorderBottom.Weight = 1.0f;
                }

                Publisher.Cell cell = table.Rows[1].Cells[1];
                cell.VerticalTextAlignment = Publisher.PbVerticalTextAlignmentType.pbVerticalTextAlignmentCenter;
                cell.BorderLeft.Weight     = 1.0f;
                cell.BorderTop.Weight      = 1.0f;
                cell.BorderRight.Weight    = 1.0f;
                cell.BorderBottom.Weight   = 1.0f;

                Publisher.TextRange cellText = cell.TextRange;
                cellText.Text = weekDays[i % 5];
                cellText.ParagraphFormat.Alignment = Publisher.PbParagraphAlignmentType.pbParagraphAlignmentCenter;
                cellText.Font.Name   = "Arial";
                cellText.Font.Bold   = MsoTriState.msoTrue;
                cellText.Font.Italic = MsoTriState.msoTrue;
                //cellText.ParagraphFormat.SpaceAfter = 0.0f;
                //cellText.ParagraphFormat.LineSpacing = 1.0f;
                //cellText.ParagraphFormat.SpaceBefore = 0.0f;

                for (int j = 0; j < 5; j++)
                {
                    cell = table.Rows[j + 2].Cells[2];
                    cell.VerticalTextAlignment = Publisher.PbVerticalTextAlignmentType.pbVerticalTextAlignmentCenter;
                    cellText = cell.TextRange;
                    cellText.ParagraphFormat.Alignment = Publisher.PbParagraphAlignmentType.pbParagraphAlignmentCenter;
                    cellText.Font.Name = "Arial";
                    cellText.Font.Bold = MsoTriState.msoTrue;
                    //cellText.ParagraphFormat.SpaceAfter = 0.0f;
                    //cellText.ParagraphFormat.LineSpacing = 1.0f;
                    //cellText.ParagraphFormat.SpaceBefore = 0.0f;
                    cellText.Text = (j + 1).ToString();
                }

                backWorker.ReportProgress((int)(i / 10.0f * 75));
            }

            // Fill data
            string subject, place, teacher_short;
            long   type_id;
            string week;
            long   day, number;
            int    index;
            int    counter = 0;

            while (dbReader.Read())
            {
                subject       = (string)dbReader["subject"];
                place         = (string)dbReader["place"];
                teacher_short = (string)dbReader["teacher_short"];
                type_id       = (long)dbReader["type_id"];
                week          = (string)dbReader["weeks"];

                if (week == "a") // all: 1,2,3,...
                {
                    day    = (long)dbReader["day"];
                    number = (long)dbReader["number"];

                    index = (int)day - 1;

                    tables[index].Rows[(int)number + 1].Cells[1].TextRange.Text = (type_id == 1 ? "+" : "");
                    tables[index].Rows[(int)number + 1].Cells[3].TextRange.Text = subject;
                    tables[index].Rows[(int)number + 1].Cells[4].TextRange.Text = place;
                    tables[index].Rows[(int)number + 1].Cells[5].TextRange.Text = teacher_short;

                    index = (int)day + 4;

                    tables[index].Rows[(int)number + 1].Cells[1].TextRange.Text = (type_id == 1 ? "+" : "");
                    tables[index].Rows[(int)number + 1].Cells[3].TextRange.Text = subject;
                    tables[index].Rows[(int)number + 1].Cells[4].TextRange.Text = place;
                    tables[index].Rows[(int)number + 1].Cells[5].TextRange.Text = teacher_short;
                }
                else if (week == "e") // even: 2,4,6,...
                {
                    day    = (long)dbReader["day"];
                    number = (long)dbReader["number"];

                    index = (int)day + 4;

                    tables[index].Rows[(int)number + 1].Cells[1].TextRange.Text = (type_id == 1 ? "+" : "");
                    tables[index].Rows[(int)number + 1].Cells[3].TextRange.Text = subject;
                    tables[index].Rows[(int)number + 1].Cells[4].TextRange.Text = place;
                    tables[index].Rows[(int)number + 1].Cells[5].TextRange.Text = teacher_short;
                }
                else if (week == "o") // odd: 1,3,5,...
                {
                    day    = (long)dbReader["day"];
                    number = (long)dbReader["number"];

                    index = (int)day - 1;

                    tables[index].Rows[(int)number + 1].Cells[1].TextRange.Text = (type_id == 1 ? "+" : "");
                    tables[index].Rows[(int)number + 1].Cells[3].TextRange.Text = subject;
                    tables[index].Rows[(int)number + 1].Cells[4].TextRange.Text = place;
                    tables[index].Rows[(int)number + 1].Cells[5].TextRange.Text = teacher_short;
                }

                backWorker.ReportProgress((int)(++counter / (float)dbReader.StepCount * 24 + 75));
            }

            publisherWindow.Caption     = "Schedule";
            publisherWindow.Visible     = true;
            publisherWindow.WindowState = Publisher.PbWindowState.pbWindowStateMaximize;
            publisherApp.ScreenUpdating = true;

            document.SaveAs(outPath);
            backWorker.ReportProgress(100);

            // Release all COM objects
            Marshal.ReleaseComObject(publisherApp);

            return(savePath);
        }