public static bool CombineMultiplePDFs(string[] fileNames, string TargetFile) { try { string outFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "SamplePDFFolder"); if (!Directory.Exists(outFile)) { Directory.CreateDirectory(outFile); } outFile = Path.Combine(outFile, "MergedPdf.pdf"); // step 1: creation of a document-object Document document = new Document(); // step 2: we create a writer that listens to the document PdfCopy writer = new PdfCopy(document, new FileStream(outFile, FileMode.Create)); if (writer == null) { return(false); } // step 3: we open the document document.Open(); foreach (string fileName in fileNames) { // 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(); File.Copy(outFile, TargetFile, true); File.Delete(outFile); return(true); } catch (Exception ex) { Log.This(ex); return(false); } }
public static byte[] CombineMultiplePDFs(List <byte[]> pdfS) { // step 1: creation of a document-object Document document = new Document(); //create newFileStream object which will be disposed at the end using var ms = new MemoryStream(); // step 2: we create a writer that listens to the document PdfCopy writer = new PdfCopy(document, ms); if (writer == null) { return(null); } // step 3: we open the document document.Open(); foreach (var pdf in pdfS) { if (pdf != null && pdf.Length > 0) { // we create a reader for a certain document PdfReader reader = new PdfReader(pdf); 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(); return(ms.ToArray()); //disposes the newFileStream object }
public void MergeFiles() { if (FileList == null || FileList.Count == 0) { MessageBox.Show("No PDF files found on folder."); } else { OutputFileName = System.IO.Path.Combine(Path, $"{OutputFileName}.pdf"); Document document = new Document(); using (FileStream newFileStream = new FileStream(OutputFileName, FileMode.Create)) { PdfCopy writer = new PdfCopy(document, newFileStream); if (writer == null) { return; } document.Open(); foreach (var file in FileList) { PdfReader reader = new PdfReader(file.FullPath); reader.ConsolidateNamedDestinations(); 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(); } writer.Close(); document.Close(); } } }
private void CombineMultiplePDFs(List <string> fileNames, string outFile) { // step 1: creation of a document-object Document document = new Document(); //create newFileStream object which will be disposed at the end using (FileStream newFileStream = new FileStream(outFile, FileMode.Create)) { // step 2: we create a writer that listens to the document PdfCopy writer = new PdfCopy(document, newFileStream); if (writer == null) { return; } // step 3: we open the document document.Open(); foreach (string fileName in fileNames) { // 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(); }//disposes the newFileStream object }
public static void CombinePdfs(string[] filenames, string outFile) { Document document = new Document(); PdfCopy writer = new PdfCopy(document, new FileStream(outFile, FileMode.Create)); if (writer == null) { return; } document.Open(); foreach (string filename in filenames) { PdfReader reader = new PdfReader(filename); reader.ConsolidateNamedDestinations(); 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(); } writer.Close(); document.Close(); }
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); } } }
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); } } }
public string getFileToPrint(HashSet <string> otid, string newPath) { //string[] directory = Directory.GetDirectories(Globale_Varriables.VAR.pathFileUpload + @"\File\"); string _fullpath = Globale_Varriables.VAR.urlFileUpload + @"\File\"; for (int i = 0; i < otid.Count; i++) { Document doc = new Document(); string _otid = otid.ElementAt(i); string _file = newPath.Replace("{0}", _otid); List <byte[]> _filesByte = new List <byte[]>(); bool _print = false; try { PdfCopy writer = new PdfCopy(doc, new FileStream(_file, FileMode.Create)); /* * PdfWriter pdfW = PdfWriter.GetInstance(doc, new FileStream(_file, FileMode.Create)); * * doc.Close(); */ doc.Open(); //for (int j = 0; j < directory.Length; j++) //{ //if (Globale_Varriables.VAR.pathFileUpload + @"\File\" + otid.ElementAt(i) == directory[j]) //{ string path = _fullpath + _otid; //directory[j]; IEnumerable <string> dirList = System.IO.Directory.EnumerateDirectories(path); foreach (string dir in dirList) { string dpath = dir; string[] type = dpath.Split('\\'); IEnumerable <string> fileList = System.IO.Directory.EnumerateFiles(dpath); DataTable dt = Configs._query.executeProc("dev_getList", "parent@string@DocType#name@string@" + type[type.Length - 1]); int nbr = 0; if (Tools.verifyDataTable(dt)) { nbr = Int32.Parse(dt.Rows[0]["filter"].ToString()); } for (int cp = 1; cp <= nbr; cp++) { string[] files = Directory.GetFiles(dpath, "*.pdf"); foreach (string file in files) { // file , otid.ElementAt(i) PdfReader reader = new PdfReader(file); reader.ConsolidateNamedDestinations(); for (int ic = 1; ic <= reader.NumberOfPages; ic++) { PdfImportedPage page = writer.GetImportedPage(reader, ic); writer.AddPage(page); } PRAcroForm form = reader.AcroForm; if (form != null) { writer.CopyAcroForm(reader); } reader.Close(); /* * byte[] bt = WriteToPdf(new FileInfo(file), otid.ElementAt(i)); * _filesByte.Add(bt); * */ _print = true; } } } string[] _files = Directory.GetFiles(path, "*.pdf"); //directory[j], "*.pdf"); foreach (string file in _files) { PdfReader reader = new PdfReader(file); reader.ConsolidateNamedDestinations(); for (int ic = 1; ic <= reader.NumberOfPages; ic++) { PdfImportedPage page = writer.GetImportedPage(reader, ic); writer.AddPage(page); } PRAcroForm form = reader.AcroForm; if (form != null) { writer.CopyAcroForm(reader); } reader.Close(); /* * //file , otid.ElementAt(i) * byte[] bt = WriteToPdf(new FileInfo(file), otid.ElementAt(i)); * * _filesByte.Add(bt); * */ _print = true; } //} //} writer.Close(); doc.Close(); if (_print) // _filesByte.Count > 0) { /* * _filesByte.Insert(0, File.ReadAllBytes(_file)); * File.WriteAllBytes(_file, PDF.PdfMerger.MergeFiles(_filesByte)); */ Print(_file); } else { File.Delete(_file); } } catch (Exception exx) { } } return(""); }