We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

How to center the HTML content in the PDF

Hello,

I am a new user of ASP.NET/Essential PDF.
I converted one of my HTML page to PDF (similar to the sample as you provided at http://samples.syncfusion.com/ASPNET/7.2.0.20/web/Pdf.Web/samples/3.5/Product%20Showcase/HtmlToPdf/cs/Default.aspx ).

My HTML form is basically made up of a single a table, but this table shows up left aligned in the PDF after conversion. How can I set the horizontal alignment to centered?


Thanks,

Bin

3 Replies

GM Geetha M Syncfusion Team June 11, 2009 10:16 AM UTC

Hi Bin,

Thank you for your interest in Syncfusion products.

Currently it is not possible to align HTML content in the page. But you can workaround this by calculating the page width. Please refer to the code snippet in the link below:

PdfMetafileLayoutFormat format = new PdfMetafileLayoutFormat();
format.Break = PdfLayoutBreakType.FitPage;
format.Layout = PdfLayoutType.Paginate;
float pos = doc.PageSettings.Width / 3;
mf.Draw(page, new PointF(pos, 50), format);

Please try this and let me know if you have any questions.

Regards,
Geetha


AD Administrator Syncfusion Team June 11, 2009 02:40 PM UTC

Hello Geetha,

Thank you for answering me so quickly. I tried your suggestion but it did not work.
It took 1/3 of the page width so that the HTML content is aligned to right and part of the table is lost.

Also, I was using HtmlToPdfResult.Render instead of PdfMetafile.draw. What is the difference between them? I am attaching my function (in VB.NET).

Thanks for your help!!

Bin




GerneratePDFSample_2df2830e.txt


SS Sri Subhashini M Syncfusion Team July 6, 2009 07:28 AM UTC

Hi Bin,

I regret for the long delay in getting back with you.

I am able to reproduce the issue that you have mentioned here. But this can be solved by using ConvertToImage method in HtmlConverter class.

Kindly try the below code snippet to draw the table completely,
Dim doc As PdfDocument = New PdfDocument()
doc.PageSettings.Margins.All = 10
doc.PageSettings.Width = 595f
'Add a page
Dim newpage As PdfPage = doc.Pages.Add()
Dim pageSize As SizeF = newpage.GetClientSize()
Dim convertor As PdfUnitConvertor = New PdfUnitConvertor()
Using html As HtmlConverter = New HtmlConverter()
'setting Javascript
html.EnableJavaScript = True
'setting pagebreak
html.AutoDetectPageBreak = True
'setting hyperlink
html.EnableHyperlinks = False
Using img As System.Drawing.Image = html.ConvertToImage(TargetURl, ImageType.Metafile, CInt(Fix(400)), -1, AspectRatio.KeepWidth)
Dim metafile As PdfMetafile = CType(PdfImage.FromImage(img), PdfMetafile)
Dim format As PdfMetafileLayoutFormat = New PdfMetafileLayoutFormat()
format.Break = PdfLayoutBreakType.FitPage
format.Layout = PdfLayoutType.Paginate
format.SplitTextLines = False
Dim pos As Single = doc.PageSettings.Width / 3
metafile.Draw(newpage, New RectangleF(pos, 0, pageSize.Width, pageSize.Height), format)
End Using
End Using
doc.Save("EPSelectProposal.pdf", Response, HttpReadType.Open)

The methods HtmlToPdfResult.Render and PdfMetafile.Draw are used to render the image in the specified PDF page. Using this method, we can`t specify the rendering image position and its size. It renders the whole image according to the page size. Using the PdfMetafile.Draw method, user can specify the position and the image size.

Please let me know if you need any information.

Regards,
Suba

Loader.
Live Chat Icon For mobile
Up arrow icon