hey i'm cameron mackenzie at cameron mcnz on twitter and i wanted to talk to you about
extracting some data from your pdfs i just did a tutorial on how to do some basic extraction as an
introduction but if you ever start doing some data extraction from your pdfs you want it to
be anchor based it's the only way to really make it work with multiple files so that's what i want
to talk about here the next tutorial i'm going to talk about doing data extraction on tabular
data and if you put the two together i think you'll really know how to work with pdf files and
i'm going uipath kick this off by creating a new process project and i'll call up pdf extract
and as that project is being created i just want to show you the pdf that i'm going to
pull some data out of you notice that i've got the invoice number the date over here
i've also got the total i want to pull that data out of this pdf file using anchors
now one thing i should point out there's tabular data on here like that mailing address is tabular
the line items here and the orders is tabular and then there's properties there's name value pairs
like invoice and the invoice number date and the date number if you want to pull out tabular data
you have to do that through data scraping i've got a tutorial on that that's one of the next ones
if you want to pull out the name value pairs the best way to do it is with the anchor tag
and so that's what i'm going to demonstrate here i'm going to pull out the invoice number the date
and the total and i'm going to do it all through an anchor activity now to kick this whole thing
off i need to actually get the name and folder of that file it's in a folder called orders here so
i'm just going to copy that and then paste that name in and i'm going to paste it into a process
starting activity so open up the main window
look for the process creation activity there's a something called start process
i'm going to drop that on there as my first step and the process that i want to run is
whatever process is going to open up that invoice a1a pdf that i just showed you
now along with kick along with kicking off that process we need to attach the adobe pdf
window to this process so that we can interact with it in order to do that you look for the
attach window command so i'm going to click the attach window command down here now it
does say what screen do you want to attach and the easiest way to do that is to kind of
move uipath studio off to the left and put your pdf to the right click indicate window on screen
and then just click on your pdf file and the tool realizes okay it's that
pdf adobe acrobat viewer that it wants to that we want to use when we go through this pdf file
now by the way in the selector they always specify the actual name of the file i don't
actually need that here and that can cause problems later if you're looping through a
bunch of files and one of the files obviously won't have that name it'll cause a problem so
i like to deselect that it's not necessary for for this this particular exercise but it's a good idea
now what i want to do is i want to grab some information here i want to grab that
invoice number the invoice date and the invoice total i'm going to grab that information i'm
going to need some variables i'm going to create those right off the bat so i've got invoice number
invoice date
and invoice total all of type string all have scope throughout the entire sequence those will
come handy in this exercise make sure you got those variables declared and then the first
thing i'm going to grab is this invoice number a1a101 and i'm going to do it by telling the
tool hey on every single invoice there'll be a section on the page where it says invoice number
now the invoice number is going to change from time to time so i want you
to look under where it says invoice number which will always be there and give me the
value associated with it and you do that by adding something in called an anchor activity
so there's the anchor base activity i'm going to drop that in here
and it's got two parts to it where's the anchor and what's the text that you want to get based on
that anchor and to fill in the anchor you go find element let me see if i can where is find element
it's actually under ui automation element fine so i'm going to go find element
and again it says what element are you looking for so again click that button
say i'm looking for something relative to the invoice number it says okay that's cool
it says what do you want to do once you've found that i'm going to say i'm going to get some text
that's associated with it says well show me what text you want and again i'll click on that link
and i'll say well it's actually the text that's right underneath it that text right there a1a001
and it looks like everything is running swimmingly now one thing i will say this this anchor base
tries to guess what you're interested in and it doesn't always guess it right that anchor invoice
number is above the string that i want so what you should always do is explicitly state in your
anchor base where that anchor is positioned in this case invoice number is positioned above the
variable above the data that i want so in anchor position you say that the anchor is above or the
anchor is on top of the element that we're after not on top on top means actually the same cell top
means just above it there's two options there that are kind of similar see on top and top we want top
okay so that is going to give us our what's it going to do that's going to give us our invoice
number and if you want you can even write that out so you can say hey the invoice number is and
then plus invoice number and that should print out what the invoice number is and then we can follow
these steps right through and get the invoice date and so again that's going to be exactly the same
it's just going to be different so we grab the anchor base activity throw it on add the find
element throw it on add the get text throw it on in this case i'm going to get the invoice date so
the value of the get text is going to be invoice date then i go through the process of associating
those two elements with the fields that i want so i'm running out of space here on the page
but what is the anchor element indicate the element inside the window for the anchor that
i'm going to be looking for well it's that date colon word there and then what text do
we want well i want the text that's right underneath it that's the text right there
okay and that all looks like it's working properly now again the anchor is above the anchors on
top of the data that i'm getting and so in that case you want to change the anchor base there to
indicate that we're going that the anchor is on top of the field that we're looking for
notice it kind of switches the weight structure on the page too i always think that's kind of cool
nice little attention to detail as they say but now i've got one more i want to
get the total so i'm going to go anchor base over here i'm going to do a find element
i'm going to do a get text the get text i didn't mean to hit that excuse me
the get text field is going to initialize the invoice total so
so i'll specify that gettext updates that as part of its output
and of course i've still got the steps to find the element and get the text so let me see do the find
element for the total indicate element and i'll click over here there's the total field
and then again last what is the text that you want so i'll click on that 115 dollar value
and again it looks like total is on top and so the last thing that i need to do here is
specify that the anchor position is top that gets me through the anchor base thing and then
i guess i can always write the line out there and say hey the total is plus invoice total
okay well all this looks good i always like to make sure you know i got things in double
quotes i often forget to do that make sure that i've got each variable for the getter pointing
to a value and why is there no value there so that get text should point to invoice number
so it looks like i did mess something up there make sure that you've got that specified so get
text for there it goes to invoice number this get well that get text goes to invoice date
and then this get text goes to
invoice total you know what i never printed out the invoice date gee that wasn't very
thoughtful of me so i'm just going to add a writeline after the invoice date and
date is invoice date okay and that looks pretty handsome there there's the well that doesn't look
too visibly visually appealing but it gives you a bit of an idea there of what's going on
um but yeah we kick off the process we load the pdf we associate it with acrobat reader and then
we based on anchors get the invoice number the invoice date and the invoice total okay
well i'm going to run this see what happens click save click run
oh there's a validation error looks like oh i got an underscore instead of a plus there silly me
okay save that again and then run opens up the file does the extract and then here you can see
down at the bottom the invoice number is a one a zero zero one the date is zero one twenty twenty
the total is one fifteen and that maps exactly to everything i've got in the form a1a 2020 and 115.
and there you go that's how you do anchor based extraction of data in your pdfs if you enjoyed
this tutorial i want you to head over to the serverside.com i've got all sorts of tutorials and
articles over there about enterprise software development if you're interested in my personal
antics you can follow me on twitter at cameronmcnz and subscribe on youtube