Ejemplo n.º 1
0
        public void gestiscoPDF()
        {
            //var text = new TikaOnDotNet.TextExtraction.TextExtractor().Extract(path).Text.Trim();

            PdfReader reader = new PdfReader(mainI.pdfPath);

            for (int page = 1; page <= reader.NumberOfPages; page++)
            {
                ITextExtractionStrategy its = new iTextSharp.text.pdf.parser.LocationTextExtractionStrategy();
                PdfTextExtractor.GetTextFromPage(reader, page, its);
                string strPage = its.GetResultantText();
                IDictionary <string, string> res = this.findDataPdf(strPage);

                if (!string.IsNullOrEmpty((string)res["nameCognome"]) && !string.IsNullOrEmpty((string)res["data"]))
                {
                    //Salvo nome e cognome + pdf

                    string nameCognome = ((string)res["nameCognome"].ToLower()).Trim();
                    string data        = ((string)res["data"]).Trim();

                    if (!mainI.result.ContainsKey(nameCognome))
                    {
                        mainI.result[nameCognome] = importPage(reader, page, nameCognome);

                        ListViewItem item1 = new ListViewItem("2", 0);
                        item1.SubItems.Add(nameCognome);
                        item1.SubItems.Add("1");

                        lv1.Items.AddRange(new ListViewItem[] { item1 });

                        lv1.EnsureVisible(lv1.Items.Count - 1);
                    }
                    else
                    {
                        // PDF DUPLICATO
                        ListViewItem item1 = new ListViewItem("2", 0);
                        item1.SubItems.Add(nameCognome);
                        item1.SubItems.Add("0");

                        lv1.Items.AddRange(new ListViewItem[] { item1 });

                        lv1.EnsureVisible(lv1.Items.Count - 1);
                    }

                    if (string.IsNullOrEmpty(mainI.data))
                    {
                        mainI.data = data.Trim();
                    }

                    if (mainI.data != data)
                    {
                        // DATE DIVERSE NEL PDF
                    }
                }
            }
            reader.Close();


            // EVENTUALMENTE DRIVE
            if (this.cbDrive.Checked)
            {
                var a = GoogleDrive.uploadOnDrive(mainI.result, lv1, checkBoxCreateFolder.Checked, pb);
            }

            MessageBox.Show("Procedimento terminato", "PdfDrive");
        }
Ejemplo n.º 2
0
        public static Google.Apis.Drive.v3.Data.File uploadOnDrive(IDictionary <string, string> listaPdf, ListView lv, bool createFolder, ToolStripProgressBar pb)
        {
            IList <Google.Apis.Drive.v3.Data.File> fldrs = GoogleDrive.listFolderDrive();


            if (createFolder)
            {
                fldrs = creaCartelleInesistenti(fldrs, listaPdf, lv);
            }

            List <string> tmpPdfAggiunti = new List <string>();

            List <ListViewItem> lwi = new List <ListViewItem>();

            pb.Value = 0;

            pb.Maximum = listaPdf.Count;

            foreach (var fld in fldrs)
            {
                string name = fld.Name.ToLower();
                string tag  = fld.Id;

                if (listaPdf.ContainsKey(name.ToLower()))
                {
                    string path = listaPdf[name];

                    Google.Apis.Drive.v3.Data.File body = new Google.Apis.Drive.v3.Data.File();

                    body.Name = mainI.data + "_" + Program.gestiscoNome(System.IO.Path.GetFileName(@path).ToUpper()) + ".pdf";

                    body.MimeType = GetMimeType(@path);
                    body.Parents  = new List <string> {
                        tag
                    };
                    byte[] byteArray = System.IO.File.ReadAllBytes(@path);
                    System.IO.MemoryStream stream = new System.IO.MemoryStream(byteArray);

                    try
                    {
                        FilesResource.CreateMediaUpload request = GoogleDrive.service.Files.Create(body, stream, body.MimeType);
                        request.SupportsTeamDrives = true;
                        request.Upload();


                        ListViewItem item1 = new ListViewItem("3", 0);
                        item1.SubItems.Add(name);
                        item1.SubItems.Add("1");

                        lwi.Add(item1);

                        tmpPdfAggiunti.Add(name);

                        pb.Value = pb.Value + 1;
                    }
                    catch (Exception e)
                    {
                        ListViewItem item1 = new ListViewItem("3");
                        item1.SubItems.Add(name);
                        item1.SubItems.Add("0");

                        lv.Items.AddRange(new ListViewItem[] { item1 });

                        pb.Value = pb.Value + 1;
                    }
                }
            }

            foreach (var namePDF in listaPdf)
            {
                if (!tmpPdfAggiunti.Contains(namePDF.Key))
                {
                    ListViewItem i = new ListViewItem("3");

                    i.SubItems.Add(namePDF.Key);
                    i.SubItems.Add("0");

                    lwi.Add(i);
                }
            }

            foreach (ListViewItem lw in lwi)
            {
                lv.Items.Add(lw);
                lv.EnsureVisible(lv.Items.Count - 1);
            }

            return(null);
        }