Solved

Add reference to itextsharp.dll in MS Access

Posted on 2009-05-14
4
5,628 Views
Last Modified: 2013-11-25
I am hoping to programatically merge PDF files and add bookmarks using itextsharp.dll or itextdonet.dll.

The problem I'm having is that I can't even add a reference to either dll.

Can I use this dll in VBA projects or do I need to create a VB6 project create then a DLL from VB6 and reference the VB6 dll in VBA projects?


Here are the attempts I have made to use the dlls after copying the dlls to C:\windows\system32\:

1) Register the dll
    regsvr32 C:\WINDOWS\system32\itextsharp.dll
   Error = C:\WINDOWS\system32\itextsharp.dll was loaded, but the DllRegisterServer entry point was not found. This file can not be registered.

2) From the Access VBA window Tools/References/Browse
    Chose the dll from C:\windows\system32\
Error = "Can't add a reference to the specified file"
0
Comment
Question by:barefoot_kiwi
4 Comments
 
LVL 2

Assisted Solution

by:michael1174
michael1174 earned 20 total points
ID: 24388129
It looks like this DLL was written in .NET, and can only be called from .NET.  I would suggest writing a C# console app using this DLL, and then from Access call the c# console app with command line arguments.
0
 
LVL 84
ID: 24389967
Or find a library or control that works in Access.
0
 

Accepted Solution

by:
barefoot_kiwi earned 0 total points
ID: 24445617
The DLL was written in C#. Since I have no idea what a console app in C# is I changed my perspective to solve the problem. The following code is a working prototype using web page in aspx that will be upgraded to accept dynamic file listings in future. The function combines a collection of pdf files into one pdf file and adds a bookmark (chapter) for each file that is added. It also appends the page number to the center of each page from page 2 to page n.

Web.config file

<add namespace="itextsharp"/>

<add namespace="itextsharp.text"/>

<add namespace="itextsharp.text.pdf"/>

<add namespace="itextsharp.text.pdf.PdfReader"/>

<add namespace="iTextSharp.text.rtf"/>

<add namespace="iTextSharp.text.rtf.HeaderFooter"/>
 
 

Default.aspx.vb
 

Partial Class _Default

    Inherits System.Web.UI.Page

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
 

        Dim strSource(3) As String

        Dim strDest As String = "C:\Inetpub\wwwroot\iText\docs\book.pdf"

        strSource(0) = "C:\data\Title Page.pdf"

        strSource(1) = "C:\data\Programs By Date.pdf"

        strSource(2) = "C:\data\Programs By Type.pdf"

        strSource(3) = "C:\data\LHM Programs.pdf"
 

        MergePdfFiles(strSource, strDest)

    End Sub
 

    Function MergePdfFiles(ByVal pdfFiles() As String, ByVal outputPath As String) As Boolean

        Dim result As Boolean = False

        Dim pdfCount As Integer = 0 'total input pdf file count

        Dim f As Integer = 0 'pointer to current input pdf file

        Dim fileName As String = String.Empty 'current input pdf filename

        Dim reader As iTextSharp.text.pdf.PdfReader = Nothing

        Dim pageCount As Integer = 0 'cureent input pdf page count

        Dim pdfDoc As iTextSharp.text.Document = Nothing 'the output pdf document

        Dim writer As PdfWriter = Nothing

        Dim cb As PdfContentByte = Nothing

        Dim strBookMark As String = ""

        'Declare a variable to hold the imported pages

        Dim page As PdfImportedPage = Nothing

        Dim rotation As Integer = 0

        'Declare a font to used for the bookmarks

        Dim bookmarkFont As iTextSharp.text.Font = iTextSharp.text.FontFactory.GetFont(iTextSharp.text.FontFactory.HELVETICA, _

        1, iTextSharp.text.Font.BOLD, iTextSharp.text.Color.WHITE)
 

        Try

            pdfCount = pdfFiles.Length
 

            If pdfCount > 1 Then

                'Open the 1st pad using PdfReader object

                fileName = pdfFiles(f)

                reader = New iTextSharp.text.pdf.PdfReader(fileName)

                'Get page count

                pageCount = reader.NumberOfPages

                'Instantiate an new instance of pdf document and set its margins. This will be the output pdf.

                'NOTE: bookmarks will be added at the 1st page of very original pdf file using its filename. The location

                'of this bookmark will be placed at the upper left hand corner of the document. So you'll need to adjust

                'the margin left and margin top values such that the bookmark won't overlay on the merged pdf page. The 

                'unit used is "points" (72 points = 1 inch), thus in this example, the bookmarks' location is at 1/4 inch from

               'The font colour is white so the user doesn't see it, change it using variable bookmarkFont 

                'left and 1/4 inch from top of the page.

                pdfDoc = New iTextSharp.text.Document(reader.GetPageSizeWithRotation(1), 18, 18, 18, 18)

                'Instantiate a PdfWriter that listens to the pdf document

                writer = PdfWriter.GetInstance(pdfDoc, New IO.FileStream(outputPath, IO.FileMode.Create))

                'Set metadata and open the document

                With pdfDoc

                    .AddAuthor("Your name here")

                    .AddCreationDate()

                    .AddCreator("Your program name here")

                    .AddSubject("Whatever subject you want to give it")

                    'Use the filename as the title... You can give it any title of course.

                    .AddTitle(IO.Path.GetFileNameWithoutExtension(outputPath))

                    'Add keywords, whatever keywords you want to attach to it

                    .AddKeywords("Report, Merged PDF, " & IO.Path.GetFileName(outputPath))

                    .Open()

                End With

                'Instantiate a PdfContentByte object

                cb = writer.DirectContent

                'Now loop thru the input pdfs

                While f < pdfCount

                    'Declare a page counter variable

                    Dim i As Integer = 0

                    'Loop thru the current input pdf's pages starting at page 1

                    While i < pageCount

                        i += 1

                        'Get the input page size

                        pdfDoc.SetPageSize(reader.GetPageSizeWithRotation(i))

                        'Create a new page on the output document

                        pdfDoc.NewPage()

                        'If it is the 1st page, we add bookmarks to the page

                        If i = 1 Then

                            'First create a paragraph using the filename as the heading

                            strBookMark = Replace(IO.Path.GetFileName(fileName), ".pdf", "")
 

                            Dim para As New iTextSharp.text.Paragraph(strBookMark, bookmarkFont)

                            'Then create a chapter from the above paragraph

                            Dim chpter As New iTextSharp.text.Chapter(para, f + 1)

                            'Finally add the chapter to the document

                            pdfDoc.Add(chpter)

                        End If

                        'Now we get the imported page

                        page = writer.GetImportedPage(reader, i)

                        'Read the imported page's rotation

                        rotation = reader.GetPageRotation(i)

                        'Then add the imported page to the PdfContentByte object as a template based on the page's rotation

                        If rotation = 90 Then

                            cb.AddTemplate(page, 0, -1.0F, 1.0F, 0, 0, reader.GetPageSizeWithRotation(i).Height)

                        ElseIf rotation = 270 Then

                            cb.AddTemplate(page, 0, 1.0F, -1.0F, 0, reader.GetPageSizeWithRotation(i).Width + 60, -30)

                        Else

                            cb.AddTemplate(page, 1.0F, 0, 0, 1.0F, 0, 0)

                        End If

                        Dim footPhraseImg As Phrase = New Phrase("Page ", FontFactory.GetFont(FontFactory.HELVETICA, 10, iTextSharp.text.Font.NORMAL))

                        Dim footer As HeaderFooter = New HeaderFooter(footPhraseImg, True)

                        footer.Border = iTextSharp.text.Rectangle.NO_BORDER

                        footer.Alignment = Element.ALIGN_RIGHT

                        footer.SetAlignment("CENTER")
 

                        pdfDoc.Footer = footer
 
 

                    End While

                    'Increment f and read the next input pdf file

                    f += 1

                    If f < pdfCount Then

                        fileName = pdfFiles(f)

                        reader = New iTextSharp.text.pdf.PdfReader(fileName)

                        pageCount = reader.NumberOfPages

                    End If

                End While

                'When all done, we close the documwent so that the pdfwriter object can write it to the output file

                pdfDoc.Close()

                result = True

            End If

        Catch ex As Exception

            System.Diagnostics.Debug.Print(ex.Message)

        End Try

        Return result

    End Function

End Class

Open in new window

0
 

Expert Comment

by:pbc83
ID: 33629942
I successfully implemented the solution proposed by michael1174, however I have one question.  Is one limited to 256 characters when calling the console app with arguments. In other words if I have a ton of fields to fill in on a given PDF, am I at all limited to the number I may pass to my console app as arguments?
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

The Adobe PDF proprietary file format is recognized as secure and formulated. But these PDF files are also prone to corruption and any external threat like virus attacks, improper storage can hit PDF file integrity.This type of damages can make cruc…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now