コード例 #1
0
        private void Action_GetColumn(string pdfPath, string column)
        {
            if (System.IO.File.Exists(pdfPath) == false)
            {
                MessageBox.Show("File does not exist");
                return;
            }

            PdfDocument doc = PdfDocument.Load(pdfPath);
            string      baseDocumentPath     = Path.GetDirectoryName(txtPdfPath.Text);
            string      baseDocumentFilename = Path.GetFileNameWithoutExtension(txtPdfPath.Text);

            IEnumerable <int> selectedPages = GetSelectedPages(doc.Pages.Count);
            var columns = new List <string>();
            int pageNum = 0;

            foreach (PdfDocumentPage page in doc.Pages)
            {
                pageNum++;
                if (selectedPages.Contains(pageNum) == false)
                {
                    continue;
                }
                PdfTextExtractor     extractor = new PdfTextExtractor(page);
                PdfTextElementColumn columnData;
                if (column.StartsWith("#"))
                {
                    string[] columnParts = column.Substring(1).Split(';');
                    double   y           = Convert.ToDouble(columnParts[0]);
                    double   x1          = Convert.ToDouble(columnParts[1]);
                    double   x2          = Convert.ToDouble(columnParts[2]);
                    columnData = extractor.GetColumn(null, y, x1, x2, x1, x2);
                }
                else
                {
                    columnData = extractor.GetColumn(column);
                }
                if (chkRender.Checked)
                {
                    var    pdfPageRenderer = new PdfPageRenderer(extractor);
                    Bitmap bmp             = pdfPageRenderer.Render();
                    pdfPageRenderer.RenderColumn(columnData, bmp);
                    string fileName = Path.Combine(baseDocumentPath, string.Format("{0}_{1:0000}.png", baseDocumentFilename, pageNum));
                    bmp.Save(fileName, ImageFormat.Png);
                    bmp.Dispose();
                    GC.Collect();
                }
                columns.AddRange(columnData.Elements.Select(t => t.VisibleText));
            }
            txtOutput.Lines = columns.ToArray();
        }