We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Import .CSS for creating .PDF

Hello,

I am using Aspose.Pdf 17.8 to create some .PDF’s from HTML.

The HTML has inline classes specified for example <div class="intro"> and intro is in a .css file within the same solution.

The .PDF generates fine, just without any styling, is there a way I can specify which .css file to use?

I’ve spent a long time searching for this but can’t find anything. If not I will have to manually move all the .css so it is inline which I’d like to avoid if possible.

Some further information: I am creating .pdf’s on the fly in server side code in ASP.NET and the .CSS files are in a folder in the save Visual Studio solution.

@rthomas95

Thanks for contacting support.

We are looking into the details of your requirement and will get back to you in a while. Please be patient.

@rthomas95

Thanks for your patience.

In case if you are creating PDF file on fly, through HTML string, please check following code snippet, with which PDF file can be generated with styling.

HtmlLoadOptions loadoptions = new HtmlLoadOptions(dataDir);
loadoptions.PageInfo.Height = Aspose.Pdf.PageSize.PageLetter.Height;
loadoptions.PageInfo.Width = Aspose.Pdf.PageSize.PageLetter.Width;
loadoptions.PageInfo.Margin.Left = 70;
loadoptions.PageInfo.Margin.Top = 30;
string HTMLcontent = @"<html>
                       <head>
                       <link rel='stylesheet' type='text/css' href='mystyle.css' />
                       </head>
                       <body>
                       <div class='intro'>
                            This is intro div.
                       </div>
                       <body>
                       </html>";
MemoryStream ms = new MemoryStream();
TextWriter tw = new StreamWriter(ms);
tw.Write(HTMLcontent);
tw.Flush();
ms.Seek(0, SeekOrigin.Begin);
Document doc = new Document(ms, loadoptions);
doc.Save("ExternalCSS.pdf");

In the above code snippet, dataDir is the complete path to the external CSS file (e.g C:\Files\ OR http://localhost/SomeFolder/). For your reference, I have attached CSS file, used in above code snippet and a generated PDF as well. In case of any further assistance, please feel free to contact us.

ExternalCSS.pdf (24.3 KB)
mystyle.zip (198 Bytes)