private static void AddPage(
     IDocListener document,
     PdfContentByte canvas,
     Rectangle documentRectangle,
     BaseColor backgroundColor)
 {
     document.NewPage();
     TextSharpHelpers.DrawRectangle(canvas, documentRectangle, backgroundColor);
 }
        public static string DrawRectangles(
            Queue <Action <PdfContentByte, Rectangle> > drawRectangleActions,
            BaseColor backgroundColor,
            string filePrefix,
            bool drawBorders = false)
        {
            Directory.CreateDirectory(@"C:\Avery");
            const int maxColumnIndex    = 3;
            const int maxRowIndex       = 19;
            var       fileName          = $@"C:\Avery\{filePrefix}Labels{DateTime.Now.ToFileTime()}.pdf";
            var       documentRectangle = new Rectangle(0, 0, PageWidth, PageHeight);

            using (var document = new Document(documentRectangle))
            {
                using (var fileStream = new FileStream(fileName, FileMode.Create))
                {
                    using (var pdfWriter = PdfWriter.GetInstance(document, fileStream))
                    {
                        var topMargin       = Utilities.InchesToPoints(.50f);
                        var labelHeight     = Utilities.InchesToPoints(.5f);
                        var labelWidth      = Utilities.InchesToPoints(1.75f);
                        var leftMargin      = Utilities.InchesToPoints(.30f);
                        var verticalSpace   = Utilities.InchesToPoints(.000f);
                        var horizontalSpace = Utilities.InchesToPoints(.30f);
                        var extraPadding    = Utilities.InchesToPoints(.00f);
                        var rowIndex        = 0;
                        var columnIndex     = 0;
                        document.Open();
                        var canvas = pdfWriter.DirectContent;

                        while (drawRectangleActions.Any())
                        {
                            if (rowIndex == 0 && columnIndex == 0)
                            {
                                AddPage(document, canvas, documentRectangle, backgroundColor);
                            }

                            var lowerLeftX        = leftMargin + extraPadding + columnIndex * (horizontalSpace + labelWidth + extraPadding * 4);
                            var lowerLeftY        = PageHeight - (topMargin + labelHeight + extraPadding + rowIndex * (verticalSpace + labelHeight + extraPadding * 2));
                            var upperRightX       = lowerLeftX + labelWidth;
                            var upperRightY       = lowerLeftY + labelHeight;
                            var rectangle         = new Rectangle(lowerLeftX, lowerLeftY, upperRightX, upperRightY);
                            var templateRectangle = new Rectangle(rectangle.Width, rectangle.Height);

                            var template   = canvas.CreateTemplate(rectangle.Width, rectangle.Height);
                            var nextAction = drawRectangleActions.Dequeue();
                            nextAction(template, templateRectangle);
                            if (drawBorders)
                            {
                                TextSharpHelpers.DrawHollowRectangle(canvas, rectangle, BaseColor.BLACK);
                            }
                            canvas.AddTemplate(template, rectangle.Left, rectangle.Bottom);
                            rowIndex++;
                            if (rowIndex > maxRowIndex)
                            {
                                rowIndex = 0;
                                columnIndex++;
                                if (columnIndex > maxColumnIndex)
                                {
                                    columnIndex = 0;
                                }
                            }
                        }
                        document.Close();
                    }
                }
            }
            return(fileName);
        }