I need to generate PDF with the following style. [![enter image description here][1]][1]
What I mean by (style) is the design as the logo in that location, the green border at the end of the sheet, and also the size of the sheet, in my case I need letter, I would like to know how I could do this, and if I can use iTextSharp or it is necessary to use another library, said previous image is a certification which I generated with PHP FPDF, but now I need to do the same but in ASP.NET MVC5.
Any help or advice would be very helpful.
I already faced a similar situation. The solution I came up with was to create an image with the necessary formatting and then put it in the document like this:
How does it work?
We define the object
doc
as an iTextSharp document of sizeLETTER
(letter) and set the margins to a period (1f
because it is of typefloat
).Once we initialize the document (
doc.Open()
) we create an image object (from the namespaceiTextSharp.text.Image
) and give it the image path.Once we set the image to use, the line
ScalePercent
follows this tutorial for inserting images and then we place the image in position0,0
withSetAbsolutePosition
.Once we have done the above, we simply place the image in the document (
doc.Add(logo)
).To continue editing freely, we put a "new line" (
Chunk.NEWLINE
).It is important to note that in my case I needed a full background , while in your case you only need a top and a bottom. In that case you can use two images instead of one and place the upper one on top (you will have to find which position
x,y
is convenient for you) and the lower one on the bottom (in0,0
) since that will lighten the weight of the final file.Another thing: the image must be made at a resolution suitable for printing at 300
dpi
( What is DPI? ) for the scale to24
work; this must be (to cover the whole page)2550x3300
pixels. In my case, I used an image suitable for printing at 600dpi
horizontally, so my resolution was6600x5100
pixels and my scale was not a24
but12
. To generate my image I used a software called Inkscape but you can make it however you want.Serve the PDF in MVC
In order to deliver the PDF through a web server, I use the following code:
How does it work? file download
We set the path to save the file to the server , since that is where the file will be generated. If the folder does not exist, we create it.
We declare a
FileStream
that will serve to transmit the file. As a parameter, it receives the full path of the file (the folder that we defined earlier + the name of the file that we are going to generate) and we declare the operation, which in this case will be to create the file (FileMode.Create
) and we instantiate the PDF creator.We generate the PDF.
We declare the end of the document (
doc.Close()
andwriter.Close()
).As the return of the method (which is a
FileResult
) we give aFile
(from the namespaceSystem.Web.Mvc
) that takes as parameters the path of the file on the server, the type of file to be downloaded and optionally the name with which it should be downloaded.Other considerations
In this way you can place a text accommodated in a certain region of the document and respecting the line breaks that are necessary:
In this block I did the following:
Wanting to use an uncommon font (SourceSans) I downloaded it (from Font Squirrel , my personal recommendation) and located the file on my server.
In the first two lines I refer to the typography to be able to use it.
Following this tutorial , the paragraph you want is placed with the font you require (you can also see it in the official documentation ).
We declare a
PdfContentByte
andColumnText
(don't worry, it doesn't add to any table) (both from the namespaceiTextSharp.text.pdf
) and place that "column" containing the paragraph in the positionx, y, x', y'
we want.x, y
is the coordinates of the lower left corner of the paragraph, whilex', y'
is the coordinates of the upper right corner . This is not how much space the text will take up, but how much space it can take up at most.We end the collocation with the statement
Go()
.Execution
Taking all these concepts, your final code should look like this: