The UIKit framework provides a set of functions for generating PDF content using native drawing code. These functions let you create a graphics context that targets a PDF file or PDF data object. You can then create one or more PDF pages and draw into those pages using the same UIKit and Core Graphics drawing routines you use when drawing to the screen. When you are done, what you are left with is a PDF version of what you drew.
Create or generate a PDF file in C# and VB.NET with the GemBox.Document library. GemBox.Document is a C# / VB.NET component that enables developers to read, write, convert, and print document files (DOCX, DOC, PDF, HTML, XPS, RTF, and TXT) from.NET applications in a simple and efficient way without the need for Microsoft Word on either the developer or client machines. AJAX Business HTML 5 Oracle SQL Server Alexa Skills C# Internet & Web PHP TypeScript Algorithms in C# C# Corner Internet of Things Power BI UWP Android C# Strings Ionic Products Visual Studio Angular C, C++, MFC iOS Project Management WCF Architecture Career Advice Java Python Web Services Artificial Intelligence Cloud Java and.NET R Windows 10. Best C# API to create PDF [closed] Ask Question 137. My work uses Winnovative's PDF generator (We've used it mainly to convert HTML to PDF, but you can generate it other ways as well) share| improve this answer. Answered May 30 '10 at 7:38. Justin Justin. 2,783 2 18 34.
The overall drawing process is similar to the process for creating any other image (described in Drawing and Creating Images). It consists of the following steps:
Create a PDF context and push it onto the graphics stack (as described in Creating and Configuring the PDF Context).
Create a page (as described in Drawing PDF Pages).
Use UIKit or Core Graphics routines to draw the content of the page.
Add links if needed (as described in Creating Links Within Your PDF Content).
Repeat steps 2, 3, and 4 as needed.
End the PDF context (as described in Creating and Configuring the PDF Context) to pop the context from the graphics stack and, depending on how the context was created, either write the resulting data to the specified PDF file or store it into the specified
NSMutableData
object.
The following sections describe the PDF creation process in more detail using a simple example. For information about the functions you use to create PDF content, see UIKit Function Reference.
Creating and Configuring the PDF Context
You create a PDF graphics context using either the UIGraphicsBeginPDFContextToData
or UIGraphicsBeginPDFContextToFile
function. These functions create the graphics context and associate it with a destination for the PDF data. For the UIGraphicsBeginPDFContextToData
function, the destination is an NSMutableData
object that you provide. And for the UIGraphicsBeginPDFContextToFile
function, the destination is a file in your app’s home directory.
PDF documents organize their content using a page-based structure. This structure imposes two restrictions on any drawing you do:
Generate Pdf From Html String C#
There must be an open page before you issue any drawing commands.
You must specify the size of each page.
The functions you use to create a PDF graphics context allow you to specify a default page size but they do not automatically open a page. After creating your context, you must explicitly open a new page using either the UIGraphicsBeginPDFPage
or UIGraphicsBeginPDFPageWithInfo
function. And each time you want to create a new page, you must call one of these functions again to mark the start of the new page. The UIGraphicsBeginPDFPage
function creates a page using the default size, while the UIGraphicsBeginPDFPageWithInfo
function lets you customize the page size and other page attributes.
When you are done drawing, you close the PDF graphics context by calling the UIGraphicsEndPDFContext
. This function closes the last page and writes the PDF content to the file or data object you specified at creation time. This function also removes the PDF context from the graphics context stack.
Listing 4-1 shows the processing loop used by an app to create a PDF file from the text in a text view. Aside from three function calls to configure and manage the PDF context, most of the code is related to drawing the desired content. The textView
member variable points to the UITextView
object containing the desired text. The app uses the Core Text framework (and more specifically a CTFramesetterRef
data type) to handle the text layout and management on successive pages. The implementations for the custom renderPageWithTextRange:andFramesetter:
and drawPageNumber:
methods are shown in Listing 4-2.
Listing 4-1 Creating a new PDF file
Drawing PDF Pages
All PDF drawing must be done in the context of a page. Every PDF document has at least one page and many may have multiple pages. You specify the start of a new page by calling the UIGraphicsBeginPDFPage
or UIGraphicsBeginPDFPageWithInfo
function. These functions close the previous page (if one was open), create a new page, and prepare it for drawing. The UIGraphicsBeginPDFPage
creates the new page using the default size while the UIGraphicsBeginPDFPageWithInfo
function lets you customize the page size or customize other aspects of the PDF page.
After you create a page, all of your subsequent drawing commands are captured by the PDF graphics context and translated into PDF commands. You can draw anything you want in the page, including text, vector shapes, and images just as you would in your app’s custom views. The drawing commands you issue are captured by the PDF context and translated into PDF data. Placement of content on the the page is completely up to you but must take place within the bounding rectangle of the page.
Listing 4-2 shows two custom methods used to draw content inside a PDF page. The renderPageWithTextRange:andFramesetter:
method uses Core Text to create a text frame that fits the page and then lay out some text inside that frame. After laying out the text, it returns an updated range that reflects the end of the current page and the beginning of the next page. The drawPageNumber:
method uses the NSString
drawing capabilities to draw a page number string at the bottom of each PDF page.
Note: This code snippet makes use of the Core Text framework. Be sure to add it to your project.
Listing 4-2 Drawing page-based content
Creating Links Within Your PDF Content
Besides drawing content, you can also include links that take the user to another page in the same PDF file or to an external URL. To create a single link, you must add a source rectangle and a link destination to your PDF pages. One of the attributes of the link destination is a string that serves as the unique identifier for that link. To create a link to a specific destination, you specify the unique identifier for that destination when creating the source rectangle.
To add a new link destination to your PDF content, you use the UIGraphicsAddPDFContextDestinationAtPoint
function. This function associates a named destination with a specific point on the current page. When you want to link to that destination point, you use UIGraphicsSetPDFContextDestinationForRect
function to specify the source rectangle for the link. Figure 4-1 shows the relationship between these two function calls when applied to the pages of your PDF documents. Tapping on the rectangle surrounding the “see Chapter 1” text takes the user to the corresponding destination point, which is located at the top of Chapter 1.
In addition to creating links within a document, you can also use the UIGraphicsSetPDFContextURLForRect
function to create links to content located outside of the document. When using this function to create links, you specify the target URL and the source rectangle on the current page.
Generate Pdf From Html C# Free
Copyright © 2012 Apple Inc. All Rights Reserved. Terms of Use | Privacy Policy | Updated: 2012-09-19
I need to generate a high quality report based on information in a SQL Server database, and I want very explicit control of the layout and appearance from inside C#.
I have several choices that I know of that are already being used for various other reports at our company:
1) SQL Server's built in Reporting Services
2) Adobe Forms
3) Crystal Reports
This information I need as PDF directly parallels what is already being displayed in the user's web browser as HTML, so creating a print stylesheet and converting the browser body to PDF is an option as well.
So this creates option 4:
4) JavaScript convert HTML to PDF (my preference at this time)
Does anybody have a recommendation as to which approach I should take, or even better an alternative? All the choices seem pretty horrible.
Joel Coehoorn7 Answers
I've used iTextSharp with very good results. It is an open-source .NET port of a java library. It works really well for creating PDFsfrom scratch. Remember that editing PDFs will always be hacky with any library, because PDF is an output format, not a read-write format.
Provided your HTML is fairly clean (remove javascript postbacks, anchors, ...),the iText HtmlWorker
can convert HTML to PDF, if you prefer that route.
HTML to PDF in using iTextSharp:
Also here.
I have used two other PDF report libraries with great success; Active Reports and Telerik Reporting. Personally I prefer the latter when it comes to programmatic control of layout and such.
OstemarOstemarTake a look also at the DevExpress Reporting (non-free 3rd party tool):
MikhailMikhailI would suggest using .Net ReportViewer control in local mode (no report server required). It works in both webforms and winforms. You create a client-side report (.rdlc) file (which contains all the visuals as well as placement of data fields), link it up to the ReportViewer, and supply the data (DataTable or collection of objects, as long as the fields match, it doesn't matter). In client mode it supports exporting to pdf and excel (and Word too? don't remember). By default these done by a dropdown in the control itself however you can programmatically export to any of the supported formats as well. You'll end up with a byte array you can shove into a file stream.
Basically you get most of the good parts of SSRS without all of that backend complexity. There should be a ReportViewer folder in %programFiles%Microsoft Visual Studio 10.0ReportViewer - but versions exist for 2005 and 2008 as well. Check out http://gotreportviewer.com/
Yes, you should use the best tools to get the best solution. The best tool in this case probably is SSRS.
But that's just looking at the capabilities of the tool.Don't forget to look at your own capabilities!
My story: I know SQL, I know C#. (Both intermediate, I'm not a guru.)Then I lay my hands on SSRS. And burnt them, once, twice, etc. At the end, there was a nice result. So burning your fingers is not a wrong thing to do.But first try to pull your html through an html to pdf converter (demo version) and see if the result it serves your needs.
Currently I'm using both:
- SSRS for creating invoices, because amounts have to be transported from one page to the next
- Winnovative to generate documents that only need page numbers
I think the 4th option is the best. In this case you don't need to change either layout of the HTML page or a layout of PDF, if one of them has been changed.It is also more convenient making a nice design via HTML than programmatically via C# :)
Take a look at WebToPDF.NET which is a .NET component written in C# that converts HTML to PDF. The converter supports HTML 4.01, XHTML 1.0, XHTML 1.1 and CSS 2.1 including page breaks, forms and links. It passes all W3C tests (except BIDI).
zavolokaszavolokas