private void DrawSimulatedDiv(PdfPage page, IDictionary <int, Object> styles, float[] margins, bool drawBackground
                                      , int pageNumber, bool recalculateBodyAreaForContentSize)
        {
            Div pageBordersSimulation = new Div().SetFillAvailableArea(true);

            foreach (KeyValuePair <int, Object> entry in styles)
            {
                if ((entry.Key == Property.BACKGROUND || entry.Key == Property.BACKGROUND_IMAGE) && !drawBackground)
                {
                    continue;
                }
                pageBordersSimulation.SetProperty(entry.Key, entry.Value);
            }
            pageBordersSimulation.GetAccessibilityProperties().SetRole(StandardRoles.ARTIFACT);
            Rectangle backgroundArea = new Rectangle(page.GetTrimBox()).ApplyMargins(margins[0], margins[1], margins[2
                                                                                     ], margins[3], false);

            if (recalculateBodyAreaForContentSize)
            {
                if (pageStylesPropertiesMap.Get(pageNumber).lowestAndHighest == null)
                {
                    return;
                }
                HtmlBodyStylesApplierHandler.LowestAndHighest lowestAndHighest = pageStylesPropertiesMap.Get(pageNumber).lowestAndHighest;
                RecalculateBackgroundAreaForBody(backgroundArea, pageBordersSimulation, lowestAndHighest);
            }
            if (pdfCanvas == null)
            {
                pdfCanvas = new PdfCanvas(page.NewContentStreamBefore(), page.GetResources(), page.GetDocument());
            }
            iText.Layout.Canvas canvas = new iText.Layout.Canvas(pdfCanvas, backgroundArea);
            canvas.EnableAutoTagging(page);
            canvas.Add(pageBordersSimulation);
            canvas.Close();
        }
Exemple #2
0
 private void UpdateLowestAndHighestPoints(Rectangle rectangle, int page)
 {
     if (!pageStylesPropertiesMap.ContainsKey(page))
     {
         return;
     }
     HtmlBodyStylesApplierHandler.LowestAndHighest currentPagePoints = pageStylesPropertiesMap.Get(page).lowestAndHighest;
     if (currentPagePoints == null)
     {
         pageStylesPropertiesMap.Get(page).lowestAndHighest = new HtmlBodyStylesApplierHandler.LowestAndHighest(rectangle
                                                                                                                .GetY(), rectangle.GetY() + rectangle.GetHeight());
     }
     else
     {
         float newLowestPoint  = rectangle.GetY();
         float newHighestPoint = rectangle.GetY() + rectangle.GetHeight();
         currentPagePoints.lowest  = Math.Min(newLowestPoint, currentPagePoints.lowest);
         currentPagePoints.highest = Math.Max(newHighestPoint, currentPagePoints.highest);
     }
 }
        private void RecalculateBackgroundAreaForBody(Rectangle backgroundArea, Div pageBordersSimulation, HtmlBodyStylesApplierHandler.LowestAndHighest
                                                      lowestAndHighest)
        {
            UnitValue marginTop          = pageBordersSimulation.GetOwnProperty <UnitValue>(Property.MARGIN_TOP);
            UnitValue marginBottom       = pageBordersSimulation.GetOwnProperty <UnitValue>(Property.MARGIN_BOTTOM);
            float     marginTopWidth     = marginTop == null ? 0 : marginTop.GetValue();
            float     marginBottomWidth  = marginBottom == null ? 0 : marginBottom.GetValue();
            Border    borderTop          = pageBordersSimulation.GetOwnProperty <Border>(Property.BORDER_TOP);
            Border    borderBottom       = pageBordersSimulation.GetOwnProperty <Border>(Property.BORDER_BOTTOM);
            float     borderTopWidth     = borderTop == null ? 0 : borderTop.GetWidth();
            float     borderBottomWidth  = borderBottom == null ? 0 : borderBottom.GetWidth();
            UnitValue paddingTop         = pageBordersSimulation.GetOwnProperty <UnitValue>(Property.PADDING_TOP);
            UnitValue paddingBottom      = pageBordersSimulation.GetOwnProperty <UnitValue>(Property.PADDING_BOTTOM);
            float     paddingTopWidth    = paddingTop == null ? 0 : paddingTop.GetValue();
            float     paddingBottomWidth = paddingBottom == null ? 0 : paddingBottom.GetValue();
            float     oldHighest         = backgroundArea.GetY() + backgroundArea.GetHeight();

            if (lowestAndHighest.lowest >= backgroundArea.GetY())
            {
                backgroundArea.SetY(lowestAndHighest.lowest - paddingBottomWidth - borderBottomWidth - marginBottomWidth);
            }
            float newHighest = lowestAndHighest.highest - lowestAndHighest.lowest + paddingTopWidth + paddingBottomWidth
                               + borderTopWidth + borderBottomWidth + marginTopWidth + marginBottomWidth + backgroundArea.GetY();

            if (newHighest <= oldHighest)
            {
                backgroundArea.SetHeight(newHighest - backgroundArea.GetY());
            }
        }