Adding the ability to convert HTML or webpages into Word documents to your application has never been easier with GrabzIt's Java API. However before you start remember that after calling the URLToDOCX, HTMLToDOCX or FileToDOCX methods the Save or SaveTo 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 Save or SaveTo method
grabzIt.HTMLToDOCX("<html><body><h1>Hello World!</h1></body></html>"); //Then call the Save or SaveTo method
grabzIt.FileToDOCX("example.html"); //Then call the Save or SaveTo method
You can pass a custom identifier to the DOCX methods as shown below, this value is then returned to your GrabzIt Java handler. For instance this custom identifier could be a database identifier, allowing a DOCX document to be associated with a particular database record.
GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret"); DOCXOptions options = new DOCXOptions(); options.setCustomId("123456"); grabzIt.URLToDOCX("https://www.tesla.com", options); //Then call the Save method grabzIt.Save("http://www.example.com/handler");
GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret"); DOCXOptions options = new DOCXOptions(); options.setCustomId("123456"); grabzIt.HTMLToDOCX("<html><body><h1>Hello World!</h1></body></html>", options); //Then call the Save method grabzIt.Save("http://www.example.com/handler");
GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret"); DOCXOptions options = new DOCXOptions(); options.setCustomId("123456"); grabzIt.FileToDOCX("example.html", options); //Then call the Save method grabzIt.Save("http://www.example.com/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".
GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret"); DOCXOptions options = new DOCXOptions(); options.setTemplateId("my template"); grabzIt.URLToDOCX("https://www.tesla.com", options); //Then call the Save or SaveTo method grabzIt.SaveTo("result.docx");
GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret"); DOCXOptions options = new DOCXOptions(); options.setTemplateId("my template"); grabzIt.HTMLToDOCX("<html><body><h1>Hello World!</h1></body></html>", options); //Then call the Save or SaveTo method grabzIt.SaveTo("result.docx");
GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret"); DOCXOptions options = new DOCXOptions(); options.setTemplateId("my template"); grabzIt.FileToDOCX("example.html", options); //Then call the Save or SaveTo method grabzIt.SaveTo("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 Java library. You must pass the CSS selector of the HTML element you wish to convert to the setTargetElement
method 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.
GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret"); DOCXOptions options = new DOCXOptions(); options.setTargetElement("#Article"); grabzIt.URLToDOCX("http://www.bbc.co.uk/news", options); //Then call the Save or SaveTo method grabzIt.SaveTo("result.docx");