Adding the ability to convert HTML or webpages into Word documents to your application has never been easier with GrabzIt's ASP.NET API. However before you start remember that after calling the URLToDOCX, HTMLToDOCX or FileToDOCX methods the SaveAsync or SaveToAsync method must be called to actually create the DOCX.
Capturing webpages as DOCX converts the entire web page into a Word document that can consist of many pages. Only one parameter is required in order to convert a web page into a Word document or to convert HTML to DOCX as shown in the below examples.
grabzIt.URLToDOCX("https://www.tesla.com"); //Then call the SaveAsync or SaveToAsync method
grabzIt.HTMLToDOCX("<html><body><h1>Hello World!</h1></body></html>"); //Then call the SaveAsync or SaveToAsync method
grabzIt.FileToDOCX("example.html"); //Then call the SaveAsync or SaveToAsync method
You can pass a custom identifier to the DOCX methods as shown below, this value is then returned to your GrabzIt ASP.NET handler. For instance this custom identifier could be a database identifier, allowing a DOCX document to be associated with a particular database record.
//The client should be stored somewhere and reused! GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret", client); DOCXOptions options = new DOCXOptions(); options.CustomId = "123456"; grabzIt.URLToDOCX("https://www.tesla.com", options); //Then call the SaveAsync method await grabzIt.SaveAsync("http://www.example.com/Home/Handler");
//The client should be stored somewhere and reused! GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret", client); DOCXOptions options = new DOCXOptions(); options.CustomId = "123456"; grabzIt.HTMLToDOCX("<html><body><h1>Hello World!</h1></body></html>", options); //Then call the SaveAsync method await grabzIt.SaveAsync("http://www.example.com/Home/Handler");
//The client should be stored somewhere and reused! GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret", client); DOCXOptions options = new DOCXOptions(); options.CustomId = "123456"; grabzIt.FileToDOCX("example.html", options); //Then call the SaveAsync method await grabzIt.SaveAsync("http://www.example.com/Home/Handler");
To add a header or footer to a Word document you can request that you want to apply a particular template to the DOCX being generated. This template must be saved in advance and will specify the contents of the header and footer along with any special variables. In the example code below the user is using a template they created called "my template".
//The client should be stored somewhere and reused! GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret", client); DOCXOptions options = new DOCXOptions(); options.TemplateId = "my template"; grabzIt.URLToDOCX("https://www.tesla.com", options); //Then call the SaveAsync or SaveToAsync method await grabzIt.SaveToAsync("result.docx");
//The client should be stored somewhere and reused! GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret", client); DOCXOptions options = new DOCXOptions(); options.TemplateId = "my template"; grabzIt.HTMLToDOCX("<html><body><h1>Hello World!</h1></body></html>", options); //Then call the SaveAsync or SaveToAsync method await grabzIt.SaveToAsync("result.docx");
//The client should be stored somewhere and reused! GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret", client); DOCXOptions options = new DOCXOptions(); options.TemplateId = "my template"; grabzIt.FileToDOCX("example.html", options); //Then call the SaveAsync or SaveToAsync method await grabzIt.SaveToAsync("result.docx");
If you want to just convert a HTML element such as a div or span directly into a Word document you can with GrabzIt's ASP.NET library. You must pass the CSS selector of the HTML element you wish to convert to the TargetElement
property of DOCXOptions class.
... <span id="Article"> <p>This is the content I am interested in.</p> <img src="myimage.jpg"> </span> ...
In this example, we wish to capture all the content in the span which has the id of Article
, therefore we pass this to GrabzIt API as shown below.
//The client should be stored somewhere and reused! GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret", client); DOCXOptions options = new DOCXOptions(); options.TargetElement = "#Article"; grabzIt.URLToDOCX("http://www.bbc.co.uk/news", options); //Then call the SaveAsync or SaveToAsync method await grabzIt.SaveToAsync("result.docx");