PDFA, PDF/UA, ZUGFeRD DLL: (Optional) If you want to produce PDF/A or PDF/UA or ZUGFeRD PDFs rather than plain old PDFs, then you also need itextsharp.pdfa.dll from itextsharp-dll-pdfa.zip. I would highly recommend making your PDFs at least accessible for the blind and the visually impaired. Most developer just don't care about PDF/UA and that's problematic.
XML Worker: (Required) Since you want to convert HTML to PDF, you also need to download XML Worker: itextsharp.xmlworker-all-5.5.7.zip. In this zip, you'll find itextsharp-dll-xmlworker.zip. You need itextsharp.xmlworker.dll because that's where you'll find all the XML Worker classes.
The following code samples illustrate a number of basic and advanced features of iTextSharp. Included are examples of basic text layout and formatting, image insertion, page sizing, page labeling, metadata assignment, bullet lists, and linking.
The code starts by setting up the fonts that will be used within the PDF. In fact, these are used in most of the following code samples. You can see that various font faces, sizes, weights, and colors can be specified.
The first significant lines of the Build() method initialize the file (ScienceReport.pdf) that will be built. Next, margins and page size are set. Following that you see AddTitle(), AddCreator(), and AddKeyword() being called to add metadata to the file. An additional form of metadata is added by the CreateXmpMetadata() function, which will be explained later.
The next code sample shows two methods: AddPageWithBasicFormatting(), which is one of the methods used to add a page to the document, and AddParagraph(), which is a helper function used to add a paragraph to current page of the document.
The AddPageWithBasicFormatting() method illustrates the basic methods for adding text and images to a PDF document. It starts by calling the AddParagraph() helper method to add two short text strings to the current page. Notice that when adding a paragraph, you can specify the alignment and font to be used to render the paragraph contents. Next, a small JPG image is read from disk and inserted into the document. The method finishes up by adding two more paragraphs to the page.
The AddParagraph() method simplifies the process of adding a paragraph to a document by wrapping the basic actions that need to be performed to properly format a new paragraph. These actions include setting the alignment, font, and content. Notice that the content is not restricted to text. Anything that supports the iTextSharp.txt.IElement interface can form the content of a paragraph. This means that plain text, anchor tags, external links, and other objects can be used.
The AddPageWithInternalLinks() method, shown in the next code sample, demonstrates how to add links that reference other locations within the PDF document. If you are familiar with how to link to anchor tags in an HTML document, then you should understand what is happening in this example.
After the Anchor objects are created, a new page is added to the document, a paragraph of text is added to the page, and then the three Anchor objects are added to the page. Notice that our AddParagraph() helper method is used to add the Anchor objects.
The method in the next code sample, AddPageWithBulletList(), builds on the previous sample. It shows how to create the named anchors that were referenced by the anchors created in the previous example. In addition, it shows a new concept, a bulleted list.
After the List object has been instantiated, some additional customizations are made. These include a modification to the leading symbol of each list item (the default hyphen is changed to the bullet symbol) and the indentation of the entire list. Once these actions are complete, five ListItem objects are added to the list, and the list is added to the page.
The next sample is very similar to the earlier example that shows how to add links to locations within the PDF. This one shows a method that adds links to external resources. The key difference to note between the method shown here (AddPageWithExternalLinks()) and the one shown earlier (AddPageWithInternalLinks()) is that the Reference properties of the anchors are set to external URLs instead of to internal named anchors.
The key thing to notice here is that the modifications to the margins and page size are made before the new page is added. Modifications to margins and page size take affect when a new page is added; the current page is unaffected.
The last code sample shows a method that was called in the Build() method shown in the first code sample. CreateXmpMetadata() adds XMP metadata to a PDF document. You may be familiar with EXIF metadata that many digital cameras embed within photos. XMP is an XML- based metadata standard that is similar to EXIF. It can be embedded in many types of files, including PDFs. Some reference managers and PDF cataloging tools can take advantage of this metadata if is is available.
The method begins by creating an XmpSchema object and adding metadata to it. It then creates a XmpWriter object and writes the XmpSchema to a byte stream. Then (and this is important), the byte stream is shrunk to the size of the metadata that was placed into it. Once that is done, the byte stream is written to the PDF document.
A ready-to-run Visual Studio 2010 solution can be downloaded from here. The download includes all of the code samples discussed in this post. Many of them include more detail than what is shown here. If you want to skip straight to the output, an example of the PDF created by the ready-to-run code is available here.
How would you go about using iTextSharp to convert an existing webpage to a PDF? Is it simple to just use it for something like this? Id like to just add a button to my page (export to PDF) something along those lines..
If you are asking about the dimensions in pixels of an image after it is inserted into the PDF, then that is something you can control. By default, an image is not scaled when placed into the PDF. If you notice in the AddPageWithImage method of my example app, I am scaling the page to fit the image.
Is the correct iTextSharp.dll located in the folder with your .exe? I see that newer distributions of iTextSharp include two assemblies that appear to be optional add-ons: itextsharp.pdfa.dll and itextsharp.xtra.dll. If your project references either of those you will also need them in the folder with your .exe.
I searched for .NET Library for PDF and the one which have received good reviews from users is Aspose.PDF for .NET, has anyone tried or use this library. Please i need feedback before using this API.
I have not used the Aspose product myself, but have read good things about it. It seems a little pricey compared to iTextSharp (which is free). Depending on what you need out of the tool, the price may be worthwhile.
You are right Aspose Components are not free and i have tried their trial version to use some of the codes for my application and i was very satisfied with the results so i purchased one of their package and it is so worth while because you can ask for the code for a specific features on their support forum and their support team help you as fast as they can which i have found the best thing about this component. They not only provide new features every week for their components but also available to help their customer in every possible way they can.
3a8082e126