Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So at the moment, I have a PDF I am parsing. I have split it where I need it to

CYT-HMI-S-005 CarrierSCAC: BPUS Stop Number:1 Release Type: AUTO
Route Name:
Days to 
Pick-up Delivery 
Final 
Order Due @ 
Frequency: Frequency:
Location
Carrier Arrival Carrier Departure Plant Destination Dock Code Initial Dest Final Location
Initial Arrival
10:45 11:15 HMI IHMI1 HMI 14:45 14:45 0 MTWRFS MTWRFS // This is the only line I need to parse and add to the datagrid.


The data in this line
10:15 11:00 HMI IOSL1 OSL 17:15 17:15 0 MTWRFS MTWRFS
is just data that includes time arrived, time departure, company, ship-to site and so fourth. The data isn't the problem. The issue is importing that line into the datagrid under specific columns.

So after every "Initial Arrival" I would like to parse that text line
10:15 11:00 HMI IOSL1 OSL 17:15 17:15 0 MTWRFS MTWRFS
and insert it into a datagrid.

Or Plan B:
I run a query from the database that returns the shipments and is able to match results with parsed pdf data via a column

TLDR: There is important data stored in 2 different places. The PDF data is sent monthly from a company. I need to be able to match columns up from the database and the pdf parsed data.

What I have tried:

public static void ReadPDF()
        {
            PdfReader reader = new PdfReader(@"file.pdf");
            int intPageNum = reader.NumberOfPages;
            string text;
            string[] words;
            string line;

            for (int i = 1; i <= intPageNum; i++)
            {
                text = PdfTextExtractor.GetTextFromPage(reader, i, new LocationTextExtractionStrategy());

                words = text.Split($"Delivery Frequency:");
                for (int j = 0, len = words.Length; j < len; j++)
                {
                    line = Encoding.UTF8.GetString(Encoding.UTF8.GetBytes(words[j]));
                    Debug.Print(line);
                }
            }


This is the code I am using to parse the PDF data.
Posted
Updated 9-Jun-20 6:23am
v2

You just need to collect the relevant columns into a List<t> or similar collection, which you use as the source of the grid. The first thing to decide is which fields you want to display and in what order.
 
Share this answer
 
Comments
Knowledged 9-Jun-20 11:04am    
I would just like to insert as is into a datagrid for now. One step at a time.
Richard MacCutchan 9-Jun-20 11:07am    
So you just insert that as a string into whichever cell you want it.
Knowledged 9-Jun-20 11:08am    
I would like seperate columns by the spaces in between. Could you help me out with an example?
Richard MacCutchan 9-Jun-20 11:10am    
Use String.Split Method (System) | Microsoft Docs[^] to separate the different fields, then add them into the separate cells of your datagrid row, and add the row to the grid.
Knowledged 9-Jun-20 11:11am    
Yes I get that could you help me with adding a string split to a grid row? let's say column[2]
This will add the ten fields to the next row of the DataGridView.
C#
// split the fields of the text line
string[] fields = pdfLine.Split(new char[] { ' ' });

// add a new row
int rowNum = dataGridView1.Rows.Add();
DataGridViewRow row = dataGridView1.Rows[rowNum];

// add the fields to the separate columns of the new row
for (int i = 0; i < fields.Length; ++i)
{
    row.Cells[i].Value = fields[i];
}
 
Share this answer
 
Comments
Knowledged 9-Jun-20 12:22pm    
I have updated the question to show what the data looks like that I need to parse.
Richard MacCutchan 9-Jun-20 12:24pm    
Yes, and that is the data that I used in my example.
Knowledged 9-Jun-20 12:35pm    
Okay so I am executing readpdf from the view model and the datagrid is on a different view. So I am unable to use the rows add
Richard MacCutchan 9-Jun-20 13:52pm    
Sorry I have not used MVVM so that does not mean anything to me.
Knowledged 9-Jun-20 12:39pm    
CYT-ELP-S-007
CarrierSCAC:
VEXP-N
Stop
Number:1
Release
Type:
AUTO
Route
Name:
Days
to

Pick-up
Delivery

Final

Order
Due
@

Frequency:
Frequency:
Location
Carrier
Arrival
Carrier
Departure
Plant
Destination
Dock
Code
Initial
Dest
Final
Location
Initial
Arrival
13:30
14:00
ELP
ELP1
ELP
15:15
15:15
0
MTWRFS
MTWRFS


So with the code you provided this is what is printing in debug.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900