github.com/unidoc/unidoc@v2.2.0+incompatible/pdf/creator/table_test.go (about) 1 /* 2 * This file is subject to the terms and conditions defined in 3 * file 'LICENSE.md', which is part of this source code package. 4 */ 5 6 package creator 7 8 import ( 9 "fmt" 10 "io/ioutil" 11 "testing" 12 13 "github.com/unidoc/unidoc/pdf/model/fonts" 14 ) 15 16 func TestTableMultiParagraphWrapped(t *testing.T) { 17 c := New() 18 19 pageHistoryTable := NewTable(4) 20 pageHistoryTable.SetColumnWidths(0.1, 0.6, 0.15, 0.15) 21 content := [][]string{ 22 {"1", "FullText Search Highlight the Term in Results \n\nissues 60", "120", "130"}, 23 {"1", "FullText Search Highlight the Term in Results \n\nissues 60", "120", "130"}, 24 {"1", "FullText Search Highlight the Term in Results \n\nissues 60", "120", "130"}, 25 {"1", "FullText Search Highlight the Term in Results \n\nissues 60", "120", "130"}, 26 {"1", "FullText Search Highlight the Term in Results \n\nissues 60", "120", "130"}, 27 } 28 fontHelvetica := fonts.NewFontHelvetica() 29 for _, rows := range content { 30 for _, txt := range rows { 31 p := NewParagraph(txt) 32 p.SetFontSize(12) 33 p.SetFont(fontHelvetica) 34 p.SetColor(ColorBlack) 35 p.SetEnableWrap(true) 36 37 cell := pageHistoryTable.NewCell() 38 cell.SetBorder(CellBorderStyleBox, 1) 39 cell.SetContent(p) 40 } 41 } 42 43 err := c.Draw(pageHistoryTable) 44 if err != nil { 45 t.Fatalf("Error drawing: %v", err) 46 } 47 48 err = c.WriteToFile("/tmp/table_pagehist.pdf") 49 if err != nil { 50 t.Fatalf("Fail: %v\n", err) 51 } 52 } 53 54 func TestTableWithImage(t *testing.T) { 55 c := New() 56 57 pageHistoryTable := NewTable(4) 58 pageHistoryTable.SetColumnWidths(0.1, 0.6, 0.15, 0.15) 59 content := [][]string{ 60 {"1", "FullText Search Highlight the Term in Results \n\nissues 60", "120", "130"}, 61 {"1", "FullText Search Highlight the Term in Results \n\nissues 60", "120", "130"}, 62 {"1", "FullText Search Highlight the Term in Results \n\nissues 60", "120", "130"}, 63 {"1", "FullText Search Highlight the Term in Results \n\nissues 60", "120", "130"}, 64 {"1", "FullText Search Highlight the Term in Results \n\nissues 60", "120", "130"}, 65 } 66 fontHelvetica := fonts.NewFontHelvetica() 67 for _, rows := range content { 68 for _, txt := range rows { 69 p := NewParagraph(txt) 70 p.SetFontSize(12) 71 p.SetFont(fontHelvetica) 72 p.SetColor(ColorBlack) 73 p.SetEnableWrap(true) 74 75 cell := pageHistoryTable.NewCell() 76 cell.SetBorder(CellBorderStyleBox, 1) 77 err := cell.SetContent(p) 78 if err != nil { 79 t.Fatalf("Error: %v", err) 80 } 81 } 82 } 83 84 pageHistoryTable.SkipCells(1) 85 86 // Add image. 87 imgData, err := ioutil.ReadFile(testImageFile1) 88 if err != nil { 89 t.Errorf("Fail: %v\n", err) 90 return 91 } 92 img, err := NewImageFromData(imgData) 93 if err != nil { 94 t.Errorf("Fail: %v\n", err) 95 return 96 } 97 98 img.margins.top = 2.0 99 img.margins.bottom = 2.0 100 img.margins.left = 2.0 101 img.margins.bottom = 2.0 102 img.ScaleToWidth(0.3 * c.Width()) 103 fmt.Printf("Scaling image to width: %v\n", 0.5*c.Width()) 104 105 cell := pageHistoryTable.NewCell() 106 cell.SetContent(img) 107 cell.SetBorder(CellBorderStyleBox, 1) 108 109 err = c.Draw(pageHistoryTable) 110 if err != nil { 111 t.Fatalf("Error drawing: %v", err) 112 } 113 114 err = c.WriteToFile("/tmp/table_pagehist_with_img.pdf") 115 if err != nil { 116 t.Fatalf("Fail: %v\n", err) 117 } 118 } 119 120 func TestTableWithDiv(t *testing.T) { 121 c := New() 122 123 pageHistoryTable := NewTable(4) 124 pageHistoryTable.SetColumnWidths(0.1, 0.6, 0.15, 0.15) 125 126 headings := []string{ 127 "", "Description", "Passing", "Total", 128 } 129 content := [][]string{ 130 {"1", "FullText Search Highlight the Term in Results \n\nissues 60", "120", "130"}, 131 {"2", "FullText Search Highlight the Term in Results \n\nissues 60", "120", "130"}, 132 {"3", "FullText Search Highlight the Term in Results \n\nissues 60", "120", "130"}, 133 {"3", "FullText Search Highlight the Term in Results. Going hunting in the winter can be fruitful, especially if it has not been too cold and the deer are well fed. \n\nissues 60", "120 90 30", "130 1"}, 134 {"4", "FullText Search Highlight the Term in Results \n\nissues 60", "120", "130"}, 135 {"5", "FullText Search Highlight the Term in Results \n\nissues 60", "120", "130"}, 136 {"6", "FullText Search Highlight the Term in Results \n\nissues 60", "120", "130 a b c d e f g"}, 137 {"7", "FullText Search Highlight the Term in Results \n\nissues 60", "120", "130"}, 138 {"8", "FullText Search Highlight the Term in Results \n\nissues 60", "120", "130 gogogoggogoogogo"}, 139 {"9", "FullText Search Highlight the Term in Results \n\nissues 60", "120", "130"}, 140 {"10", "FullText Search Highlight the Term in Results \n\nissues 60", "120", "130"}, 141 {"11", "FullText Search Highlight the Term in Results \n\nissues 60", "120", "130"}, 142 {"12", "FullText Search Highlight the Term in Results \n\nissues 60", "120", "130"}, 143 {"13", "FullText Search Highlight the Term in Results \n\nissues 60", "120", "130"}, 144 {"14", "FullText Search Highlight the Term in Results \n\nissues 60", "120", "130"}, 145 {"15", "FullText Search Highlight the Term in Results \n\nissues 60", "120", "130"}, 146 {"16", "FullText Search Highlight the Term in Results \n\nissues 60", "120", "130"}, 147 {"17", "FullText Search Highlight the Term in Results \n\nissues 60", "120", "130"}, 148 {"18", "FullText Search Highlight the Term in Results \n\nissues 60", "120", "130"}, 149 {"19", "FullText Search Highlight the Term in Results \n\nissues 60", "120", "130"}, 150 {"20", "FullText Search Highlight the Term in Results \n\nissues 60", "120", "130"}, 151 } 152 fontHelvetica := fonts.NewFontHelvetica() 153 fontHelveticaBold := fonts.NewFontHelveticaBold() 154 for _, rows := range content { 155 for colIdx, txt := range rows { 156 p := NewParagraph(txt) 157 p.SetFontSize(12) 158 p.SetFont(fontHelvetica) 159 p.SetColor(ColorBlack) 160 p.SetMargins(0, 5, 10.0, 10.0) 161 if len(txt) > 10 { 162 p.SetTextAlignment(TextAlignmentJustify) 163 } else { 164 p.SetTextAlignment(TextAlignmentCenter) 165 } 166 167 // Place cell contents (header and text) inside a div. 168 div := NewDivision() 169 170 if len(headings[colIdx]) > 0 { 171 heading := NewParagraph(headings[colIdx]) 172 heading.SetFontSize(14) 173 heading.SetFont(fontHelveticaBold) 174 heading.SetColor(ColorRed) 175 heading.SetTextAlignment(TextAlignmentCenter) 176 err := div.Add(heading) 177 if err != nil { 178 t.Fatalf("Error: %v", err) 179 } 180 } 181 182 err := div.Add(p) 183 if err != nil { 184 t.Fatalf("Error: %v", err) 185 } 186 187 cell := pageHistoryTable.NewCell() 188 cell.SetBorder(CellBorderStyleBox, 1) 189 err = cell.SetContent(div) 190 if err != nil { 191 t.Fatalf("Error: %v", err) 192 } 193 } 194 } 195 196 pageHistoryTable.SkipCells(1) 197 198 // Add image. 199 imgData, err := ioutil.ReadFile(testImageFile1) 200 if err != nil { 201 t.Errorf("Fail: %v\n", err) 202 return 203 } 204 img, err := NewImageFromData(imgData) 205 if err != nil { 206 t.Errorf("Fail: %v\n", err) 207 return 208 } 209 210 img.margins.top = 2.0 211 img.margins.bottom = 2.0 212 img.margins.left = 2.0 213 img.margins.bottom = 2.0 214 img.ScaleToWidth(0.2 * c.Width()) 215 fmt.Printf("Scaling image to width: %v\n", 0.5*c.Width()) 216 217 cell := pageHistoryTable.NewCell() 218 cell.SetContent(img) 219 cell.SetBorder(CellBorderStyleBox, 1) 220 221 err = c.Draw(pageHistoryTable) 222 if err != nil { 223 t.Fatalf("Error drawing: %v", err) 224 } 225 226 err = c.WriteToFile("/tmp/table_pagehist_with_div.pdf") 227 if err != nil { 228 t.Fatalf("Fail: %v\n", err) 229 } 230 }