Exemple #1
0
        private void BtnSaveAndMerge_Click(object sender, EventArgs e)
        {
            if (Global.ScanFiles.Count == 0)
            {
                MessageBox.Show(@"Brak plików na liście!", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (Global.ScanFiles.Values.Any(o => string.IsNullOrEmpty(o.Prefix)))
            {
                MessageBox.Show(@"Nie wszystkie skany zostały zindeksowane!", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            List <string> typesToMerge = Global.DokDict.Values.Where(d => d.Scal).Select(p => p.Prefix).ToList();

            List <string> typesFromFile = Global.ScanFiles.Values.Where(s => !string.IsNullOrEmpty(s.Prefix)).Select(p => p.Prefix).Distinct().ToList();

            typesToMerge = typesToMerge.Intersect(typesFromFile).ToList();

            string pdfMergeFolder = Path.Combine(Global.ScanFiles[0].Path, textBoxOperat.Text, "merge");

            Directory.CreateDirectory(pdfMergeFolder);     //  utwórz folder wynikowy

            List <ScanFile> outputPdfFiles = new List <ScanFile>();

            long sizeBefore = 0;
            long sizeAfter  = 0;

            foreach (string prefix in typesToMerge)
            {
                ScanFile mergedScanFile = new ScanFile
                {
                    Prefix      = prefix,
                    TypeCounter = 1
                };

                MemoryStream outputStream = new MemoryStream();

                using (PdfDocument outputPdf = new PdfDocument(new PdfWriter(outputStream)))
                {
                    PdfDocumentInfo info = outputPdf.GetDocumentInfo();

                    info.SetCreator("GISNET ScanHelper");

                    List <ScanFile> scanFilesForPrefix = Global.ScanFiles.Values.Where(s => s.Prefix == prefix).ToList();

                    //  przypisanie atrybutów pierwszego pliku do pliku wynikowego
                    mergedScanFile.IdFile   = scanFilesForPrefix[0].IdFile;
                    mergedScanFile.FileName = scanFilesForPrefix[0].FileName;

                    PdfMerger pdfMerger = new PdfMerger(outputPdf);

                    foreach (ScanFile scanFile in scanFilesForPrefix)
                    {
                        Global.ScanFiles[scanFile.IdFile].Merged = true;

                        sizeBefore += scanFile.PdfFile.Length;

                        using (MemoryStream sourceStream = new MemoryStream(scanFile.PdfFile))
                            using (PdfDocument inputPdf = new PdfDocument(new PdfReader(sourceStream)))
                            {
                                pdfMerger.Merge(inputPdf, 1, inputPdf.GetNumberOfPages());
                            }
                    }
                }

                outputStream.Close();

                mergedScanFile.PdfFile = outputStream.ToArray();

                sizeAfter += mergedScanFile.PdfFile.Length;

                outputPdfFiles.Add(mergedScanFile);
            }

            // lista plików, które nie zostały połączone w jeden i ich prefiks nie jest "skip"
            List <ScanFile> scanFilesWithoutMerge = Global.ScanFiles.Values.Where(scan => scan.Merged == false && scan.Prefix != "skip").ToList();

            foreach (ScanFile scanFile in scanFilesWithoutMerge)
            {
                sizeBefore += scanFile.PdfFile.Length;
                sizeAfter  += scanFile.PdfFile.Length;

                outputPdfFiles.Add(scanFile);
            }

            List <ScanFile> outputPdfFilesOrdered = outputPdfFiles.OrderBy(x => x.IdFile).ToList();

            for (int i = 0; i < outputPdfFilesOrdered.Count; i++)
            {
                ScanFile pdfFile = outputPdfFilesOrdered[i];

                string fileName = textBoxOperat.Text +
                                  "_" +
                                  (i + 1) +
                                  "-" +
                                  pdfFile.Prefix +
                                  "-" +
                                  pdfFile.TypeCounter.ToString().PadLeft(3, '0') +
                                  Path.GetExtension(pdfFile.FileName);

                fileName = Path.Combine(pdfMergeFolder, fileName);

                File.WriteAllBytes(fileName, pdfFile.PdfFile);
            }

            MessageBox.Show("Pliki połączono!\n\n" +
                            $"Rozmiar przed:\t{Math.Round(sizeBefore / (double)1024, 2)} MB\n" +
                            $"Rozmiar po:\t{Math.Round(sizeAfter / (double)1024, 2)} MB\n\n" +
                            $"Współczynnik zmiany rozmiaru: {Math.Round(sizeAfter / (double)sizeBefore, 2)}",
                            Application.ProductName,
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Information);
        }
Exemple #2
0
        private void BtnOpen_Click(object sender, EventArgs e)
        {
            string[] fileNames;     //  nazwy wybranych plików

            string buttonName = ((Button)sender).Name;

            switch (buttonName)
            {
            case "buttonOpenFiles":

                using (OpenFileDialog ofDialog = new OpenFileDialog())
                {
                    ofDialog.Filter           = @"Dokumenty (*.pdf)|*.pdf";
                    ofDialog.Multiselect      = true;
                    ofDialog.InitialDirectory = Global.LastDirectory;

                    DialogResult dialogResult = ofDialog.ShowDialog();

                    if (dialogResult == DialogResult.OK)
                    {
                        fileNames = ofDialog.FileNames;
                        Array.Sort(fileNames, new NaturalStringComparer());     //  sortowanie naturalne po nazwach plików

                        Global.LastDirectory = Path.GetDirectoryName(ofDialog.FileName);
                    }
                    else
                    {
                        return;
                    }
                }

                break;

            case "buttonOpenDirectory":

                using (FolderBrowserDialog fbdOpen = new FolderBrowserDialog())
                {
                    fbdOpen.ShowNewFolderButton = false;
                    fbdOpen.SelectedPath        = Global.LastDirectory;

                    DialogResult dialogResult = fbdOpen.ShowDialog();

                    if (dialogResult == DialogResult.OK)
                    {
                        fileNames = Directory.GetFiles(fbdOpen.SelectedPath, "*.pdf", SearchOption.TopDirectoryOnly);
                        //fileNames = fileNames.Union(Directory.GetFiles(fbdOpen.SelectedPath, "*.jpg", SearchOption.TopDirectoryOnly)).ToArray();
                        Array.Sort(fileNames, new NaturalStringComparer());

                        Global.LastDirectory = fbdOpen.SelectedPath;

                        if (fileNames.Length == 0)
                        {
                            //  załaduj plik startowy do okienka z podglądem
                            Global.Zoom = GetFitZoom(File.ReadAllBytes("ScanHelper.pdf"), out int _);
                            pdfDocumentViewer.LoadFromStream(new MemoryStream(File.ReadAllBytes("ScanHelper.pdf")));
                            pdfDocumentViewer.ZoomTo(Global.Zoom);
                            pdfDocumentViewer.EnableHandTool();

                            return;
                        }
                    }
                    else
                    {
                        return;
                    }
                }

                break;

            default:
                return;
            }

            //  -----------------------------------------------------------------------------------
            //  Zbudowanie listy obiektów z wczytanymi plikami i dodanie ich do listBox
            //  -----------------------------------------------------------------------------------
            Global.ScanFiles = new ScanFileDict();                    //  Nowa lista plików do przetworzenia

            Global.DokDict.Values.ToList().ForEach(c => c.Count = 0); //  wyzeruj ilość rodzajów dokumentów danego typu

            listBoxFiles.Items.Clear();                               //  wyczyść aktualną listę plików w okienku z listą plików

            int idFile = 0;

            foreach (string fileName in fileNames)
            {
                ScanFile scanFile = new ScanFile
                {
                    IdFile          = idFile++,
                    PathAndFileName = fileName,
                    Path            = Path.GetDirectoryName(fileName),
                    FileName        = Path.GetFileName(fileName),
                    Prefix          = null,
                    PdfFile         = File.ReadAllBytes(fileName)
                };

                Global.ScanFiles.Add(scanFile.IdFile, scanFile);

                if (scanFile.FileName != null)
                {
                    listBoxFiles.Items.Add(scanFile.FileName);
                }
            }
            //  -----------------------------------------------------------------------------------

            listBoxFiles.Focus();
            listBoxFiles.SetSelected(0, true);      // zaznacz pierwszy plik na liście plików

            // uaktywnij przyciski, które mają wartości
            foreach (Button button in groupBoxButtons.Controls.OfType <Button>())
            {
                if (button.Text != @"brak")
                {
                    button.Enabled = true;
                }
            }

            //  załaduj do okienka z podglądem pierwszy z wybranych plików
            Global.Zoom = GetFitZoom(Global.ScanFiles[0].PdfFile, out int _);
            pdfDocumentViewer.LoadFromStream(new MemoryStream(Global.ScanFiles[0].PdfFile));
            pdfDocumentViewer.ZoomTo(Global.Zoom);
            pdfDocumentViewer.EnableHandTool();

            textBoxOperat.Text = Global.LastDirectory?.Split(Path.DirectorySeparatorChar).Last();    //  wpisz nazwę katalogu ze skanami do pola tekstowego
        }