|
6 | 6 | using System.Dynamic;
|
7 | 7 | using System.Globalization;
|
8 | 8 | using Bold = DocumentFormat.OpenXml.Wordprocessing.Bold;
|
| 9 | +using Break = DocumentFormat.OpenXml.Drawing.Break; |
9 | 10 | using Paragraph = DocumentFormat.OpenXml.Wordprocessing.Paragraph;
|
10 | 11 | using Run = DocumentFormat.OpenXml.Wordprocessing.Run;
|
11 | 12 | using RunProperties = DocumentFormat.OpenXml.Wordprocessing.RunProperties;
|
@@ -597,6 +598,67 @@ public void BindCollection()
|
597 | 598 | "I'm only here if NumericValue is greater than 0 - INNERVALUE2B will be replaced X"));
|
598 | 599 | }
|
599 | 600 |
|
| 601 | + |
| 602 | + |
| 603 | + [Test] |
| 604 | + public void SupTemplateTest() |
| 605 | + { |
| 606 | + var template = @"<w:p xmlns:w=""http://schemas.openxmlformats.org/wordprocessingml/2006/main""> |
| 607 | + <w:pPr> |
| 608 | + <w:pBdr> |
| 609 | + <w:bottom w:val=""double"" w:sz=""6"" w:space=""1"" w:color=""auto""/> |
| 610 | + </w:pBdr> |
| 611 | + </w:pPr> |
| 612 | + <w:r> |
| 613 | + <w:t>Test {{ds.Name}} {{ds.Number}}</w:t> |
| 614 | + </w:r> |
| 615 | + </w:p>"; |
| 616 | + |
| 617 | + using var memStream = new MemoryStream(); |
| 618 | + using var wpDocument = WordprocessingDocument.Create(memStream, WordprocessingDocumentType.Document); |
| 619 | + |
| 620 | + MainDocumentPart mainPart = wpDocument.AddMainDocumentPart(); |
| 621 | + mainPart.Document = new Document(new Body( |
| 622 | + new Paragraph( |
| 623 | + new Run(new Text("Start of Document")), |
| 624 | + new Break(), |
| 625 | + new Run(new Text("{{#ds.Items}}")) |
| 626 | + ), |
| 627 | + new Paragraph( |
| 628 | + new Run(new Text("{{.Name}}")), |
| 629 | + new Run(new Text("{{.}:T('ds.Template')}")) |
| 630 | + ), |
| 631 | + new Paragraph( |
| 632 | + new Run(new Text("{{/ds.Items}}")) |
| 633 | + ) |
| 634 | + )); |
| 635 | + wpDocument.Save(); |
| 636 | + memStream.Position = 0; |
| 637 | + var docTemplate = new DocxTemplate(memStream); |
| 638 | + docTemplate.BindModel("ds", |
| 639 | + new |
| 640 | + { |
| 641 | + Template = template, |
| 642 | + Items = new[] |
| 643 | + { |
| 644 | + new {Name = "Item1 ", Number = 55 }, |
| 645 | + new {Name = "Item2 ", Number = 96 } |
| 646 | + } |
| 647 | + }); |
| 648 | + var result = docTemplate.Process(); |
| 649 | + //docTemplate.Validate(); |
| 650 | + Assert.That(result, Is.Not.Null); |
| 651 | + result.Position = 0; |
| 652 | + |
| 653 | + var document = WordprocessingDocument.Open(result, false); |
| 654 | + var body = document.MainDocumentPart.Document.Body; |
| 655 | + //check values have been replaced |
| 656 | + Assert.That(body.InnerText, Is.EqualTo("Start of DocumentItem1 Test Item1 55Item2 Test Item2 96")); |
| 657 | + |
| 658 | + |
| 659 | + } |
| 660 | + |
| 661 | + |
600 | 662 | [Test]
|
601 | 663 | public void BindCollectionToTable()
|
602 | 664 | {
|
@@ -657,7 +719,7 @@ public void BindCollectionToTable()
|
657 | 719 | docTemplate.Validate();
|
658 | 720 | Assert.That(result, Is.Not.Null);
|
659 | 721 | result.Position = 0;
|
660 |
| - // result.SaveAsFileAndOpenInWord(); |
| 722 | + result.SaveAsFileAndOpenInWord(); |
661 | 723 | var document = WordprocessingDocument.Open(result, false);
|
662 | 724 | var body = document.MainDocumentPart.Document.Body;
|
663 | 725 | var table = body.Descendants<Table>().First();
|
@@ -757,6 +819,125 @@ public void ProcessBillTemplate2()
|
757 | 819 | result.SaveAsFileAndOpenInWord();
|
758 | 820 | }
|
759 | 821 |
|
| 822 | + private enum RowType |
| 823 | + { |
| 824 | + Normal = 1, |
| 825 | + Underscore = 2, |
| 826 | + Red = 3, |
| 827 | + FromTemplate = 4, |
| 828 | + } |
| 829 | + |
| 830 | + [Test] |
| 831 | + public void ConditionalTableRowsExtended() |
| 832 | + { |
| 833 | + // TODO: allow usage of enums in template |
| 834 | + using var fileStream = File.OpenRead("Resources/ConditionalTableRows.docx"); |
| 835 | + var docTemplate = new DocxTemplate(fileStream); |
| 836 | + var model = new |
| 837 | + { |
| 838 | + RowTemplate = File.ReadAllBytes("Resources/RowTemplate.docx"), |
| 839 | + Positions = new[] |
| 840 | + { |
| 841 | + new { Type = (int)RowType.Normal, Description = "Description1", Tax = 20.5, Count = 55, Price = 55.20, TotalPrice = 20.9 }, |
| 842 | + new { Type = (int)RowType.Underscore, Description = "Underscore 2", Tax = 20.5, Count = 55, Price = 55.20, TotalPrice = 20.9 }, |
| 843 | + new { Type = (int)RowType.Normal, Description = "Description3", Tax = 200.5, Count = 550, Price = 550.20, TotalPrice = 200.9 }, |
| 844 | + new { Type = (int)RowType.Red, Description = "Description4", Tax = 200.5, Count = 550, Price = 550.20, TotalPrice = 200.9 }, |
| 845 | + new { Type = (int)RowType.Normal, Description = "Description5", Tax = 200.5, Count = 550, Price = 550.20, TotalPrice = 200.9 }, |
| 846 | + new { Type = (int)RowType.FromTemplate, Description = "Description 6", Tax = 200.5, Count = 550, Price = 550.20, TotalPrice = 200.9 }, |
| 847 | + new { Type = (int)RowType.FromTemplate, Description = "Description 7", Tax = 200.5, Count = 550, Price = 550.20, TotalPrice = 200.9 }, |
| 848 | + } |
| 849 | + }; |
| 850 | + docTemplate.BindModel("ds", model); |
| 851 | + docTemplate.BindModel("RowType", Enum.GetValues<RowType>().ToDictionary(x => x.ToString(), x => (int)x)); |
| 852 | + var result = docTemplate.Process(); |
| 853 | + docTemplate.Validate(); |
| 854 | + result.Position = 0; |
| 855 | + result.SaveAsFileAndOpenInWord(); |
| 856 | + } |
| 857 | + |
| 858 | + [Test] |
| 859 | + public void ConditionalTableRows() |
| 860 | + { |
| 861 | + var model = new |
| 862 | + { |
| 863 | + Positions = new[] |
| 864 | + { |
| 865 | + new { Type = 1, Index = 1, Description = "Description", Tax = 20.5, Count = 55, Price = 55.20, TotalPrice = 20.9 }, |
| 866 | + new { Type = 2, Index = 2, Description = "Description1", Tax = 20.5, Count = 55, Price = 55.20, TotalPrice = 20.9 }, |
| 867 | + new { Type = 1, Index = 3, Description = "Description2", Tax = 200.5, Count = 550, Price = 550.20, TotalPrice = 200.9 }, |
| 868 | + } |
| 869 | + }; |
| 870 | + |
| 871 | + var xml = @"<w:tbl xmlns:w=""http://schemas.openxmlformats.org/wordprocessingml/2006/main""> |
| 872 | + <w:tblPr> |
| 873 | + <w:tblW w:w=""5000"" w:type=""pct""/> |
| 874 | + <w:tblBorders> |
| 875 | + <w:top w:val=""single"" w:sz=""4"" w:space=""0"" w:color=""auto""/> |
| 876 | + <w:left w:val=""single"" w:sz=""4"" w:space=""0"" w:color=""auto""/> |
| 877 | + <w:bottom w:val=""single"" w:sz=""4"" w:space=""0"" w:color=""auto""/> |
| 878 | + <w:right w:val=""single"" w:sz=""4"" w:space=""0"" w:color=""auto""/> |
| 879 | + </w:tblBorders> |
| 880 | + </w:tblPr> |
| 881 | + <w:tblGrid> |
| 882 | + <w:gridCol w:w=""10296""/> |
| 883 | + </w:tblGrid> |
| 884 | + <w:tr> |
| 885 | + <w:tc> |
| 886 | + <w:p><w:r><w:t>Header Col 1</w:t></w:r></w:p> |
| 887 | + </w:tc> |
| 888 | + <w:tc> |
| 889 | + <w:p><w:r><w:t>Header Col 2</w:t></w:r></w:p> |
| 890 | + </w:tc> |
| 891 | + </w:tr> |
| 892 | + <w:tr> |
| 893 | + <w:tc> |
| 894 | + <w:tcPr> |
| 895 | + <w:tcW w:w=""0"" w:type=""auto""/> |
| 896 | + <w:tcBorders> |
| 897 | + <w:top w:val=""single"" w:color=""auto"" w:sz=""4"" w:space=""0"" /> |
| 898 | + </w:tcBorders> |
| 899 | + </w:tcPr> |
| 900 | + <w:p><w:r><w:t>{{#Positions}}{?{.Type == 1}}</w:t><w:t>{{.Index}}</w:t></w:r></w:p> |
| 901 | + </w:tc> |
| 902 | + <w:tc> |
| 903 | + <w:tcPr> |
| 904 | + <w:tcW w:w=""0"" w:type=""auto""/> |
| 905 | + </w:tcPr> |
| 906 | + <w:p><w:r><w:t>{{.Description}}{{/}}</w:t></w:r></w:p> |
| 907 | + </w:tc> |
| 908 | + </w:tr> |
| 909 | + <w:tr> |
| 910 | + <w:tc> |
| 911 | + <w:tcPr> |
| 912 | + <w:tcW w:w=""0"" w:type=""auto""/> |
| 913 | + </w:tcPr> |
| 914 | + <w:p><w:r><w:t>{?{.Type == 2}}{{.Tax}} Other Row</w:t></w:r></w:p> |
| 915 | + </w:tc> |
| 916 | + <w:tc> |
| 917 | + <w:tcPr> |
| 918 | + <w:tcW w:w=""0"" w:type=""auto""/> |
| 919 | + </w:tcPr> |
| 920 | + <w:p><w:r><w:t>{{.Count}} Other Row Col 2{{/}}{{/Positions}}</w:t></w:r></w:p> |
| 921 | + </w:tc> |
| 922 | + </w:tr> |
| 923 | + </w:tbl>"; |
| 924 | + |
| 925 | + using var memStream = new MemoryStream(); |
| 926 | + using var wpDocument = WordprocessingDocument.Create(memStream, WordprocessingDocumentType.Document); |
| 927 | + MainDocumentPart mainPart = wpDocument.AddMainDocumentPart(); |
| 928 | + mainPart.Document = new Document(new Body(new Table(xml))); |
| 929 | + wpDocument.Save(); |
| 930 | + memStream.Position = 0; |
| 931 | + var docTemplate = new DocxTemplate(memStream); |
| 932 | + |
| 933 | + |
| 934 | + docTemplate.BindModel("ds", model); |
| 935 | + var result = docTemplate.Process(); |
| 936 | + docTemplate.Validate(); |
| 937 | + result.Position = 0; |
| 938 | + result.SaveAsFileAndOpenInWord(); |
| 939 | + } |
| 940 | + |
760 | 941 | private static DriveStudentOverviewReportingModel CrateBillTemplate2Model()
|
761 | 942 | {
|
762 | 943 | DriveStudentOverviewReportingModel model = new()
|
|
0 commit comments