How to fix Explorer.exe 100% working tutorial (stopped working,not responding) for windows 7 - Duration: 8:28. NIIT is a market leading, global learning outsourcing company which provides a comprehensive suite of managed training outsourcing services including Curriculum. Technology keeps you connected everywhere you go, helps you capture every moment & makes your life a bit easier; stay up-to-date with tips & tricks from eHow. Forum 11-Sep-2016 21:33. Looking for help with Outlook programming projects — VSTO, add-ins, VBA, custom Outlook forms, etc.? Manipulating PDF files with i. Text. Sharp and VB. NET 2. 01. 2Introduction. Recently, I had to make a VB. NET program that reads PDF file contents and replace it with customized text. VB. NET unfortunately doesn't have a built in PDF file reader object, so I had to make use of a third party's product called i. Text. Sharp. From the moment I started using it, I fell in love with it. With this article I will demonstrate how to use i. Text. Sharp with VB. NET to manipulate PDF files. Validating user input can be quite a pain, especially if you do not know what techniques and what namespaces are at your disposal. Because everyone has a. Tutorial: Outlook Sending E-mail Without Security Warning. Prevents the 'A program is trying to automatically send e-mail on your behalf' security message. PDF files. A detailed explanation of PDF files can be found here. Text. Sharp. A detailed explanation, and download of i. Text. Sharp can be found here. As you can see, i. Text. Sharp is mostly for C# and Java; hence this Visual Basic. NET article. I would suggest that you go through the documentation properly before proceeding with our project. I cannot do everything for you, you need to have some input as well. Our Project. Purpose. Our project's aim is to read from a PDF file, change some of the contents and then add a watermark to the PDF document's pages. Sound easy enough, yes, with the help of the i. Text. Sharp library you will see how simple it is. Design. Our project doesn't have much of a design. All we need is a progress bar and a button. Mine looks like Figure 1 : Figure 1 - Our Design. Code. Before we can jump in and code, you need to make sure that you have downloaded the i. Text. Sharp libraries. Once that is done, we need to add a reference to it by clicking Project- > Add Reference- > i. Text. Sharp. dll. Once we have the project reference set up, we need to reference the i. Text. Sharp libraries in our code. Add the following Imports statements: Imports System. IO 'Working With Files. Imports System. Text 'Working With Text. Text. Sharp Libraries. Imports i. Text. Sharp. Core PDF Text Functionalities. Imports i. Text. Sharp. PDF Content. Imports i. Text. Sharp. text. Content Parser. This imports all the needed capabilities for our little program. Add the following Sub Procedure: Public Sub Replace. PDFText(By. Val str. Search As String, By. Val sc. Case As String. Comparison, By. Val str. Source As String, By. Val str. Dest As String). Dim ps. Stamp As Pdf. Stamper = Nothing 'PDF Stamper Object. Dim pcb. Content As Pdf. Content. Byte = Nothing 'Read PDF Content. If File. Exists(str. Source) Then 'Check If File Exists. Dim pdf. File. Reader As New Pdf. Reader(str. Source) 'Read Our File. Stamp = New Pdf. Stamper(pdf. File. Reader, New File. Stream(str. Dest, File. Mode. Create)) 'Read Underlying Content of PDF File. Progress. Value = 0 'Set Progressbar Minimum Value. Progress. Maximum = pdf. File. Reader. Number. Of. Pages 'Set Progressbar Maximum Value. For int. Curr. Page As Integer = 1 To pdf. File. Reader. Number. Of. Pages 'Loop Through All Pages. Dim lte. Strategy As Loc. Text. Extraction. Strategy = New Loc. Text. Extraction. Strategy 'Read PDF File Content Blocks. Content = ps. Stamp. Get. Under. Content(int. Curr. Page) 'Look At Current Block. Determine Spacing of Block To See If It Matches Our Search String. Strategy. Undercontent. Character. Spacing = pcb. Content. Character. Spacing. lte. Strategy. Undercontent. Horizontal. Scaling = pcb. Content. Horizontal. Scaling. Trigger The Block Reading Process. Dim current. Text As String = Pdf. Text. Extractor. Get. Text. From. Page(pdf. File. Reader, int. Curr. Page, lte. Strategy). Determine Match(es). Dim lst. Matches As List(Of i. Text. Sharp. text. Rectangle) = lte. Strategy. Get. Text. Locations(str. Search, sc. Case). Dim pd. Layer As Pdf. Layer 'Create New Layer. Layer = New Pdf. Layer(! What a mouthful! Before you freak out; this code is actually not so bad. Let's have a look at it step by step: We create a Stamper object and a Content object. The Stamper object is to enable us to write our content onto the PDF file. The content object helps us to identify the appropriate content on the file that we need to replace. We determine if the PDF file exists, and read its underlying content. We also set up our Progress. Bar to compensate for the amount of pages in the PDF document. We commence our For Loop (to loop through each page) and create a Location. Text. Extraction. Strategy object. This object enables us to extract our desired text. This class also forms part of the i. Text. Sharp download. We need to add this file to our project - but we'll do that a bit later. Once we know what text we need, and what diameters the text use, we could continue to loop through all the pages until a match is found. We store each match and create a new layer for each match to be replaced. We then replace the found text with our new layer that is filled in order to highlight our change. The trick here is to replace the layer's exact dimensions. A PDF file does not work similar to a Word document where we could just find and replace text. Because each little word or phrase is actually a block, or a layer; so, to replace that particular block, we need the exact dimensions. If we do not have the exact dimensions, the layered text will not appear at the exact same place. Lastly, we include a call to the Add. PDFWatermark sub (which we will create now) to add a watermark on each page. The file that is written will be stored onto the C: \. Make sense now? Add the next Sub procedure: Public Shared Sub Add. PDFWatermark(By. Val str. Source As String, By. Val str. Dest As String, By. Val img. Source As String). Dim pdf. File. Reader As Pdf. Reader = Nothing 'Read File. Dim ps. Stamp As Pdf. Stamper = Nothing 'PDF Stamper Object. Dim img. Water. Mark As Image = Nothing 'Watermark Image. Dim pcb. Content As Pdf. Content. Byte = Nothing 'Read PDF Content. Dim rct. Rect As Rectangle = Nothing 'Create New Rectangle To Host Image. Dim sng. X, sng. Y As Single 'Page Dimensions. Dim int. Page. Count As Integer = 0 'Page Count. File. Reader = New Pdf. Reader(str. Source) 'Read File. Rect = pdf. File. Reader. Get. Page. Size. With. Rotation(1) 'Store Page Size. Stamp = New Pdf. Stamper(pdf. File. Reader, New System. IO. File. Stream(str. Dest, IO. File. Mode. Create)) 'Create new Stamper Object. Water. Mark = Image. Get. Instance(img. Source) 'Get Image To Be Used For The Watermark. If img. Water. Mark. Width > rct. Rect. Width Or. Else img. Water. Mark. Height > rct. Rect. Height Then 'Make Sure Image Can Fit On Page. Water. Mark. Scale. To. Fit(rct. Rect. Width, rct. Rect. Height). sng. X = (rct. Rect. Width - img. Water. Mark. Scaled. Width) / 2. sng. Y = (rct. Rect. Height - img. Water. Mark. Scaled. Height) / 2. Else 'Put In Center Of Page. X = (rct. Rect. Width - img. Water. Mark. Width) / 2. Y = (rct. Rect. Height - img. Water. Mark. Height) / 2. End If. img. Water. Mark. Set. Absolute. Position(sng. X, sng. Y). int. Page. Count = pdf. File. Reader. Number. Of. Pages() 'Apply To All Pages. For i As Integer = 1 To int. Page. Count. pcb. Content = ps. Stamp. Get. Under. Content(i). Content. Add. Image(img. Water. Mark). Next. Stamp. Close(). pdf. File. Reader. Close(). Catch ex As Exception. Throw ex 'Something Went Wrong. End Try. End Sub. This sub adds a watermark to each PDF page. You will notice that here, we almost do the same as we did in the previous sub. The only difference here is that we added an image to the undercontent of each page, instead of replacing textlayers. The last piece of code we need to add for this form is the call to the Replace. PDFText sub from our start button: Private Sub Start. Now, we will have two files. Obviously, this is just and example and it would be easy to combine all of the changes into one file. Location. Text. Extraction. Strategy. A full explanation can be found here. This file forms part of the i. Text. Sharp download I mentioned earlier. We need to add this file as is, to our project. Remember, we didn't create this file or logic, neither have I. But without this file we will not be able to identify the content strings we are looking for. This demonstrates the real power of i. Text. Sharp, and this is why i. Text. Sharp is my preferred choice when it comes to doing any PDF manipulation. Add a new class and add the following to it (in case you didn't download the i. Text. Sharp files at the location I've mentioned): Imports System. Imports System. Collections. Generic. Imports System. Text. Imports i. Text. Sharp. text. pdf. Imports i. Text. Sharp. This file is part of the i. Text project. Buying such a license is mandatory as soon as you. Text software without. Add the following Imports statement to your form's code: Imports PDF. Sadly, the i. Text. Sharp. dll is quite big, and unfortunately too big to include here; so you need to download it through the steps I have outlined for you. Conclusion. Thank you for reading my article. Obviously, I am only human (don't be so surprised!), and I can only do so much; but I couldn't have written this article if it wasn't for some help I received from a gentleman called jcis. Thank you - sometimes I bite off more than I can chew.. I hope you have enjoyed this article, and actually learned a thing or two from it. Now I'm off to see what new projects I can do and why VB. NET always seem to be second choice and C# first choice for real hardcore complicated projects..
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
December 2016
Categories |