diff --git a/docio/generateorderdetails/CustomerDetails.java b/docio/generateorderdetails/CustomerDetails.java new file mode 100644 index 0000000..4f733b2 --- /dev/null +++ b/docio/generateorderdetails/CustomerDetails.java @@ -0,0 +1,82 @@ +package generateorderdetails; + +import com.syncfusion.javahelper.system.collections.generic.ListSupport; + +public class CustomerDetails { + private String m_customerName; + private String m_address; + private String m_city; + private String m_postalCode; + private String m_country; + private String m_phone; + private ListSupport m_orders; + + public String getCustomerName() throws Exception { + return m_customerName; + } + + public String setCustomerName(String value) throws Exception { + m_customerName = value; + return value; + } + + public String getAddress() throws Exception { + return m_address; + } + + public String setAddress(String value) throws Exception { + m_address = value; + return value; + } + + public String getCity() throws Exception { + return m_city; + } + + public String setCity(String value) throws Exception { + m_city = value; + return value; + } + + public String getPostalCode() throws Exception { + return m_postalCode; + } + + public String setPostalCode(String value) throws Exception { + m_postalCode = value; + return value; + } + + public String getCountry() throws Exception { + return m_country; + } + + public String setCountry(String value) throws Exception { + m_country = value; + return value; + } + + public String getPhone() throws Exception { + return m_phone; + } + + public String setPhone(String value) throws Exception { + m_phone = value; + return value; + } + + public ListSupport getOrders() throws Exception { + if (m_orders == null) + m_orders = new ListSupport(OrderDetails.class); + return m_orders; + } + + public ListSupport setOrders(ListSupport value) throws Exception { + m_orders = value; + return value; + } + + public CustomerDetails() throws Exception { + m_orders = new ListSupport(OrderDetails.class); + } +} diff --git a/docio/generateorderdetails/GenerateOrderDetails.java b/docio/generateorderdetails/GenerateOrderDetails.java new file mode 100644 index 0000000..98a7d6e --- /dev/null +++ b/docio/generateorderdetails/GenerateOrderDetails.java @@ -0,0 +1,326 @@ +package generateorderdetails; + +import java.io.FileInputStream; +import com.syncfusion.docio.*; +import com.syncfusion.javahelper.system.RefSupport; +import com.syncfusion.javahelper.system.StringSupport; +import com.syncfusion.javahelper.system.collections.DictionaryEntrySupport; +import com.syncfusion.javahelper.system.collections.generic.DictionarySupport; +import com.syncfusion.javahelper.system.collections.generic.IDictionarySupport; +import com.syncfusion.javahelper.system.collections.generic.IEnumeratorSupport; +import com.syncfusion.javahelper.system.collections.generic.ListSupport; +import com.syncfusion.javahelper.system.io.*; +import com.syncfusion.javahelper.system.xml.XmlDocumentSupport; +import com.syncfusion.javahelper.system.xml.XmlNodeSupport; +import com.syncfusion.javahelper.system.xml.XmlNodeType; +import com.syncfusion.javahelper.system.xml.XmlReaderSupport; +import java.io.File; + +public class GenerateOrderDetails +{ + + public static void main(String[] args) throws Exception + { + //Creates new Word document instance for Word processing. + WordDocument document = new WordDocument(); + //Opens the template Word document. + String basePath = getDataDir("Template.docx"); + document.open(basePath, FormatType.Docx); + //Retrieves the mail merge data. + MailMergeDataTable dataTable = getMailMergeDataTable(); + //Executes nested Mail merge using implicit relational data. + document.getMailMerge().executeNestedGroup(dataTable); + //Removes empty page at the end of Word document. + removeEmptyPage(document); + //Save the document in the given name and format. + document.save("Sample.docx", FormatType.Docx); + //Release the resources occupied by the WordDocument instance. + document.close(); + System.out.println("Word document generated successfully."); + } + + /** + * + * Gets the mail merge data table. + * + * @return + */ + private static MailMergeDataTable getMailMergeDataTable() throws Exception + { + //Gets the customer details implicit as "IEnumerable" collection. + ListSupport customers = new ListSupport(CustomerDetails.class); + + FileStreamSupport stream = new FileStreamSupport(getDataDir("CustomerDetails.xml"), FileMode.OpenOrCreate); + + XmlReaderSupport reader = XmlReaderSupport.create(stream); + + while (reader.getNodeType().getEnumValue() != XmlNodeType.Element.getEnumValue()) + reader.read(); + + reader.read(); + + while (reader.getNodeType().getEnumValue() == XmlNodeType.Whitespace.getEnumValue()) + reader.read(); + + while (!(reader.getLocalName() == "CustomerDetails")) + { + if (reader.getNodeType().getEnumValue() == XmlNodeType.Element.getEnumValue()) + { + switch ((reader.getLocalName()) == null ? "string_null_value" : (reader.getLocalName())) + { + case "Customers": + customers.add(getCustomer(reader)); + break; + } + } + else + { + reader.read(); + if ((reader.getLocalName() == "CustomerDetails") + && reader.getNodeType().getEnumValue() == XmlNodeType.EndElement.getEnumValue()) + break; + } + } + + reader.close(); + stream.close(); + + // Creates an instance of "MailMergeDataTable" by specifying mail merge group + // name and "IEnumerable" collection. + MailMergeDataTable dataTable = new MailMergeDataTable("Customers", customers); + return dataTable; + } + + /** + * + * Gets the customer. + * + * @param reader The reader. + * @return + */ + private static CustomerDetails getCustomer(XmlReaderSupport reader) throws Exception + { + while (reader.getNodeType().getEnumValue() != XmlNodeType.Element.getEnumValue()) + reader.read(); + + reader.read(); + + while (reader.getNodeType().getEnumValue() == XmlNodeType.Whitespace.getEnumValue()) + reader.read(); + + CustomerDetails customer = new CustomerDetails(); + + while (!(reader.getLocalName() == "Customers")) + { + if (reader.getNodeType().getEnumValue() == XmlNodeType.Element.getEnumValue()) + { + switch ((reader.getLocalName()) == null ? "string_null_value" : (reader.getLocalName())) + { + case "CustomerName": + customer.setCustomerName(reader.readContentAsString()); + break; + case "Address": + customer.setAddress(reader.readContentAsString()); + break; + case "City": + customer.setCity(reader.readContentAsString()); + break; + case "PostalCode": + customer.setPostalCode(reader.readContentAsString()); + break; + case "Country": + customer.setCountry(reader.readContentAsString()); + break; + case "Phone": + customer.setPhone(reader.readContentAsString()); + break; + case "Orders": + customer.getOrders().add(getOrder(reader)); + break; + default: + reader.skip(); + break; + } + } + else + { + reader.read(); + if ((reader.getLocalName() == "Customers") + && reader.getNodeType().getEnumValue() == XmlNodeType.EndElement.getEnumValue()) + break; + } + } + return customer; + } + + /** + * + * Gets the order. + * + * @param reader The reader. + * @return + */ + private static OrderDetails getOrder(XmlReaderSupport reader) throws Exception + { + while (reader.getNodeType().getEnumValue() != XmlNodeType.Element.getEnumValue()) + reader.read(); + + reader.read(); + + while (reader.getNodeType().getEnumValue() == XmlNodeType.Whitespace.getEnumValue()) + reader.read(); + + OrderDetails order = new OrderDetails(); + + while (!(reader.getLocalName() == "Orders")) + { + if (reader.getNodeType().getEnumValue() == XmlNodeType.Element.getEnumValue()) + { + switch ((reader.getLocalName()) == null ? "string_null_value" : (reader.getLocalName())) + { + case "CustomerName": + order.setCustomerName(reader.readContentAsString()); + break; + case "OrderID": + order.setOrderID(reader.readContentAsString()); + break; + case "OrderDate": + order.setOrderDate(reader.readContentAsString()); + break; + case "ExpectedDeliveryDate": + order.setExpectedDeliveryDate(reader.readContentAsString()); + break; + case "ShippedDate": + order.setShippedDate(reader.readContentAsString()); + break; + case "Products": + order.getProducts().add(getProduct(reader)); + break; + } + reader.read(); + } + else + { + reader.read(); + if ((reader.getLocalName() == "Orders") + && reader.getNodeType().getEnumValue() == XmlNodeType.EndElement.getEnumValue()) + break; + } + } + return order; + } + + /** + * + * Gets the product. + * + * @param reader The reader. + * @return + */ + private static ProductDetails getProduct(XmlReaderSupport reader) throws Exception + { + while (reader.getNodeType().getEnumValue() != XmlNodeType.Element.getEnumValue()) + reader.read(); + + reader.read(); + + while (reader.getNodeType().getEnumValue() == XmlNodeType.Whitespace.getEnumValue()) + reader.read(); + + ProductDetails product = new ProductDetails(); + + while (!(reader.getLocalName() == "Products")) + { + if (reader.getNodeType().getEnumValue() != XmlNodeType.EndElement.getEnumValue()) + { + switch ((reader.getLocalName()) == null ? "string_null_value" : (reader.getLocalName())) + { + case "OrderID": + product.setOrderID(reader.readContentAsString()); + break; + case "Product": + product.setProduct(reader.readContentAsString()); + break; + case "UnitPrice": + product.setUnitPrice(reader.readContentAsString()); + break; + case "Quantity": + product.setQuantity(reader.readContentAsString()); + break; + } + reader.read(); + } + else + { + reader.read(); + if ((reader.getLocalName() == "Products") + && reader.getNodeType().getEnumValue() == XmlNodeType.EndElement.getEnumValue()) + break; + } + } + return product; + } + + /** + * + * Removes empty paragraphs from the end of Word document. + * + * @param document The Word document + */ + private static void removeEmptyPage(WordDocument document) throws Exception + { + WTextBody textBody = document.getLastSection().getBody(); + + //A flag to determine any renderable item found in the Word document. + boolean IsRenderableItem = false; + //Iterates text body items. + for (int itemIndex = textBody.getChildEntities().getCount() - 1; itemIndex >= 0 && !IsRenderableItem; itemIndex--) + { + //Check item is empty paragraph and removes it. + if (textBody.getChildEntities().get(itemIndex) instanceof WParagraph) + { + WParagraph paragraph = (WParagraph) textBody.getChildEntities().get(itemIndex); + //Iterates into paragraph + for (int pIndex = paragraph.getItems().getCount() - 1; pIndex >= 0; pIndex--) + { + ParagraphItem paragraphItem = paragraph.getItems().get(pIndex); + + //If page break found in end of document, then remove it to preserve contents in same page + if ((paragraphItem instanceof Break + && ((Break) paragraphItem).getBreakType().getEnumValue() == BreakType.PageBreak.getEnumValue())) + { + paragraph.getItems().removeAt(pIndex); + } + //Check paragraph contains any renderable items. + else if (!(paragraphItem instanceof BookmarkStart || paragraphItem instanceof BookmarkEnd)) + { + IsRenderableItem = true; + //Found renderable item and break the iteration. + break; + } + } + //Remove empty paragraph and the paragraph with bookmarks only + if (paragraph.getItems().getCount() == 0 || !IsRenderableItem) + textBody.getChildEntities().removeAt(itemIndex); + } + } + } + + /** + * Get the file path + * + * @param path specifies the file path + */ + public static String getDataDir(String path) + { + File dir = new File(System.getProperty("user.dir")); + if (!(dir.toString().endsWith("samples"))) + dir = dir.getParentFile(); + dir = new File(dir, "resources"); + dir = new File(dir, path); + if (dir.isDirectory() == false) + dir.mkdir(); + return dir.toString(); + } + +} diff --git a/docio/generateorderdetails/OrderDetails.java b/docio/generateorderdetails/OrderDetails.java new file mode 100644 index 0000000..fddb442 --- /dev/null +++ b/docio/generateorderdetails/OrderDetails.java @@ -0,0 +1,72 @@ +package generateorderdetails; + +import com.syncfusion.javahelper.system.collections.generic.ListSupport; + +public class OrderDetails { + private String m_customerName; + private String m_orderID; + private String m_orderDate; + private String m_expectedDeliveryDate; + private String m_shippedDate; + private ListSupport m_products; + + public String getCustomerName() throws Exception { + return m_customerName; + } + + public String setCustomerName(String value) throws Exception { + m_customerName = value; + return value; + } + + public String getOrderID() throws Exception { + return m_orderID; + } + + public String setOrderID(String value) throws Exception { + m_orderID = value; + return value; + } + + public String getOrderDate() throws Exception { + return m_orderDate; + } + + public String setOrderDate(String value) throws Exception { + m_orderDate = value; + return value; + } + + public String getExpectedDeliveryDate() throws Exception { + return m_expectedDeliveryDate; + } + + public String setExpectedDeliveryDate(String value) throws Exception { + m_expectedDeliveryDate = value; + return value; + } + + public String getShippedDate() throws Exception { + return m_shippedDate; + } + + public String setShippedDate(String value) throws Exception { + m_shippedDate = value; + return value; + } + + public ListSupport getProducts() throws Exception { + if (m_products == null) + m_products = new ListSupport(ProductDetails.class); + return m_products; + } + + public ListSupport setProducts(ListSupport value) throws Exception { + m_products = value; + return value; + } + + public OrderDetails() throws Exception { + m_products = new ListSupport(ProductDetails.class); + } +} diff --git a/docio/generateorderdetails/ProductDetails.java b/docio/generateorderdetails/ProductDetails.java new file mode 100644 index 0000000..da6200f --- /dev/null +++ b/docio/generateorderdetails/ProductDetails.java @@ -0,0 +1,44 @@ +package generateorderdetails; + +public class ProductDetails { + private String m_orderID; + private String m_product; + private String m_unitPrice; + private String m_quantity; + + public String getOrderID() throws Exception { + return m_orderID; + } + + public String setOrderID(String value) throws Exception { + m_orderID = value; + return value; + } + + public String getProduct() throws Exception { + return m_product; + } + + public String setProduct(String value) throws Exception { + m_product = value; + return value; + } + + public String getUnitPrice() throws Exception { + return m_unitPrice; + } + + public String setUnitPrice(String value) throws Exception { + m_unitPrice = value; + return value; + } + + public String getQuantity() throws Exception { + return m_quantity; + } + + public String setQuantity(String value) throws Exception { + m_quantity = value; + return value; + } +} diff --git a/docio/gettingstarted/GettingStarted.java b/docio/gettingstarted/GettingStarted.java new file mode 100644 index 0000000..2f08feb --- /dev/null +++ b/docio/gettingstarted/GettingStarted.java @@ -0,0 +1,135 @@ +package gettingstarted; + +import com.syncfusion.docio.FormatType; +import java.io.FileInputStream; +import java.io.File; +import com.syncfusion.docio.*; + +public class GettingStarted { + + public static void main(String[] args) throws Exception + { + //Create an instance of WordDocument Instance (Empty Word Document). + WordDocument document = new WordDocument(); + //Add a new section into the Word document. + IWSection section = document.addSection(); + //Specifies the page margins. + section.getPageSetup().getMargins().setAll(50f); + //Add a new simple paragraph into the section. + IWParagraph firstParagraph = section.addParagraph(); + //Set the paragraph's horizontal alignment as justify. + firstParagraph.getParagraphFormat().setHorizontalAlignment(HorizontalAlignment.Justify); + //Add a text range into the paragraph. + IWTextRange firstTextRange = firstParagraph.appendText("AdventureWorks Cycles,"); + //set the font formatting of the text range. + firstTextRange.getCharacterFormat().setBoldBidi(true); + firstTextRange.getCharacterFormat().setFontName("Calibri"); + firstTextRange.getCharacterFormat().setFontSize(14) ; + //Add another text range into the paragraph. + IWTextRange secondTextRange = firstParagraph.appendText(" the fictitious company on which the AdventureWorks sample databases are based, is a large, multinational manufacturing company."); + //set the font formatting of the text range. + secondTextRange.getCharacterFormat().setFontName("Calibri"); + secondTextRange.getCharacterFormat().setFontSize(11); + //Add another paragraph and aligns it as a center. + IWParagraph paragraph = section.addParagraph(); + paragraph.getParagraphFormat().setHorizontalAlignment(HorizontalAlignment.Center); + //Set after spacing for paragraph. + paragraph.getParagraphFormat().setAfterSpacing(6); + //Add a picture into the paragraph. + IWPicture picture = paragraph.appendPicture(new FileInputStream(getDataDir("Image.png"))); + //Specify the size of the picture. + picture.setHeight(86); + picture.setWidth(81); + IWTable table = section.addTable(); + //Create the specified number of rows and columns. + table.resetCells(2,2); + //Access the instance of the cell (first row, first cell). + WTableCell firstCell = table.getRows().get(0).getCells().get(0); + //Specifies the width of the cell. + firstCell.setWidth(150); + //Add a paragraph into the cell; a cell must have atleast 1 paragraph. + paragraph=firstCell.addParagraph(); + IWTextRange textRange = paragraph.appendText("Profile picture"); + textRange.getCharacterFormat().setBold(true); + //Access the instance of cell (first row, second cell). + WTableCell secondCell = table.getRows().get(0).getCells().get(1); + secondCell.setWidth(330); + paragraph=secondCell.addParagraph(); + //Add text to the paragraph. + textRange=paragraph.appendText("Description"); + textRange.getCharacterFormat().setBold(true); + firstCell=table.getRows().get(1).getCells().get(0); + firstCell.setWidth(150); + //Add image to the paragraph. + paragraph=firstCell.addParagraph(); + //Set after spacing for paragraph. + paragraph.getParagraphFormat().setAfterSpacing(6); + IWPicture profilePicture = paragraph.appendPicture(new FileInputStream(getDataDir("Image.png"))); + //Set the height and width for the image. + profilePicture.setHeight(98); + profilePicture.setWidth(95); + //Access the instance of cell (second row, second cell) and adds text. + secondCell=table.getRows().get(1).getCells().get(1); + secondCell.setWidth(330); + paragraph=secondCell.addParagraph(); + textRange=paragraph.appendText("AdventureWorks Cycles, the fictitious company on which the AdventureWorks sample databases are based, is a large, multinational manufacturing company."); + paragraph=section.addParagraph(); + //Set before spacing for paragraph. + paragraph.getParagraphFormat().setBeforeSpacing(6); + paragraph.appendText("Level 0"); + //Apply the default numbered list formats. + paragraph.getListFormat().applyDefNumberedStyle(); + paragraph=section.addParagraph(); + paragraph.appendText("Level 1"); + //Specify the list format to continue from the last list. + paragraph.getListFormat().continueListNumbering(); + //Increment the list level. + paragraph.getListFormat().increaseIndentLevel(); + paragraph=section.addParagraph(); + paragraph.appendText("Level 0"); + //Decrement the list level. + paragraph.getListFormat().continueListNumbering(); + paragraph.getListFormat().decreaseIndentLevel(); + //Write the default bulleted list. + section.addParagraph(); + paragraph=section.addParagraph(); + paragraph.appendText("Level 0"); + //Apply the default bulleted list formats. + paragraph.getListFormat().applyDefBulletStyle(); + paragraph=section.addParagraph(); + paragraph.appendText("Level 1"); + //Specify the list format to continue from the last list. + paragraph.getListFormat().continueListNumbering(); + //Increment the list level. + paragraph.getListFormat().increaseIndentLevel(); + paragraph=section.addParagraph(); + paragraph.appendText("Level 0"); + //Specify the list format to continue from the last list. + paragraph.getListFormat().continueListNumbering(); + //Decrement the list level. + paragraph.getListFormat().decreaseIndentLevel(); + section.addParagraph(); + //Save the document in the given name and format. + document.save("Sample.docx",FormatType.Docx); + //Release the resources occupied by the WordDocument instance. + document.close(); + System.out.println("Word document generated successfully."); + } + + /** + * Get the file path + * + * @param path specifies the file path + */ + public static String getDataDir(String path) + { + File dir = new File(System.getProperty("user.dir")); + if(!(dir.toString().endsWith("samples"))) + dir = dir.getParentFile(); + dir = new File(dir, "resources"); + dir = new File(dir, path); + if (dir.isDirectory() == false) + dir.mkdir(); + return dir.toString(); + } +} diff --git a/docio/resources/CustomerDetails.xml b/docio/resources/CustomerDetails.xml new file mode 100644 index 0000000..aa14c9a --- /dev/null +++ b/docio/resources/CustomerDetails.xml @@ -0,0 +1,251 @@ + + + + Paul Henriot +
59 rue de l'Abbaye
+ Reims + 51100 + France + 26.47.15.10 + + Paul Henriot + 10248 + 07/04/2018 + 08/01/2018 + 07/16/2018 + + 10248 + Queso Cabrales + $14.00 + 12 + + + 10248 + Singaporean Hokkien Fried Mee + $9.80 + 10 + + + 10248 + Mozzarella di Giovanni + $34.80 + 5 + + + + Paul Henriot + 10274 + 08/06/2018 + 09/03/2018 + 08/16/2018 + + 10274 + Fløtemysost + $17.20 + 20 + + + 10274 + Mozzarella di Giovanni + $27.80 + 7 + + + + Paul Henriot + 10295 + 09/02/2018 + 09/30/2018 + 09/10/2018 + + 10295 + Gnocchi di nonna Alice + $30.40 + 4 + + +
+ + Maria Anders +
Obere Str. 57
+ Berlin + 12209 + Germany + 030-0074321 + + Maria Anders + 10643 + 08/25/2018 + 09/22/2018 + 09/02/2018 + + 10643 + Rössle Sauerkraut + $45.60 + 15 + + + 10643 + Chartreuse verte + $18.00 + 21 + + + 10643 + Spegesild + $12.00 + 2 + + + + Maria Anders + 10702 + 10/13/2018 + 11/24/2018 + 10/21/2018 + + 10702 + Aniseed Syrup + $10.00 + 6 + + + 10702 + Lakkalikööri + $18.00 + 15 + + +
+ + Pedro Afonso +
Av. dos Lusíadas, 23
+ São Paulo + 05432-043 + Brazil + (11) 555-7647 + + Pedro Afonso + 10290 + 08/27/2018 + 09/24/2018 + 09/03/2018 + + 10290 + Chef Anton's Gumbo Mix + $17.00 + 20 + + + 10290 + Thüringer Rostbratwurst + $99.00 + 15 + + + 10290 + Maxilaku + $16.00 + 15 + + + + Pedro Afonso + 10969 + 03/23/2019 + 04/20/2019 + 03/30/2019 + + 10969 + Spegesild + $12.00 + 9 + + +
+ + Alexander Feuer +
Heerstr. 22
+ Leipzig + 04179 + Germany + 0342-023176 + + Alexander Feuer + 10277 + 08/09/2018 + 09/06/2018 + 08/13/2018 + + 10277 + Rössle Sauerkraut + $36.40 + 20 + + + 10277 + Tarte au sucre + $39.40 + 12 + + +
+ + Sergio Gutiérrez +
Av. del Libertador 900
+ Buenos Aires + 1010 + Argentina + (1) 123-5555 + + Sergio Gutiérrez + 10716 + 10/24/2018 + 11/21/2018 + 10/27/2018 + + 10716 + Sir Rodney's Scones + $10.00 + 5 + + + 10716 + Manjimup Dried Apples + $53.00 + 7 + + + 10716 + Sirop d'érable + $28.50 + 10 + + + + Sergio Gutiérrez + 10916 + 02/27/2019 + 03/27/2019 + 03/09/2019 + + 10916 + Pavlova + $17.45 + 6 + + + 10916 + Mascarpone Fabioli + $32.00 + 6 + + + 10916 + Ravioli Angelo + $19.50 + 20 + + +
+
\ No newline at end of file diff --git a/docio/resources/Image.png b/docio/resources/Image.png new file mode 100644 index 0000000..5e610f0 Binary files /dev/null and b/docio/resources/Image.png differ diff --git a/docio/resources/Template.docx b/docio/resources/Template.docx new file mode 100644 index 0000000..43f58ba Binary files /dev/null and b/docio/resources/Template.docx differ