private static void FillPdfPageWithEnvelope(PdfPage pdfPage, string senderAddress, string recipientAddress, EnvelopeFormat format, XFont xfont)
        {
            using (XGraphics graphicsDc = XGraphics.FromPdfPage(pdfPage, XGraphicsUnit.Inch))
            {
                //sender Address Area
                float   left       = format.SenderCell.Left;
                float   top        = format.SenderCell.Top;
                float   bottom     = top + format.SenderCell.Height;
                float   right      = left + format.SenderCell.Width;
                PdfCell returnCell = new PdfCell()
                {
                    Left       = left,
                    Top        = top,
                    Width      = right - left,
                    Height     = bottom - top,
                    TopMargin  = format.SenderCell.TopMargin,
                    LeftMargin = format.SenderCell.LeftMargin
                };
                returnCell.XFont = xfont;

                if (returnCell.ShowMarkupLines)
                {
                    PaintPdfCellLines(graphicsDc, left, right, top, bottom);
                }
                PaintPdfCellText(graphicsDc, senderAddress, returnCell);

                //Recipient Address Area
                left = format.RecipientCell.Left;
                //top = format.RecipientCell.Top;
                top    = 2;
                bottom = top + format.RecipientCell.Height;
                right  = left + format.RecipientCell.Width;
                PdfCell recipientCell = new PdfCell()
                {
                    //Left = left,
                    Left      = 0,
                    Top       = top,
                    Width     = right - left,
                    Height    = bottom - top,
                    TopMargin = format.RecipientCell.TopMargin,
                    //LeftMargin = format.RecipientCell.LeftMargin
                    LeftMargin = 4
                };
                recipientCell.XFont = xfont;
                if (recipientCell.ShowMarkupLines)
                {
                    PaintPdfCellLines(graphicsDc, left, right, top, bottom);
                }
                PaintPdfCellText(graphicsDc, recipientAddress, recipientCell);
            }
        }
        private static void PaintPdfCellText(XGraphics graphicsDc, string text, PdfCell cell)
        {
            XRect rc = new XRect(cell.Left + cell.LeftMargin, cell.Top + cell.TopMargin, cell.Width - cell.LeftMargin, cell.Height);

            XStringFormat xfmt = new XStringFormat();

            xfmt.Alignment = XStringAlignment.Near;
            XTextFormatter textFmt = new XTextFormatter(graphicsDc);

            textFmt.LayoutRectangle = rc;
            textFmt.Alignment       = XParagraphAlignment.Left;
            textFmt.Font            = cell.XFont;
            XSize textSz = graphicsDc.MeasureString(text, cell.XFont, xfmt);

            if (textSz.Width > rc.Width ||
                textSz.Height > rc.Height ||
                text.Contains("\r") ||
                text.Contains("\n"))
            {
                try
                {
                    textFmt.DrawString(text ?? string.Empty,
                                       cell.XFont,
                                       XBrushes.Black,
                                       rc,
                                       xfmt);
                }
                catch
                {
                    try
                    {
                        graphicsDc.DrawString(text ?? string.Empty,
                                              cell.XFont,
                                              XBrushes.Black,
                                              rc,
                                              xfmt);
                    }
                    catch { }
                }
            }
            else
            {
                graphicsDc.DrawString(text ?? string.Empty,
                                      cell.XFont,
                                      XBrushes.Black,
                                      rc,
                                      xfmt);
            }
        }
        private static int FillPdfPageWithLabels(PdfPage pdfPage, LabelFormat labelFormat, XFont xfont, Dictionary <int, EnvelopeAddress> labels, int lastLabelIdx)
        {
            string cellText;

            using (XGraphics graphicsDc = XGraphics.FromPdfPage(pdfPage, XGraphicsUnit.Inch))
            {
                float left = labelFormat.LabelCell.Left;
                float top  = labelFormat.LabelCell.Top;
                float bottom;
                float right;
                float cellWidth     = labelFormat.LabelCell.Width;
                float cellHeight    = labelFormat.LabelCell.Height;
                float columnSpacing = labelFormat.ColumnSpacing;
                int   lablelIdx     = lastLabelIdx;

                for (int j = 0; j < labelFormat.CellsCntPerVertical; j++)
                {
                    if (lablelIdx > labels.Count)
                    {
                        break;
                    }

                    bottom = top + cellHeight;

                    for (int k = 0; k < labelFormat.CellsCntPerHorizontal; k++)
                    {
                        EnvelopeAddress envelope;
                        if (!labels.TryGetValue(lablelIdx++, out envelope))
                        {
                            continue;
                        }
                        cellText = envelope.RecipientText;

                        right = left + cellWidth;
                        if (labelFormat.LabelCell.ShowMarkupLines)
                        {
                            PaintPdfCellLines(graphicsDc, left, right, top, bottom);
                        }
                        float   rightMargin = labelFormat.LabelCell.LeftMargin;
                        PdfCell newCell     = new PdfCell()
                        {
                            Left       = left,
                            Top        = top,
                            Width      = right - left - rightMargin,
                            Height     = bottom - top,
                            LeftMargin = labelFormat.LabelCell.LeftMargin,
                            TopMargin  = labelFormat.LabelCell.TopMargin
                        };
                        newCell.XFont = xfont;

                        PaintPdfCellText(graphicsDc, cellText, newCell);

                        left = right + columnSpacing;
                    }
                    top  = bottom;
                    left = labelFormat.LabelCell.Left;
                }

                return(lablelIdx);
            }
        }