private void CreateBookmarkForTable(Document doc, Section section) { //Add a paragraph Paragraph paragraph = section.AddParagraph(); //Append text for added paragraph TextRange txtRange = paragraph.AppendText("The following example demonstrates how to create bookmark for a table in a Word document."); //Set the font in italic txtRange.CharacterFormat.Italic = true; //Append bookmark start paragraph.AppendBookmarkStart("CreateBookmark"); //Append bookmark end paragraph.AppendBookmarkEnd("CreateBookmark"); //Add table Table table = section.AddTable(true); //Set the number of rows and columns table.ResetCells(2, 2); //Append text for table cells TextRange range = table[0, 0].AddParagraph().AppendText("sampleA"); range = table[0, 1].AddParagraph().AppendText("sampleB"); range = table[1, 0].AddParagraph().AppendText("120"); range = table[1, 1].AddParagraph().AppendText("260"); //Get the bookmark by index. Bookmark bookmark = doc.Bookmarks[0]; //Get the name of bookmark. String bookmarkName = bookmark.Name; //Locate the bookmark by name. BookmarksNavigator navigator = new BookmarksNavigator(doc); navigator.MoveToBookmark(bookmarkName); //Add table to TextBodyPart TextBodyPart part = navigator.GetBookmarkContent(); part.BodyItems.Add(table); //Replace bookmark cotent with table navigator.ReplaceBookmarkContent(part); }
/// <summary> /// Change tab leader for table of contents in the Word document. /// </summary> /// <param name="toc"></param> private static void ChangeTabLeaderForTableOfContents(TableOfContent toc, TabLeader tabLeader) { //Inserts the bookmark start before the TOC instance. BookmarkStart bkmkStart = new BookmarkStart(toc.Document, "tableOfContent"); toc.OwnerParagraph.Items.Insert(toc.OwnerParagraph.Items.IndexOf(toc), bkmkStart); Entity lastItem = FindLastTOCItem(toc); //Insert the bookmark end to next of TOC last item. BookmarkEnd bkmkEnd = new BookmarkEnd(toc.Document, "tableOfContent"); WParagraph paragraph = lastItem.Owner as WParagraph; paragraph.Items.Insert(paragraph.Items.IndexOf(lastItem) + 1, bkmkEnd); //Creates the bookmark navigator instance to access the bookmark BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(toc.Document); //Moves the virtual cursor to the location before the end of the bookmark "tableOfContent" bookmarkNavigator.MoveToBookmark("tableOfContent"); //Gets the bookmark content TextBodyPart part = bookmarkNavigator.GetBookmarkContent(); //Iterates the items from the bookmark to change the spacing in the table of content for (int i = 0; i < part.BodyItems.Count; i++) { paragraph = part.BodyItems[i] as WParagraph; //Sets the tab leader if (paragraph.ParagraphFormat.Tabs.Count != 0) { paragraph.ParagraphFormat.Tabs[0].TabLeader = tabLeader; } } //Remove the bookmark which we add to get the paragraphs in the table of contents Bookmark bookmark = toc.Document.Bookmarks.FindByName("tableOfContent"); toc.Document.Bookmarks.Remove(bookmark); }
private void button1_Click(object sender, EventArgs e) { //Load Document string input = @"..\..\..\..\..\..\Data\BookmarkTemplate.docx"; Document doc = new Document(); doc.LoadFromFile(input); //Creates a BookmarkNavigator instance to access the bookmark BookmarksNavigator navigator = new BookmarksNavigator(doc); //Locate a specific bookmark by bookmark name navigator.MoveToBookmark("Content"); TextBodyPart textBodyPart = navigator.GetBookmarkContent(); //Iterate through the items in the bookmark content to get the text string text = null; foreach (var item in textBodyPart.BodyItems) { if (item is Paragraph) { foreach (var childObject in (item as Paragraph).ChildObjects) { if (childObject is TextRange) { text += (childObject as TextRange).Text; } } } } //Save to TXT File and launch it string output = "ExtractBookmarkText.txt"; File.WriteAllText(output, text); Viewer(output); }
public ActionResult BookmarkNavigation(string Group1) { if (Group1 == null) { return(View()); } #region BookmarkNavigation // Creating a new document. WordDocument document = new WordDocument(); //Adds section with one empty paragraph to the Word document document.EnsureMinimal(); //sets the page margins document.LastSection.PageSetup.Margins.All = 72f; //Appends bookmark to the paragraph document.LastParagraph.AppendBookmarkStart("NorthwindDatabase"); document.LastParagraph.AppendText("Northwind database with normalization concept"); document.LastParagraph.AppendBookmarkEnd("NorthwindDatabase"); // Open an existing template document with single section to get Northwind.information WordDocument nwdInformation = new WordDocument(ResolveApplicationDataPath("Bookmark_Template.doc", "App_Data\\DocIO")); // Open an existing template document with multiple section to get Northwind data. WordDocument templateDocument = new WordDocument(ResolveApplicationDataPath("BkmkDocumentPart_Template.doc", "App_Data\\DocIO")); // Creating a bookmark navigator. Which help us to navigate through the // bookmarks in the template document. BookmarksNavigator bk = new BookmarksNavigator(templateDocument); // Move to the NorthWind bookmark in template document bk.MoveToBookmark("NorthWind"); //Gets the bookmark content as WordDocumentPart WordDocumentPart documentPart = bk.GetContent(); // Creating a bookmark navigator. Which help us to navigate through the // bookmarks in the Northwind information document. bk = new BookmarksNavigator(nwdInformation); // Move to the information bookmark bk.MoveToBookmark("Information"); // Get the content of information bookmark. TextBodyPart bodyPart = bk.GetBookmarkContent(); // Creating a bookmark navigator. Which help us to navigate through the // bookmarks in the destination document. bk = new BookmarksNavigator(document); // Move to the NorthWind database in the destination document bk.MoveToBookmark("NorthwindDatabase"); //Replace the bookmark content using word document parts bk.ReplaceContent(documentPart); // Move to the Northwind_Information in the destination document bk.MoveToBookmark("Northwind_Information"); // Replacing content of Northwind_Information bookmark. bk.ReplaceBookmarkContent(bodyPart); // Move to the text bookmark bk.MoveToBookmark("Text"); //Deletes the bookmark content bk.DeleteBookmarkContent(true); // Inserting text inside the bookmark. This will preserve the source formatting bk.InsertText("Northwind Database contains the following table:"); #region tableinsertion WTable tbl = new WTable(document); tbl.TableFormat.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.None; tbl.TableFormat.IsAutoResized = true; tbl.ResetCells(8, 2); IWParagraph paragraph; tbl.Rows[0].IsHeader = true; paragraph = tbl[0, 0].AddParagraph(); paragraph.AppendText("Suppliers"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[0, 1].AddParagraph(); paragraph.AppendText("1"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[1, 0].AddParagraph(); paragraph.AppendText("Customers"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[1, 1].AddParagraph(); paragraph.AppendText("1"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[2, 0].AddParagraph(); paragraph.AppendText("Employees"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[2, 1].AddParagraph(); paragraph.AppendText("3"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[3, 0].AddParagraph(); paragraph.AppendText("Products"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[3, 1].AddParagraph(); paragraph.AppendText("1"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[4, 0].AddParagraph(); paragraph.AppendText("Inventory"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[4, 1].AddParagraph(); paragraph.AppendText("2"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[5, 0].AddParagraph(); paragraph.AppendText("Shippers"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[5, 1].AddParagraph(); paragraph.AppendText("1"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[6, 0].AddParagraph(); paragraph.AppendText("PO Transactions"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[6, 1].AddParagraph(); paragraph.AppendText("3"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[7, 0].AddParagraph(); paragraph.AppendText("Sales Transactions"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[7, 1].AddParagraph(); paragraph.AppendText("7"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; bk.InsertTable(tbl); #endregion tableinsertion //Move to image bookmark bk.MoveToBookmark("Image"); //Deletes the bookmark content bk.DeleteBookmarkContent(true); // Inserting image to the bookmark. IWPicture pic = bk.InsertParagraphItem(ParagraphItemType.Picture) as WPicture; pic.LoadImage(System.Drawing.Image.FromFile(ResolveApplicationDataPath("Northwind.png", "Content\\DocIO"))); pic.WidthScale = 50f; // It reduce the image size because it don't fit pic.HeightScale = 75f; // in document page. #endregion BookmarkNavigation #region Document SaveOption //Save as .doc format if (Group1 == "WordDoc") { return(document.ExportAsActionResult("Sample.doc", FormatType.Doc, HttpContext.ApplicationInstance.Response, HttpContentDisposition.Attachment)); } //Save as .docx format else if (Group1 == "WordDocx") { return(document.ExportAsActionResult("Sample.docx", FormatType.Docx, HttpContext.ApplicationInstance.Response, HttpContentDisposition.Attachment)); } // Save as WordML(.xml) format else if (Group1 == "WordML") { return(document.ExportAsActionResult("Sample.xml", FormatType.WordML, HttpContext.ApplicationInstance.Response, HttpContentDisposition.Attachment)); } //Save as .pdf format else if (Group1 == "Pdf") { DocToPDFConverter converter = new DocToPDFConverter(); PdfDocument pdfDoc = converter.ConvertToPDF(document); return(pdfDoc.ExportAsActionResult("sample.pdf", HttpContext.ApplicationInstance.Response, HttpReadType.Save)); } #endregion Document SaveOption return(View()); }
private async void Button_Click_1(object sender, RoutedEventArgs e) { Assembly execAssem = typeof(BookmarkNavigationDemo).GetTypeInfo().Assembly; // Creating a new document. WordDocument document = new WordDocument(); //Adds section with one empty paragraph to the Word document document.EnsureMinimal(); //sets the page margins document.LastSection.PageSetup.Margins.All = 72f; //Appends bookmark to the paragraph document.LastParagraph.AppendBookmarkStart("NorthwindDatabase"); document.LastParagraph.AppendText("Northwind database with relational data"); document.LastParagraph.AppendBookmarkEnd("NorthwindDatabase"); // Open an existing template document with single section. WordDocument nwdInformation = new WordDocument(); Stream inputStream = execAssem.GetManifestResourceStream("Syncfusion.SampleBrowser.UWP.DocIO.DocIO.Assets.Bookmark_Template.doc"); // Open an existing template document. await nwdInformation.OpenAsync(inputStream, FormatType.Doc); inputStream.Dispose(); // Open an existing template document with multiple section. WordDocument templateDocument = new WordDocument(); inputStream = execAssem.GetManifestResourceStream("Syncfusion.SampleBrowser.UWP.DocIO.DocIO.Assets.BkmkDocumentPart_Template.doc"); // Open an existing template document. await templateDocument.OpenAsync(inputStream, FormatType.Doc); inputStream.Dispose(); // Creating a bookmark navigator. Which help us to navigate through the // bookmarks in the template document. BookmarksNavigator bk = new BookmarksNavigator(templateDocument); // Move to the NorthWind bookmark in template document bk.MoveToBookmark("NorthWind"); //Gets the bookmark content as WordDocumentPart WordDocumentPart documentPart = bk.GetContent(); // Creating a bookmark navigator. Which help us to navigate through the // bookmarks in the Northwind information document. bk = new BookmarksNavigator(nwdInformation); // Move to the information bookmark bk.MoveToBookmark("Information"); // Get the content of information bookmark. TextBodyPart bodyPart = bk.GetBookmarkContent(); // Creating a bookmark navigator. Which help us to navigate through the // bookmarks in the destination document. bk = new BookmarksNavigator(document); // Move to the NorthWind database in the destination document bk.MoveToBookmark("NorthwindDatabase"); //Replace the bookmark content using word document parts bk.ReplaceContent(documentPart); // Move to the Northwind_Information in the destination document bk.MoveToBookmark("Northwind_Information"); // Replacing content of Northwind_Information bookmark. bk.ReplaceBookmarkContent(bodyPart); // Move to the text bookmark bk.MoveToBookmark("Text"); //Deletes the bookmark content bk.DeleteBookmarkContent(true); // Inserting text inside the bookmark. This will preserve the source formatting bk.InsertText("Northwind Database contains the following table:"); #region tableinsertion WTable tbl = new WTable(document); tbl.TableFormat.Borders.BorderType = BorderStyle.None; tbl.TableFormat.IsAutoResized = true; tbl.ResetCells(8, 2); IWParagraph paragraph; tbl.Rows[0].IsHeader = true; paragraph = tbl[0, 0].AddParagraph(); paragraph.AppendText("Suppliers"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[0, 1].AddParagraph(); paragraph.AppendText("1"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[1, 0].AddParagraph(); paragraph.AppendText("Customers"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[1, 1].AddParagraph(); paragraph.AppendText("1"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[2, 0].AddParagraph(); paragraph.AppendText("Employees"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[2, 1].AddParagraph(); paragraph.AppendText("3"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[3, 0].AddParagraph(); paragraph.AppendText("Products"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[3, 1].AddParagraph(); paragraph.AppendText("1"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[4, 0].AddParagraph(); paragraph.AppendText("Inventory"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[4, 1].AddParagraph(); paragraph.AppendText("2"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[5, 0].AddParagraph(); paragraph.AppendText("Shippers"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[5, 1].AddParagraph(); paragraph.AppendText("1"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[6, 0].AddParagraph(); paragraph.AppendText("PO Transactions"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[6, 1].AddParagraph(); paragraph.AppendText("3"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[7, 0].AddParagraph(); paragraph.AppendText("Sales Transactions"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[7, 1].AddParagraph(); paragraph.AppendText("7"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; bk.InsertTable(tbl); #endregion //Move to image bookmark bk.MoveToBookmark("Image"); //Deletes the bookmark bk.DeleteBookmarkContent(true); // Inserting image to the bookmark. IWPicture pic = bk.InsertParagraphItem(ParagraphItemType.Picture) as WPicture; inputStream = execAssem.GetManifestResourceStream("Syncfusion.SampleBrowser.UWP.DocIO.DocIO.Assets.Northwind.png"); pic.LoadImage(inputStream); inputStream.Dispose(); pic.WidthScale = 50f; // It reduce the image size because it don't fit pic.HeightScale = 75f; // in document page. Save(rdDoc.IsChecked == true, document); }
void OnButtonClicked(object sender, EventArgs e) { Assembly assembly = Assembly.GetExecutingAssembly(); // Creating a new document. WordDocument document = new WordDocument(); //Adds section with one empty paragraph to the Word document document.EnsureMinimal(); //sets the page margins document.LastSection.PageSetup.Margins.All = 72f; //Appends bookmark to the paragraph document.LastParagraph.AppendBookmarkStart("NorthwindDatabase"); document.LastParagraph.AppendText("Northwind database with relational data"); document.LastParagraph.AppendBookmarkEnd("NorthwindDatabase"); // Open an existing template document with single section. WordDocument nwdInformation = new WordDocument(); Stream inputStream = assembly.GetManifestResourceStream("SampleBrowser.Samples.DocIO.Templates.Bookmark_Template.docx"); // Open an existing template document. nwdInformation.Open(inputStream, FormatType.Doc); inputStream.Dispose(); // Open an existing template document with multiple section. WordDocument templateDocument = new WordDocument(); inputStream = assembly.GetManifestResourceStream("SampleBrowser.Samples.DocIO.Templates.BkmkDocumentPart_Template.docx"); // Open an existing template document. templateDocument.Open(inputStream, FormatType.Doc); inputStream.Dispose(); // Creating a bookmark navigator. Which help us to navigate through the // bookmarks in the template document. BookmarksNavigator bk = new BookmarksNavigator(templateDocument); // Move to the NorthWind bookmark in template document bk.MoveToBookmark("NorthWind"); //Gets the bookmark content as WordDocumentPart WordDocumentPart documentPart = bk.GetContent(); // Creating a bookmark navigator. Which help us to navigate through the // bookmarks in the Northwind information document. bk = new BookmarksNavigator(nwdInformation); // Move to the information bookmark bk.MoveToBookmark("Information"); // Get the content of information bookmark. TextBodyPart bodyPart = bk.GetBookmarkContent(); // Creating a bookmark navigator. Which help us to navigate through the // bookmarks in the destination document. bk = new BookmarksNavigator(document); // Move to the NorthWind database in the destination document bk.MoveToBookmark("NorthwindDatabase"); //Replace the bookmark content using word document parts bk.ReplaceContent(documentPart); // Move to the Northwind_Information in the destination document bk.MoveToBookmark("Northwind_Information"); // Replacing content of Northwind_Information bookmark. bk.ReplaceBookmarkContent(bodyPart); #region Bookmark selection for table // Creating a bookmark navigator. Which help us to navigate through the // bookmarks in the Northwind information document. bk = new BookmarksNavigator(nwdInformation); bk.MoveToBookmark("SuppliersTable"); //Sets the column index where the bookmark starts within the table bk.CurrentBookmark.FirstColumn = 1; //Sets the column index where the bookmark ends within the table bk.CurrentBookmark.LastColumn = 5; // Get the content of suppliers table bookmark. bodyPart = bk.GetBookmarkContent(); // Creating a bookmark navigator. Which help us to navigate through the // bookmarks in the destination document. bk = new BookmarksNavigator(document); bk.MoveToBookmark("Table"); bk.ReplaceBookmarkContent(bodyPart); #endregion // Move to the text bookmark bk.MoveToBookmark("Text"); //Deletes the bookmark content bk.DeleteBookmarkContent(true); // Inserting text inside the bookmark. This will preserve the source formatting bk.InsertText("Northwind Database contains the following table:"); #region tableinsertion WTable tbl = new WTable(document); tbl.TableFormat.Borders.BorderType = BorderStyle.None; tbl.TableFormat.IsAutoResized = true; tbl.ResetCells(8, 2); IWParagraph paragraph; tbl.Rows[0].IsHeader = true; paragraph = tbl[0, 0].AddParagraph(); paragraph.AppendText("Suppliers"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[0, 1].AddParagraph(); paragraph.AppendText("1"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[1, 0].AddParagraph(); paragraph.AppendText("Customers"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[1, 1].AddParagraph(); paragraph.AppendText("1"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[2, 0].AddParagraph(); paragraph.AppendText("Employees"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[2, 1].AddParagraph(); paragraph.AppendText("3"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[3, 0].AddParagraph(); paragraph.AppendText("Products"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[3, 1].AddParagraph(); paragraph.AppendText("1"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[4, 0].AddParagraph(); paragraph.AppendText("Inventory"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[4, 1].AddParagraph(); paragraph.AppendText("2"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[5, 0].AddParagraph(); paragraph.AppendText("Shippers"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[5, 1].AddParagraph(); paragraph.AppendText("1"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[6, 0].AddParagraph(); paragraph.AppendText("PO Transactions"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[6, 1].AddParagraph(); paragraph.AppendText("3"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[7, 0].AddParagraph(); paragraph.AppendText("Sales Transactions"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[7, 1].AddParagraph(); paragraph.AppendText("7"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; bk.InsertTable(tbl); #endregion bk.MoveToBookmark("Image"); bk.DeleteBookmarkContent(true); // Inserting image to the bookmark. IWPicture pic = bk.InsertParagraphItem(ParagraphItemType.Picture) as WPicture; inputStream = assembly.GetManifestResourceStream("SampleBrowser.Samples.DocIO.Templates.Northwind.png"); pic.LoadImage(inputStream); inputStream.Dispose(); pic.WidthScale = 50f; // It reduce the image size because it don't fit pic.HeightScale = 75f; // in document page. #region Saving Document MemoryStream stream = new MemoryStream(); document.Save(stream, FormatType.Word2013); document.Close(); if (stream != null) { SaveAndroid androidSave = new SaveAndroid(); androidSave.Save("BookmarkNavigation.docx", "application/msword", stream, m_context); } #endregion }
private void button1_Click(object sender, System.EventArgs e) { try { string dataPath = @"..\..\..\..\..\..\..\Common\Data\DocIO\"; // Creating a new document. WordDocument document = new WordDocument(); //Adds section with one empty paragraph to the Word document document.EnsureMinimal(); //sets the page margins document.LastSection.PageSetup.Margins.All = 72f; //Appends bookmark to the paragraph document.LastParagraph.AppendBookmarkStart("NorthwindDatabase"); document.LastParagraph.AppendText("Northwind database with normalization concept"); document.LastParagraph.AppendBookmarkEnd("NorthwindDatabase"); // Open an existing template document with single section to get Northwind.information WordDocument nwdInformation = new WordDocument(dataPath + "Bookmark_Template.doc"); // Open an existing template document with multiple section to get Northwind data. WordDocument templateDocument = new WordDocument(dataPath + "BkmkDocumentPart_Template.doc"); // Creating a bookmark navigator. Which help us to navigate through the // bookmarks in the template document. BookmarksNavigator bk = new BookmarksNavigator(templateDocument); // Move to the NorthWind bookmark in template document bk.MoveToBookmark("NorthWind"); //Gets the bookmark content as WordDocumentPart WordDocumentPart documentPart = bk.GetContent(); // Creating a bookmark navigator. Which help us to navigate through the // bookmarks in the Northwind information document. bk = new BookmarksNavigator(nwdInformation); // Move to the information bookmark bk.MoveToBookmark("Information"); // Get the content of information bookmark. TextBodyPart bodyPart = bk.GetBookmarkContent(); // Creating a bookmark navigator. Which help us to navigate through the // bookmarks in the destination document. bk = new BookmarksNavigator(document); // Move to the NorthWind database in the destination document bk.MoveToBookmark("NorthwindDatabase"); //Replace the bookmark content using word document parts bk.ReplaceContent(documentPart); // Move to the Northwind_Information in the destination document bk.MoveToBookmark("Northwind_Information"); // Replacing content of Northwind_Information bookmark. bk.ReplaceBookmarkContent(bodyPart); // Move to the text bookmark bk.MoveToBookmark("Text"); //Deletes the bookmark content bk.DeleteBookmarkContent(true); // Inserting text inside the bookmark. This will preserve the source formatting bk.InsertText("Northwind Database contains the following table:"); #region tableinsertion WTable tbl = new WTable(document); tbl.TableFormat.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.None; tbl.TableFormat.IsAutoResized = true; tbl.ResetCells(8, 2); IWParagraph paragraph; tbl.Rows[0].IsHeader = true; paragraph = tbl[0, 0].AddParagraph(); paragraph.AppendText("Suppliers"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[0, 1].AddParagraph(); paragraph.AppendText("1"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[1, 0].AddParagraph(); paragraph.AppendText("Customers"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[1, 1].AddParagraph(); paragraph.AppendText("1"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[2, 0].AddParagraph(); paragraph.AppendText("Employees"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[2, 1].AddParagraph(); paragraph.AppendText("3"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[3, 0].AddParagraph(); paragraph.AppendText("Products"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[3, 1].AddParagraph(); paragraph.AppendText("1"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[4, 0].AddParagraph(); paragraph.AppendText("Inventory"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[4, 1].AddParagraph(); paragraph.AppendText("2"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[5, 0].AddParagraph(); paragraph.AppendText("Shippers"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[5, 1].AddParagraph(); paragraph.AppendText("1"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[6, 0].AddParagraph(); paragraph.AppendText("PO Transactions"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[6, 1].AddParagraph(); paragraph.AppendText("3"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[7, 0].AddParagraph(); paragraph.AppendText("Sales Transactions"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[7, 1].AddParagraph(); paragraph.AppendText("7"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; bk.InsertTable(tbl); #endregion //Move to image bookmark bk.MoveToBookmark("Image"); //Deletes the bookmark content bk.DeleteBookmarkContent(true); // Inserting image to the bookmark. IWPicture pic = bk.InsertParagraphItem(ParagraphItemType.Picture) as WPicture; pic.LoadImage(System.Drawing.Image.FromFile(@"..\..\..\..\..\..\..\Common\images\DocIO\Northwind.png")); pic.WidthScale = 50f; // It reduces the image size because it doesnot fit pic.HeightScale = 75f; // in document page. bodyPart.Close(); documentPart.Close(); #region save document //Save as doc format if (wordDocRadioBtn.Checked) { //Saving the document to disk. document.Save("Sample.doc"); //Message box confirmation to view the created document. if (MessageBoxAdv.Show("Do you want to view the generated Word document?", "Document has been created", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes) { //Launching the MS Word file using the default Application.[MS Word Or Free WordViewer] System.Diagnostics.Process.Start("Sample.doc"); //Exit this.Close(); } } //Save as docx format else if (wordDocxRadioBtn.Checked) { //Saving the document as .docx document.Save("Sample.docx", FormatType.Docx); //Message box confirmation to view the created document. if (MessageBoxAdv.Show("Do you want to view the generated Word document?", "Document has been created", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes) { try { //Launching the MS Word file using the default Application.[MS Word Or Free WordViewer] System.Diagnostics.Process.Start("Sample.docx"); //Exit this.Close(); } catch (Win32Exception ex) { MessageBoxAdv.Show("Microsoft Word Viewer or Microsoft Word is not installed in this system"); Console.WriteLine(ex.ToString()); } } } //Save as pdf format else if (pdfRadioBtn.Checked) { DocToPDFConverter converter = new DocToPDFConverter(); //Convert word document into PDF document PdfDocument pdfDoc = converter.ConvertToPDF(document); //Save the pdf file pdfDoc.Save("Sample.pdf"); //Message box confirmation to view the created document. if (MessageBoxAdv.Show("Do you want to view the generated PDF?", " Document has been created", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes) { try { System.Diagnostics.Process.Start("Sample.pdf"); //Exit this.Close(); } catch (Exception ex) { MessageBoxAdv.Show("PDF Viewer is not installed in this system"); Console.WriteLine(ex.ToString()); } } } #endregion else { // Exit this.Close(); } } catch (Exception Ex) { MessageBox.Show(Ex.Message); } }
public ActionResult BookmarkNavigation(string Group1) { if (Group1 == null) { return(View()); } #region BookmarkNavigation // Creating a new document. WordDocument document = new WordDocument(); //Adds section with one empty paragraph to the Word document document.EnsureMinimal(); //sets the page margins document.LastSection.PageSetup.Margins.All = 72f; //Appends bookmark to the paragraph document.LastParagraph.AppendBookmarkStart("NorthwindDatabase"); document.LastParagraph.AppendText("Northwind database with normalization concept"); document.LastParagraph.AppendBookmarkEnd("NorthwindDatabase"); string basePath = _hostingEnvironment.WebRootPath; string dataPath = basePath + @"/DocIO/Bookmark_Template.doc"; string dataPathTemp = basePath + @"/DocIO/BkmkDocumentPart_Template.doc"; // Open an existing template document with single section to get Northwind.information WordDocument nwdInformation = new WordDocument(); FileStream fileStream = new FileStream(dataPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); nwdInformation.Open(fileStream, FormatType.Doc); fileStream.Dispose(); fileStream = null; // Open an existing template document with multiple section to get Northwind data. WordDocument templateDocument = new WordDocument(); fileStream = new FileStream(dataPathTemp, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); templateDocument.Open(fileStream, FormatType.Doc); fileStream.Dispose(); fileStream = null; // Creating a bookmark navigator. Which help us to navigate through the // bookmarks in the template document. BookmarksNavigator bk = new BookmarksNavigator(templateDocument); // Move to the NorthWind bookmark in template document bk.MoveToBookmark("NorthWind"); //Gets the bookmark content as WordDocumentPart WordDocumentPart documentPart = bk.GetContent(); // Creating a bookmark navigator. Which help us to navigate through the // bookmarks in the Northwind information document. bk = new BookmarksNavigator(nwdInformation); // Move to the information bookmark bk.MoveToBookmark("Information"); // Get the content of information bookmark. TextBodyPart bodyPart = bk.GetBookmarkContent(); // Creating a bookmark navigator. Which help us to navigate through the // bookmarks in the destination document. bk = new BookmarksNavigator(document); // Move to the NorthWind database in the destination document bk.MoveToBookmark("NorthwindDatabase"); //Replace the bookmark content using word document parts bk.ReplaceContent(documentPart); // Move to the Northwind_Information in the destination document bk.MoveToBookmark("Northwind_Information"); // Replacing content of Northwind_Information bookmark. bk.ReplaceBookmarkContent(bodyPart); // Move to the text bookmark bk.MoveToBookmark("Text"); //Deletes the bookmark content bk.DeleteBookmarkContent(true); // Inserting text inside the bookmark. This will preserve the source formatting bk.InsertText("Northwind Database contains the following table:"); #region tableinsertion WTable tbl = new WTable(document); tbl.TableFormat.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.None; tbl.TableFormat.IsAutoResized = true; tbl.ResetCells(8, 2); IWParagraph paragraph; tbl.Rows[0].IsHeader = true; paragraph = tbl[0, 0].AddParagraph(); paragraph.AppendText("Suppliers"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[0, 1].AddParagraph(); paragraph.AppendText("1"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[1, 0].AddParagraph(); paragraph.AppendText("Customers"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[1, 1].AddParagraph(); paragraph.AppendText("1"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[2, 0].AddParagraph(); paragraph.AppendText("Employees"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[2, 1].AddParagraph(); paragraph.AppendText("3"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[3, 0].AddParagraph(); paragraph.AppendText("Products"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[3, 1].AddParagraph(); paragraph.AppendText("1"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[4, 0].AddParagraph(); paragraph.AppendText("Inventory"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[4, 1].AddParagraph(); paragraph.AppendText("2"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[5, 0].AddParagraph(); paragraph.AppendText("Shippers"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[5, 1].AddParagraph(); paragraph.AppendText("1"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[6, 0].AddParagraph(); paragraph.AppendText("PO Transactions"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[6, 1].AddParagraph(); paragraph.AppendText("3"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[7, 0].AddParagraph(); paragraph.AppendText("Sales Transactions"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[7, 1].AddParagraph(); paragraph.AppendText("7"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; bk.InsertTable(tbl); #endregion tableinsertion //Move to image bookmark bk.MoveToBookmark("Image"); //Deletes the bookmark content bk.DeleteBookmarkContent(true); // Inserting image to the bookmark. IWPicture pic = bk.InsertParagraphItem(ParagraphItemType.Picture) as WPicture; FileStream imageStream = new FileStream(basePath + @"/images/DocIO/Northwind.png", FileMode.Open, FileAccess.Read); pic.LoadImage(imageStream); pic.WidthScale = 50f; // It reduce the image size because it don't fit pic.HeightScale = 75f; // in document page. #endregion BookmarkNavigation FormatType type = FormatType.Docx; string filename = "Sample.docx"; string contenttype = "application/vnd.ms-word.document.12"; #region Document SaveOption //Save as .doc format if (Group1 == "WordDoc") { type = FormatType.Doc; filename = "Sample.doc"; contenttype = "application/msword"; } //Save as .xml format else if (Group1 == "WordML") { type = FormatType.WordML; filename = "Sample.xml"; contenttype = "application/msword"; } #endregion Document SaveOption MemoryStream ms = new MemoryStream(); document.Save(ms, type); document.Close(); ms.Position = 0; return(File(ms, contenttype, filename)); }
private void ManipulateBookmarkContents() { // Creating a new document. using (WordDocument document = new WordDocument()) { #region Document Manipulations Assembly assembly = typeof(App).GetTypeInfo().Assembly; //Adds section with one empty paragraph to the Word document document.EnsureMinimal(); //sets the page margins document.LastSection.PageSetup.Margins.All = 72f; //Appends bookmark to the paragraph document.LastParagraph.AppendBookmarkStart("NorthwindDatabase"); document.LastParagraph.AppendText("Northwind database with relational data"); document.LastParagraph.AppendBookmarkEnd("NorthwindDatabase"); // Open an existing template document with single section. WordDocument nwdInformation = new WordDocument(); Stream inputStream = assembly.GetManifestResourceStream("SampleBrowser.Samples.DocIO.Templates.Bookmark_Template.doc"); // Open an existing template document. nwdInformation.Open(inputStream, FormatType.Doc); inputStream.Dispose(); // Open an existing template document with multiple section. WordDocument templateDocument = new WordDocument(); inputStream = assembly.GetManifestResourceStream("SampleBrowser.Samples.DocIO.Templates.BkmkDocumentPart_Template.doc"); // Open an existing template document. templateDocument.Open(inputStream, FormatType.Doc); inputStream.Dispose(); // Creating a bookmark navigator. Which help us to navigate through the // bookmarks in the template document. BookmarksNavigator bk = new BookmarksNavigator(templateDocument); // Move to the NorthWind bookmark in template document bk.MoveToBookmark("NorthWind"); //Gets the bookmark content as WordDocumentPart WordDocumentPart documentPart = bk.GetContent(); // Creating a bookmark navigator. Which help us to navigate through the // bookmarks in the Northwind information document. bk = new BookmarksNavigator(nwdInformation); // Move to the information bookmark bk.MoveToBookmark("Information"); // Get the content of information bookmark. TextBodyPart bodyPart = bk.GetBookmarkContent(); // Creating a bookmark navigator. Which help us to navigate through the // bookmarks in the destination document. bk = new BookmarksNavigator(document); // Move to the NorthWind database in the destination document bk.MoveToBookmark("NorthwindDatabase"); //Replace the bookmark content using word document parts bk.ReplaceContent(documentPart); // Move to the Northwind_Information in the destination document bk.MoveToBookmark("Northwind_Information"); // Replacing content of Northwind_Information bookmark. bk.ReplaceBookmarkContent(bodyPart); // Move to the text bookmark bk.MoveToBookmark("Text"); //Deletes the bookmark content bk.DeleteBookmarkContent(true); // Inserting text inside the bookmark. This will preserve the source formatting bk.InsertText("Northwind Database contains the following table:"); #region tableinsertion WTable tbl = new WTable(document); tbl.TableFormat.Borders.BorderType = BorderStyle.None; tbl.TableFormat.IsAutoResized = true; tbl.ResetCells(8, 2); IWParagraph paragraph; tbl.Rows[0].IsHeader = true; paragraph = tbl[0, 0].AddParagraph(); paragraph.AppendText("Suppliers"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[0, 1].AddParagraph(); paragraph.AppendText("1"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[1, 0].AddParagraph(); paragraph.AppendText("Customers"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[1, 1].AddParagraph(); paragraph.AppendText("1"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[2, 0].AddParagraph(); paragraph.AppendText("Employees"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[2, 1].AddParagraph(); paragraph.AppendText("3"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[3, 0].AddParagraph(); paragraph.AppendText("Products"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[3, 1].AddParagraph(); paragraph.AppendText("1"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[4, 0].AddParagraph(); paragraph.AppendText("Inventory"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[4, 1].AddParagraph(); paragraph.AppendText("2"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[5, 0].AddParagraph(); paragraph.AppendText("Shippers"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[5, 1].AddParagraph(); paragraph.AppendText("1"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[6, 0].AddParagraph(); paragraph.AppendText("PO Transactions"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[6, 1].AddParagraph(); paragraph.AppendText("3"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[7, 0].AddParagraph(); paragraph.AppendText("Sales Transactions"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[7, 1].AddParagraph(); paragraph.AppendText("7"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; bk.InsertTable(tbl); #endregion bk.MoveToBookmark("Image"); bk.DeleteBookmarkContent(true); // Inserting image to the bookmark. IWPicture pic = bk.InsertParagraphItem(ParagraphItemType.Picture) as WPicture; inputStream = assembly.GetManifestResourceStream("SampleBrowser.Samples.DocIO.Templates.Northwind.png"); pic.LoadImage(inputStream); inputStream.Dispose(); pic.WidthScale = 50f; // It reduce the image size because it don't fit pic.HeightScale = 75f; // in document page. #endregion #region Saving Document //Save the word document to stream. MemoryStream stream = new MemoryStream(); document.Save(stream, FormatType.Docx); //Save file in the disk based on specfic OS if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows) { Xamarin.Forms.DependencyService.Get <ISaveWindowsPhone>().Save("BookMarkNavigation.docx", "application/msword", stream); } else { Xamarin.Forms.DependencyService.Get <ISave>().Save("BookMarkNavigation.docx", "application/msword", stream); } #endregion } }
private void button1_Click(object sender, EventArgs e) { //Create the source document Document sourcedocument = new Document(); //Load the source document from disk. sourcedocument.LoadFromFile(@"..\..\..\..\..\..\Data\Bookmark.docx"); //Create a destination document Document destinationDoc = new Document(); //Add a section for destination document Section section = destinationDoc.AddSection(); //Add a paragraph for destination document Paragraph paragraph = section.AddParagraph(); //Locate the bookmark in source document BookmarksNavigator navigator = new BookmarksNavigator(sourcedocument); //Find bookmark by name navigator.MoveToBookmark("Test", true, true); //get text body part TextBodyPart textBodyPart = navigator.GetBookmarkContent(); //Create a TextRange type list List <TextRange> list = new List <TextRange>(); //Traverse the items of text body foreach (var item in textBodyPart.BodyItems) { //if it is paragraph if (item is Paragraph) { //Traverse the ChildObjects of the paragraph foreach (var childObject in (item as Paragraph).ChildObjects) { //if it is TextRange if (childObject is TextRange) { //Add it into list TextRange range = childObject as TextRange; list.Add(range); } } } } //Add the extract content to destinationDoc document for (int m = 0; m < list.Count; m++) { paragraph.Items.Add(list[m].Clone()); } //Save the document. destinationDoc.SaveToFile("Output.docx", FileFormat.Docx); //Launch the Word file. WordDocViewer("Output.docx"); }
void OnButtonClicked(object sender, EventArgs e) { Assembly assembly = typeof(App).GetTypeInfo().Assembly; // Creating a new document. WordDocument document = new WordDocument(); //Adds section with one empty paragraph to the Word document document.EnsureMinimal(); //sets the page margins document.LastSection.PageSetup.Margins.All = 72f; //Appends bookmark to the paragraph document.LastParagraph.AppendBookmarkStart("NorthwindDatabase"); document.LastParagraph.AppendText("Northwind database with relational data"); document.LastParagraph.AppendBookmarkEnd("NorthwindDatabase"); // Open an existing template document with single section. WordDocument nwdInformation = new WordDocument(); Stream inputStream = assembly.GetManifestResourceStream("SampleBrowser.Samples.DocIO.Templates.Bookmark_Template.doc"); // Open an existing template document. nwdInformation.Open(inputStream, FormatType.Doc); inputStream.Dispose(); // Open an existing template document with multiple section. WordDocument templateDocument = new WordDocument(); inputStream = assembly.GetManifestResourceStream("SampleBrowser.Samples.DocIO.Templates.BkmkDocumentPart_Template.doc"); // Open an existing template document. templateDocument.Open(inputStream, FormatType.Doc); inputStream.Dispose(); // Creating a bookmark navigator. Which help us to navigate through the // bookmarks in the template document. BookmarksNavigator bk = new BookmarksNavigator(templateDocument); // Move to the NorthWind bookmark in template document bk.MoveToBookmark("NorthWind"); //Gets the bookmark content as WordDocumentPart WordDocumentPart documentPart = bk.GetContent(); // Creating a bookmark navigator. Which help us to navigate through the // bookmarks in the Northwind information document. bk = new BookmarksNavigator(nwdInformation); // Move to the information bookmark bk.MoveToBookmark("Information"); // Get the content of information bookmark. TextBodyPart bodyPart = bk.GetBookmarkContent(); // Creating a bookmark navigator. Which help us to navigate through the // bookmarks in the destination document. bk = new BookmarksNavigator(document); // Move to the NorthWind database in the destination document bk.MoveToBookmark("NorthwindDatabase"); //Replace the bookmark content using word document parts bk.ReplaceContent(documentPart); // Move to the Northwind_Information in the destination document bk.MoveToBookmark("Northwind_Information"); // Replacing content of Northwind_Information bookmark. bk.ReplaceBookmarkContent(bodyPart); // Move to the text bookmark bk.MoveToBookmark("Text"); //Deletes the bookmark content bk.DeleteBookmarkContent(true); // Inserting text inside the bookmark. This will preserve the source formatting bk.InsertText("Northwind Database contains the following table:"); #region tableinsertion WTable tbl = new WTable(document); tbl.TableFormat.Borders.BorderType = BorderStyle.None; tbl.TableFormat.IsAutoResized = true; tbl.ResetCells(8, 2); IWParagraph paragraph; tbl.Rows[0].IsHeader = true; paragraph = tbl[0, 0].AddParagraph(); paragraph.AppendText("Suppliers"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[0, 1].AddParagraph(); paragraph.AppendText("1"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[1, 0].AddParagraph(); paragraph.AppendText("Customers"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[1, 1].AddParagraph(); paragraph.AppendText("1"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[2, 0].AddParagraph(); paragraph.AppendText("Employees"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[2, 1].AddParagraph(); paragraph.AppendText("3"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[3, 0].AddParagraph(); paragraph.AppendText("Products"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[3, 1].AddParagraph(); paragraph.AppendText("1"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[4, 0].AddParagraph(); paragraph.AppendText("Inventory"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[4, 1].AddParagraph(); paragraph.AppendText("2"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[5, 0].AddParagraph(); paragraph.AppendText("Shippers"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[5, 1].AddParagraph(); paragraph.AppendText("1"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[6, 0].AddParagraph(); paragraph.AppendText("PO Transactions"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[6, 1].AddParagraph(); paragraph.AppendText("3"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[7, 0].AddParagraph(); paragraph.AppendText("Sales Transactions"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; paragraph = tbl[7, 1].AddParagraph(); paragraph.AppendText("7"); paragraph.BreakCharacterFormat.FontName = "Calibri"; paragraph.BreakCharacterFormat.FontSize = 10; bk.InsertTable(tbl); #endregion bk.MoveToBookmark("Image"); bk.DeleteBookmarkContent(true); // Inserting image to the bookmark. IWPicture pic = bk.InsertParagraphItem(ParagraphItemType.Picture) as WPicture; inputStream = assembly.GetManifestResourceStream("SampleBrowser.Samples.DocIO.Templates.Northwind.png"); pic.LoadImage(inputStream); inputStream.Dispose(); pic.WidthScale = 50f; // It reduce the image size because it don't fit pic.HeightScale = 75f; // in document page. #region Saving Document MemoryStream stream = new MemoryStream(); document.Save(stream, FormatType.Word2013); document.Close(); if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows) Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().Save("BookMarkNavigation.docx", "application/msword", stream); else Xamarin.Forms.DependencyService.Get<ISave>().Save("BookMarkNavigation.docx", "application/msword", stream); #endregion }