All right.
How was that?
Hopefully you were able to figure it out.
If not, don't worry.
Here's the solution.
What I'm going to do here is just keep the pie pdf import and start from scratch.
The first thing I want to do is I want to grab this super dope PDF that we're going to process.
So again, I'll just call it the template and this template will be the pi pdf.
We'll use the PDF File Reader object.
And I'll actually show you a little shortcut instead of just opening it the way we have been before.
We can just inside of here say, open.
And then say super dot pdf, which is what we're going to open and then open it.
With the second parameter being read binary.
Just like this.
We're going to create a file reader.
Now, you can do it this way or the way that I've shown you before.
This is just a cleaner syntax that I like using.
Next.
I also want to open up the watermark pdf, so I'm going to call it watermark.
And again, we're going to use the same thing.
So I'm going to say file reader and instead of opening the super pdf, I'll open the watermark pdf.
And then finally I want to be able to write my output and combine these two.
So I'm going to say output equals.
And we'll say pi.
PDF Dot pdf file writer.
To create our right object in memory.
Now from here.
This is the tricky part.
Ideally, we loop through the pages here of the template and add the watermark to it.
Now we are not going to know how many pages super dot PDF has.
I mean, we might want to change this program so that we use the command line here to give it a file.
But ideally, we want to make a program that is flexible, and no matter how many pages the given PDF
has, we can process it.
So what I'm going to do is use a for loop here and we can also create a function if we want.
But for now, because this is going to be just one block of code, we can just keep it without a function.
And I'm going to say for I in range, because we don't know how big this file is going to be.
And what I'm going to say is template dot get.
Numb pages.
Get nom pages is going to tell me how many pages the template has.
Remember, we've also seen the way of saying numb pages in a previous lesson, but we can just use get
numb pages just to show you something different.
So now it's going to get the number of pages.
It's going to create a range object out of it, and we're going to loop through it.
So if we have five pages, it's going to loop five times.
Now in here, what we can do is have a page object.
So we're going to say template, get page at the index that we're at.
So this is going to go through each time and it's going to get the first page the first time around,
the second page, the third page, the fourth page, so that we go through everything.
And that's going to be our page object.
Next.
Where we're going to do is say page dot merge page.
And this merge page is simply going to get the watermark dot get page two index of zero.
Because the watermark pdf only has that one page that has the watermark.
So I'm going to merge onto that page, the watermark.
Now notice here that I didn't use the PDF file merger and I'll show you why merge page works the way
it does.
When we finished a project.
But you can use either one.
The key here with my page is that I like the PDF File merger which combines the files one after another
merge page actually well merges the page like we want with the watermark.
Now this is something that you would have had to read through the documentation if you tried the exercise
on your own.
Again, one of those things that you just have to practice that muscle.
Finally, now that we have the page, we can say output, which is the PDF writer that we're going to
have.
Our output will be the ad page.
And we're going to add this page that we've merged.
And now finally, what we can do in here is say with open.
And we'll call this new PDF, let's call it watermarked.
Output.
Dot pdf.
We're going to say.
Right, binary.
And we're going to call it as.
So that now if we do output dot right and write this file, let's see what happens.
I'm going to run my program.
And there you go, nowhere.
So let's see if it worked.
We see that we have a watermarked output.
Now, again, this is gibberish.
But if we open this.
Check that out.
We have our PDF file.
We have the draft over here on the second page, third page and even the fourth page with the rotation.
How cool is that?
We just created a PDF Water Marker.
That was quite simple, right?
Again, if this is confusing and you're saying, hey, Andre, I didn't get this, am I a bad programmer?
No, you're not.
This is very library specific rate.
You're never going to get asked this exact syntax in an interview.
Instead, you read the documentation you want to merge pages.
So you google something like merge page on the official documentation of a library and you figure out,
hey, merge pages right here.
I can read what Merge page does and we see.
Oh yeah.
Merges the content streams of two pages into one.
All right, that's what we need.
So that's what we use.
And you experiment, go back and forth until you have a working project.
There you have it.
You can no work in Hollywood.
Congratulations.
I'll see you in the next video.
Bye bye.