Ejemplo n.º 1
0
        private void BtnGenerate_Click(object sender, RoutedEventArgs e)
        {
            //try
            //{//17-July-2018
            //    string[] existingPdfFiles = Directory.GetFiles(_pdfLocation, "*.pdf");
            //    if (existingPdfFiles != null)
            //        if (existingPdfFiles.Count() > 0)
            //        {
            //            MessageBox.Show("Please delete all existing files in "+ _pdfLocation +" before generating PDF", Title, MessageBoxButton.OK, MessageBoxImage.Warning);
            //            return;
            //        }
            //}
            //catch { }
            string docName = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Name;

            try
            {//10-05-2018
                Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
                acDoc.Database.SaveAs(acDoc.Name, true, DwgVersion.Current, acDoc.Database.SecurityParameters);
                //Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Database.Save();
            }
            catch (Exception ex)
            {
                Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage(ex.ToString());
            }
            if (AcadFunctions.AcadLayersList.Count > 0)
            {
                if (Layerlisodwg.Any(x => x.Include))
                {
                    InitializeLayouts();
                    //Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.Open(docName);
                    Document docPublish = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
                    //Layerlisodwg.Where(x => x.LayerName.Equals("TITLE_1", StringComparison.InvariantCultureIgnoreCase) ||
                    // x.LayerName.Equals("BORDER", StringComparison.InvariantCultureIgnoreCase)).ToList().ForEach(a => a.Include = true);

                    var lstLayers = Layerlisodwg.Where(x => x.Include && x.EntityAvail).Select(x => x).ToList();


                    gridProcess.Visibility = System.Windows.Visibility.Visible;
                    ProgressHeaderPublisher.PublishData("Processing...");
                    txtPercentage.Text = string.Empty;
                    ProgressStatusPublisher.PublishData("");
                    ProgressBarMaximumPublisher.PublishData(lstLayers.Count + 1);
                    pbStatus.Visibility      = System.Windows.Visibility.Visible;
                    txtPercentage.Visibility = System.Windows.Visibility.Visible;

                    int cnt = 0;
                    lstLayers.ToList().ForEach(x =>
                    {
                        cnt++;
                        ProgressHeaderPublisher.PublishData("Processing " + cnt + " of " + lstLayers.Count);
                        ProgressStatusPublisher.PublishData("Publishing " + x.LayerName + "...");
                        if (x.Include)// && x.EntityAvail)
                        {
                            var layerName = x.LayerName;
                            OnOrOffParticularLayer(true, layerName);

                            docPublish.SendStringToExecute("zoom e ", true, false, true);

                            Thread.Sleep(1000);

                            //new SingleSheetPdf(_pdfLocation, GetLayoutIdList(Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Database)).Publish(x.LayerName);

                            new SingleSheetPdf(_pdfLocation, GetLayoutIdList(Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Database)).PublishNew(_pdfLocation, x.LayerName);

                            OnOrOffParticularLayer(false, layerName);
                        }
                        ProgressValuePublisher.PublishData(1);
                    });
                    SetDefaultLayout(docPublish);
                    OffAllLayers(true);

                    ProgressHeaderPublisher.PublishData("Processing....");
                    ProgressStatusPublisher.PublishData("Merging pdf");

                    try
                    {
                        string   path           = _pdfLocation;
                        string[] pdfFiles       = Directory.GetFiles(path, "*.pdf");
                        var      completedFiles = new List <string>();
                        docName = System.IO.Path.GetFileNameWithoutExtension(docName);
                        foreach (var item in pdfFiles)
                        {
                            if (System.IO.Path.GetFileNameWithoutExtension(item).StartsWith(docName))
                            {
                                var layer = Layerlisodwg.Where(y => y.Include && item.Contains(y.LayerName)).Select(y => y).FirstOrDefault();
                                if (layer != null)
                                {
                                    completedFiles.Add(item);
                                }
                            }
                        }
                        // step 1: creation of a document-object
                        iTextSharp.text.Document document = new iTextSharp.text.Document();

                        if (File.Exists(System.IO.Path.Combine(path, docName + ".pdf")))
                        {
                            File.Delete(System.IO.Path.Combine(path, docName + ".pdf"));
                        }
                        if (completedFiles.Count > 0)
                        {
                            // step 2: we create a writer that listens to the document
                            PdfCopy writer = new PdfCopy(document, new FileStream(System.IO.Path.Combine(path, docName + ".pdf"), FileMode.Create));
                            if (writer == null)
                            {
                                return;
                            }

                            // step 3: we open the document
                            document.Open();

                            foreach (string fileName in completedFiles)
                            {
                                // we create a reader for a certain document
                                PdfReader reader = new PdfReader(fileName);
                                reader.ConsolidateNamedDestinations();

                                // step 4: we add content
                                for (int i = 1; i <= reader.NumberOfPages; i++)
                                {
                                    PdfImportedPage page = writer.GetImportedPage(reader, i);
                                    writer.AddPage(page);
                                }

                                PRAcroForm form = reader.AcroForm;
                                if (form != null)
                                {
                                    writer.CopyAcroForm(reader);
                                }

                                reader.Close();
                            }

                            // step 5: we close the document and writer
                            writer.Close();
                            document.Close();


                            foreach (var item in completedFiles)
                            {
                                try
                                {
                                    if (System.IO.File.Exists(item))
                                    {
                                        System.IO.File.Delete(item);
                                    }
                                }
                                catch { }
                            }
                            ProgressValuePublisher.PublishData(1);
                            ProgressHeaderPublisher.PublishData("Processed " + cnt + " of " + lstLayers.Count);
                            ProgressStatusPublisher.PublishData("Completed");
                            MessageBox.Show("PDF Generation Completed", Title, MessageBoxButton.OK, MessageBoxImage.Information);
                            gridProcess.Visibility   = System.Windows.Visibility.Collapsed;
                            pbStatus.Visibility      = System.Windows.Visibility.Collapsed;
                            txtPercentage.Visibility = System.Windows.Visibility.Collapsed;
                            //10-05-2018
                            if (System.IO.File.Exists(System.IO.Path.Combine(path, docName + ".pdf")))
                            {
                                System.Diagnostics.Process.Start(System.IO.Path.Combine(path, docName + ".pdf"));
                                //System.IO.File.Open(System.IO.Path.Combine(path, docName + ".pdf"), FileMode.Open);
                            }
                        }
                        else
                        {
                            ProgressValuePublisher.PublishData(1);
                            ProgressHeaderPublisher.PublishData("Processed " + cnt + " of " + lstLayers.Count);
                            ProgressStatusPublisher.PublishData("Failed");
                            MessageBox.Show("Failed to merge pdf.", Title, MessageBoxButton.OK, MessageBoxImage.Information);
                            gridProcess.Visibility   = System.Windows.Visibility.Collapsed;
                            pbStatus.Visibility      = System.Windows.Visibility.Collapsed;
                            txtPercentage.Visibility = System.Windows.Visibility.Collapsed;
                        }
                    }
                    catch (Exception ex)
                    {
                    }
                }
                else
                {
                    MessageBox.Show("Atleast one layer should be selected.", Title, MessageBoxButton.OK, MessageBoxImage.Warning);
                }
            }
        }
Ejemplo n.º 2
0
        private void generatePDF(string BOMExcel)
        {
            string docName = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Name;

            try
            {
                Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
                acDoc.Database.SaveAs(acDoc.Name, true, DwgVersion.Current, acDoc.Database.SecurityParameters);
            }
            catch (Exception ex)
            {
                Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage(ex.ToString());
            }
            if (AcadFunctions.AcadLayersList.Count > 0)
            {
                if (Layerlisodwg.Any(x => x.Include))
                {
                    InitializeLayouts();
                    Document docPublish = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;

                    var lstLayers = Layerlisodwg.Where(x => x.Include && x.EntityAvail).Select(x => x).ToList();


                    ProgressHeaderPublisher.PublishData("Processing...");
                    ProgressStatusPublisher.PublishData("");
                    ProgressBarMaximumPublisher.PublishData(lstLayers.Count + 1);

                    int cnt = 0;

                    bool Page_1Available = false;
                    if (lstLayers.ToList().Count > 0)
                    {
                        Page_1Available = lstLayers.Where(a => a.Include == true && (a.LayerName.Equals("PAGE_1", StringComparison.InvariantCultureIgnoreCase) || a.LayerName.Equals("PAGE1", StringComparison.InvariantCultureIgnoreCase))).Any();
                    }
                    lstLayers.ToList().ForEach(x =>
                    {
                        cnt++;
                        ProgressHeaderPublisher.PublishData("Processing " + cnt + " of " + lstLayers.Count);
                        ProgressStatusPublisher.PublishData("Publishing " + x.LayerName + "...");
                        if (x.Include)
                        {
                            var layerName = x.LayerName;
                            OnOrOffParticularLayer(true, layerName, Page_1Available);

                            docPublish.SendStringToExecute("zoom e ", true, false, true);

                            Thread.Sleep(1000);

                            new SingleSheetPdf(_pdfLocation, GetLayoutIdList(Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Database)).PublishNew(_pdfLocation, x.LayerName);

                            OnOrOffParticularLayer(false, layerName, Page_1Available);
                        }
                        ProgressValuePublisher.PublishData(1);
                    });
                    SetDefaultLayout(docPublish);
                    OffAllLayers(true);

                    ProgressHeaderPublisher.PublishData("Processing....");
                    ProgressStatusPublisher.PublishData("Merging pdf");

                    try
                    {
                        string   path           = _pdfLocation;
                        string[] pdfFiles       = Directory.GetFiles(path, "*.pdf").Select(f => new FileInfo(f)).OrderBy(f => f.CreationTime).Select(d => d.FullName).ToArray();
                        var      completedFiles = new List <string>();
                        docName = System.IO.Path.GetFileNameWithoutExtension(docName);
                        foreach (var item in pdfFiles)
                        {
                            if (System.IO.Path.GetFileNameWithoutExtension(item).StartsWith(docName))
                            {
                                var layer = Layerlisodwg.Where(y => y.Include && item.Contains(y.LayerName)).Select(y => y).FirstOrDefault();
                                if (layer != null)
                                {
                                    completedFiles.Add(item);
                                }
                            }
                        }
                        statusText = "Appending BOM Spreadsheet to PDF... (Task 6 of 6)";
                        lblpbBarContent.Content = statusText;
                        pbBar.Value             = pbBar.Value + 10;
                        DoEventsHandler.DoEvents();
                        // step 1: creation of a document-object
                        iTextSharp.text.Document document = new iTextSharp.text.Document();

                        if (File.Exists(System.IO.Path.Combine(path, docName + ".pdf")))
                        {
                            File.Delete(System.IO.Path.Combine(path, docName + ".pdf"));
                        }
                        if (completedFiles.Count > 0)
                        {
                            // step 2: we create a writer that listens to the document
                            PdfCopy writer = new PdfCopy(document, new FileStream(System.IO.Path.Combine(path, docName + ".pdf"), FileMode.Create));
                            if (writer == null)
                            {
                                return;
                            }

                            // step 3: we open the document
                            document.Open();

                            foreach (string fileName in completedFiles)
                            {
                                // we create a reader for a certain document
                                PdfReader reader = new PdfReader(fileName);
                                reader.ConsolidateNamedDestinations();

                                // step 4: we add content
                                for (int i = 1; i <= reader.NumberOfPages; i++)
                                {
                                    PdfImportedPage page = writer.GetImportedPage(reader, i);
                                    writer.AddPage(page);
                                }

                                PRAcroForm form = reader.AcroForm;
                                if (form != null)
                                {
                                    writer.CopyAcroForm(reader);
                                }

                                reader.Close();
                            }

                            // step 5: we close the document and writer
                            writer.Close();
                            document.Close();
                            if (System.IO.File.Exists(BOMExcel))
                            {
                                String       strDwgPDF    = System.IO.Path.Combine(path, docName + ".pdf");
                                string       strBOMPDF    = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(BOMExcel), System.IO.Path.GetFileNameWithoutExtension(BOMExcel) + ".pdf");
                                string       strResultPDF = System.IO.Path.Combine(path, docName + "_PROD.pdf");
                                string       shellCommand = "C:\\ACAD2018_LivaNova\\Interface\\PDFTK.exe " + strDwgPDF + " " + strBOMPDF + " cat output " + strResultPDF;
                                AcadCommands oAcad        = new AcadCommands();
                                oAcad.ExecuteCommandSync(shellCommand);
                            }

                            foreach (var item in completedFiles)
                            {
                                try
                                {
                                    if (System.IO.File.Exists(item))
                                    {
                                        System.IO.File.Delete(item);
                                    }
                                }
                                catch { }
                            }
                            ProgressValuePublisher.PublishData(1);
                            ProgressHeaderPublisher.PublishData("Processed " + cnt + " of " + lstLayers.Count);
                            ProgressStatusPublisher.PublishData("Completed");
                            //MessageBox.Show("PDF Generation Completed", Title, MessageBoxButton.OK, MessageBoxImage.Information);
                            //10-05-2018
                            if (System.IO.File.Exists(System.IO.Path.Combine(path, docName + "_PROD.pdf")))
                            {
                                System.Diagnostics.Process.Start(System.IO.Path.Combine(path, docName + "_PROD.pdf"));
                                //System.IO.File.Open(System.IO.Path.Combine(path, docName + ".pdf"), FileMode.Open);
                            }
                        }
                        else
                        {
                            ProgressValuePublisher.PublishData(1);
                            ProgressHeaderPublisher.PublishData("Processed " + cnt + " of " + lstLayers.Count);
                            ProgressStatusPublisher.PublishData("Failed");
                            MessageBox.Show("Failed to merge pdf.", Title, MessageBoxButton.OK, MessageBoxImage.Information);
                        }
                    }
                    catch (Exception ex)
                    {
                    }
                }
                else
                {
                    MessageBox.Show("Atleast one layer should be selected.", Title, MessageBoxButton.OK, MessageBoxImage.Warning);
                }
            }
        }