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.
Unfortunately, activation email could not send to your email. Please try again.

How to center the HTML content in the PDF

Thread ID:





82372 Jun 10,2009 05:17 PM Jul 6,2009 03:28 AM ASP.NET Web Forms (Classic) 3
Tags: PDF
Administrator [Syncfusion]
Asked On June 10, 2009 05:17 PM


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/ ).

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?



Geetha M [Syncfusion]
Replied On June 11, 2009 06:16 AM

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.


Administrator [Syncfusion]
Replied On June 11, 2009 10:40 AM

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!!



Sri Subhashini M [Syncfusion]
Replied On July 6, 2009 03:28 AM

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.



This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.