|
22 | 22 | using iText.Layout.Borders; |
23 | 23 | using iText.Layout.Element; |
24 | 24 | using iText.Layout.Font; |
| 25 | +using iText.Layout.Layout; |
25 | 26 | using iText.Layout.Properties; |
26 | 27 | using log4net; |
27 | 28 | using NetTopologySuite.Utilities; |
@@ -407,6 +408,7 @@ void GxDrawRect(int left, int top, int right, int bottom, int pen, Color foreCol |
407 | 408 | public override void GxDrawLine(int left, int top, int right, int bottom, int width, int foreRed, int foreGreen, int foreBlue, int style) |
408 | 409 | { |
409 | 410 | PdfCanvas cb = new PdfCanvas(pdfPage); |
| 411 | + |
410 | 412 | Color foreColor = new DeviceRgb(foreRed, foreGreen, foreBlue); |
411 | 413 |
|
412 | 414 | float widthAux = (float)convertScale(width); |
@@ -447,6 +449,7 @@ public override void GxDrawLine(int left, int top, int right, int bottom, int wi |
447 | 449 | } |
448 | 450 | public override void GxDrawBitMap(String bitmap, int left, int top, int right, int bottom, int aspectRatio) |
449 | 451 | { |
| 452 | + |
450 | 453 | try |
451 | 454 | { |
452 | 455 | Image image; |
@@ -497,7 +500,7 @@ public override void GxDrawBitMap(String bitmap, int left, int top, int right, i |
497 | 500 | float bottomAux = (float)convertScale(bottom); |
498 | 501 | float leftAux = (float)convertScale(left); |
499 | 502 | float topAux = (float)convertScale(top); |
500 | | - image.SetFixedPosition(leftAux + leftMargin, this.pageSize.GetTop() - bottomAux - topMargin - bottomMargin); |
| 503 | + image.SetFixedPosition(this.getPage(),leftAux + leftMargin, this.pageSize.GetTop() - bottomAux - topMargin - bottomMargin); |
501 | 504 | if (aspectRatio == 0) |
502 | 505 | image.ScaleAbsolute(rightAux - leftAux, bottomAux - topAux); |
503 | 506 | else |
@@ -769,26 +772,30 @@ public override void GxDrawText(String sTxt, int left, int top, int right, int b |
769 | 772 | bottomAux = (float)convertScale(bottom); |
770 | 773 | topAux = (float)convertScale(top); |
771 | 774 |
|
772 | | - |
773 | | - Canvas cb = new Canvas(canvas, new Rectangle(leftAux + leftMargin, this.pageSize.GetTop() - bottomAux - topMargin - bottomMargin, rightAux - leftAux, bottomAux - topAux)); |
| 775 | + Rectangle htmlRectangle = new Rectangle(leftAux + leftMargin, this.pageSize.GetTop() - bottomAux - topMargin - bottomMargin, rightAux - leftAux, bottomAux - topAux); |
| 776 | + Canvas cb = new Canvas(canvas, htmlRectangle); |
774 | 777 | TextAlignment colAlignment = (TextAlignment)GetTextAlignment(alignment); |
775 | 778 | cb.SetTextAlignment(colAlignment); |
776 | 779 |
|
777 | 780 | try |
778 | 781 | { |
779 | | - IList<IElement> objects = HtmlConverter.ConvertToElements(sTxt, properties); |
| 782 | + bottomAux = (float)convertScale(bottom); |
| 783 | + topAux = (float)convertScale(top); |
| 784 | + float drawingPageHeight = this.pageSize.GetTop() - topMargin - bottomMargin; |
780 | 785 |
|
781 | | - foreach (IElement element in objects) |
782 | | - { |
783 | | - Paragraph p = element as Paragraph; |
784 | | - if (p != null) |
785 | | - { |
786 | | - if (alignment != 0) |
787 | | - p.SetTextAlignment(colAlignment); |
788 | | - } |
789 | | - |
790 | | - cb.Add((IBlockElement)element); |
791 | | - } |
| 786 | + float llx = leftAux + leftMargin; |
| 787 | + float lly = drawingPageHeight - bottomAux; |
| 788 | + float urx = rightAux + leftMargin; |
| 789 | + float ury = drawingPageHeight - topAux; |
| 790 | + |
| 791 | + YPosition yPosition = new YPosition(htmlRectangle.GetTop()); |
| 792 | + |
| 793 | + ConverterProperties converterProperties = new ConverterProperties(); |
| 794 | + converterProperties.SetFontProvider(document.GetFontProvider()); |
| 795 | + //Iterate over the elements (a.k.a the parsed HTML string) and handle each case accordingly |
| 796 | + IList<IElement> elements = HtmlConverter.ConvertToElements(sTxt, properties); |
| 797 | + foreach (IElement element in elements) |
| 798 | + processHTMLElement(cb, colAlignment, htmlRectangle, yPosition, (IBlockElement)element); |
792 | 799 | } |
793 | 800 | catch (Exception ex1) |
794 | 801 | { |
@@ -974,6 +981,94 @@ public override void GxDrawText(String sTxt, int left, int top, int right, int b |
974 | 981 |
|
975 | 982 | } |
976 | 983 |
|
| 984 | + private void processHTMLElement(Canvas cb, TextAlignment colAlignment, Rectangle htmlRectangle, YPosition currentYPosition, IBlockElement blockElement) |
| 985 | + { |
| 986 | + Div div = blockElement as Div; |
| 987 | + if (div != null) { |
| 988 | + // Iterate through the children of the Div and process each child element recursively |
| 989 | + foreach (IElement child in div.GetChildren()) |
| 990 | + if (child is IBlockElement) |
| 991 | + processHTMLElement(cb, colAlignment, htmlRectangle, currentYPosition, (IBlockElement)child); |
| 992 | + |
| 993 | + } |
| 994 | + |
| 995 | + float blockElementHeight = getBlockElementHeight(blockElement, htmlRectangle); |
| 996 | + float availableSpace = currentYPosition.CurrentYPosition - htmlRectangle.GetBottom(); |
| 997 | + if (blockElementHeight > availableSpace) |
| 998 | + { |
| 999 | + GXLogging.Error(log, "You are trying to render an element of height " + blockElementHeight + " in a space of height " + availableSpace); |
| 1000 | + return; |
| 1001 | + } |
| 1002 | + |
| 1003 | + Link anchor = blockElement as Link; |
| 1004 | + if (anchor != null) |
| 1005 | + { |
| 1006 | + anchor.SetFixedPosition(this.getPage(), htmlRectangle.GetX(), currentYPosition.CurrentYPosition - blockElementHeight, htmlRectangle.GetWidth()); |
| 1007 | + document.Add((IBlockElement) anchor); |
| 1008 | + currentYPosition.CurrentYPosition = currentYPosition.CurrentYPosition - blockElementHeight; |
| 1009 | + return; |
| 1010 | + } |
| 1011 | + |
| 1012 | + Paragraph p = blockElement as Paragraph; |
| 1013 | + if (p != null) |
| 1014 | + { |
| 1015 | + p.SetFixedPosition(this.getPage(), htmlRectangle.GetX(), currentYPosition.CurrentYPosition - blockElementHeight, htmlRectangle.GetWidth()); |
| 1016 | + document.Add(p); |
| 1017 | + currentYPosition.CurrentYPosition = currentYPosition.CurrentYPosition - blockElementHeight; |
| 1018 | + return; |
| 1019 | + } |
| 1020 | + |
| 1021 | + Table table = blockElement as Table; |
| 1022 | + if (table != null) |
| 1023 | + { |
| 1024 | + table.SetFixedPosition(this.getPage(), htmlRectangle.GetX(), currentYPosition.CurrentYPosition - blockElementHeight, htmlRectangle.GetWidth()); |
| 1025 | + table.SetTextAlignment(colAlignment); |
| 1026 | + cb.Add(table); |
| 1027 | + currentYPosition.CurrentYPosition = currentYPosition.CurrentYPosition - blockElementHeight; |
| 1028 | + return; |
| 1029 | + } |
| 1030 | + |
| 1031 | + List list = blockElement as List; |
| 1032 | + if (list != null) |
| 1033 | + { |
| 1034 | + // This is a hack for the specific case of rendering a list as cb.Add(list) fails to add numeration to each element but document.Add(list) fails to |
| 1035 | + // consider the numeration of each element as part of it. Solution is to use document.Add(list) and move the list to the right. |
| 1036 | + float numWidth = new Paragraph("1. ").CreateRendererSubTree().SetParent(document.GetRenderer()).Layout(new LayoutContext(new LayoutArea(this.getPage(), htmlRectangle))).GetOccupiedArea().GetBBox().GetHeight(); |
| 1037 | + list.SetFixedPosition(this.getPage(), htmlRectangle.GetX() + numWidth, currentYPosition.CurrentYPosition - blockElementHeight, htmlRectangle.GetWidth()); |
| 1038 | + |
| 1039 | + list.SetTextAlignment(colAlignment); |
| 1040 | + document.Add(list); |
| 1041 | + currentYPosition.CurrentYPosition = currentYPosition.CurrentYPosition - blockElementHeight; |
| 1042 | + return; |
| 1043 | + } |
| 1044 | + |
| 1045 | + Image image = blockElement as Image; |
| 1046 | + if (image != null) |
| 1047 | + { |
| 1048 | + image.SetFixedPosition(this.getPage(), htmlRectangle.GetX(), currentYPosition.CurrentYPosition - blockElementHeight, htmlRectangle.GetWidth()); |
| 1049 | + image.SetTextAlignment(colAlignment); |
| 1050 | + document.Add(image); |
| 1051 | + currentYPosition.CurrentYPosition = currentYPosition.CurrentYPosition - blockElementHeight; |
| 1052 | + return; |
| 1053 | + } |
| 1054 | + |
| 1055 | + } |
| 1056 | + |
| 1057 | + private float getBlockElementHeight(IBlockElement blockElement, Rectangle htmlRectangle) |
| 1058 | + { |
| 1059 | + return blockElement.CreateRendererSubTree().SetParent(document.GetRenderer()).Layout(new LayoutContext(new LayoutArea(this.getPage(), htmlRectangle))).GetOccupiedArea().GetBBox().GetHeight(); |
| 1060 | + } |
| 1061 | + |
| 1062 | + private class YPosition |
| 1063 | + { |
| 1064 | + public YPosition(float initialYPosition) |
| 1065 | + { |
| 1066 | + CurrentYPosition = initialYPosition; |
| 1067 | + } |
| 1068 | + |
| 1069 | + public float CurrentYPosition { get; set; } |
| 1070 | + } |
| 1071 | + |
977 | 1072 | private BaseDirection? GetBaseDirection(int runDirection) |
978 | 1073 | { |
979 | 1074 | switch (runDirection) |
|
0 commit comments