Esempio n. 1
0
        /*
         * (non-Javadoc)
         *
         * @see
         * com.itextpdf.tool.xml.ITagProcessor#endElement(com.itextpdf.tool.xml.Tag,
         * java.util.List, com.itextpdf.text.Document)
         */
        public override IList <IElement> End(IWorkerContext ctx, Tag tag, IList <IElement> currentContent)
        {
            try {
                bool   percentage = false;
                String widthValue = null;
                tag.CSS.TryGetValue(HTML.Attribute.WIDTH, out widthValue);
                if (!tag.CSS.TryGetValue(HTML.Attribute.WIDTH, out widthValue) &&
                    !tag.Attributes.TryGetValue(HTML.Attribute.WIDTH, out widthValue))
                {
                    widthValue = null;
                }
                if (widthValue != null && widthValue.Trim().EndsWith("%"))
                {
                    percentage = true;
                }
                int numberOfColumns = 0;
                List <TableRowElement> tableRows          = new List <TableRowElement>(currentContent.Count);
                IList <IElement>       invalidRowElements = new List <IElement>(1);
                String repeatHeader;
                tag.CSS.TryGetValue(CSS.Property.REPEAT_HEADER, out repeatHeader);
                String repeatFooter;
                tag.CSS.TryGetValue(CSS.Property.REPEAT_FOOTER, out repeatFooter);
                int headerRows = 0;
                int footerRows = 0;
                foreach (IElement e in currentContent)
                {
                    int localNumCols = 0;
                    if (e is TableRowElement)
                    {
                        TableRowElement tableRowElement = (TableRowElement)e;
                        foreach (HtmlCell cell in tableRowElement.Content)
                        {
                            localNumCols += cell.Colspan;
                        }
                        if (localNumCols > numberOfColumns)
                        {
                            numberOfColumns = localNumCols;
                        }
                        tableRows.Add(tableRowElement);
                        if (repeatHeader != null && Util.EqualsIgnoreCase(repeatHeader, "yes") && tableRowElement.RowPlace.Equals(TableRowElement.Place.HEADER))
                        {
                            headerRows++;
                        }
                        if (repeatFooter != null && Util.EqualsIgnoreCase(repeatFooter, "yes") && tableRowElement.RowPlace.Equals(TableRowElement.Place.FOOTER))
                        {
                            footerRows++;
                        }
                    }
                    else
                    {
                        invalidRowElements.Add(e);
                    }
                }
                if (repeatFooter == null || !Util.EqualsIgnoreCase(repeatFooter, "yes"))
                {
                    InsertionSort <TableRowElement>(tableRows, delegate(TableRowElement o1, TableRowElement o2) {
                        return(o1.RowPlace.Normal.CompareTo(o2.RowPlace.Normal));
                    });
                }
                else
                {
                    InsertionSort <TableRowElement>(tableRows, delegate(TableRowElement o1, TableRowElement o2) {
                        return(o1.RowPlace.Repeated.CompareTo(o2.RowPlace.Repeated));
                    });
                }
                PdfPTable table = new PdfPTable(numberOfColumns);
                table.HeaderRows          = headerRows + footerRows;
                table.FooterRows          = footerRows;
                table.HorizontalAlignment = Element.ALIGN_LEFT;
                TableStyleValues styleValues = SetStyleValues(tag);
                table.TableEvent = new TableBorderEvent(styleValues);
                SetVerticalMargin(table, tag, styleValues, ctx);
                WidenLastCell(tableRows, styleValues.HorBorderSpacing);
                float[] columnWidths                = new float[numberOfColumns];
                float[] widestWords                 = new float[numberOfColumns];
                float[] fixedWidths                 = new float[numberOfColumns];
                float[] colspanWidestWords          = new float[numberOfColumns];
                int[]   rowspanValue                = new int[numberOfColumns];
                float   largestColumn               = 0;
                float   largestColspanColumn        = 0;
                int     indexOfLargestColumn        = -1;
                int     indexOfLargestColspanColumn = -1;

                // Initial fill of the widths arrays
                foreach (TableRowElement row in tableRows)
                {
                    int column = 0;
                    foreach (HtmlCell cell in row.Content)
                    {
                        // check whether the current column should be skipped due to a
                        // rowspan value of higher cell in this column.
                        while (rowspanValue[column] > 1)
                        {
                            rowspanValue[column] = rowspanValue[column] - 1;
                            ++column;
                        }
                        // sets a rowspan counter for current column (counter not
                        // needed for last column).
                        if (cell.Rowspan > 1 && column != numberOfColumns - 1)
                        {
                            rowspanValue[column] = cell.Rowspan - 1;
                        }
                        int colspan = cell.Colspan;
                        if (cell.FixedWidth != 0)
                        {
                            float fixedWidth = cell.FixedWidth + GetCellStartWidth(cell);
                            fixedWidth /= colspan;
                            for (int i = 0; i < colspan; i++)
                            {
                                int c = column + i;
                                if (fixedWidth > fixedWidths[c])
                                {
                                    fixedWidths[c]  = fixedWidth;
                                    columnWidths[c] = fixedWidth;
                                }
                            }
                        }
                        if (cell.CompositeElements != null)
                        {
                            float[] widthValues      = SetCellWidthAndWidestWord(cell);
                            float   cellWidth        = widthValues[0] / colspan;
                            float   widestWordOfCell = widthValues[1] / colspan;
                            for (int i = 0; i < colspan; i++)
                            {
                                int c = column + i;
                                if (fixedWidths[c] == 0 && cellWidth > columnWidths[c])
                                {
                                    columnWidths[c] = cellWidth;
                                    if (colspan == 1)
                                    {
                                        if (cellWidth > largestColumn)
                                        {
                                            largestColumn        = cellWidth;
                                            indexOfLargestColumn = c;
                                        }
                                    }
                                    else
                                    {
                                        if (cellWidth > largestColspanColumn)
                                        {
                                            largestColspanColumn        = cellWidth;
                                            indexOfLargestColspanColumn = c;
                                        }
                                    }
                                }
                                if (colspan == 1)
                                {
                                    if (widestWordOfCell > widestWords[c])
                                    {
                                        widestWords[c] = widestWordOfCell;
                                    }
                                }
                                else
                                {
                                    if (widestWordOfCell > colspanWidestWords[c])
                                    {
                                        colspanWidestWords[c] = widestWordOfCell;
                                    }
                                }
                            }
                        }
                        if (colspan > 1)
                        {
                            if (LOG.IsLogging(Level.TRACE))
                            {
                                LOG.Trace(String.Format(LocaleMessages.GetInstance().GetMessage(LocaleMessages.COLSPAN), colspan));
                            }
                            column += colspan - 1;
                        }
                        column++;
                    }
                }

                if (indexOfLargestColumn == -1)
                {
                    indexOfLargestColumn = indexOfLargestColspanColumn;
                    if (indexOfLargestColumn == -1)
                    {
                        indexOfLargestColumn = 0;
                    }

                    for (int column = 0; column < numberOfColumns; column++)
                    {
                        widestWords[column] = colspanWidestWords[column];
                    }
                }
                float outerWidth        = GetTableOuterWidth(tag, styleValues.HorBorderSpacing, ctx);
                float initialTotalWidth = GetTableWidth(columnWidths, 0);
                //          float targetWidth = calculateTargetWidth(tag, columnWidths, outerWidth, ctx);
                float targetWidth = 0;
                HtmlPipelineContext htmlPipelineContext = GetHtmlPipelineContext(ctx);
                float max             = htmlPipelineContext.PageSize.Width - outerWidth;
                bool  tableWidthFixed = false;
                if (tag.Attributes.ContainsKey(CSS.Property.WIDTH) || tag.CSS.ContainsKey(CSS.Property.WIDTH))
                {
                    targetWidth = new WidthCalculator().GetWidth(tag, htmlPipelineContext.GetRootTags(), htmlPipelineContext.PageSize.Width);
                    if (targetWidth > max)
                    {
                        targetWidth = max;
                    }
                    tableWidthFixed = true;
                }
                else if (initialTotalWidth <= max)
                {
                    targetWidth = initialTotalWidth;
                }
                else if (null == tag.Parent || (null != tag.Parent && htmlPipelineContext.GetRootTags().Contains(tag.Parent.Name)))
                {
                    targetWidth = max;
                }
                else   /* this table is an inner table and width adjustment is done in outer table */
                {
                    targetWidth = GetTableWidth(columnWidths, outerWidth);
                }
                float totalFixedColumnWidth = GetTableWidth(fixedWidths, 0);
                float targetPercentage      = 0;
                if (totalFixedColumnWidth == initialTotalWidth)   // all column widths are fixed
                {
                    targetPercentage = targetWidth / initialTotalWidth;
                    if (initialTotalWidth > targetWidth)
                    {
                        for (int column = 0; column < columnWidths.Length; column++)
                        {
                            columnWidths[column] *= targetPercentage;
                        }
                    }
                    else if (tableWidthFixed && targetPercentage != 1)
                    {
                        for (int column = 0; column < columnWidths.Length; column++)
                        {
                            columnWidths[column] *= targetPercentage;
                        }
                    }
                }
                else
                {
                    targetPercentage = (targetWidth - totalFixedColumnWidth) / (initialTotalWidth - totalFixedColumnWidth);
                    // Reduce width of columns if the columnWidth array + borders +
                    // paddings
                    // is too large for the given targetWidth.
                    if (initialTotalWidth > targetWidth)
                    {
                        float leftToReduce = 0;
                        for (int column = 0; column < columnWidths.Length; column++)
                        {
                            if (fixedWidths[column] == 0)
                            {
                                // Reduce width of the column to its targetWidth, if
                                // widestWord of column still fits in the targetWidth of
                                // the
                                // column.
                                if (widestWords[column] <= columnWidths[column] * targetPercentage)
                                {
                                    columnWidths[column] *= targetPercentage;
                                    // else take the widest word and calculate space
                                    // left to
                                    // reduce.
                                }
                                else
                                {
                                    columnWidths[column] = widestWords[column];
                                    leftToReduce        += widestWords[column] - columnWidths[column] * targetPercentage;
                                }
                                // if widestWord of a column does not fit in the
                                // fixedWidth,
                                // set the column width to the widestWord.
                            }
                            else if (fixedWidths[column] < widestWords[column])
                            {
                                columnWidths[column] = widestWords[column];
                                leftToReduce        += widestWords[column] - fixedWidths[column];
                            }
                        }
                        if (leftToReduce != 0)
                        {
                            // Reduce width of the column with the most text, if its
                            // widestWord still fits in the reduced column.
                            if (widestWords[indexOfLargestColumn] <= columnWidths[indexOfLargestColumn] - leftToReduce)
                            {
                                columnWidths[indexOfLargestColumn] -= leftToReduce;
                            }
                            else     // set all columns to their minimum, with the
                                     // widestWord array.
                            {
                                for (int column = 0; leftToReduce != 0 && column < columnWidths.Length; column++)
                                {
                                    if (fixedWidths[column] == 0 && columnWidths[column] > widestWords[column])
                                    {
                                        float difference = columnWidths[column] - widestWords[column];
                                        if (difference <= leftToReduce)
                                        {
                                            leftToReduce        -= difference;
                                            columnWidths[column] = widestWords[column];
                                        }
                                        else
                                        {
                                            columnWidths[column] -= leftToReduce;
                                            leftToReduce          = 0;
                                        }
                                    }
                                }
                                if (leftToReduce != 0)
                                {
                                    // If the table has an insufficient fixed width
                                    // by
                                    // an
                                    // attribute or style, try to enlarge the table
                                    // to
                                    // its
                                    // minimum width (= widestWords array).
                                    float pageWidth = GetHtmlPipelineContext(ctx).PageSize.Width;
                                    if (GetTableWidth(widestWords, outerWidth) < pageWidth)
                                    {
                                        targetWidth  = GetTableWidth(widestWords, outerWidth);
                                        leftToReduce = 0;
                                    }
                                    else
                                    {
                                        // If all columnWidths are set to the
                                        // widestWordWidths and the table is still
                                        // to
                                        // wide
                                        // content will fall off the edge of a page,
                                        // which
                                        // is similar to HTML.
                                        targetWidth  = pageWidth - outerWidth;
                                        leftToReduce = 0;
                                    }
                                }
                            }
                        }
                        // Enlarge width of columns to fit the targetWidth.
                    }
                    else if (initialTotalWidth < targetWidth)
                    {
                        for (int column = 0; column < columnWidths.Length; column++)
                        {
                            if (fixedWidths[column] == 0)
                            {
                                columnWidths[column] *= targetPercentage;
                            }
                        }
                    }
                }
                try {
                    table.SetTotalWidth(columnWidths);
                    table.LockedWidth        = true;
                    table.DefaultCell.Border = Rectangle.NO_BORDER;
                } catch (DocumentException e) {
                    throw new RuntimeWorkerException(LocaleMessages.GetInstance().GetMessage(LocaleMessages.NO_CUSTOM_CONTEXT), e);
                }
                float?tableHeight    = new HeightCalculator().GetHeight(tag, GetHtmlPipelineContext(ctx).PageSize.Height);
                float?tableRowHeight = null;
                if (tableHeight != null && tableHeight > 0)
                {
                    tableRowHeight = tableHeight / tableRows.Count;
                }
                int rowNumber = 0;
                foreach (TableRowElement row in tableRows)
                {
                    int   columnNumber      = -1;
                    float?computedRowHeight = null;

                    /*if ( tableHeight != null &&  tableRows.IndexOf(row) == tableRows.Count - 1) {
                     *  float computedTableHeigt = table.CalculateHeights();
                     *  computedRowHeight = tableHeight - computedTableHeigt;
                     * }*/
                    foreach (HtmlCell cell in row.Content)
                    {
                        IList <IElement> compositeElements = cell.CompositeElements;
                        if (compositeElements != null)
                        {
                            foreach (IElement baseLevel in compositeElements)
                            {
                                if (baseLevel is PdfPTable)
                                {
                                    TableStyleValues cellValues        = cell.CellValues;
                                    float            totalBordersWidth = cellValues.IsLastInRow ? styleValues.HorBorderSpacing * 2
                                            : styleValues.HorBorderSpacing;
                                    totalBordersWidth += cellValues.BorderWidthLeft + cellValues.BorderWidthRight;
                                    float columnWidth = 0;
                                    for (int currentColumnNumber = columnNumber + 1; currentColumnNumber <= columnNumber + cell.Colspan; currentColumnNumber++)
                                    {
                                        columnWidth += columnWidths[currentColumnNumber];
                                    }
                                    IPdfPTableEvent  tableEvent       = ((PdfPTable)baseLevel).TableEvent;
                                    TableStyleValues innerStyleValues = ((TableBorderEvent)tableEvent).TableStyleValues;
                                    totalBordersWidth += innerStyleValues.BorderWidthLeft;
                                    totalBordersWidth += innerStyleValues.BorderWidthRight;
                                    ((PdfPTable)baseLevel).TotalWidth = columnWidth - totalBordersWidth;
                                }
                            }
                        }
                        columnNumber += cell.Colspan;

                        table.AddCell(cell);
                    }
                    table.CompleteRow();
                    if ((computedRowHeight == null || computedRowHeight <= 0) && tableRowHeight != null)
                    {
                        computedRowHeight = tableRowHeight;
                    }
                    if (computedRowHeight != null && computedRowHeight > 0)
                    {
                        float rowHeight = table.GetRow(rowNumber).MaxHeights;
                        if (rowHeight < computedRowHeight)
                        {
                            table.GetRow(rowNumber).MaxHeights = computedRowHeight.Value;
                        }
                        else if (tableRowHeight != null && tableRowHeight < rowHeight)
                        {
                            tableRowHeight = (tableHeight - rowHeight - rowNumber * tableRowHeight)
                                             / (tableRows.Count - rowNumber - 1);
                        }
                    }
                    rowNumber++;
                }
                if (percentage)
                {
                    table.WidthPercentage = utils.ParsePxInCmMmPcToPt(widthValue);
                    table.LockedWidth     = false;
                }
                List <IElement> elems = new List <IElement>();
                if (invalidRowElements.Count > 0)
                {
                    // all invalid row elements taken as caption
                    int i          = 0;
                    Tag captionTag = tag.Children[i++];
                    while (!Util.EqualsIgnoreCase(captionTag.Name, HTML.Tag.CAPTION) && i < tag.Children.Count)
                    {
                        captionTag = tag.Children[i];
                        i++;
                    }
                    String captionSideValue;
                    captionTag.CSS.TryGetValue(CSS.Property.CAPTION_SIDE, out captionSideValue);
                    if (captionSideValue != null && Util.EqualsIgnoreCase(captionSideValue, CSS.Value.BOTTOM))
                    {
                        elems.Add(table);
                        elems.AddRange(invalidRowElements);
                    }
                    else
                    {
                        elems.AddRange(invalidRowElements);
                        elems.Add(table);
                    }
                }
                else
                {
                    elems.Add(table);
                }
                return(elems);
            } catch (NoCustomContextException e) {
                throw new RuntimeWorkerException(LocaleMessages.GetInstance().GetMessage(LocaleMessages.NO_CUSTOM_CONTEXT), e);
            }
        }
        public MemoryStream GeneratePdfTemplate(CostCalculationRetailViewModel viewModel)
        {
            BaseFont bf          = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1250, BaseFont.NOT_EMBEDDED);
            BaseFont bf_bold     = BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1250, BaseFont.NOT_EMBEDDED);
            Font     normal_font = FontFactory.GetFont(BaseFont.HELVETICA, BaseFont.CP1250, BaseFont.NOT_EMBEDDED, 7);
            Font     bold_font   = FontFactory.GetFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1250, BaseFont.NOT_EMBEDDED, 7);
            DateTime now         = DateTime.Now;

            Document     document = new Document(PageSize.A4, 10, 10, 10, 10);
            MemoryStream stream   = new MemoryStream();
            PdfWriter    writer   = PdfWriter.GetInstance(document, stream);

            writer.CloseStream = false;
            document.Open();
            PdfContentByte cb = writer.DirectContent;

            float    margin          = 10;
            float    printedOnHeight = 10;
            float    startY          = 840 - margin;
            PdfPCell cell_colon      = new PdfPCell()
            {
                Border = Rectangle.NO_BORDER, HorizontalAlignment = Element.ALIGN_LEFT, VerticalAlignment = Element.ALIGN_MIDDLE, Phrase = new Phrase(":", normal_font)
            };

            #region Header
            cb.BeginText();
            cb.SetFontAndSize(bf, 10);
            cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "PT. EFRATA RETAILINDO", 10, 820, 0);
            cb.SetFontAndSize(bf_bold, 12);
            cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "BUDGET PRODUCTION", 10, 805, 0);
            cb.EndText();
            #endregion

            #region Detail 1 (Top)
            PdfPTable table_detail1 = new PdfPTable(8);
            table_detail1.TotalWidth = 570f;

            float[] detail1_widths = new float[] { 1f, 0.1f, 2f, 3f, 1f, 0.1f, 2f, 3f };
            table_detail1.SetWidths(detail1_widths);

            PdfPCell cell_detail1 = new PdfPCell()
            {
                Border = Rectangle.NO_BORDER, HorizontalAlignment = Element.ALIGN_LEFT, VerticalAlignment = Element.ALIGN_MIDDLE, PaddingRight = 1, PaddingBottom = 2, PaddingTop = 2
            };

            cell_detail1.Phrase = new Phrase("RO", normal_font);
            table_detail1.AddCell(cell_detail1);
            table_detail1.AddCell(cell_colon);
            cell_detail1.Phrase = new Phrase($"{viewModel.RO}", normal_font);
            table_detail1.AddCell(cell_detail1);
            cell_detail1.Phrase = new Phrase("", normal_font);
            table_detail1.AddCell(cell_detail1);
            cell_detail1.Phrase = new Phrase("SEASON", normal_font);
            table_detail1.AddCell(cell_detail1);
            table_detail1.AddCell(cell_colon);
            cell_detail1.Phrase = new Phrase($"{viewModel.Season.name}", normal_font);
            table_detail1.AddCell(cell_detail1);
            cell_detail1.Phrase = new Phrase("", normal_font);
            table_detail1.AddCell(cell_detail1);
            #endregion

            #region Draw Detail 1
            float row1Y = 800;
            table_detail1.WriteSelectedRows(0, -1, 10, row1Y, cb);
            #endregion

            #region Detail 2 (Bottom, Column 1)
            PdfPTable table_detail2 = new PdfPTable(2);
            table_detail2.TotalWidth = 230f;

            float[] detail2_widths = new float[] { 2f, 5f };
            table_detail2.SetWidths(detail2_widths);

            PdfPCell cell_detail2 = new PdfPCell()
            {
                Border = Rectangle.TOP_BORDER | Rectangle.LEFT_BORDER | Rectangle.BOTTOM_BORDER | Rectangle.RIGHT_BORDER, HorizontalAlignment = Element.ALIGN_LEFT, VerticalAlignment = Element.ALIGN_MIDDLE, PaddingRight = 2, PaddingBottom = 7, PaddingLeft = 2, PaddingTop = 7
            };

            cell_detail2.Phrase = new Phrase("BUYER", normal_font);
            table_detail2.AddCell(cell_detail2);
            cell_detail2.Phrase = new Phrase($"{viewModel.Buyer.Name}", normal_font);
            table_detail2.AddCell(cell_detail2);

            cell_detail2.Phrase = new Phrase("ARTICLE", normal_font);
            table_detail2.AddCell(cell_detail2);
            cell_detail2.Phrase = new Phrase($"{viewModel.Article}", normal_font);
            table_detail2.AddCell(cell_detail2);

            cell_detail2.Phrase = new Phrase("DESCRIPTION", normal_font);
            table_detail2.AddCell(cell_detail2);
            cell_detail2.Phrase = new Phrase($"{viewModel.Description}", normal_font);
            table_detail2.AddCell(cell_detail2);

            cell_detail2.Phrase = new Phrase("QTY", normal_font);
            table_detail2.AddCell(cell_detail2);
            cell_detail2.Phrase = new Phrase($"{viewModel.Quantity} PCS", normal_font);
            table_detail2.AddCell(cell_detail2);

            cell_detail2.Phrase = new Phrase("DELIVERY", normal_font);
            table_detail2.AddCell(cell_detail2);
            cell_detail2.Phrase = new Phrase($"{viewModel.DeliveryDate.ToString("dd/MM/yyyy")}", normal_font);
            table_detail2.AddCell(cell_detail2);
            #endregion

            #region Signature
            PdfPTable table_signature = new PdfPTable(5);
            table_signature.TotalWidth = 570f;

            float[] signature_widths = new float[] { 1f, 1f, 1f, 1f, 1f };
            table_signature.SetWidths(signature_widths);

            PdfPCell cell_signature = new PdfPCell()
            {
                Border = Rectangle.NO_BORDER, HorizontalAlignment = Element.ALIGN_CENTER, VerticalAlignment = Element.ALIGN_MIDDLE, Padding = 2
            };

            cell_signature.Phrase = new Phrase("Membuat,", normal_font);
            table_signature.AddCell(cell_signature);
            cell_signature.Phrase = new Phrase("", normal_font);
            table_signature.AddCell(cell_signature);
            cell_signature.Phrase = new Phrase("Mengetahui,", normal_font);
            table_signature.AddCell(cell_signature);
            cell_signature.Phrase = new Phrase("", normal_font);
            table_signature.AddCell(cell_signature);
            cell_signature.Phrase = new Phrase("Menyetujui,", normal_font);
            table_signature.AddCell(cell_signature);

            string signatureArea = string.Empty;
            for (int i = 0; i < 4; i++)
            {
                signatureArea += Environment.NewLine;
            }

            cell_signature.Phrase = new Phrase(signatureArea, normal_font);
            table_signature.AddCell(cell_signature);
            table_signature.AddCell(cell_signature);
            table_signature.AddCell(cell_signature);
            table_signature.AddCell(cell_signature);
            table_signature.AddCell(cell_signature);

            cell_signature.Phrase = new Phrase("Merchandiser", normal_font);
            table_signature.AddCell(cell_signature);
            cell_signature.Phrase = new Phrase("Ka. Sie Merchandiser", normal_font);
            table_signature.AddCell(cell_signature);
            cell_signature.Phrase = new Phrase("Ka. Sie Pembelian", normal_font);
            table_signature.AddCell(cell_signature);
            cell_signature.Phrase = new Phrase("Ka. Bag Produksi", normal_font);
            table_signature.AddCell(cell_signature);
            cell_signature.Phrase = new Phrase("Direktur Marketing", normal_font);
            table_signature.AddCell(cell_signature);
            #endregion

            #region Cost Calculation Material
            PdfPTable table_ccm = new PdfPTable(10);
            table_ccm.TotalWidth = 570f;

            float[] ccm_widths = new float[] { 1f, 3f, 4f, 6f, 2f, 3f, 3f, 2f, 3f, 3f };
            table_ccm.SetWidths(ccm_widths);

            PdfPCell cell_ccm = new PdfPCell()
            {
                Border = Rectangle.TOP_BORDER | Rectangle.LEFT_BORDER | Rectangle.BOTTOM_BORDER | Rectangle.RIGHT_BORDER, HorizontalAlignment = Element.ALIGN_CENTER, VerticalAlignment = Element.ALIGN_MIDDLE, Padding = 2
            };

            cell_ccm.Phrase = new Phrase("NO", bold_font);
            table_ccm.AddCell(cell_ccm);
            cell_ccm.Phrase = new Phrase("CATEGORIES", bold_font);
            table_ccm.AddCell(cell_ccm);
            cell_ccm.Phrase = new Phrase("MATERIALS", bold_font);
            table_ccm.AddCell(cell_ccm);
            cell_ccm.Phrase = new Phrase("DESCRIPTION", bold_font);
            table_ccm.AddCell(cell_ccm);
            cell_ccm.Phrase = new Phrase("USAGE", bold_font);
            table_ccm.AddCell(cell_ccm);
            cell_ccm.Phrase = new Phrase("PRICE", bold_font);
            table_ccm.AddCell(cell_ccm);
            cell_ccm.Phrase = new Phrase("QUANTITY", bold_font);
            table_ccm.AddCell(cell_ccm);
            cell_ccm.Phrase = new Phrase("UNIT", bold_font);
            table_ccm.AddCell(cell_ccm);
            cell_ccm.Phrase = new Phrase("AMOUNT", bold_font);
            table_ccm.AddCell(cell_ccm);
            cell_ccm.Phrase = new Phrase("PO NUMBER", bold_font);
            table_ccm.AddCell(cell_ccm);

            float  row2Y               = row1Y - table_detail1.TotalHeight - 10;
            float  row3Height          = table_detail2.TotalHeight;
            float  row2RemainingHeight = row2Y - 10 - row3Height - printedOnHeight - margin;
            float  row2AllowedHeight   = row2Y - printedOnHeight - margin;
            double totalBudget         = 0;

            #region Process Cost
            double ol          = viewModel.OL.CalculatedValue ?? 0;
            double processCost = ol;
            #endregion

            for (int i = 0; i < viewModel.CostCalculationRetail_Materials.Count; i++)
            {
                cell_ccm.Phrase = new Phrase((i + 1).ToString(), normal_font);
                table_ccm.AddCell(cell_ccm);

                cell_ccm.HorizontalAlignment = Element.ALIGN_LEFT;

                cell_ccm.Phrase = new Phrase(viewModel.CostCalculationRetail_Materials[i].Category.SubCategory != null ? String.Format("{0} - {1}", viewModel.CostCalculationRetail_Materials[i].Category.Name, viewModel.CostCalculationRetail_Materials[i].Category.SubCategory) : viewModel.CostCalculationRetail_Materials[i].Category.Name, normal_font);
                table_ccm.AddCell(cell_ccm);

                cell_ccm.Phrase = new Phrase(viewModel.CostCalculationRetail_Materials[i].Material.Name, normal_font);
                table_ccm.AddCell(cell_ccm);

                cell_ccm.Phrase = new Phrase(viewModel.CostCalculationRetail_Materials[i].Description, normal_font);
                table_ccm.AddCell(cell_ccm);

                cell_ccm.HorizontalAlignment = Element.ALIGN_RIGHT;

                double usage = viewModel.CostCalculationRetail_Materials[i].Quantity ?? 0;
                cell_ccm.Phrase = new Phrase(usage.ToString(), normal_font);
                table_ccm.AddCell(cell_ccm);

                double price = viewModel.CostCalculationRetail_Materials[i].Price ?? 0;
                cell_ccm.Phrase = new Phrase(String.Format("{0}/{1}", Number.ToRupiahWithoutSymbol(price), viewModel.CostCalculationRetail_Materials[i].UOMPrice.Name), normal_font);
                table_ccm.AddCell(cell_ccm);

                double factor;
                if (viewModel.CostCalculationRetail_Materials[i].Category.Name == "ACC")
                {
                    factor = viewModel.AccessoriesAllowance ?? 0;
                }
                else
                {
                    factor = viewModel.FabricAllowance ?? 0;
                }
                double totalQuantity   = viewModel.Quantity ?? 0;
                double conversion      = (double)viewModel.CostCalculationRetail_Materials[i].Conversion;
                double usageConversion = usage / conversion;
                double quantity        = (100 + factor) / 100 * usageConversion * totalQuantity;

                quantity = Math.Ceiling(quantity);

                cell_ccm.Phrase = new Phrase(quantity.ToString(), normal_font);
                table_ccm.AddCell(cell_ccm);

                cell_ccm.HorizontalAlignment = Element.ALIGN_CENTER;
                cell_ccm.Phrase = new Phrase(viewModel.CostCalculationRetail_Materials[i].UOMPrice.Name, normal_font);
                table_ccm.AddCell(cell_ccm);

                cell_ccm.HorizontalAlignment = Element.ALIGN_RIGHT;
                double amount = quantity * price;
                cell_ccm.Phrase = new Phrase(Number.ToRupiahWithoutSymbol(amount), normal_font);
                table_ccm.AddCell(cell_ccm);

                cell_ccm.HorizontalAlignment = Element.ALIGN_CENTER;
                cell_ccm.Phrase = new Phrase(viewModel.CostCalculationRetail_Materials[i].PO, normal_font);
                table_ccm.AddCell(cell_ccm);

                totalBudget += amount;
                float currentHeight = table_ccm.TotalHeight;
                if (currentHeight / row2RemainingHeight > 1)
                {
                    if (currentHeight / row2AllowedHeight > 1)
                    {
                        PdfPRow headerRow = table_ccm.GetRow(0);
                        PdfPRow lastRow   = table_ccm.GetRow(table_ccm.Rows.Count - 1);
                        table_ccm.DeleteLastRow();
                        table_ccm.WriteSelectedRows(0, -1, 10, row2Y, cb);
                        table_ccm.DeleteBodyRows();
                        this.DrawPrintedOn(now, bf, cb);
                        document.NewPage();
                        table_ccm.Rows.Add(headerRow);
                        table_ccm.Rows.Add(lastRow);
                        table_ccm.CalculateHeights();
                        row2Y = startY;
                        row2RemainingHeight = row2Y - 10 - row3Height - printedOnHeight - margin;
                        row2AllowedHeight   = row2Y - printedOnHeight - margin;
                    }
                }
            }
            #endregion

            #region Detail 3 (Bottom, Column 2)
            PdfPTable table_detail3 = new PdfPTable(8);
            table_detail3.TotalWidth = 330f;

            float[] detail3_widths = new float[] { 3.25f, 4.75f, 1.9f, 0.2f, 1.9f, 1.9f, 0.2f, 1.9f };
            table_detail3.SetWidths(detail3_widths);

            //double budgetCost = viewModel.HPP;
            double totalProcessCost = processCost * (double)viewModel.Quantity;
            double budgetCost       = totalBudget / (double)viewModel.Quantity;

            PdfPCell cell_detail3 = new PdfPCell()
            {
                HorizontalAlignment = Element.ALIGN_LEFT, VerticalAlignment = Element.ALIGN_MIDDLE, PaddingRight = 2, PaddingBottom = 7, PaddingLeft = 2, PaddingTop = 7
            };
            PdfPCell cell_detail3_right = new PdfPCell()
            {
                HorizontalAlignment = Element.ALIGN_RIGHT, VerticalAlignment = Element.ALIGN_MIDDLE, PaddingRight = 2, PaddingBottom = 7, PaddingLeft = 2, PaddingTop = 7
            };
            PdfPCell cell_detail3_colspan6 = new PdfPCell()
            {
                Border = Rectangle.TOP_BORDER | Rectangle.LEFT_BORDER | Rectangle.RIGHT_BORDER, HorizontalAlignment = Element.ALIGN_LEFT, VerticalAlignment = Element.ALIGN_MIDDLE, PaddingRight = 2, PaddingBottom = 7, PaddingLeft = 2, PaddingTop = 7, Colspan = 6
            };
            PdfPCell cell_detail3_colspan8 = new PdfPCell()
            {
                Border = Rectangle.TOP_BORDER | Rectangle.LEFT_BORDER | Rectangle.BOTTOM_BORDER | Rectangle.RIGHT_BORDER, HorizontalAlignment = Element.ALIGN_LEFT, VerticalAlignment = Element.ALIGN_MIDDLE, PaddingRight = 2, PaddingBottom = 7, PaddingLeft = 2, PaddingTop = 7, Colspan = 8
            };

            cell_detail3.Border = Rectangle.TOP_BORDER | Rectangle.LEFT_BORDER;
            cell_detail3.Phrase = new Phrase("TOTAL BUDGET", normal_font);
            table_detail3.AddCell(cell_detail3);
            cell_detail3.Border = Rectangle.TOP_BORDER | Rectangle.RIGHT_BORDER;
            cell_detail3.Phrase = new Phrase($"{Number.ToRupiah(totalBudget)}", normal_font);
            table_detail3.AddCell(cell_detail3);
            cell_detail3_colspan6.Phrase = new Phrase("STANDARD HOURS", normal_font);
            table_detail3.AddCell(cell_detail3_colspan6);

            cell_detail3.Border = Rectangle.LEFT_BORDER;
            cell_detail3.Phrase = new Phrase("", normal_font);
            table_detail3.AddCell(cell_detail3);
            cell_detail3.Border = Rectangle.RIGHT_BORDER;
            table_detail3.AddCell(cell_detail3);
            cell_detail3.Border = Rectangle.LEFT_BORDER;
            cell_detail3.Phrase = new Phrase("SMV. CUT", normal_font);
            table_detail3.AddCell(cell_detail3);
            table_detail3.AddCell(cell_colon);
            cell_detail3.Border = Rectangle.NO_BORDER;
            double SH_Cutting = viewModel.SH_Cutting ?? 0;
            cell_detail3.Phrase = new Phrase($"{SH_Cutting}", normal_font);
            table_detail3.AddCell(cell_detail3);
            cell_detail3.Border = Rectangle.NO_BORDER;
            cell_detail3.Phrase = new Phrase("SMV. SEW", normal_font);
            table_detail3.AddCell(cell_detail3);
            table_detail3.AddCell(cell_colon);
            cell_detail3.Border = Rectangle.RIGHT_BORDER;
            double SH_Sewing = viewModel.SH_Sewing ?? 0;
            cell_detail3.Phrase = new Phrase($"{SH_Sewing}", normal_font);
            table_detail3.AddCell(cell_detail3);

            cell_detail3.Border = Rectangle.BOTTOM_BORDER | Rectangle.LEFT_BORDER;
            cell_detail3.Phrase = new Phrase("", normal_font);
            table_detail3.AddCell(cell_detail3);
            cell_detail3.Border = Rectangle.BOTTOM_BORDER | Rectangle.RIGHT_BORDER;
            cell_detail3.Phrase = new Phrase("", normal_font);
            table_detail3.AddCell(cell_detail3);
            cell_detail3.Border = Rectangle.BOTTOM_BORDER | Rectangle.LEFT_BORDER;
            cell_detail3.Phrase = new Phrase("SMV. FIN", normal_font);
            table_detail3.AddCell(cell_detail3);
            table_detail3.AddCell(cell_colon);
            cell_detail3.Border = Rectangle.BOTTOM_BORDER;
            double SH_Finishing = viewModel.SH_Finishing ?? 0;
            cell_detail3.Phrase = new Phrase($"{SH_Finishing}", normal_font);
            table_detail3.AddCell(cell_detail3);
            cell_detail3.Border = Rectangle.BOTTOM_BORDER;
            cell_detail3.Phrase = new Phrase("SMV. TOT", normal_font);
            table_detail3.AddCell(cell_detail3);
            table_detail3.AddCell(cell_colon);
            cell_detail3.Border = Rectangle.BOTTOM_BORDER | Rectangle.RIGHT_BORDER;
            double SH_Total = SH_Cutting + SH_Sewing + SH_Finishing;
            cell_detail3.Phrase = new Phrase($"{SH_Total}", normal_font);
            table_detail3.AddCell(cell_detail3);

            cell_detail3_colspan8.Phrase = new Phrase("BUDGET COST / PCS" + "".PadRight(5) + $"{Number.ToRupiah(budgetCost)}", normal_font);
            table_detail3.AddCell(cell_detail3_colspan8);
            cell_detail3_colspan8.Phrase = new Phrase("PROCESS COST" + "".PadRight(5) + $"{Number.ToRupiah(processCost)}", normal_font);
            table_detail3.AddCell(cell_detail3_colspan8);
            cell_detail3_colspan8.Phrase = new Phrase("TOTAL PROCESS COST" + "".PadRight(5) + $"{Number.ToRupiah(totalProcessCost)}", normal_font);
            table_detail3.AddCell(cell_detail3_colspan8);
            #endregion

            #region Draw Others
            table_ccm.WriteSelectedRows(0, -1, 10, row2Y, cb);

            float row3Y = row2Y - table_ccm.TotalHeight - 10;
            float row3RemainigHeight = row3Y - printedOnHeight - margin;
            if (row3RemainigHeight < row3Height)
            {
                this.DrawPrintedOn(now, bf, cb);
                row3Y = startY;
                document.NewPage();
            }

            table_detail2.WriteSelectedRows(0, -1, margin, row3Y, cb);

            table_detail3.WriteSelectedRows(0, -1, margin + table_detail2.TotalWidth + 10, row3Y, cb);

            float signatureY = row3Y - row3Height - 10;
            signatureY = signatureY - 20;
            float signatureRemainingHeight = signatureY - printedOnHeight - margin;
            if (signatureRemainingHeight < table_signature.TotalHeight)
            {
                this.DrawPrintedOn(now, bf, cb);
                signatureY = startY;
                document.NewPage();
            }
            table_signature.WriteSelectedRows(0, -1, margin, signatureY, cb);

            this.DrawPrintedOn(now, bf, cb);
            #endregion

            document.Close();

            byte[] byteInfo = stream.ToArray();
            stream.Write(byteInfo, 0, byteInfo.Length);
            stream.Position = 0;

            return(stream);
        }
Esempio n. 3
0
        public MemoryStream GeneratePdfTemplate(CostCalculationRetailViewModel viewModel)
        {
            BaseFont bf          = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1250, BaseFont.NOT_EMBEDDED);
            BaseFont bf_bold     = BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1250, BaseFont.NOT_EMBEDDED);
            Font     normal_font = FontFactory.GetFont(BaseFont.HELVETICA, BaseFont.CP1250, BaseFont.NOT_EMBEDDED, 7);
            Font     bold_font   = FontFactory.GetFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1250, BaseFont.NOT_EMBEDDED, 7);
            Font     bold_font_8 = FontFactory.GetFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1250, BaseFont.NOT_EMBEDDED, 8);
            Font     font_9      = FontFactory.GetFont(BaseFont.HELVETICA, BaseFont.CP1250, BaseFont.NOT_EMBEDDED, 9);
            DateTime now         = DateTime.Now;

            Document     document = new Document(PageSize.A4, 10, 10, 10, 10);
            MemoryStream stream   = new MemoryStream();
            PdfWriter    writer   = PdfWriter.GetInstance(document, stream);

            writer.CloseStream = false;
            document.Open();
            PdfContentByte cb = writer.DirectContent;

            float margin          = 10;
            float printedOnHeight = 10;
            float startY          = 840 - margin;

            #region Header
            cb.BeginText();
            cb.SetFontAndSize(bf, 10);
            cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "PT. EFRATA RETAILINDO", 10, 820, 0);
            cb.SetFontAndSize(bf_bold, 12);
            cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "COST CALCULATION RETAIL", 10, 805, 0);
            cb.EndText();
            #endregion

            #region Top
            PdfPTable table_top = new PdfPTable(9);
            table_top.TotalWidth = 500f;

            float[] top_widths = new float[] { 1f, 0.1f, 2f, 1f, 0.1f, 2f, 1f, 0.1f, 2f };
            table_top.SetWidths(top_widths);

            PdfPCell cell_top = new PdfPCell()
            {
                Border = Rectangle.NO_BORDER, HorizontalAlignment = Element.ALIGN_LEFT, VerticalAlignment = Element.ALIGN_MIDDLE, PaddingRight = 1, PaddingBottom = 2, PaddingTop = 2
            };
            PdfPCell cell_colon = new PdfPCell()
            {
                Border = Rectangle.NO_BORDER, HorizontalAlignment = Element.ALIGN_LEFT, VerticalAlignment = Element.ALIGN_MIDDLE
            };
            PdfPCell cell_top_keterangan = new PdfPCell()
            {
                Border = Rectangle.NO_BORDER, HorizontalAlignment = Element.ALIGN_LEFT, VerticalAlignment = Element.ALIGN_MIDDLE, PaddingRight = 1, PaddingBottom = 2, PaddingTop = 2, Colspan = 7
            };
            cell_colon.Phrase = new Phrase(":", normal_font);

            cell_top.Phrase = new Phrase("RO", normal_font);
            table_top.AddCell(cell_top);
            table_top.AddCell(cell_colon);
            cell_top.Phrase = new Phrase($"{viewModel.RO}", normal_font);
            table_top.AddCell(cell_top);
            cell_top.Phrase = new Phrase("BUYER", normal_font);
            table_top.AddCell(cell_top);
            table_top.AddCell(cell_colon);
            cell_top.Phrase = new Phrase($"{viewModel.Buyer.Name}", normal_font);
            table_top.AddCell(cell_top);
            cell_top.Phrase = new Phrase("OL", normal_font);
            table_top.AddCell(cell_top);
            table_top.AddCell(cell_colon);
            double OLValue = viewModel.OL.Value ?? 0;
            string OL      = OLValue > 0 ? OLValue.ToString() + " menit" : OLValue.ToString();
            cell_top.Phrase = new Phrase($"{OL}", normal_font);
            table_top.AddCell(cell_top);

            cell_top.Phrase = new Phrase("ARTICLE", normal_font);
            table_top.AddCell(cell_top);
            table_top.AddCell(cell_colon);
            cell_top.Phrase = new Phrase($"{viewModel.Article}", normal_font);
            table_top.AddCell(cell_top);
            cell_top.Phrase = new Phrase("DELIVERY", normal_font);
            table_top.AddCell(cell_top);
            table_top.AddCell(cell_colon);
            cell_top.Phrase = new Phrase($"{viewModel.DeliveryDate.ToString("dd MMMM yyyy")}", normal_font);
            table_top.AddCell(cell_top);
            cell_top.Phrase = new Phrase("OTL 1", normal_font);
            table_top.AddCell(cell_top);
            table_top.AddCell(cell_colon);
            double OTL1Value = viewModel.OTL1.Value ?? 0;
            string OTL1      = OTL1Value > 0 ? OTL1Value.ToString() + " detik" : OTL1Value.ToString();
            cell_top.Phrase = new Phrase($"{OTL1}", normal_font);
            table_top.AddCell(cell_top);

            cell_top.Phrase = new Phrase("STYLE", normal_font);
            table_top.AddCell(cell_top);
            table_top.AddCell(cell_colon);
            cell_top.Phrase = new Phrase($"{viewModel.Style.name}", normal_font);
            table_top.AddCell(cell_top);
            cell_top.Phrase = new Phrase("SIZE RANGE", normal_font);
            table_top.AddCell(cell_top);
            table_top.AddCell(cell_colon);
            cell_top.Phrase = new Phrase($"{viewModel.SizeRange.Name}", normal_font);
            table_top.AddCell(cell_top);
            cell_top.Phrase = new Phrase("OTL 2", normal_font);
            table_top.AddCell(cell_top);
            table_top.AddCell(cell_colon);
            double OTL2Value = viewModel.OTL2.Value ?? 0;
            string OTL2      = OTL2Value > 0 ? OTL2Value.ToString() + " detik" : OTL2Value.ToString();
            cell_top.Phrase = new Phrase($"{OTL2}", normal_font);
            table_top.AddCell(cell_top);

            cell_top.Phrase = new Phrase("SEASON", normal_font);
            table_top.AddCell(cell_top);
            table_top.AddCell(cell_colon);
            cell_top.Phrase = new Phrase($"{viewModel.Season.name}", normal_font);
            table_top.AddCell(cell_top);
            cell_top.Phrase = new Phrase("EFFICIENCY", normal_font);
            table_top.AddCell(cell_top);
            table_top.AddCell(cell_colon);
            cell_top.Phrase = new Phrase($"{viewModel.Efficiency.Value}%", normal_font);
            table_top.AddCell(cell_top);
            cell_top.Phrase = new Phrase("OTL 3", normal_font);
            table_top.AddCell(cell_top);
            table_top.AddCell(cell_colon);
            double OTL3Value = viewModel.OTL3.Value ?? 0;
            string OTL3      = OTL3Value > 0 ? OTL3Value.ToString() + " detik" : OTL3Value.ToString();
            cell_top.Phrase = new Phrase($"{OTL3}", normal_font);
            table_top.AddCell(cell_top);

            cell_top.Phrase = new Phrase("COUNTER", normal_font);
            table_top.AddCell(cell_top);
            table_top.AddCell(cell_colon);
            cell_top.Phrase = new Phrase($"{viewModel.Counter.name}", normal_font);
            table_top.AddCell(cell_top);
            cell_top.Phrase = new Phrase("RISK", normal_font);
            table_top.AddCell(cell_top);
            table_top.AddCell(cell_colon);
            cell_top.Phrase = new Phrase($"{viewModel.Risk}%", normal_font);
            table_top.AddCell(cell_top);
            cell_top.Phrase = new Phrase("TOTAL SMV", normal_font);
            table_top.AddCell(cell_top);
            table_top.AddCell(cell_colon);
            double STD_HourValue = viewModel.SH_Cutting.Value + viewModel.SH_Finishing.Value + viewModel.SH_Sewing.Value;
            string STD_Hour      = STD_HourValue > 0 ? STD_HourValue.ToString() : STD_HourValue.ToString();
            cell_top.Phrase = new Phrase($"{STD_Hour}", normal_font);
            table_top.AddCell(cell_top);

            cell_top.Phrase = new Phrase("KETERANGAN", normal_font);
            table_top.AddCell(cell_top);
            table_top.AddCell(cell_colon);
            cell_top_keterangan.Phrase = new Phrase($"{viewModel.Description}", normal_font);
            table_top.AddCell(cell_top_keterangan);
            #endregion

            #region Draw Image
            float imageHeight;
            try
            {
                byte[] imageByte = Convert.FromBase64String(Base64.GetBase64File(viewModel.ImageFile));
                Image  image     = Image.GetInstance(imgb: imageByte);
                if (image.Width > 60)
                {
                    float percentage = 0.0f;
                    percentage = 60 / image.Width;
                    image.ScalePercent(percentage * 100);
                }
                imageHeight = image.ScaledHeight;
                float imageY = 800 - imageHeight;
                image.SetAbsolutePosition(520, imageY);
                cb.AddImage(image, inlineImage: true);
            }
            catch (Exception)
            {
                imageHeight = 0;
            }
            #endregion

            #region Draw Top
            float row1Y = 800;
            table_top.WriteSelectedRows(0, -1, 10, row1Y, cb);
            #endregion

            #region Detail (Bottom, Column 1.2)
            PdfPTable table_detail = new PdfPTable(2);
            table_detail.TotalWidth = 280f;

            float[] detail_widths = new float[] { 1f, 1f };
            table_detail.SetWidths(detail_widths);

            PdfPCell cell_detail = new PdfPCell()
            {
                Border = Rectangle.TOP_BORDER | Rectangle.LEFT_BORDER | Rectangle.BOTTOM_BORDER | Rectangle.RIGHT_BORDER, HorizontalAlignment = Element.ALIGN_LEFT, VerticalAlignment = Element.ALIGN_MIDDLE, Padding = 5, Rowspan = 4
            };

            double total = Convert.ToDouble(viewModel.OL.CalculatedValue + viewModel.OTL1.CalculatedValue + viewModel.OTL2.CalculatedValue + viewModel.OTL3.CalculatedValue);
            cell_detail.Phrase = new Phrase(
                "OL".PadRight(22) + ": " + viewModel.OL.CalculatedValue + Environment.NewLine + Environment.NewLine +
                "OTL 1".PadRight(20) + ": " + viewModel.OTL1.CalculatedValue + Environment.NewLine + Environment.NewLine +
                "OTL 2".PadRight(20) + ": " + viewModel.OTL2.CalculatedValue + Environment.NewLine + Environment.NewLine +
                "OTL 3".PadRight(20) + ": " + viewModel.OTL3.CalculatedValue + Environment.NewLine + Environment.NewLine +
                "Total".PadRight(22) + ": " + total + Environment.NewLine
                , normal_font);
            table_detail.AddCell(cell_detail);

            cell_detail = new PdfPCell()
            {
                Border = Rectangle.TOP_BORDER | Rectangle.LEFT_BORDER | Rectangle.RIGHT_BORDER, HorizontalAlignment = Element.ALIGN_LEFT, VerticalAlignment = Element.ALIGN_BOTTOM, Padding = 5
            };
            cell_detail.Phrase = new Phrase("HPP", normal_font);
            table_detail.AddCell(cell_detail);
            cell_detail = new PdfPCell()
            {
                Border = Rectangle.LEFT_BORDER | Rectangle.BOTTOM_BORDER | Rectangle.RIGHT_BORDER, HorizontalAlignment = Element.ALIGN_LEFT, VerticalAlignment = Element.ALIGN_TOP, Padding = 5
            };
            cell_detail.Phrase = new Phrase(Number.ToRupiah(viewModel.HPP), font_9);
            table_detail.AddCell(cell_detail);
            cell_detail = new PdfPCell()
            {
                Border = Rectangle.LEFT_BORDER | Rectangle.RIGHT_BORDER, HorizontalAlignment = Element.ALIGN_LEFT, VerticalAlignment = Element.ALIGN_BOTTOM, Padding = 5
            };
            cell_detail.Phrase = new Phrase("Wholesale Price: HPP X 2.20", normal_font);
            table_detail.AddCell(cell_detail);
            cell_detail = new PdfPCell()
            {
                Border = Rectangle.LEFT_BORDER | Rectangle.BOTTOM_BORDER | Rectangle.RIGHT_BORDER, HorizontalAlignment = Element.ALIGN_LEFT, VerticalAlignment = Element.ALIGN_TOP, Padding = 5
            };
            cell_detail.Phrase = new Phrase(Number.ToRupiah(viewModel.WholesalePrice), font_9);
            table_detail.AddCell(cell_detail);
            #endregion

            #region Signature (Bottom, Column 1.2)
            PdfPTable table_signature = new PdfPTable(3);
            table_signature.TotalWidth = 280f;

            float[] signature_widths = new float[] { 1f, 1f, 1f };
            table_signature.SetWidths(signature_widths);

            PdfPCell cell_signature = new PdfPCell()
            {
                Border = Rectangle.NO_BORDER, HorizontalAlignment = Element.ALIGN_CENTER, VerticalAlignment = Element.ALIGN_MIDDLE, Padding = 2
            };

            cell_signature.Phrase = new Phrase("Mengetahui,", normal_font);
            table_signature.AddCell(cell_signature);
            cell_signature.Phrase = new Phrase("", normal_font);
            table_signature.AddCell(cell_signature);
            cell_signature.Phrase = new Phrase("Menyetujui,", normal_font);
            table_signature.AddCell(cell_signature);
            cell_signature.Phrase = new Phrase("Ka. Sie Merchandiser", normal_font);
            table_signature.AddCell(cell_signature);
            cell_signature.Phrase = new Phrase("Direktur Operasional", normal_font);
            table_signature.AddCell(cell_signature);
            cell_signature.Phrase = new Phrase("Wakil Direktur Utama", normal_font);
            table_signature.AddCell(cell_signature);

            string signatureArea = string.Empty;
            for (int i = 0; i < 5; i++)
            {
                signatureArea += Environment.NewLine;
            }
            cell_signature.Phrase = new Phrase(signatureArea, normal_font);
            table_signature.AddCell(cell_signature);
            cell_signature.Phrase = new Phrase(signatureArea, normal_font);
            table_signature.AddCell(cell_signature);
            cell_signature.Phrase = new Phrase(signatureArea, normal_font);
            table_signature.AddCell(cell_signature);

            cell_signature.Phrase = new Phrase("(                           )", normal_font);
            table_signature.AddCell(cell_signature);
            cell_signature.Phrase = new Phrase("Haenis Gunarto ", normal_font);
            table_signature.AddCell(cell_signature);
            cell_signature.Phrase = new Phrase("Ninuk Setyawati", normal_font);
            table_signature.AddCell(cell_signature);
            #endregion

            #region Price (Bottom, Column 2)
            PdfPTable table_price = new PdfPTable(5);
            table_price.TotalWidth = 280f;

            float[] price_widths = new float[] { 1.6f, 3f, 3f, 4f, 1f };
            table_price.SetWidths(price_widths);

            PdfPCell cell_price_center = new PdfPCell()
            {
                Border = Rectangle.TOP_BORDER | Rectangle.LEFT_BORDER | Rectangle.BOTTOM_BORDER | Rectangle.RIGHT_BORDER, HorizontalAlignment = Element.ALIGN_CENTER, VerticalAlignment = Element.ALIGN_MIDDLE, Padding = 2
            };
            PdfPCell cell_price_left = new PdfPCell()
            {
                Border = Rectangle.TOP_BORDER | Rectangle.LEFT_BORDER | Rectangle.BOTTOM_BORDER | Rectangle.RIGHT_BORDER, HorizontalAlignment = Element.ALIGN_LEFT, VerticalAlignment = Element.ALIGN_MIDDLE, Padding = 2
            };
            PdfPCell cell_price_right = new PdfPCell()
            {
                Border = Rectangle.TOP_BORDER | Rectangle.LEFT_BORDER | Rectangle.BOTTOM_BORDER | Rectangle.RIGHT_BORDER, HorizontalAlignment = Element.ALIGN_RIGHT, VerticalAlignment = Element.ALIGN_MIDDLE, Padding = 2
            };

            cell_price_center.Phrase = new Phrase("KET (X)", bold_font);
            table_price.AddCell(cell_price_center);
            cell_price_center.Phrase = new Phrase("HARGA (Rp)", bold_font);
            table_price.AddCell(cell_price_center);
            cell_price_center.Phrase = new Phrase("PEMBULATAN HARGA (Rp)", bold_font);
            table_price.AddCell(cell_price_center);
            cell_price_center.Phrase = new Phrase("KETERANGAN", bold_font);
            table_price.AddCell(cell_price_center);
            cell_price_center.Phrase = new Phrase("", bold_font);
            table_price.AddCell(cell_price_center);

            cell_price_left.Phrase = new Phrase("2", normal_font);
            table_price.AddCell(cell_price_left);
            cell_price_right.Phrase = new Phrase(Number.ToRupiahWithoutSymbol(viewModel.Proposed20), normal_font);
            table_price.AddCell(cell_price_right);
            cell_price_right.Phrase = new Phrase(Number.ToRupiahWithoutSymbol(viewModel.Rounding20), normal_font);
            table_price.AddCell(cell_price_right);
            cell_price_left.Phrase = new Phrase("", normal_font);
            table_price.AddCell(cell_price_left);
            cell_price_center.Phrase = new Phrase(viewModel.SelectedRounding.ToString().Equals("Rounding20") ? "*" : "", normal_font);
            table_price.AddCell(cell_price_center);

            cell_price_left.Phrase = new Phrase("2.1", normal_font);
            table_price.AddCell(cell_price_left);
            cell_price_right.Phrase = new Phrase(Number.ToRupiahWithoutSymbol(viewModel.Proposed21), normal_font);
            table_price.AddCell(cell_price_right);
            cell_price_right.Phrase = new Phrase(Number.ToRupiahWithoutSymbol(viewModel.Rounding21), normal_font);
            table_price.AddCell(cell_price_right);
            cell_price_left.Phrase = new Phrase("", normal_font);
            table_price.AddCell(cell_price_left);
            cell_price_center.Phrase = new Phrase(viewModel.SelectedRounding.ToString().Equals("Rounding21") ? "*" : "", normal_font);
            table_price.AddCell(cell_price_center);

            cell_price_left.Phrase = new Phrase("2.2", normal_font);
            table_price.AddCell(cell_price_left);
            cell_price_right.Phrase = new Phrase(Number.ToRupiahWithoutSymbol(viewModel.Proposed22), normal_font);
            table_price.AddCell(cell_price_right);
            cell_price_right.Phrase = new Phrase(Number.ToRupiahWithoutSymbol(viewModel.Rounding22), normal_font);
            table_price.AddCell(cell_price_right);
            cell_price_left.Phrase = new Phrase("", normal_font);
            table_price.AddCell(cell_price_left);
            cell_price_center.Phrase = new Phrase(viewModel.SelectedRounding.ToString().Equals("Rounding22") ? "*" : "", normal_font);
            table_price.AddCell(cell_price_center);

            cell_price_left.Phrase = new Phrase("2.3", normal_font);
            table_price.AddCell(cell_price_left);
            cell_price_right.Phrase = new Phrase(Number.ToRupiahWithoutSymbol(viewModel.Proposed23), normal_font);
            table_price.AddCell(cell_price_right);
            cell_price_right.Phrase = new Phrase(Number.ToRupiahWithoutSymbol(viewModel.Rounding23), normal_font);
            table_price.AddCell(cell_price_right);
            cell_price_left.Phrase = new Phrase("", normal_font);
            table_price.AddCell(cell_price_left);
            cell_price_center.Phrase = new Phrase(viewModel.SelectedRounding.ToString().Equals("Rounding23") ? "*" : "", normal_font);
            table_price.AddCell(cell_price_center);

            cell_price_left.Phrase = new Phrase("2.4", normal_font);
            table_price.AddCell(cell_price_left);
            cell_price_right.Phrase = new Phrase(Number.ToRupiahWithoutSymbol(viewModel.Proposed24), normal_font);
            table_price.AddCell(cell_price_right);
            cell_price_right.Phrase = new Phrase(Number.ToRupiahWithoutSymbol(viewModel.Rounding24), normal_font);
            table_price.AddCell(cell_price_right);
            cell_price_left.Phrase = new Phrase("", normal_font);
            table_price.AddCell(cell_price_left);
            cell_price_center.Phrase = new Phrase(viewModel.SelectedRounding.ToString().Equals("Rounding24") ? "*" : "", normal_font);
            table_price.AddCell(cell_price_center);

            cell_price_left.Phrase = new Phrase("2.5", normal_font);
            table_price.AddCell(cell_price_left);
            cell_price_right.Phrase = new Phrase(Number.ToRupiahWithoutSymbol(viewModel.Proposed25), normal_font);
            table_price.AddCell(cell_price_right);
            cell_price_right.Phrase = new Phrase(Number.ToRupiahWithoutSymbol(viewModel.Rounding25), normal_font);
            table_price.AddCell(cell_price_right);
            cell_price_left.Phrase = new Phrase("", normal_font);
            table_price.AddCell(cell_price_left);
            cell_price_center.Phrase = new Phrase(viewModel.SelectedRounding.ToString().Equals("Rounding25") ? "*" : "", normal_font);
            table_price.AddCell(cell_price_center);

            cell_price_left.Phrase = new Phrase("2.6", normal_font);
            table_price.AddCell(cell_price_left);
            cell_price_right.Phrase = new Phrase(Number.ToRupiahWithoutSymbol(viewModel.Proposed26), normal_font);
            table_price.AddCell(cell_price_right);
            cell_price_right.Phrase = new Phrase(Number.ToRupiahWithoutSymbol(viewModel.Rounding26), normal_font);
            table_price.AddCell(cell_price_right);
            cell_price_left.Phrase = new Phrase("", normal_font);
            table_price.AddCell(cell_price_left);
            cell_price_center.Phrase = new Phrase(viewModel.SelectedRounding.ToString().Equals("Rounding26") ? "*" : "", normal_font);
            table_price.AddCell(cell_price_center);

            cell_price_left.Phrase = new Phrase("2.7", normal_font);
            table_price.AddCell(cell_price_left);
            cell_price_right.Phrase = new Phrase(Number.ToRupiahWithoutSymbol(viewModel.Proposed27), normal_font);
            table_price.AddCell(cell_price_right);
            cell_price_right.Phrase = new Phrase(Number.ToRupiahWithoutSymbol(viewModel.Rounding27), normal_font);
            table_price.AddCell(cell_price_right);
            cell_price_left.Phrase = new Phrase("", normal_font);
            table_price.AddCell(cell_price_left);
            cell_price_center.Phrase = new Phrase(viewModel.SelectedRounding.ToString().Equals("Rounding27") ? "*" : "", normal_font);
            table_price.AddCell(cell_price_center);

            cell_price_left.Phrase = new Phrase("2.8", normal_font);
            table_price.AddCell(cell_price_left);
            cell_price_right.Phrase = new Phrase(Number.ToRupiahWithoutSymbol(viewModel.Proposed28), normal_font);
            table_price.AddCell(cell_price_right);
            cell_price_right.Phrase = new Phrase(Number.ToRupiahWithoutSymbol(viewModel.Rounding28), normal_font);
            table_price.AddCell(cell_price_right);
            cell_price_left.Phrase = new Phrase("", normal_font);
            table_price.AddCell(cell_price_left);
            cell_price_center.Phrase = new Phrase(viewModel.SelectedRounding.ToString().Equals("Rounding28") ? "*" : "", normal_font);
            table_price.AddCell(cell_price_center);

            cell_price_left.Phrase = new Phrase("2.9", normal_font);
            table_price.AddCell(cell_price_left);
            cell_price_right.Phrase = new Phrase(Number.ToRupiahWithoutSymbol(viewModel.Proposed29), normal_font);
            table_price.AddCell(cell_price_right);
            cell_price_right.Phrase = new Phrase(Number.ToRupiahWithoutSymbol(viewModel.Rounding29), normal_font);
            table_price.AddCell(cell_price_right);
            cell_price_left.Phrase = new Phrase("", normal_font);
            table_price.AddCell(cell_price_left);
            cell_price_center.Phrase = new Phrase(viewModel.SelectedRounding.ToString().Equals("Rounding29") ? "*" : "", normal_font);
            table_price.AddCell(cell_price_center);

            cell_price_left.Phrase = new Phrase("3.0", normal_font);
            table_price.AddCell(cell_price_left);
            cell_price_right.Phrase = new Phrase(Number.ToRupiahWithoutSymbol(viewModel.Proposed30), normal_font);
            table_price.AddCell(cell_price_right);
            cell_price_right.Phrase = new Phrase(Number.ToRupiahWithoutSymbol(viewModel.Rounding30), normal_font);
            table_price.AddCell(cell_price_right);
            cell_price_left.Phrase = new Phrase("", normal_font);
            table_price.AddCell(cell_price_left);
            cell_price_center.Phrase = new Phrase(viewModel.SelectedRounding.ToString().Equals("Rounding30") ? "*" : "", normal_font);
            table_price.AddCell(cell_price_center);

            cell_price_left.Phrase = new Phrase("Others", normal_font);
            table_price.AddCell(cell_price_left);
            cell_price_right.Phrase = new Phrase("", normal_font);
            table_price.AddCell(cell_price_right);
            cell_price_right.Phrase = new Phrase(viewModel.RoundingOthers > 0 ? Number.ToRupiahWithoutSymbol(viewModel.RoundingOthers) : "", normal_font);
            table_price.AddCell(cell_price_right);
            cell_price_left.Phrase = new Phrase("", normal_font);
            table_price.AddCell(cell_price_left);
            cell_price_center.Phrase = new Phrase(viewModel.SelectedRounding.ToString().Equals("RoundingOthers") ? "*" : "", normal_font);
            table_price.AddCell(cell_price_center);
            #endregion

            #region Cost Calculation Material
            PdfPTable table_ccm = new PdfPTable(7);
            table_ccm.TotalWidth = 570f;

            float[] ccm_widths = new float[] { 1.25f, 3.5f, 4f, 9f, 3f, 4f, 4f };
            table_ccm.SetWidths(ccm_widths);

            PdfPCell cell_ccm_center = new PdfPCell()
            {
                Border = Rectangle.TOP_BORDER | Rectangle.LEFT_BORDER | Rectangle.BOTTOM_BORDER | Rectangle.RIGHT_BORDER, HorizontalAlignment = Element.ALIGN_CENTER, VerticalAlignment = Element.ALIGN_MIDDLE, Padding = 2
            };
            PdfPCell cell_ccm_left = new PdfPCell()
            {
                Border = Rectangle.TOP_BORDER | Rectangle.LEFT_BORDER | Rectangle.BOTTOM_BORDER | Rectangle.RIGHT_BORDER, HorizontalAlignment = Element.ALIGN_LEFT, VerticalAlignment = Element.ALIGN_MIDDLE, Padding = 2
            };
            PdfPCell cell_ccm_right = new PdfPCell()
            {
                Border = Rectangle.TOP_BORDER | Rectangle.LEFT_BORDER | Rectangle.BOTTOM_BORDER | Rectangle.RIGHT_BORDER, HorizontalAlignment = Element.ALIGN_RIGHT, VerticalAlignment = Element.ALIGN_MIDDLE, Padding = 2
            };

            cell_ccm_center.Phrase = new Phrase("NO", bold_font);
            table_ccm.AddCell(cell_ccm_center);

            cell_ccm_center.Phrase = new Phrase("CATEGORIES", bold_font);
            table_ccm.AddCell(cell_ccm_center);

            cell_ccm_center.Phrase = new Phrase("MATERIALS", bold_font);
            table_ccm.AddCell(cell_ccm_center);

            cell_ccm_center.Phrase = new Phrase("DESCRIPTION", bold_font);
            table_ccm.AddCell(cell_ccm_center);

            cell_ccm_center.Phrase = new Phrase("QUANTITY", bold_font);
            table_ccm.AddCell(cell_ccm_center);

            cell_ccm_center.Phrase = new Phrase("RP. PTC/PC", bold_font);
            table_ccm.AddCell(cell_ccm_center);

            cell_ccm_center.Phrase = new Phrase("RP. TOTAL", bold_font);
            table_ccm.AddCell(cell_ccm_center);

            double Total               = 0;
            float  row1Height          = imageHeight > table_top.TotalHeight ? imageHeight : table_top.TotalHeight;
            float  row2Y               = row1Y - row1Height - 10;
            float  calculatedHppHeight = 7;
            float  row3LeftHeight      = table_detail.TotalHeight + 5 + table_signature.TotalHeight;
            float  row3RightHeight     = table_price.TotalHeight;
            float  row3Height          = row3LeftHeight > row3RightHeight ? row3LeftHeight : row3RightHeight;
            float  remainingRow2Height = row2Y - 10 - row3Height - printedOnHeight - margin;
            float  allowedRow2Height   = row2Y - printedOnHeight - margin;
            for (int i = 0; i < viewModel.CostCalculationRetail_Materials.Count; i++)
            {
                cell_ccm_center.Phrase = new Phrase((i + 1).ToString(), normal_font);
                table_ccm.AddCell(cell_ccm_center);

                cell_ccm_left.Phrase = new Phrase(viewModel.CostCalculationRetail_Materials[i].Category.SubCategory != null ? String.Format("{0} - {1}", viewModel.CostCalculationRetail_Materials[i].Category.Name, viewModel.CostCalculationRetail_Materials[i].Category.SubCategory) : viewModel.CostCalculationRetail_Materials[i].Category.Name, normal_font);
                table_ccm.AddCell(cell_ccm_left);

                cell_ccm_left.Phrase = new Phrase(viewModel.CostCalculationRetail_Materials[i].Material.Name, normal_font);
                table_ccm.AddCell(cell_ccm_left);

                cell_ccm_left.Phrase = new Phrase(viewModel.CostCalculationRetail_Materials[i].Description, normal_font);
                table_ccm.AddCell(cell_ccm_left);

                cell_ccm_right.Phrase = new Phrase(String.Format("{0} {1}", viewModel.CostCalculationRetail_Materials[i].Quantity, viewModel.CostCalculationRetail_Materials[i].UOMQuantity.Name), normal_font);
                table_ccm.AddCell(cell_ccm_right);

                cell_ccm_right.Phrase = new Phrase(String.Format("{0}/{1}", Number.ToRupiahWithoutSymbol(viewModel.CostCalculationRetail_Materials[i].Price), viewModel.CostCalculationRetail_Materials[i].UOMPrice.Name), normal_font);
                table_ccm.AddCell(cell_ccm_right);

                cell_ccm_right.Phrase = new Phrase(Number.ToRupiahWithoutSymbol(viewModel.CostCalculationRetail_Materials[i].Total), normal_font);
                table_ccm.AddCell(cell_ccm_right);
                Total += viewModel.CostCalculationRetail_Materials[i].Total;

                float currentHeight = table_ccm.TotalHeight;
                if (currentHeight / remainingRow2Height > 1)
                {
                    if (currentHeight / allowedRow2Height > 1)
                    {
                        PdfPRow headerRow = table_ccm.GetRow(0);
                        PdfPRow lastRow   = table_ccm.GetRow(table_ccm.Rows.Count - 1);
                        table_ccm.DeleteLastRow();
                        table_ccm.WriteSelectedRows(0, -1, 10, row2Y, cb);
                        table_ccm.DeleteBodyRows();
                        this.DrawPrintedOn(now, bf, cb);
                        document.NewPage();
                        table_ccm.Rows.Add(headerRow);
                        table_ccm.Rows.Add(lastRow);
                        table_ccm.CalculateHeights();
                        row2Y = startY;
                        remainingRow2Height = row2Y - 10 - row3Height - printedOnHeight - margin;
                        allowedRow2Height   = row2Y - printedOnHeight - margin;
                    }
                }
            }

            cell_ccm_right = new PdfPCell()
            {
                Border = Rectangle.TOP_BORDER | Rectangle.LEFT_BORDER | Rectangle.BOTTOM_BORDER | Rectangle.RIGHT_BORDER, HorizontalAlignment = Element.ALIGN_RIGHT, VerticalAlignment = Element.ALIGN_MIDDLE, Padding = 2, Colspan = 6
            };
            cell_ccm_right.Phrase = new Phrase("TOTAL", bold_font_8);
            table_ccm.AddCell(cell_ccm_right);
            cell_ccm_right = new PdfPCell()
            {
                Border = Rectangle.TOP_BORDER | Rectangle.LEFT_BORDER | Rectangle.BOTTOM_BORDER | Rectangle.RIGHT_BORDER, HorizontalAlignment = Element.ALIGN_RIGHT, VerticalAlignment = Element.ALIGN_MIDDLE, Padding = 2
            };
            cell_ccm_right.Phrase = new Phrase(Number.ToRupiah(Total), bold_font_8);
            table_ccm.AddCell(cell_ccm_right);
            #endregion

            #region Draw Middle and Bottom
            table_ccm.WriteSelectedRows(0, -1, 10, row2Y, cb);

            float row3Y = row2Y - table_ccm.TotalHeight - 10;
            float remainingRow3Height = row3Y - printedOnHeight - margin;
            if (remainingRow3Height < row3Height)
            {
                this.DrawPrintedOn(now, bf, cb);
                row3Y = startY;
                document.NewPage();
            }

            #region Calculated HPP
            float calculatedHppY = row3Y - calculatedHppHeight;
            cb.BeginText();
            cb.SetFontAndSize(bf, 8);
            cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "KALKULASI HPP: (OL + OTL1 + OTL2 + FABRIC + ACC) + ((OL + OTL1 + OTL2 + FABRIC + ACC) * Risk)", 10, calculatedHppY, 0);
            cb.EndText();
            #endregion

            float table_detailY = calculatedHppY - 5;
            table_detail.WriteSelectedRows(0, -1, 10, table_detailY, cb);

            float table_signatureY = table_detailY - row3Height + table_signature.TotalHeight;
            table_signature.WriteSelectedRows(0, -1, 10, table_signatureY, cb);

            table_price.WriteSelectedRows(0, -1, 300, table_detailY, cb);

            this.DrawPrintedOn(now, bf, cb);
            #endregion

            document.Close();

            byte[] byteInfo = stream.ToArray();
            stream.Write(byteInfo, 0, byteInfo.Length);
            stream.Position = 0;

            return(stream);
        }
Esempio n. 4
0
        public MemoryStream GeneratePdfTemplate(RO_RetailViewModel viewModel)
        {
            BaseFont bf          = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1250, BaseFont.NOT_EMBEDDED);
            BaseFont bf_bold     = BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1250, BaseFont.NOT_EMBEDDED);
            Font     normal_font = FontFactory.GetFont(BaseFont.HELVETICA, BaseFont.CP1250, BaseFont.NOT_EMBEDDED, 7);
            Font     bold_font   = FontFactory.GetFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1250, BaseFont.NOT_EMBEDDED, 7);
            Font     bold_font_8 = FontFactory.GetFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1250, BaseFont.NOT_EMBEDDED, 8);
            Font     font_9      = FontFactory.GetFont(BaseFont.HELVETICA, BaseFont.CP1250, BaseFont.NOT_EMBEDDED, 9);
            DateTime now         = DateTime.Now;

            Document     document = new Document(PageSize.A4, 10, 10, 10, 10);
            MemoryStream stream   = new MemoryStream();
            PdfWriter    writer   = PdfWriter.GetInstance(document, stream);

            writer.CloseStream = false;
            document.Open();
            PdfContentByte cb = writer.DirectContent;

            float margin          = 10;
            float printedOnHeight = 10;
            float startY          = 840 - margin;

            #region Header

            cb.BeginText();
            cb.SetFontAndSize(bf, 10);
            cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "PT. EFRATA RETAILINDO", 10, 820, 0);
            cb.SetFontAndSize(bf_bold, 12);
            cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "RO RETAIL", 10, 805, 0);
            cb.EndText();
            #endregion


            #region Top

            PdfPTable table_top  = new PdfPTable(9);
            float[]   top_widths = new float[] { 1f, 0.1f, 2f, 1f, 0.1f, 2f, 1f, 0.1f, 2f };

            table_top.TotalWidth = 500f;
            table_top.SetWidths(top_widths);

            PdfPCell cell_top = new PdfPCell()
            {
                Border = Rectangle.NO_BORDER,
                HorizontalAlignment = Element.ALIGN_LEFT,
                VerticalAlignment   = Element.ALIGN_MIDDLE,
                PaddingRight        = 1,
                PaddingBottom       = 2,
                PaddingTop          = 2
            };

            PdfPCell cell_colon = new PdfPCell()
            {
                Border = Rectangle.NO_BORDER,
                HorizontalAlignment = Element.ALIGN_LEFT,
                VerticalAlignment   = Element.ALIGN_MIDDLE
            };

            PdfPCell cell_top_keterangan = new PdfPCell()
            {
                Border = Rectangle.NO_BORDER,
                HorizontalAlignment = Element.ALIGN_LEFT,
                VerticalAlignment   = Element.ALIGN_MIDDLE,
                PaddingRight        = 1,
                PaddingBottom       = 2,
                PaddingTop          = 2,
                Colspan             = 7
            };

            cell_colon.Phrase = new Phrase(":", normal_font);
            cell_top.Phrase   = new Phrase("NO RO", normal_font);
            table_top.AddCell(cell_top);
            table_top.AddCell(cell_colon);
            cell_top.Phrase = new Phrase($"{viewModel.CostCalculationRetail.RO}", normal_font);
            table_top.AddCell(cell_top);
            cell_top.Phrase = new Phrase("ARTICLE", normal_font);
            table_top.AddCell(cell_top);
            table_top.AddCell(cell_colon);
            cell_top.Phrase = new Phrase($"{viewModel.CostCalculationRetail.Article}", normal_font);
            table_top.AddCell(cell_top);
            cell_top.Phrase = new Phrase("STYLE", normal_font);
            table_top.AddCell(cell_top);
            table_top.AddCell(cell_colon);
            cell_top.Phrase = new Phrase($"{viewModel.CostCalculationRetail.Style.name}", normal_font);
            table_top.AddCell(cell_top);

            cell_top.Phrase = new Phrase("COUNTER", normal_font);
            table_top.AddCell(cell_top);
            table_top.AddCell(cell_colon);
            cell_top.Phrase = new Phrase($"{viewModel.CostCalculationRetail.Counter.name}", normal_font);
            table_top.AddCell(cell_top);
            cell_top.Phrase = new Phrase("COLOUR", normal_font);
            table_top.AddCell(cell_top);
            table_top.AddCell(cell_colon);
            cell_top.Phrase = new Phrase($"{viewModel.Color.name}", normal_font);
            table_top.AddCell(cell_top);
            cell_top.Phrase = new Phrase("DELIVERY DATE", normal_font);
            table_top.AddCell(cell_top);
            table_top.AddCell(cell_colon);
            cell_top.Phrase = new Phrase($"{viewModel.CostCalculationRetail.DeliveryDate.ToString("dd MMMM yyyy")}", normal_font);
            table_top.AddCell(cell_top);

            cell_top.Phrase = new Phrase("RO QUANTITY", normal_font);
            table_top.AddCell(cell_top);
            table_top.AddCell(cell_colon);
            cell_top_keterangan.Phrase = new Phrase($"{viewModel.Total}", normal_font);
            table_top.AddCell(cell_top_keterangan);
            #endregion

            #region Image

            byte[] imageByte;
            try
            {
                imageByte = Convert.FromBase64String(Base64.GetBase64File(viewModel.CostCalculationRetail.ImageFile));
            }
            catch (Exception)
            {
                var webClient = new WebClient();
                imageByte = webClient.DownloadData("https://bateeqstorage.blob.core.windows.net/other/no-image.jpg");
            }

            Image image = Image.GetInstance(imgb: imageByte);

            if (image.Width > 60)
            {
                float percentage = 0.0f;
                percentage = 60 / image.Width;
                image.ScalePercent(percentage * 100);
            }
            #endregion

            #region Draw Top

            float row1Y  = 800;
            float imageY = 800 - image.ScaledHeight;

            table_top.WriteSelectedRows(0, -1, 10, row1Y, cb);
            image.SetAbsolutePosition(520, imageY);
            cb.AddImage(image, inlineImage: true);
            #endregion

            #region Fabric Table Title

            PdfPTable table_fabric_top = new PdfPTable(1);
            table_fabric_top.TotalWidth = 570f;

            float[] fabric_widths_top = new float[] { 5f };
            table_fabric_top.SetWidths(fabric_widths_top);

            PdfPCell cell_top_fabric = new PdfPCell()
            {
                Border = Rectangle.NO_BORDER,
                HorizontalAlignment = Element.ALIGN_LEFT,
                VerticalAlignment   = Element.ALIGN_MIDDLE,
                PaddingRight        = 1,
                PaddingBottom       = 2,
                PaddingTop          = 2
            };

            cell_top_fabric.Phrase = new Phrase("FABRIC", bold_font);
            table_fabric_top.AddCell(cell_top_fabric);

            float row1Height        = image.ScaledHeight > table_top.TotalHeight ? image.ScaledHeight : table_top.TotalHeight;
            float rowYTittleFab     = row1Y - row1Height - 10;
            float allowedRow2Height = rowYTittleFab - printedOnHeight - margin;
            table_fabric_top.WriteSelectedRows(0, -1, 10, rowYTittleFab, cb);
            #endregion

            #region Fabric Table
            PdfPTable table_fabric = new PdfPTable(5);
            table_fabric.TotalWidth = 570f;

            float[] fabric_widths = new float[] { 5f, 5f, 5f, 5f, 5f };
            table_fabric.SetWidths(fabric_widths);

            PdfPCell cell_fabric_center = new PdfPCell()
            {
                Border = Rectangle.TOP_BORDER | Rectangle.LEFT_BORDER | Rectangle.BOTTOM_BORDER | Rectangle.RIGHT_BORDER,
                HorizontalAlignment = Element.ALIGN_CENTER,
                VerticalAlignment   = Element.ALIGN_MIDDLE,
                Padding             = 2
            };

            PdfPCell cell_fabric_left = new PdfPCell()
            {
                Border = Rectangle.TOP_BORDER | Rectangle.LEFT_BORDER | Rectangle.BOTTOM_BORDER | Rectangle.RIGHT_BORDER,
                HorizontalAlignment = Element.ALIGN_LEFT,
                VerticalAlignment   = Element.ALIGN_MIDDLE,
                Padding             = 2
            };

            float rowYFab = rowYTittleFab - table_fabric_top.TotalHeight - 5;
            float allowedRow2HeightFab = rowYFab - printedOnHeight - margin;

            cell_fabric_center.Phrase = new Phrase("FABRIC", bold_font);
            table_fabric.AddCell(cell_fabric_center);

            cell_fabric_center.Phrase = new Phrase("NAME", bold_font);
            table_fabric.AddCell(cell_fabric_center);

            cell_fabric_center.Phrase = new Phrase("DESCRIPTION", bold_font);
            table_fabric.AddCell(cell_fabric_center);

            cell_fabric_center.Phrase = new Phrase("QUANTITY", bold_font);
            table_fabric.AddCell(cell_fabric_center);

            cell_fabric_center.Phrase = new Phrase("REMARK", bold_font);
            table_fabric.AddCell(cell_fabric_center);

            foreach (var materialModel in viewModel.CostCalculationRetail.CostCalculationRetail_Materials)
            {
                if (materialModel.Category.Name == "FAB")
                {
                    cell_fabric_left.Phrase = new Phrase(materialModel.Category.SubCategory != null ? materialModel.Category.SubCategory : "", normal_font);
                    table_fabric.AddCell(cell_fabric_left);

                    cell_fabric_left.Phrase = new Phrase(materialModel.Material.Name != null ? materialModel.Material.Name : "", normal_font);
                    table_fabric.AddCell(cell_fabric_left);

                    cell_fabric_left.Phrase = new Phrase(materialModel.Description != null ? materialModel.Description : "", normal_font);
                    table_fabric.AddCell(cell_fabric_left);

                    cell_fabric_left.Phrase = new Phrase(materialModel.Quantity != null ? String.Format("{0} " + materialModel.UOMQuantity.Name, materialModel.Quantity) : "0", normal_font);
                    table_fabric.AddCell(cell_fabric_left);

                    cell_fabric_left.Phrase = new Phrase(materialModel.Information != null ? materialModel.Information : "", normal_font);
                    table_fabric.AddCell(cell_fabric_left);
                }
            }

            table_fabric.WriteSelectedRows(0, -1, 10, rowYFab, cb);
            #endregion


            #region Accessoris Table Title

            PdfPTable table_acc_top = new PdfPTable(1);
            table_acc_top.TotalWidth = 570f;

            float[] acc_width_top = new float[] { 5f };
            table_acc_top.SetWidths(acc_width_top);

            PdfPCell cell_top_acc = new PdfPCell()
            {
                Border = Rectangle.NO_BORDER,
                HorizontalAlignment = Element.ALIGN_LEFT,
                VerticalAlignment   = Element.ALIGN_MIDDLE,
                PaddingRight        = 1,
                PaddingBottom       = 2,
                PaddingTop          = 2
            };

            cell_top_acc.Phrase = new Phrase("ACCESSORIES", bold_font);
            table_acc_top.AddCell(cell_top_acc);

            float rowYTittleAcc           = rowYFab - table_fabric.TotalHeight - 10;
            float allowedRow2HeightTopAcc = rowYTittleFab - printedOnHeight - margin;
            table_acc_top.WriteSelectedRows(0, -1, 10, rowYTittleAcc, cb);
            #endregion

            #region Accessoris Table

            PdfPTable table_accessories = new PdfPTable(5);
            table_accessories.TotalWidth = 570f;

            float[] accessories_widths = new float[] { 5f, 5f, 5f, 5f, 5f };
            table_accessories.SetWidths(accessories_widths);

            PdfPCell cell_acc_center = new PdfPCell()
            {
                Border = Rectangle.TOP_BORDER | Rectangle.LEFT_BORDER | Rectangle.BOTTOM_BORDER | Rectangle.RIGHT_BORDER,
                HorizontalAlignment = Element.ALIGN_CENTER,
                VerticalAlignment   = Element.ALIGN_MIDDLE,
                Padding             = 2
            };

            PdfPCell cell_acc_left = new PdfPCell()
            {
                Border = Rectangle.TOP_BORDER | Rectangle.LEFT_BORDER | Rectangle.BOTTOM_BORDER | Rectangle.RIGHT_BORDER,
                HorizontalAlignment = Element.ALIGN_LEFT,
                VerticalAlignment   = Element.ALIGN_MIDDLE,
                Padding             = 2
            };

            float rowYAcc = rowYTittleAcc - table_fabric_top.TotalHeight - 5;
            float allowedRow2HeightAcc = rowYAcc - printedOnHeight - margin;

            cell_acc_center.Phrase = new Phrase("ACCESSORIES", bold_font);
            table_accessories.AddCell(cell_acc_center);

            cell_acc_center.Phrase = new Phrase("NAME", bold_font);
            table_accessories.AddCell(cell_acc_center);

            cell_acc_center.Phrase = new Phrase("DESCRIPTION", bold_font);
            table_accessories.AddCell(cell_acc_center);

            cell_acc_center.Phrase = new Phrase("QUANTITY", bold_font);
            table_accessories.AddCell(cell_acc_center);

            cell_acc_center.Phrase = new Phrase("REMARK", bold_font);
            table_accessories.AddCell(cell_acc_center);

            foreach (var materialModel in viewModel.CostCalculationRetail.CostCalculationRetail_Materials)
            {
                if (materialModel.Category.Name == "ACC")
                {
                    cell_acc_left.Phrase = new Phrase(materialModel.Category.SubCategory != null ? materialModel.Category.SubCategory : "", normal_font);
                    table_accessories.AddCell(cell_acc_left);

                    cell_acc_left.Phrase = new Phrase(materialModel.Material.Name != null ? materialModel.Material.Name : "", normal_font);
                    table_accessories.AddCell(cell_acc_left);

                    cell_acc_left.Phrase = new Phrase(materialModel.Description != null ? materialModel.Description : "", normal_font);
                    table_accessories.AddCell(cell_acc_left);

                    cell_acc_left.Phrase = new Phrase(materialModel.Quantity != null ? String.Format("{0} " + materialModel.UOMQuantity.Name, materialModel.Quantity) : "0", normal_font);
                    table_accessories.AddCell(cell_acc_left);

                    cell_acc_left.Phrase = new Phrase(materialModel.Information != null ? materialModel.Information : "", normal_font);
                    table_accessories.AddCell(cell_acc_left);
                }
            }

            table_accessories.WriteSelectedRows(0, -1, 10, rowYAcc, cb);
            #endregion

            #region Ongkos Table Title

            PdfPTable table_ong_top = new PdfPTable(1);
            table_ong_top.TotalWidth = 570f;

            float[] ong_width_top = new float[] { 5f };
            table_ong_top.SetWidths(ong_width_top);

            PdfPCell cell_top_ong = new PdfPCell()
            {
                Border = Rectangle.NO_BORDER,
                HorizontalAlignment = Element.ALIGN_LEFT,
                VerticalAlignment   = Element.ALIGN_MIDDLE,
                PaddingRight        = 1,
                PaddingBottom       = 2,
                PaddingTop          = 2
            };

            cell_top_ong.Phrase = new Phrase("ONGKOS", bold_font);
            table_ong_top.AddCell(cell_top_ong);

            float rowYTittleOng           = rowYAcc - table_accessories.TotalHeight - 10;
            float allowedRow2HeightTopOng = rowYTittleOng - printedOnHeight - margin;

            #endregion

            #region Ongkos Table

            PdfPTable table_budget = new PdfPTable(5);
            table_budget.TotalWidth = 570f;

            float[] budget_widths = new float[] { 5f, 5f, 5f, 5f, 5f };
            table_budget.SetWidths(budget_widths);

            var ongIndex = 0;

            PdfPCell cell_budget_center = new PdfPCell()
            {
                Border = Rectangle.TOP_BORDER | Rectangle.LEFT_BORDER | Rectangle.BOTTOM_BORDER | Rectangle.RIGHT_BORDER,
                HorizontalAlignment = Element.ALIGN_CENTER,
                VerticalAlignment   = Element.ALIGN_MIDDLE,
                Padding             = 2
            };

            PdfPCell cell_budget_left = new PdfPCell()
            {
                Border = Rectangle.TOP_BORDER | Rectangle.LEFT_BORDER | Rectangle.BOTTOM_BORDER | Rectangle.RIGHT_BORDER,
                HorizontalAlignment = Element.ALIGN_LEFT,
                VerticalAlignment   = Element.ALIGN_MIDDLE,
                Padding             = 2
            };

            float rowYBudget = rowYTittleOng - table_ong_top.TotalHeight - 5;
            float allowedRow2HeightBudget = rowYBudget - printedOnHeight - margin;

            cell_budget_center.Phrase = new Phrase("ONGKOS", bold_font);
            table_budget.AddCell(cell_budget_center);

            cell_budget_center.Phrase = new Phrase("NAME", bold_font);
            table_budget.AddCell(cell_budget_center);

            cell_budget_center.Phrase = new Phrase("DESCRIPTION", bold_font);
            table_budget.AddCell(cell_budget_center);

            cell_budget_center.Phrase = new Phrase("QUANTITY", bold_font);
            table_budget.AddCell(cell_budget_center);

            cell_budget_center.Phrase = new Phrase("REMARK", bold_font);
            table_budget.AddCell(cell_budget_center);

            foreach (var materialModel in viewModel.CostCalculationRetail.CostCalculationRetail_Materials)
            {
                if (materialModel.Category.Name == "ONG")
                {
                    cell_budget_left.Phrase = new Phrase(materialModel.Category.SubCategory != null ? materialModel.Category.SubCategory : "", normal_font);
                    table_budget.AddCell(cell_budget_left);

                    cell_budget_left.Phrase = new Phrase(materialModel.Material.Name != null ? materialModel.Material.Name : "", normal_font);
                    table_budget.AddCell(cell_budget_left);

                    cell_budget_left.Phrase = new Phrase(materialModel.Description != null ? materialModel.Description : "", normal_font);
                    table_budget.AddCell(cell_budget_left);

                    cell_budget_left.Phrase = new Phrase(materialModel.Quantity != null ? String.Format("{0} " + materialModel.UOMQuantity.Name, materialModel.Quantity) : "0", normal_font);
                    table_budget.AddCell(cell_budget_left);

                    cell_budget_left.Phrase = new Phrase(materialModel.Information != null ? materialModel.Information : "", normal_font);
                    table_budget.AddCell(cell_budget_left);

                    ongIndex++;
                }
            }

            if (ongIndex != 0)
            {
                table_budget.WriteSelectedRows(0, -1, 10, rowYBudget, cb);
                table_ong_top.WriteSelectedRows(0, -1, 10, rowYTittleOng, cb);
            }
            #endregion

            #region Size Breakdown Title

            PdfPTable table_breakdown_top = new PdfPTable(1);
            table_breakdown_top.TotalWidth = 570f;

            float[] breakdown_width_top = new float[] { 5f };
            table_breakdown_top.SetWidths(breakdown_width_top);

            PdfPCell cell_top_breakdown = new PdfPCell()
            {
                Border = Rectangle.NO_BORDER,
                HorizontalAlignment = Element.ALIGN_LEFT,
                VerticalAlignment   = Element.ALIGN_MIDDLE,
                PaddingRight        = 1,
                PaddingBottom       = 2,
                PaddingTop          = 2
            };

            cell_top_breakdown.Phrase = new Phrase("SIZE BREAKDOWN", bold_font);
            table_breakdown_top.AddCell(cell_top_breakdown);

            float rowYTittleBreakDown        = rowYBudget - table_budget.TotalHeight - 10;
            float allowedRow2HeightBreakdown = rowYTittleBreakDown - printedOnHeight - margin;
            table_breakdown_top.WriteSelectedRows(0, -1, 10, rowYTittleBreakDown, cb);
            #endregion

            #region == Table Size Breakdown ==
            var tableBreakdownColumn = 3;

            PdfPCell cell_breakDown_center = new PdfPCell()
            {
                Border = Rectangle.TOP_BORDER | Rectangle.LEFT_BORDER | Rectangle.BOTTOM_BORDER | Rectangle.RIGHT_BORDER,
                HorizontalAlignment = Element.ALIGN_CENTER,
                VerticalAlignment   = Element.ALIGN_MIDDLE,
                Padding             = 2
            };

            PdfPCell cell_breakDown_left = new PdfPCell()
            {
                Border = Rectangle.TOP_BORDER | Rectangle.LEFT_BORDER | Rectangle.BOTTOM_BORDER | Rectangle.RIGHT_BORDER,
                HorizontalAlignment = Element.ALIGN_LEFT,
                VerticalAlignment   = Element.ALIGN_MIDDLE,
                Padding             = 2
            };

            PdfPCell cell_breakDown_total = new PdfPCell()
            {
                Border = Rectangle.TOP_BORDER | Rectangle.LEFT_BORDER | Rectangle.BOTTOM_BORDER,
                HorizontalAlignment = Element.ALIGN_LEFT,
                VerticalAlignment   = Element.ALIGN_MIDDLE,
                Padding             = 2
            };

            PdfPCell cell_breakDown_total_2 = new PdfPCell()
            {
                Border = Rectangle.TOP_BORDER | Rectangle.BOTTOM_BORDER | Rectangle.RIGHT_BORDER,
                HorizontalAlignment = Element.ALIGN_LEFT,
                VerticalAlignment   = Element.ALIGN_MIDDLE,
                Padding             = 2
            };

            float rowYbreakDown = rowYTittleBreakDown - table_breakdown_top.TotalHeight - 5;
            float allowedRow2HeightBreakDown   = rowYbreakDown - printedOnHeight - margin;
            var   remainingRowToHeightBrekdown = rowYbreakDown - 5 - printedOnHeight - margin;

            List <String> breakdownSizes = new List <string>();

            foreach (var size in viewModel.RO_Retail_SizeBreakdowns)
            {
                var sizes = size.SizeQuantity.Keys;

                foreach (var values in sizes)
                {
                    if (!breakdownSizes.Contains(values))
                    {
                        breakdownSizes.Add(values);
                        tableBreakdownColumn++;
                    }
                }
            }

            PdfPTable table_breakDown = new PdfPTable(tableBreakdownColumn);
            table_breakDown.TotalWidth = 570f;


            cell_breakDown_center.Phrase = new Phrase("STORE CODE", bold_font);
            table_breakDown.AddCell(cell_breakDown_center);

            cell_breakDown_center.Phrase = new Phrase("STORE", bold_font);
            table_breakDown.AddCell(cell_breakDown_center);

            foreach (var size in breakdownSizes)
            {
                cell_breakDown_center.Phrase = new Phrase(size, bold_font);
                table_breakDown.AddCell(cell_breakDown_center);
            }

            cell_breakDown_center.Phrase = new Phrase("TOTAL", bold_font);
            table_breakDown.AddCell(cell_breakDown_center);

            foreach (var productRetail in viewModel.RO_Retail_SizeBreakdowns)
            {
                if (productRetail.Total != 0)
                {
                    cell_breakDown_left.Phrase = new Phrase(productRetail.Store.code != null ? productRetail.Store.code : "", normal_font);
                    table_breakDown.AddCell(cell_breakDown_left);

                    cell_breakDown_left.Phrase = new Phrase(productRetail.Store.name != null ? productRetail.Store.name : "", normal_font);
                    table_breakDown.AddCell(cell_breakDown_left);

                    foreach (var size in productRetail.SizeQuantity)
                    {
                        foreach (var sizeHeader in breakdownSizes)
                        {
                            if (size.Key == sizeHeader)
                            {
                                cell_breakDown_left.Phrase = new Phrase(size.Value.ToString() != null ? size.Value.ToString() : "0", normal_font);
                                table_breakDown.AddCell(cell_breakDown_left);
                            }
                        }
                    }

                    cell_breakDown_left.Phrase = new Phrase(productRetail.Total.ToString() != null ? productRetail.Total.ToString() : "0", normal_font);
                    table_breakDown.AddCell(cell_breakDown_left);
                }

                var tableBreakdownCurrentHeight = table_breakDown.TotalHeight;

                if (tableBreakdownCurrentHeight / remainingRowToHeightBrekdown > 1)
                {
                    if (tableBreakdownCurrentHeight / allowedRow2HeightBreakDown > 1)
                    {
                        PdfPRow headerRow = table_breakDown.GetRow(0);
                        PdfPRow lastRow   = table_breakDown.GetRow(table_breakDown.Rows.Count - 1);
                        table_breakDown.DeleteLastRow();
                        table_breakDown.WriteSelectedRows(0, -1, 10, rowYbreakDown, cb);
                        table_breakDown.DeleteBodyRows();
                        this.DrawPrintedOn(now, bf, cb);
                        document.NewPage();
                        table_breakDown.Rows.Add(headerRow);
                        table_breakDown.Rows.Add(lastRow);
                        table_breakDown.CalculateHeights();
                        rowYbreakDown = startY;
                        remainingRowToHeightBrekdown = rowYbreakDown - 5 - printedOnHeight - margin;
                        allowedRow2HeightBreakDown   = remainingRowToHeightBrekdown - printedOnHeight - margin;
                    }
                }
            }

            cell_breakDown_total.Phrase = new Phrase(" ", bold_font);
            table_breakDown.AddCell(cell_breakDown_total);

            cell_breakDown_total_2.Phrase = new Phrase("TOTAL", bold_font);
            table_breakDown.AddCell(cell_breakDown_total_2);

            foreach (var sizeTotal in viewModel.SizeQuantityTotal)
            {
                cell_breakDown_left.Phrase = new Phrase(sizeTotal.Value.ToString() != null ? sizeTotal.Value.ToString() : "0", normal_font);
                table_breakDown.AddCell(cell_breakDown_left);
            }
            cell_breakDown_left.Phrase = new Phrase(viewModel.Total.ToString() != null ? viewModel.Total.ToString() : "0", normal_font);
            table_breakDown.AddCell(cell_breakDown_left);

            table_breakDown.WriteSelectedRows(0, -1, 10, rowYbreakDown, cb);
            #endregion

            #region Table Instruksi

            PdfPTable table_instruction  = new PdfPTable(1);
            float[]   instruction_widths = new float[] { 5f };

            table_instruction.TotalWidth = 500f;
            table_instruction.SetWidths(instruction_widths);

            PdfPCell cell_top_instruction = new PdfPCell()
            {
                Border = Rectangle.NO_BORDER,
                HorizontalAlignment = Element.ALIGN_LEFT,
                VerticalAlignment   = Element.ALIGN_MIDDLE,
                PaddingRight        = 1,
                PaddingBottom       = 2,
                PaddingTop          = 2
            };

            PdfPCell cell_colon_instruction = new PdfPCell()
            {
                Border = Rectangle.NO_BORDER,
                HorizontalAlignment = Element.ALIGN_LEFT,
                VerticalAlignment   = Element.ALIGN_MIDDLE
            };

            PdfPCell cell_top_keterangan_instruction = new PdfPCell()
            {
                Border = Rectangle.NO_BORDER,
                HorizontalAlignment = Element.ALIGN_LEFT,
                VerticalAlignment   = Element.ALIGN_MIDDLE,
                PaddingRight        = 1,
                PaddingBottom       = 2,
                PaddingTop          = 2,
                Colspan             = 7
            };

            cell_top_instruction.Phrase = new Phrase("INSTRUCTION", normal_font);
            table_instruction.AddCell(cell_top_instruction);
            table_instruction.AddCell(cell_colon_instruction);
            cell_top_keterangan_instruction.Phrase = new Phrase($"{viewModel.Instruction}", normal_font);
            table_instruction.AddCell(cell_top_keterangan_instruction);

            float rowYInstruction = rowYbreakDown - table_breakDown.TotalHeight - 5;
            float allowedRow2HeightInstruction    = rowYInstruction - printedOnHeight - margin;
            var   remainingRowToHeightInstruction = rowYInstruction - 5 - printedOnHeight - margin;
            var   tableInstructionCurrentHeight   = table_instruction.TotalHeight;

            if (remainingRowToHeightInstruction < 0)
            {
                remainingRowToHeightInstruction = remainingRowToHeightInstruction * -1;
            }

            if (allowedRow2HeightInstruction < 0)
            {
                allowedRow2HeightInstruction = allowedRow2HeightInstruction * -1;
            }

            if (tableInstructionCurrentHeight / remainingRowToHeightInstruction > 1)
            {
                if (tableInstructionCurrentHeight / allowedRow2HeightInstruction > 1)
                {
                    PdfPRow headerRow = table_instruction.GetRow(0);
                    PdfPRow lastRow   = table_instruction.GetRow(table_instruction.Rows.Count - 1);
                    table_instruction.DeleteLastRow();
                    table_instruction.WriteSelectedRows(0, -1, 10, rowYInstruction, cb);
                    table_instruction.DeleteBodyRows();
                    this.DrawPrintedOn(now, bf, cb);
                    document.NewPage();
                    table_instruction.Rows.Add(headerRow);
                    table_instruction.Rows.Add(lastRow);
                    table_instruction.CalculateHeights();
                    rowYInstruction = startY;
                    remainingRowToHeightInstruction = rowYInstruction - 5 - printedOnHeight - margin;
                    allowedRow2HeightInstruction    = remainingRowToHeightInstruction - printedOnHeight - margin;
                }
            }

            table_instruction.WriteSelectedRows(0, -1, 10, rowYInstruction, cb);
            #endregion

            #region RO Image
            var    countImageRo = 0;
            byte[] roImage;

            foreach (var index in viewModel.ImagesFile)
            {
                countImageRo++;
            }

            if (countImageRo > 5)
            {
                countImageRo = 5;
            }

            PdfPTable table_ro_image = new PdfPTable(countImageRo);
            float[]   ro_widths      = new float[countImageRo];

            for (var i = 0; i < countImageRo; i++)
            {
                ro_widths.SetValue(5f, i);
            }

            if (countImageRo != 0)
            {
                table_ro_image.SetWidths(ro_widths);
            }

            table_ro_image.TotalWidth = 570f;
            float rowYRoImage = rowYInstruction - table_instruction.TotalHeight - 5;

            foreach (var imageFromRo in viewModel.ImagesFile)
            {
                try
                {
                    roImage = Convert.FromBase64String(Base64.GetBase64File(imageFromRo));
                }
                catch (Exception)
                {
                    var webClient = new WebClient();
                    roImage = webClient.DownloadData("https://bateeqstorage.blob.core.windows.net/other/no-image.jpg");
                }

                Image images = Image.GetInstance(imgb: roImage);

                if (images.Width > 60)
                {
                    float percentage = 0.0f;
                    percentage = 60 / images.Width;
                    images.ScalePercent(percentage * 100);
                }

                PdfPCell imageCell = new PdfPCell(images);
                imageCell.Border = 0;
                table_ro_image.AddCell(imageCell);
            }

            PdfPCell cell_image = new PdfPCell()
            {
                Border = Rectangle.NO_BORDER,
                HorizontalAlignment = Element.ALIGN_LEFT,
                VerticalAlignment   = Element.ALIGN_MIDDLE,
                Padding             = 2,
            };

            foreach (var name in viewModel.ImagesName)
            {
                cell_image.Phrase = new Phrase(name, normal_font);
                table_ro_image.AddCell(cell_image);
            }

            table_ro_image.WriteSelectedRows(0, -1, 10, rowYRoImage, cb);
            #endregion

            #region Signature (Bottom, Column 1.2)

            PdfPTable table_signature = new PdfPTable(6);
            table_signature.TotalWidth = 570f;

            float[] signature_widths = new float[] { 1f, 1f, 1f, 1f, 1f, 1f };
            table_signature.SetWidths(signature_widths);

            PdfPCell cell_signature = new PdfPCell()
            {
                Border = Rectangle.NO_BORDER,
                HorizontalAlignment = Element.ALIGN_CENTER,
                VerticalAlignment   = Element.ALIGN_MIDDLE,
                Padding             = 2,
            };

            PdfPCell cell_signature_noted = new PdfPCell()
            {
                Border = Rectangle.NO_BORDER,
                HorizontalAlignment = Element.ALIGN_CENTER,
                VerticalAlignment   = Element.ALIGN_MIDDLE,
                Padding             = 2,
                PaddingTop          = 50
            };

            cell_signature.Phrase = new Phrase("Dibuat", normal_font);
            table_signature.AddCell(cell_signature);
            cell_signature.Phrase = new Phrase("Kasie Merchandiser", normal_font);
            table_signature.AddCell(cell_signature);
            cell_signature.Phrase = new Phrase("R & D", normal_font);
            table_signature.AddCell(cell_signature);
            cell_signature.Phrase = new Phrase("Ka Produksi", normal_font);
            table_signature.AddCell(cell_signature);
            cell_signature.Phrase = new Phrase("Mengetahui", normal_font);
            table_signature.AddCell(cell_signature);
            cell_signature.Phrase = new Phrase("Menyetujui", normal_font);
            table_signature.AddCell(cell_signature);

            cell_signature_noted.Phrase = new Phrase("(                           )", normal_font);
            table_signature.AddCell(cell_signature_noted);
            cell_signature_noted.Phrase = new Phrase("(                           )", normal_font);
            table_signature.AddCell(cell_signature_noted);
            cell_signature_noted.Phrase = new Phrase("(                           )", normal_font);
            table_signature.AddCell(cell_signature_noted);
            cell_signature_noted.Phrase = new Phrase("(                           )", normal_font);
            table_signature.AddCell(cell_signature_noted);
            cell_signature_noted.Phrase = new Phrase("(Haenis Gunarto)", normal_font);
            table_signature.AddCell(cell_signature_noted);
            cell_signature_noted.Phrase = new Phrase("(Michelle Tjokrosaputro)", normal_font);
            table_signature.AddCell(cell_signature_noted);

            float table_signatureY = rowYRoImage - table_ro_image.TotalHeight - 5;
            table_signature.WriteSelectedRows(0, -1, 10, table_signatureY, cb);
            #endregion

            this.DrawPrintedOn(now, bf, cb);
            document.Close();

            byte[] byteInfo = stream.ToArray();
            stream.Write(byteInfo, 0, byteInfo.Length);
            stream.Position = 0;

            return(stream);
        }
        public MemoryStream GeneratePdfTemplate(CostCalculationGarmentViewModel viewModel, int timeoffset)
        {
            BaseFont bf          = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1250, BaseFont.NOT_EMBEDDED);
            BaseFont bf_bold     = BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1250, BaseFont.NOT_EMBEDDED);
            Font     normal_font = FontFactory.GetFont(BaseFont.HELVETICA, BaseFont.CP1250, BaseFont.NOT_EMBEDDED, 7);
            Font     bold_font   = FontFactory.GetFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1250, BaseFont.NOT_EMBEDDED, 7);
            Font     bold_font_8 = FontFactory.GetFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1250, BaseFont.NOT_EMBEDDED, 8);
            DateTime now         = DateTime.Now;

            Document     document = new Document(PageSize.A4, 10, 10, 10, 10);
            MemoryStream stream   = new MemoryStream();
            PdfWriter    writer   = PdfWriter.GetInstance(document, stream);

            writer.CloseStream = false;
            document.Open();
            PdfContentByte cb = writer.DirectContent;

            float margin          = 10;
            float printedOnHeight = 10;
            float startY          = 840 - margin;

            #region Header
            cb.BeginText();
            cb.SetFontAndSize(bf, 10);
            cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "PT. DAN LIRIS", 10, 820, 0);
            cb.SetFontAndSize(bf_bold, 12);
            cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "COST CALCULATION EXPORT GARMENT", 10, 805, 0);
            cb.EndText();
            #endregion

            #region Detail 1 (Top)
            PdfPTable table_detail1 = new PdfPTable(9);
            table_detail1.TotalWidth = 500f;

            float[] detail1_widths = new float[] { 1f, 0.1f, 2f, 1f, 0.1f, 2f, 1f, 0.1f, 2f };
            table_detail1.SetWidths(detail1_widths);

            PdfPCell cell_detail1 = new PdfPCell()
            {
                Border = Rectangle.NO_BORDER, HorizontalAlignment = Element.ALIGN_LEFT, VerticalAlignment = Element.ALIGN_MIDDLE, PaddingRight = 1, PaddingBottom = 2, PaddingTop = 2
            };
            PdfPCell cell_colon = new PdfPCell()
            {
                Border = Rectangle.NO_BORDER, HorizontalAlignment = Element.ALIGN_LEFT, VerticalAlignment = Element.ALIGN_MIDDLE
            };
            cell_colon.Phrase = new Phrase(":", normal_font);

            cell_detail1.Phrase = new Phrase("RO", normal_font);
            table_detail1.AddCell(cell_detail1);
            table_detail1.AddCell(cell_colon);
            cell_detail1.Phrase = new Phrase($"{viewModel.RO_Number}", normal_font);
            table_detail1.AddCell(cell_detail1);
            cell_detail1.Phrase = new Phrase("SIZE RANGE", normal_font);
            table_detail1.AddCell(cell_detail1);
            table_detail1.AddCell(cell_colon);
            cell_detail1.Phrase = new Phrase($"{viewModel.SizeRange}", normal_font);
            table_detail1.AddCell(cell_detail1);
            cell_detail1.Phrase = new Phrase("LEAD TIME", normal_font);
            table_detail1.AddCell(cell_detail1);
            table_detail1.AddCell(cell_colon);
            cell_detail1.Phrase = new Phrase($"{viewModel.LeadTime} hari", normal_font);
            table_detail1.AddCell(cell_detail1);

            cell_detail1.Phrase = new Phrase("ARTICLE", normal_font);
            table_detail1.AddCell(cell_detail1);
            table_detail1.AddCell(cell_colon);
            cell_detail1.Phrase = new Phrase($"{viewModel.Article}", normal_font);
            table_detail1.AddCell(cell_detail1);
            cell_detail1.Phrase = new Phrase("SECTION", normal_font);
            table_detail1.AddCell(cell_detail1);
            table_detail1.AddCell(cell_colon);
            cell_detail1.Phrase = new Phrase($"{viewModel.Section}", normal_font);
            table_detail1.AddCell(cell_detail1);
            cell_detail1.Phrase = new Phrase("FABRIC", normal_font);
            table_detail1.AddCell(cell_detail1);
            table_detail1.AddCell(cell_colon);
            cell_detail1.Phrase = new Phrase($"{viewModel.FabricAllowance}%", normal_font);
            table_detail1.AddCell(cell_detail1);

            cell_detail1.Phrase = new Phrase("DATE", normal_font);
            table_detail1.AddCell(cell_detail1);
            table_detail1.AddCell(cell_colon);
            cell_detail1.Phrase = new Phrase($"{viewModel._CreatedUtc.ToString("dd MMMM yyyy")}", normal_font);
            table_detail1.AddCell(cell_detail1);
            cell_detail1.Phrase = new Phrase("COMMODITY", normal_font);
            table_detail1.AddCell(cell_detail1);
            table_detail1.AddCell(cell_colon);
            cell_detail1.Phrase = new Phrase($"{viewModel.Commodity.name}", normal_font);
            table_detail1.AddCell(cell_detail1);
            cell_detail1.Phrase = new Phrase("ACC", normal_font);
            table_detail1.AddCell(cell_detail1);
            table_detail1.AddCell(cell_colon);
            cell_detail1.Phrase = new Phrase($"{viewModel.AccessoriesAllowance}%", normal_font);
            table_detail1.AddCell(cell_detail1);

            cell_detail1.Phrase = new Phrase("KONVEKSI", normal_font);
            table_detail1.AddCell(cell_detail1);
            table_detail1.AddCell(cell_colon);
            cell_detail1.Phrase = new Phrase($"{viewModel.Convection}", normal_font);
            table_detail1.AddCell(cell_detail1);
            cell_detail1.Phrase = new Phrase("", normal_font);
            table_detail1.AddCell(cell_detail1);
            table_detail1.AddCell(cell_detail1);
            table_detail1.AddCell(cell_detail1);
            table_detail1.AddCell(cell_detail1);
            table_detail1.AddCell(cell_detail1);
            table_detail1.AddCell(cell_detail1);
            #endregion

            #region Image
            float imageHeight;
            try
            {
                byte[] imageByte = Convert.FromBase64String(Base64.GetBase64File(viewModel.ImageFile));
                Image  image     = Image.GetInstance(imgb: imageByte);
                if (image.Width > 60)
                {
                    float percentage = 0.0f;
                    percentage = 60 / image.Width;
                    image.ScalePercent(percentage * 100);
                }
                imageHeight = image.ScaledHeight;
                float imageY = 800 - imageHeight;
                image.SetAbsolutePosition(520, imageY);
                cb.AddImage(image, inlineImage: true);
            }
            catch (Exception)
            {
                imageHeight = 0;
            }
            #endregion

            #region Draw Top
            float row1Y = 800;
            table_detail1.WriteSelectedRows(0, -1, 10, row1Y, cb);
            #endregion

            bool isDollar = viewModel.Rate.Id != 0;

            #region Detail 2.1 (Bottom, Column 1.1)
            PdfPTable table_bottom_column1_1 = new PdfPTable(2);
            table_bottom_column1_1.TotalWidth = 180f;

            float[] table_bottom_column1_1_widths = new float[] { 1f, 2f };
            table_bottom_column1_1.SetWidths(table_bottom_column1_1_widths);

            PdfPCell cell_bottom_column1_1 = new PdfPCell()
            {
                Border = Rectangle.TOP_BORDER | Rectangle.LEFT_BORDER | Rectangle.BOTTOM_BORDER | Rectangle.RIGHT_BORDER, HorizontalAlignment = Element.ALIGN_LEFT, VerticalAlignment = Element.ALIGN_MIDDLE, PaddingRight = 2, PaddingBottom = 7, PaddingLeft = 2, PaddingTop = 7
            };

            cell_bottom_column1_1.Phrase = new Phrase("QTY", normal_font);
            table_bottom_column1_1.AddCell(cell_bottom_column1_1);
            cell_bottom_column1_1.Phrase = new Phrase($"{viewModel.Quantity} {viewModel.UOM.unit}", normal_font);
            table_bottom_column1_1.AddCell(cell_bottom_column1_1);

            cell_bottom_column1_1.Phrase = new Phrase("DESCRIPTION", normal_font);
            table_bottom_column1_1.AddCell(cell_bottom_column1_1);
            cell_bottom_column1_1.Phrase = new Phrase($"{viewModel.CommodityDescription}", normal_font);
            table_bottom_column1_1.AddCell(cell_bottom_column1_1);

            cell_bottom_column1_1.Phrase = new Phrase("CONT/STYLE", normal_font);
            table_bottom_column1_1.AddCell(cell_bottom_column1_1);
            cell_bottom_column1_1.Phrase = new Phrase($"{viewModel.Article}", normal_font);
            table_bottom_column1_1.AddCell(cell_bottom_column1_1);

            cell_bottom_column1_1.Phrase = new Phrase("BUYER", normal_font);
            table_bottom_column1_1.AddCell(cell_bottom_column1_1);
            cell_bottom_column1_1.Phrase = new Phrase($"{viewModel.Buyer.name}", normal_font);
            table_bottom_column1_1.AddCell(cell_bottom_column1_1);

            cell_bottom_column1_1.Phrase = new Phrase("DELIVERY", normal_font);
            table_bottom_column1_1.AddCell(cell_bottom_column1_1);
            cell_bottom_column1_1.Phrase = new Phrase($"{viewModel.DeliveryDate.AddHours(timeoffset).ToString("dd/MM/yyyy")}", normal_font);
            table_bottom_column1_1.AddCell(cell_bottom_column1_1);
            #endregion

            #region Detail 2_2 (Bottom, Column 1.2)
            PdfPTable table_bottom_column1_2 = new PdfPTable(2);
            table_bottom_column1_2.TotalWidth = 180f;

            float[] table_bottom_column1_2_widths = new float[] { 1f, 1f };
            table_bottom_column1_2.SetWidths(table_bottom_column1_2_widths);

            PdfPCell cell_bottom_column1_2 = new PdfPCell()
            {
                Border = Rectangle.TOP_BORDER | Rectangle.LEFT_BORDER | Rectangle.BOTTOM_BORDER | Rectangle.RIGHT_BORDER, HorizontalAlignment = Element.ALIGN_CENTER, VerticalAlignment = Element.ALIGN_MIDDLE, Padding = 3
            };

            cell_bottom_column1_2.Phrase = new Phrase("FOB PRICE", bold_font);
            table_bottom_column1_2.AddCell(cell_bottom_column1_2);
            cell_bottom_column1_2.Phrase = new Phrase("CMT PRICE", bold_font);
            table_bottom_column1_2.AddCell(cell_bottom_column1_2);

            double CM_Price = 0;
            foreach (CostCalculationGarment_MaterialViewModel item in viewModel.CostCalculationGarment_Materials)
            {
                CM_Price += item.CM_Price ?? 0;
            }
            double ConfirmPrice = viewModel.ConfirmPrice ?? 0;
            double CMT          = CM_Price > 0 ? ConfirmPrice : 0;
            string CMT_Price    = this.GetCurrencyValue(CMT, isDollar);
            double FOB          = ConfirmPrice + CM_Price;
            string FOB_Price    = this.GetCurrencyValue(FOB, isDollar);
            cell_bottom_column1_2.Phrase = new Phrase($"{FOB_Price}", normal_font);
            table_bottom_column1_2.AddCell(cell_bottom_column1_2);
            cell_bottom_column1_2.Phrase = new Phrase($"{CMT_Price}", normal_font);
            table_bottom_column1_2.AddCell(cell_bottom_column1_2);
            #endregion

            #region Detail 2.3 (Bottom, Column 1.3)
            PdfPTable table_bottom_column1_3 = new PdfPTable(2);
            table_bottom_column1_3.TotalWidth = 180f;

            float[] table_bottom_column1_3_widths = new float[] { 1f, 1f };
            table_bottom_column1_3.SetWidths(table_bottom_column1_3_widths);

            PdfPCell cell_bottom_column1_3 = new PdfPCell()
            {
                Border = Rectangle.TOP_BORDER | Rectangle.LEFT_BORDER | Rectangle.BOTTOM_BORDER | Rectangle.RIGHT_BORDER, HorizontalAlignment = Element.ALIGN_CENTER, VerticalAlignment = Element.ALIGN_MIDDLE, Padding = 3
            };

            cell_bottom_column1_3.Phrase = new Phrase("CNF PRICE", bold_font);
            table_bottom_column1_3.AddCell(cell_bottom_column1_3);
            cell_bottom_column1_3.Phrase = new Phrase("CIF PRICE", bold_font);
            table_bottom_column1_3.AddCell(cell_bottom_column1_3);

            string CNF_Price = this.GetCurrencyValue(0, isDollar);
            cell_bottom_column1_3.Phrase = new Phrase($"{CNF_Price}", normal_font);
            table_bottom_column1_3.AddCell(cell_bottom_column1_3);
            string CIF_Price = this.GetCurrencyValue(0, isDollar);
            cell_bottom_column1_3.Phrase = new Phrase($"{CIF_Price}", normal_font);
            table_bottom_column1_3.AddCell(cell_bottom_column1_3);
            #endregion

            #region Detail 3.1 (Bottom, Column 2.1)
            PdfPTable table_bottom_column2_1 = new PdfPTable(3);
            table_bottom_column2_1.TotalWidth = 190f;

            float[] table_bottom_column2_1_widths = new float[] { 1.5f, 1f, 1.5f };
            table_bottom_column2_1.SetWidths(table_bottom_column2_1_widths);

            PdfPCell cell_bottom_column2_1 = new PdfPCell()
            {
                Border = Rectangle.TOP_BORDER | Rectangle.LEFT_BORDER | Rectangle.BOTTOM_BORDER | Rectangle.RIGHT_BORDER, HorizontalAlignment = Element.ALIGN_LEFT, VerticalAlignment = Element.ALIGN_MIDDLE, Padding = 3
            };

            PdfPCell cell_bottom_column2_1_colspan2 = new PdfPCell()
            {
                Border = Rectangle.TOP_BORDER | Rectangle.LEFT_BORDER | Rectangle.BOTTOM_BORDER | Rectangle.RIGHT_BORDER, HorizontalAlignment = Element.ALIGN_LEFT, VerticalAlignment = Element.ALIGN_MIDDLE, Padding = 3, Colspan = 2
            };

            cell_bottom_column2_1.Phrase = new Phrase("TOTAL", normal_font);
            table_bottom_column2_1.AddCell(cell_bottom_column2_1);
            double total = 0;
            foreach (CostCalculationGarment_MaterialViewModel item in viewModel.CostCalculationGarment_Materials)
            {
                total += item.Total;
            }
            total += viewModel.ProductionCost;
            cell_bottom_column2_1_colspan2.Phrase = new Phrase(Number.ToRupiahWithoutSymbol(total), normal_font);
            table_bottom_column2_1.AddCell(cell_bottom_column2_1_colspan2);

            cell_bottom_column2_1.Phrase = new Phrase("OTL 1", normal_font);
            table_bottom_column2_1.AddCell(cell_bottom_column2_1);
            double OTL1CalculatedValue = viewModel.OTL1.CalculatedValue ?? 0;
            cell_bottom_column2_1.Phrase = new Phrase($"{Number.ToRupiahWithoutSymbol(OTL1CalculatedValue)}", normal_font);
            table_bottom_column2_1.AddCell(cell_bottom_column2_1);
            double afterOTL1 = total + OTL1CalculatedValue;
            cell_bottom_column2_1.Phrase = new Phrase($"{Number.ToRupiahWithoutSymbol(afterOTL1)}", normal_font);
            table_bottom_column2_1.AddCell(cell_bottom_column2_1);

            cell_bottom_column2_1.Phrase = new Phrase("OTL 2", normal_font);
            table_bottom_column2_1.AddCell(cell_bottom_column2_1);
            double OTL2CalculatedValue = viewModel.OTL2.CalculatedValue ?? 0;
            cell_bottom_column2_1.Phrase = new Phrase($"{Number.ToRupiahWithoutSymbol(OTL2CalculatedValue)}", normal_font);
            table_bottom_column2_1.AddCell(cell_bottom_column2_1);
            double afterOTL2 = afterOTL1 + OTL2CalculatedValue;
            cell_bottom_column2_1.Phrase = new Phrase($"{Number.ToRupiahWithoutSymbol(afterOTL2)}", normal_font);
            table_bottom_column2_1.AddCell(cell_bottom_column2_1);

            cell_bottom_column2_1.Phrase = new Phrase("RISK", normal_font);
            table_bottom_column2_1.AddCell(cell_bottom_column2_1);
            cell_bottom_column2_1.Phrase = new Phrase(String.Format("{0:0.00}%", viewModel.Risk), normal_font);
            table_bottom_column2_1.AddCell(cell_bottom_column2_1);
            double afterRisk = (100 + viewModel.Risk) * afterOTL2 / 100;;
            cell_bottom_column2_1.Phrase = new Phrase($"{Number.ToRupiahWithoutSymbol(afterRisk)}", normal_font);
            table_bottom_column2_1.AddCell(cell_bottom_column2_1);

            cell_bottom_column2_1.Phrase = new Phrase("BEA ANGKUT", normal_font);
            table_bottom_column2_1.AddCell(cell_bottom_column2_1);
            cell_bottom_column2_1.Phrase = new Phrase($"{Number.ToRupiahWithoutSymbol(viewModel.FreightCost)}", normal_font);
            table_bottom_column2_1.AddCell(cell_bottom_column2_1);
            double afterFreightCost = afterRisk + viewModel.FreightCost;
            cell_bottom_column2_1.Phrase = new Phrase($"{Number.ToRupiahWithoutSymbol(afterFreightCost)}", normal_font);
            table_bottom_column2_1.AddCell(cell_bottom_column2_1);

            cell_bottom_column2_1.Phrase = new Phrase("SUB TOTAL", normal_font);
            table_bottom_column2_1.AddCell(cell_bottom_column2_1);
            cell_bottom_column2_1_colspan2.Phrase = new Phrase($"{Number.ToRupiahWithoutSymbol(afterFreightCost)}", normal_font);
            table_bottom_column2_1.AddCell(cell_bottom_column2_1_colspan2);

            cell_bottom_column2_1.Phrase = new Phrase("NET/FOB (%)", normal_font);
            table_bottom_column2_1.AddCell(cell_bottom_column2_1);
            cell_bottom_column2_1.Phrase = new Phrase(String.Format("{0:0.00}%", viewModel.NETFOBP), normal_font);
            table_bottom_column2_1.AddCell(cell_bottom_column2_1);
            cell_bottom_column2_1.Phrase = new Phrase($"{Number.ToRupiahWithoutSymbol(viewModel.NETFOB)}", normal_font);
            table_bottom_column2_1.AddCell(cell_bottom_column2_1);

            cell_bottom_column2_1.Phrase = new Phrase("COMM (%)", normal_font);
            table_bottom_column2_1.AddCell(cell_bottom_column2_1);
            cell_bottom_column2_1.Phrase = new Phrase(String.Format("{0:0.00}%", viewModel.CommissionPortion), normal_font);
            table_bottom_column2_1.AddCell(cell_bottom_column2_1);
            cell_bottom_column2_1.Phrase = new Phrase($"{Number.ToRupiahWithoutSymbol(viewModel.CommissionRate)}", normal_font);
            table_bottom_column2_1.AddCell(cell_bottom_column2_1);

            cell_bottom_column2_1.Phrase = new Phrase("CONFIRM PRICE", normal_font);
            table_bottom_column2_1.AddCell(cell_bottom_column2_1);;
            double confirmPrice         = viewModel.ConfirmPrice ?? 0 + viewModel.Rate.Value ?? 0;
            double confirmPriceWithRate = isDollar ? confirmPrice * viewModel.Rate.Value ?? 1 : confirmPrice;
            cell_bottom_column2_1_colspan2.Phrase = new Phrase($"{Number.ToRupiahWithoutSymbol(confirmPriceWithRate)}", normal_font);
            table_bottom_column2_1.AddCell(cell_bottom_column2_1_colspan2);
            #endregion

            #region Detail 3.2 (Bottom, Column 2.2)
            PdfPTable table_bottom_column2_2 = new PdfPTable(4);
            table_bottom_column2_2.TotalWidth = 190f;

            float[] table_bottom_column2_2_widths = new float[] { 1f, 1.25f, 1f, 1.25f };
            table_bottom_column2_2.SetWidths(table_bottom_column2_2_widths);
            PdfPCell cell_bottom_column2_2 = new PdfPCell()
            {
                Border = Rectangle.TOP_BORDER | Rectangle.LEFT_BORDER | Rectangle.BOTTOM_BORDER | Rectangle.RIGHT_BORDER, HorizontalAlignment = Element.ALIGN_LEFT, VerticalAlignment = Element.ALIGN_MIDDLE, PaddingTop = 5, PaddingRight = 3, PaddingBottom = 5, PaddingLeft = 3, Colspan = 2
            };

            cell_bottom_column2_2 = new PdfPCell()
            {
                Border = Rectangle.TOP_BORDER | Rectangle.LEFT_BORDER, HorizontalAlignment = Element.ALIGN_LEFT, VerticalAlignment = Element.ALIGN_MIDDLE, PaddingTop = 5, PaddingRight = 3, PaddingBottom = 5, PaddingLeft = 3, Colspan = 2
            };
            cell_bottom_column2_2.Phrase = new Phrase("FREIGHT", normal_font);
            table_bottom_column2_2.AddCell(cell_bottom_column2_2);
            cell_bottom_column2_2 = new PdfPCell()
            {
                Border = Rectangle.TOP_BORDER | Rectangle.RIGHT_BORDER, HorizontalAlignment = Element.ALIGN_LEFT, VerticalAlignment = Element.ALIGN_MIDDLE, PaddingTop = 5, PaddingRight = 3, PaddingBottom = 5, PaddingLeft = 3, Colspan = 2
            };
            string freight = this.GetCurrencyValue(viewModel.Freight ?? 0, isDollar);
            cell_bottom_column2_2.Phrase = new Phrase($"= {freight}", normal_font);
            table_bottom_column2_2.AddCell(cell_bottom_column2_2);

            cell_bottom_column2_2 = new PdfPCell()
            {
                Border = Rectangle.LEFT_BORDER, HorizontalAlignment = Element.ALIGN_LEFT, VerticalAlignment = Element.ALIGN_MIDDLE, PaddingTop = 5, PaddingRight = 3, PaddingBottom = 5, PaddingLeft = 3, Colspan = 2
            };
            cell_bottom_column2_2.Phrase = new Phrase("INSURANCE", normal_font);
            table_bottom_column2_2.AddCell(cell_bottom_column2_2);
            cell_bottom_column2_2 = new PdfPCell()
            {
                Border = Rectangle.RIGHT_BORDER, HorizontalAlignment = Element.ALIGN_LEFT, VerticalAlignment = Element.ALIGN_MIDDLE, PaddingTop = 5, PaddingRight = 3, PaddingBottom = 5, PaddingLeft = 3, Colspan = 2
            };
            string insurance = this.GetCurrencyValue(viewModel.Insurance ?? 0, isDollar);
            cell_bottom_column2_2.Phrase = new Phrase($"= {insurance}", normal_font);
            table_bottom_column2_2.AddCell(cell_bottom_column2_2);

            cell_bottom_column2_2 = new PdfPCell()
            {
                Border = Rectangle.LEFT_BORDER | Rectangle.BOTTOM_BORDER, HorizontalAlignment = Element.ALIGN_LEFT, VerticalAlignment = Element.ALIGN_MIDDLE, PaddingTop = 5, PaddingRight = 3, PaddingBottom = 5, PaddingLeft = 3, Colspan = 2
            };
            cell_bottom_column2_2.Phrase = new Phrase("CONFIRM PRICE", normal_font);
            table_bottom_column2_2.AddCell(cell_bottom_column2_2);
            cell_bottom_column2_2 = new PdfPCell()
            {
                Border = Rectangle.BOTTOM_BORDER | Rectangle.RIGHT_BORDER, HorizontalAlignment = Element.ALIGN_LEFT, VerticalAlignment = Element.ALIGN_MIDDLE, PaddingTop = 5, PaddingRight = 3, PaddingBottom = 5, PaddingLeft = 3, Colspan = 2
            };
            string confirmPriceFOB = this.GetCurrencyValue(viewModel.ConfirmPrice ?? 0, isDollar);
            cell_bottom_column2_2.Phrase = new Phrase($"= {confirmPriceFOB}", normal_font);
            table_bottom_column2_2.AddCell(cell_bottom_column2_2);

            cell_bottom_column2_2 = new PdfPCell()
            {
                Border = Rectangle.TOP_BORDER | Rectangle.LEFT_BORDER, HorizontalAlignment = Element.ALIGN_LEFT, VerticalAlignment = Element.ALIGN_MIDDLE, PaddingTop = 5, PaddingRight = 3, PaddingBottom = 5, PaddingLeft = 3
            };
            cell_bottom_column2_2.Phrase = new Phrase("SMV CUT", normal_font);
            table_bottom_column2_2.AddCell(cell_bottom_column2_2);
            cell_bottom_column2_2 = new PdfPCell()
            {
                Border = Rectangle.TOP_BORDER | Rectangle.RIGHT_BORDER, HorizontalAlignment = Element.ALIGN_LEFT, VerticalAlignment = Element.ALIGN_MIDDLE, PaddingTop = 5, PaddingRight = 3, PaddingBottom = 5, PaddingLeft = 3
            };
            cell_bottom_column2_2.Phrase = new Phrase($"= {viewModel.SMV_Cutting}", normal_font);
            table_bottom_column2_2.AddCell(cell_bottom_column2_2);
            cell_bottom_column2_2 = new PdfPCell()
            {
                Border = Rectangle.TOP_BORDER | Rectangle.LEFT_BORDER, HorizontalAlignment = Element.ALIGN_LEFT, VerticalAlignment = Element.ALIGN_MIDDLE, PaddingTop = 5, PaddingRight = 3, PaddingBottom = 5, PaddingLeft = 3
            };
            cell_bottom_column2_2.Phrase = new Phrase("SMV SEW", normal_font);
            table_bottom_column2_2.AddCell(cell_bottom_column2_2);
            cell_bottom_column2_2 = new PdfPCell()
            {
                Border = Rectangle.TOP_BORDER | Rectangle.RIGHT_BORDER, HorizontalAlignment = Element.ALIGN_LEFT, VerticalAlignment = Element.ALIGN_MIDDLE, PaddingTop = 5, PaddingRight = 3, PaddingBottom = 5, PaddingLeft = 3
            };
            cell_bottom_column2_2.Phrase = new Phrase($"= {viewModel.SMV_Sewing}", normal_font);
            table_bottom_column2_2.AddCell(cell_bottom_column2_2);

            cell_bottom_column2_2 = new PdfPCell()
            {
                Border = Rectangle.LEFT_BORDER | Rectangle.BOTTOM_BORDER, HorizontalAlignment = Element.ALIGN_LEFT, VerticalAlignment = Element.ALIGN_MIDDLE, PaddingTop = 5, PaddingRight = 3, PaddingBottom = 5, PaddingLeft = 3
            };
            cell_bottom_column2_2.Phrase = new Phrase("SMV FIN", normal_font);
            table_bottom_column2_2.AddCell(cell_bottom_column2_2);
            cell_bottom_column2_2 = new PdfPCell()
            {
                Border = Rectangle.RIGHT_BORDER | Rectangle.BOTTOM_BORDER, HorizontalAlignment = Element.ALIGN_LEFT, VerticalAlignment = Element.ALIGN_MIDDLE, PaddingTop = 5, PaddingRight = 3, PaddingBottom = 5, PaddingLeft = 3
            };
            cell_bottom_column2_2.Phrase = new Phrase($"= {viewModel.SMV_Finishing}", normal_font);
            table_bottom_column2_2.AddCell(cell_bottom_column2_2);
            cell_bottom_column2_2 = new PdfPCell()
            {
                Border = Rectangle.LEFT_BORDER | Rectangle.BOTTOM_BORDER, HorizontalAlignment = Element.ALIGN_LEFT, VerticalAlignment = Element.ALIGN_MIDDLE, PaddingTop = 5, PaddingRight = 3, PaddingBottom = 5, PaddingLeft = 3
            };
            cell_bottom_column2_2.Phrase = new Phrase("SMV TOT", normal_font);
            table_bottom_column2_2.AddCell(cell_bottom_column2_2);
            cell_bottom_column2_2 = new PdfPCell()
            {
                Border = Rectangle.RIGHT_BORDER | Rectangle.BOTTOM_BORDER, HorizontalAlignment = Element.ALIGN_LEFT, VerticalAlignment = Element.ALIGN_MIDDLE, PaddingTop = 5, PaddingRight = 3, PaddingBottom = 5, PaddingLeft = 3
            };
            cell_bottom_column2_2.Phrase = new Phrase($"= {viewModel.SMV_Total}", normal_font);
            table_bottom_column2_2.AddCell(cell_bottom_column2_2);
            #endregion

            #region Detail 4 (Bottom, Column 3.1)
            PdfPTable table_bottom_column3_1 = new PdfPTable(2);
            table_bottom_column3_1.TotalWidth = 180f;

            float[] table_bottom_column3_1_widths = new float[] { 1f, 2f };
            table_bottom_column3_1.SetWidths(table_bottom_column3_1_widths);

            PdfPCell cell_bottom_column3_1 = new PdfPCell()
            {
                Border = Rectangle.TOP_BORDER | Rectangle.LEFT_BORDER | Rectangle.BOTTOM_BORDER | Rectangle.RIGHT_BORDER, HorizontalAlignment = Element.ALIGN_LEFT, VerticalAlignment = Element.ALIGN_MIDDLE, PaddingRight = 2, PaddingBottom = 7, PaddingLeft = 2, PaddingTop = 7
            };

            cell_bottom_column3_1.Phrase = new Phrase("DESCRIPTION", normal_font);
            table_bottom_column3_1.AddCell(cell_bottom_column3_1);
            cell_bottom_column3_1.Phrase = new Phrase($"{viewModel.Description}", normal_font);
            table_bottom_column3_1.AddCell(cell_bottom_column3_1);
            #endregion

            #region Signature
            PdfPTable table_signature = new PdfPTable(3);
            table_signature.TotalWidth = 570f;

            float[] signature_widths = new float[] { 1f, 1f, 1f };
            table_signature.SetWidths(signature_widths);

            PdfPCell cell_signature = new PdfPCell()
            {
                Border = Rectangle.NO_BORDER, HorizontalAlignment = Element.ALIGN_CENTER, VerticalAlignment = Element.ALIGN_MIDDLE, Padding = 2
            };

            //cell_signature.Phrase = new Phrase("", normal_font);
            //table_signature.AddCell(cell_signature);
            //cell_signature.Phrase = new Phrase("", normal_font);
            //table_signature.AddCell(cell_signature);

            string signatureArea = string.Empty;
            for (int i = 0; i < 5; i++)
            {
                signatureArea += Environment.NewLine;
            }
            cell_signature.Phrase = new Phrase(signatureArea, normal_font);
            table_signature.AddCell(cell_signature);
            cell_signature.Phrase = new Phrase(signatureArea, normal_font);
            table_signature.AddCell(cell_signature);
            cell_signature.Phrase = new Phrase(signatureArea, normal_font);
            table_signature.AddCell(cell_signature);

            cell_signature.Phrase = new Phrase("Penjualan", normal_font);
            table_signature.AddCell(cell_signature);
            cell_signature.Phrase = new Phrase("Kasie. Kabag. Penjualan", normal_font);
            table_signature.AddCell(cell_signature);
            cell_signature.Phrase = new Phrase("Kadiv. Penjualan", normal_font);
            table_signature.AddCell(cell_signature);
            #endregion

            #region Cost Calculation Material
            PdfPTable table_ccm = new PdfPTable(7);
            table_ccm.TotalWidth = 570f;

            float[] ccm_widths = new float[] { 1.25f, 3.5f, 3.5f, 5f, 3f, 4f, 4f };
            table_ccm.SetWidths(ccm_widths);

            PdfPCell cell_ccm_center = new PdfPCell()
            {
                Border = Rectangle.TOP_BORDER | Rectangle.LEFT_BORDER | Rectangle.BOTTOM_BORDER | Rectangle.RIGHT_BORDER, HorizontalAlignment = Element.ALIGN_CENTER, VerticalAlignment = Element.ALIGN_MIDDLE, Padding = 2
            };
            PdfPCell cell_ccm_left = new PdfPCell()
            {
                Border = Rectangle.TOP_BORDER | Rectangle.LEFT_BORDER | Rectangle.BOTTOM_BORDER | Rectangle.RIGHT_BORDER, HorizontalAlignment = Element.ALIGN_LEFT, VerticalAlignment = Element.ALIGN_MIDDLE, Padding = 2
            };
            PdfPCell cell_ccm_right = new PdfPCell()
            {
                Border = Rectangle.TOP_BORDER | Rectangle.LEFT_BORDER | Rectangle.BOTTOM_BORDER | Rectangle.RIGHT_BORDER, HorizontalAlignment = Element.ALIGN_RIGHT, VerticalAlignment = Element.ALIGN_MIDDLE, Padding = 2
            };

            cell_ccm_center.Phrase = new Phrase("NO", bold_font);
            table_ccm.AddCell(cell_ccm_center);

            cell_ccm_center.Phrase = new Phrase("KATEGORI", bold_font);
            table_ccm.AddCell(cell_ccm_center);

            cell_ccm_center.Phrase = new Phrase("KODE PRODUK", bold_font);
            table_ccm.AddCell(cell_ccm_center);

            cell_ccm_center.Phrase = new Phrase("DESKRIPSI", bold_font);
            table_ccm.AddCell(cell_ccm_center);

            cell_ccm_center.Phrase = new Phrase("QUANTITY", bold_font);
            table_ccm.AddCell(cell_ccm_center);

            cell_ccm_center.Phrase = new Phrase("USD. PTC/PC", bold_font);
            table_ccm.AddCell(cell_ccm_center);

            cell_ccm_center.Phrase = new Phrase("USD. TOTAL", bold_font);
            table_ccm.AddCell(cell_ccm_center);

            double  Total              = 0;
            float   row1Height         = imageHeight > table_detail1.TotalHeight ? imageHeight : table_detail1.TotalHeight;
            float   row2Y              = row1Y - row1Height - 10;
            float[] row3Heights        = { table_bottom_column1_1.TotalHeight + 10 + table_bottom_column1_2.TotalHeight + 10 + table_bottom_column1_3.TotalHeight, table_bottom_column2_1.TotalHeight + 10 + table_bottom_column2_2.TotalHeight, table_bottom_column3_1.TotalHeight };
            float   dollarDetailHeight = 10;
            if (isDollar)
            {
                row3Heights[1] += dollarDetailHeight;
            }
            float row3Height = row3Heights.Max();
            float secondHighestRow3Height = row3Heights[1] > row3Heights[2] ? row3Heights[1] : row3Heights[2];
            bool  signatureInsideRow3     = row3Heights.Max() == row3Heights[0] && row3Heights[0] - 10 - secondHighestRow3Height > table_signature.TotalHeight;
            float row2RemainingHeight     = row2Y - 10 - row3Height - printedOnHeight - margin;
            float row2AllowedHeight       = row2Y - printedOnHeight - margin;

            for (int i = 0; i < viewModel.CostCalculationGarment_Materials.Count; i++)
            {
                //NO
                cell_ccm_center.Phrase = new Phrase((i + 1).ToString(), normal_font);
                table_ccm.AddCell(cell_ccm_center);

                //KATEGORI
                cell_ccm_left.Phrase = new Phrase(viewModel.CostCalculationGarment_Materials[i].Category.name, normal_font);
                table_ccm.AddCell(cell_ccm_left);

                //KODE PRODUK
                cell_ccm_left.Phrase = new Phrase(viewModel.CostCalculationGarment_Materials[i].Product.code, normal_font);
                table_ccm.AddCell(cell_ccm_left);

                //DESKRIPSI
                cell_ccm_left.Phrase = new Phrase(viewModel.CostCalculationGarment_Materials[i].Description, normal_font);
                table_ccm.AddCell(cell_ccm_left);

                cell_ccm_right.Phrase = new Phrase(String.Format("{0} {1}", viewModel.CostCalculationGarment_Materials[i].Quantity, viewModel.CostCalculationGarment_Materials[i].UOMQuantity.unit), normal_font);
                table_ccm.AddCell(cell_ccm_right);

                cell_ccm_right.Phrase = new Phrase(String.Format("{0}/{1}", Number.ToRupiahWithoutSymbol(viewModel.CostCalculationGarment_Materials[i].Price), viewModel.CostCalculationGarment_Materials[i].UOMPrice.unit), normal_font);
                table_ccm.AddCell(cell_ccm_right);

                cell_ccm_right.Phrase = new Phrase(Number.ToRupiahWithoutSymbol(viewModel.CostCalculationGarment_Materials[i].Total), normal_font);
                table_ccm.AddCell(cell_ccm_right);

                Total += viewModel.CostCalculationGarment_Materials[i].Total;
                float currentHeight = table_ccm.TotalHeight;
                if (currentHeight / row2RemainingHeight > 1)
                {
                    if (currentHeight / row2AllowedHeight > 1)
                    {
                        PdfPRow headerRow = table_ccm.GetRow(0);
                        PdfPRow lastRow   = table_ccm.GetRow(table_ccm.Rows.Count - 1);
                        table_ccm.DeleteLastRow();
                        table_ccm.WriteSelectedRows(0, -1, 10, row2Y, cb);
                        table_ccm.DeleteBodyRows();
                        this.DrawPrintedOn(now, bf, cb);
                        document.NewPage();
                        table_ccm.Rows.Add(headerRow);
                        table_ccm.Rows.Add(lastRow);
                        table_ccm.CalculateHeights();
                        row2Y = startY;
                        row2RemainingHeight = row2Y - 10 - row3Height - printedOnHeight - margin;
                        row2AllowedHeight   = row2Y - printedOnHeight - margin;
                    }
                }
            }

            cell_ccm_right = new PdfPCell()
            {
                Border = Rectangle.TOP_BORDER | Rectangle.LEFT_BORDER | Rectangle.BOTTOM_BORDER | Rectangle.RIGHT_BORDER, HorizontalAlignment = Element.ALIGN_RIGHT, VerticalAlignment = Element.ALIGN_MIDDLE, Padding = 2, Colspan = 6
            };
            cell_ccm_right.Phrase = new Phrase("TOTAL", bold_font_8);
            table_ccm.AddCell(cell_ccm_right);

            cell_ccm_right = new PdfPCell()
            {
                Border = Rectangle.TOP_BORDER | Rectangle.LEFT_BORDER | Rectangle.BOTTOM_BORDER | Rectangle.RIGHT_BORDER, HorizontalAlignment = Element.ALIGN_RIGHT, VerticalAlignment = Element.ALIGN_MIDDLE, Padding = 2
            };
            cell_ccm_right.Phrase = new Phrase(Number.ToRupiah(Total), bold_font_8);
            table_ccm.AddCell(cell_ccm_right);
            #endregion

            #region Draw Middle and Bottom
            table_ccm.WriteSelectedRows(0, -1, 10, row2Y, cb);

            float row3Y = row2Y - table_ccm.TotalHeight - 10;
            float row3RemainingHeight = row3Y - printedOnHeight - margin;
            if (row3RemainingHeight < row3Height)
            {
                this.DrawPrintedOn(now, bf, cb);
                row3Y = startY;
                document.NewPage();
            }

            table_bottom_column1_1.WriteSelectedRows(0, -1, 10, row3Y, cb);

            float detail1_2Y = row3Y - table_bottom_column1_1.TotalHeight - 10;
            table_bottom_column1_2.WriteSelectedRows(0, -1, 10, detail1_2Y, cb);

            float detail1_3Y = detail1_2Y - table_bottom_column1_2.TotalHeight - 10;
            table_bottom_column1_3.WriteSelectedRows(0, -1, 10, detail1_3Y, cb);

            table_bottom_column2_1.WriteSelectedRows(0, -1, 200, row3Y, cb);

            float noteY = row3Y - table_bottom_column2_1.TotalHeight;
            float table_bottom_column2_2Y;
            if (isDollar)
            {
                noteY = noteY - 15;
                table_bottom_column2_2Y = noteY - 5;
                cb.BeginText();
                cb.SetFontAndSize(bf, 7);
                cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, $"NOTE: 1 US$ = {Number.ToRupiah(viewModel.Rate.Value)}", 200, noteY, 0);
                cb.EndText();
            }
            else
            {
                table_bottom_column2_2Y = noteY - 10;
            }
            table_bottom_column2_2.WriteSelectedRows(0, -1, 200, table_bottom_column2_2Y, cb);

            //table_bottom_column1_2.WriteSelectedRows(0, -1, 400, row3Y, cb);
            table_bottom_column3_1.WriteSelectedRows(0, -1, 400, row3Y, cb);

            float table_signatureX;
            float table_signatureY;
            if (signatureInsideRow3)
            {
                table_signatureX           = margin + table_bottom_column2_2.TotalWidth + 10;
                table_signatureY           = row3Y - row3Height + table_signature.TotalHeight;
                table_signature.TotalWidth = 390f;
            }
            else
            {
                table_signatureX = margin;
                table_signatureY = row3Y - row3Height - 10;
                float signatureRemainingHeight = table_signatureY - printedOnHeight - margin;
                if (signatureRemainingHeight < table_signature.TotalHeight)
                {
                    this.DrawPrintedOn(now, bf, cb);
                    table_signatureY = startY;
                    document.NewPage();
                }
            }
            table_signature.WriteSelectedRows(0, -1, table_signatureX, table_signatureY, cb);

            this.DrawPrintedOn(now, bf, cb);
            #endregion

            document.Close();

            byte[] byteInfo = stream.ToArray();
            stream.Write(byteInfo, 0, byteInfo.Length);
            stream.Position = 0;

            return(stream);
        }
Esempio n. 6
0
        /**
         * Splits a row to newHeight.
         * The returned row is the remainder. It will return null if the newHeight
         * was so small that only an empty row would result.
         *
         * @param new_height the new height
         * @return the remainder row or null if the newHeight was so small that only
         * an empty row would result
         */
        public PdfPRow SplitRow(PdfPTable table, int rowIndex, float new_height)
        {
            PdfPCell[] newCells = new PdfPCell[cells.Length];
            float[]    fixHs    = new float[cells.Length];
            float[]    minHs    = new float[cells.Length];
            bool       allEmpty = true;

            for (int k = 0; k < cells.Length; ++k)
            {
                float    newHeight = new_height;
                PdfPCell cell      = cells[k];
                if (cell == null)
                {
                    int index = rowIndex;
                    if (table.RowSpanAbove(index, k))
                    {
                        newHeight += table.GetRowHeight(index);
                        while (table.RowSpanAbove(--index, k))
                        {
                            newHeight += table.GetRowHeight(index);
                        }
                        PdfPRow row = table.GetRow(index);
                        if (row != null && row.GetCells()[k] != null)
                        {
                            newCells[k] = new PdfPCell(row.GetCells()[k]);
                            newCells[k].ConsumeHeight(newHeight);
                            newCells[k].Rowspan = row.GetCells()[k].Rowspan - rowIndex + index;
                            allEmpty            = false;
                        }
                    }
                    continue;
                }
                fixHs[k] = cell.FixedHeight;
                minHs[k] = cell.MinimumHeight;
                Image    img     = cell.Image;
                PdfPCell newCell = new PdfPCell(cell);
                if (img != null)
                {
                    if (newHeight > cell.EffectivePaddingBottom + cell.EffectivePaddingTop + 2)
                    {
                        newCell.Phrase = null;
                        allEmpty       = false;
                    }
                }
                else
                {
                    float      y;
                    ColumnText ct     = ColumnText.Duplicate(cell.Column);
                    float      left   = cell.Left + cell.EffectivePaddingLeft;
                    float      bottom = cell.Top + cell.EffectivePaddingBottom - newHeight;
                    float      right  = cell.Right - cell.EffectivePaddingRight;
                    float      top    = cell.Top - cell.EffectivePaddingTop;
                    switch (cell.Rotation)
                    {
                    case 90:
                    case 270:
                        y = SetColumn(ct, bottom, left, top, right);
                        break;

                    default:
                        y = SetColumn(ct, left, bottom, cell.NoWrap ? RIGHT_LIMIT : right, top);
                        break;
                    }
                    int status;
                    status = ct.Go(true);
                    bool thisEmpty = (ct.YLine == y);
                    if (thisEmpty)
                    {
                        newCell.Column = ColumnText.Duplicate(cell.Column);
                        ct.FilledWidth = 0;
                    }
                    else if ((status & ColumnText.NO_MORE_TEXT) == 0)
                    {
                        newCell.Column = ct;
                        ct.FilledWidth = 0;
                    }
                    else
                    {
                        newCell.Phrase = null;
                    }
                    allEmpty = (allEmpty && thisEmpty);
                }
                newCells[k]      = newCell;
                cell.FixedHeight = newHeight;
            }
            if (allEmpty)
            {
                for (int k = 0; k < cells.Length; ++k)
                {
                    PdfPCell cell = cells[k];
                    if (cell == null)
                    {
                        continue;
                    }
                    if (fixHs[k] > 0)
                    {
                        cell.FixedHeight = fixHs[k];
                    }
                    else
                    {
                        cell.MinimumHeight = minHs[k];
                    }
                }
                return(null);
            }
            CalculateHeights();
            PdfPRow split = new PdfPRow(newCells);

            split.widths = (float[])widths.Clone();
            split.CalculateHeights();
            return(split);
        }
        public MemoryStream GeneratePdfTemplate(CostCalculationGarmentViewModel viewModel, int timeoffset)
        {
            BaseFont bf          = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1250, BaseFont.NOT_EMBEDDED);
            BaseFont bf_bold     = BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1250, BaseFont.NOT_EMBEDDED);
            Font     normal_font = FontFactory.GetFont(BaseFont.HELVETICA, BaseFont.CP1250, BaseFont.NOT_EMBEDDED, 7);
            Font     bold_font   = FontFactory.GetFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1250, BaseFont.NOT_EMBEDDED, 7);
            DateTime now         = DateTime.Now;

            Document     document = new Document(PageSize.A4, 10, 10, 10, 10);
            MemoryStream stream   = new MemoryStream();
            PdfWriter    writer   = PdfWriter.GetInstance(document, stream);

            writer.CloseStream = false;
            document.Open();
            PdfContentByte cb = writer.DirectContent;

            float    margin          = 10;
            float    printedOnHeight = 10;
            float    startY          = 840 - margin;
            PdfPCell cell_colon      = new PdfPCell()
            {
                Border = Rectangle.NO_BORDER, HorizontalAlignment = Element.ALIGN_LEFT, VerticalAlignment = Element.ALIGN_MIDDLE, Phrase = new Phrase(":", normal_font)
            };

            #region Header
            cb.BeginText();
            cb.SetFontAndSize(bf, 10);
            cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "PT. DAN LIRIS", 10, 820, 0);
            cb.SetFontAndSize(bf_bold, 12);
            cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "BUDGET EXPORT GARMENT", 10, 805, 0);
            cb.EndText();
            #endregion

            #region Detail 1 (Top)
            PdfPTable table_detail1 = new PdfPTable(9);
            table_detail1.TotalWidth = 570f;

            float[] detail1_widths = new float[] { 1f, 0.1f, 2f, 1f, 0.1f, 2f, 1.5f, 0.1f, 2f };
            table_detail1.SetWidths(detail1_widths);

            PdfPCell cell_detail1 = new PdfPCell()
            {
                Border = Rectangle.NO_BORDER, HorizontalAlignment = Element.ALIGN_LEFT, VerticalAlignment = Element.ALIGN_MIDDLE, PaddingRight = 1, PaddingBottom = 2, PaddingTop = 2
            };

            cell_detail1.Phrase = new Phrase("RO", normal_font);
            table_detail1.AddCell(cell_detail1);
            table_detail1.AddCell(cell_colon);
            cell_detail1.Phrase = new Phrase($"{viewModel.RO_Number}", normal_font);
            table_detail1.AddCell(cell_detail1);
            cell_detail1.Phrase = new Phrase("SECTION", normal_font);
            table_detail1.AddCell(cell_detail1);
            table_detail1.AddCell(cell_colon);
            cell_detail1.Phrase = new Phrase($"{viewModel.Section}", normal_font);
            table_detail1.AddCell(cell_detail1);
            cell_detail1.Phrase = new Phrase("CONFIRM ORDER", normal_font);
            table_detail1.AddCell(cell_detail1);
            table_detail1.AddCell(cell_colon);
            cell_detail1.Phrase = new Phrase($"{viewModel.ConfirmDate.AddHours(timeoffset).ToString("dd/MM/yyyy")}", normal_font);
            table_detail1.AddCell(cell_detail1);
            #endregion

            #region Draw Detail 1
            float row1Y = 800;
            table_detail1.WriteSelectedRows(0, -1, 10, row1Y, cb);
            #endregion

            bool isDollar = viewModel.Rate.Id != 0;

            #region Detail 2 (Bottom, Column 1)
            PdfPTable table_detail2 = new PdfPTable(2);
            table_detail2.TotalWidth = 230f;

            float[] detail2_widths = new float[] { 2f, 5f };
            table_detail2.SetWidths(detail2_widths);

            PdfPCell cell_detail2 = new PdfPCell()
            {
                Border = Rectangle.TOP_BORDER | Rectangle.LEFT_BORDER | Rectangle.BOTTOM_BORDER | Rectangle.RIGHT_BORDER, HorizontalAlignment = Element.ALIGN_LEFT, VerticalAlignment = Element.ALIGN_MIDDLE, PaddingRight = 2, PaddingBottom = 7, PaddingLeft = 2, PaddingTop = 7
            };

            cell_detail2.Phrase = new Phrase("BUYER", normal_font);
            table_detail2.AddCell(cell_detail2);
            cell_detail2.Phrase = new Phrase($"{viewModel.Buyer.name}", normal_font);
            table_detail2.AddCell(cell_detail2);

            cell_detail2.Phrase = new Phrase("ARTICLE", normal_font);
            table_detail2.AddCell(cell_detail2);
            cell_detail2.Phrase = new Phrase($"{viewModel.Article}", normal_font);
            table_detail2.AddCell(cell_detail2);

            cell_detail2.Phrase = new Phrase("DESCRIPTION", normal_font);
            table_detail2.AddCell(cell_detail2);
            cell_detail2.Phrase = new Phrase($"{viewModel.CommodityDescription}", normal_font);
            table_detail2.AddCell(cell_detail2);

            cell_detail2.Phrase = new Phrase("QTY", normal_font);
            table_detail2.AddCell(cell_detail2);
            cell_detail2.Phrase = new Phrase($"{viewModel.Quantity} PCS", normal_font);
            table_detail2.AddCell(cell_detail2);

            cell_detail2.Phrase = new Phrase("DELIVERY", normal_font);
            table_detail2.AddCell(cell_detail2);
            cell_detail2.Phrase = new Phrase($"{viewModel.DeliveryDate.AddHours(timeoffset).ToString("dd/MM/yyyy")}", normal_font);
            table_detail2.AddCell(cell_detail2);
            #endregion

            #region Detail 3 (Bottom, Column 2)
            PdfPTable table_detail3 = new PdfPTable(8);
            table_detail3.TotalWidth = 330f;

            float[] detail3_widths = new float[] { 3.25f, 4.75f, 1.9f, 0.2f, 1.9f, 1.9f, 0.2f, 1.9f };
            table_detail3.SetWidths(detail3_widths);

            double   budgetCost   = isDollar ? viewModel.ConfirmPrice * viewModel.Rate.Value ?? 0 : viewModel.ConfirmPrice ?? 0;
            double   totalBudget  = budgetCost * viewModel.Quantity ?? 0;
            PdfPCell cell_detail3 = new PdfPCell()
            {
                HorizontalAlignment = Element.ALIGN_LEFT, VerticalAlignment = Element.ALIGN_MIDDLE, PaddingRight = 2, PaddingBottom = 7, PaddingLeft = 2, PaddingTop = 7
            };
            PdfPCell cell_detail3_right = new PdfPCell()
            {
                HorizontalAlignment = Element.ALIGN_RIGHT, VerticalAlignment = Element.ALIGN_MIDDLE, PaddingRight = 2, PaddingBottom = 7, PaddingLeft = 2, PaddingTop = 7
            };
            PdfPCell cell_detail3_colspan6 = new PdfPCell()
            {
                Border = Rectangle.TOP_BORDER | Rectangle.LEFT_BORDER | Rectangle.RIGHT_BORDER, HorizontalAlignment = Element.ALIGN_LEFT, VerticalAlignment = Element.ALIGN_MIDDLE, PaddingRight = 2, PaddingBottom = 7, PaddingLeft = 2, PaddingTop = 7, Colspan = 6
            };
            PdfPCell cell_detail3_colspan8 = new PdfPCell()
            {
                Border = Rectangle.TOP_BORDER | Rectangle.LEFT_BORDER | Rectangle.BOTTOM_BORDER | Rectangle.RIGHT_BORDER, HorizontalAlignment = Element.ALIGN_LEFT, VerticalAlignment = Element.ALIGN_MIDDLE, PaddingRight = 2, PaddingBottom = 7, PaddingLeft = 2, PaddingTop = 7, Colspan = 8
            };

            cell_detail3.Border = Rectangle.TOP_BORDER | Rectangle.LEFT_BORDER;
            cell_detail3.Phrase = new Phrase("TOTAL BUDGET", normal_font);
            table_detail3.AddCell(cell_detail3);
            cell_detail3.Border = Rectangle.TOP_BORDER | Rectangle.RIGHT_BORDER;
            cell_detail3.Phrase = new Phrase($"{Number.ToRupiah(totalBudget)}", normal_font);
            table_detail3.AddCell(cell_detail3);
            cell_detail3_colspan6.Phrase = new Phrase("STANDARD HOURS", normal_font);
            table_detail3.AddCell(cell_detail3_colspan6);

            double freightCost = 0;
            foreach (CostCalculationGarment_MaterialViewModel item in viewModel.CostCalculationGarment_Materials)
            {
                freightCost += item.TotalShippingFee;
            }

            cell_detail3.Border = Rectangle.LEFT_BORDER;
            cell_detail3.Phrase = new Phrase("BEA ANGKUT", normal_font);
            table_detail3.AddCell(cell_detail3);
            cell_detail3.Border = Rectangle.RIGHT_BORDER;
            cell_detail3.Phrase = new Phrase($"{Number.ToRupiah(freightCost)}", normal_font);
            table_detail3.AddCell(cell_detail3);
            cell_detail3.Border = Rectangle.LEFT_BORDER;
            cell_detail3.Phrase = new Phrase("SMV. CUT", normal_font);
            table_detail3.AddCell(cell_detail3);
            table_detail3.AddCell(cell_colon);
            cell_detail3.Border = Rectangle.NO_BORDER;
            cell_detail3.Phrase = new Phrase($"{viewModel.SMV_Cutting}", normal_font);
            table_detail3.AddCell(cell_detail3);
            cell_detail3.Border = Rectangle.NO_BORDER;
            cell_detail3.Phrase = new Phrase("SMV. SEW", normal_font);
            table_detail3.AddCell(cell_detail3);
            table_detail3.AddCell(cell_colon);
            cell_detail3.Border = Rectangle.RIGHT_BORDER;
            cell_detail3.Phrase = new Phrase($"{viewModel.SMV_Sewing}", normal_font);
            table_detail3.AddCell(cell_detail3);

            cell_detail3.Border = Rectangle.BOTTOM_BORDER | Rectangle.LEFT_BORDER;
            cell_detail3.Phrase = new Phrase("", normal_font);
            table_detail3.AddCell(cell_detail3);
            cell_detail3.Border = Rectangle.BOTTOM_BORDER | Rectangle.RIGHT_BORDER;
            cell_detail3.Phrase = new Phrase("", normal_font);
            table_detail3.AddCell(cell_detail3);
            cell_detail3.Border = Rectangle.BOTTOM_BORDER | Rectangle.LEFT_BORDER;
            cell_detail3.Phrase = new Phrase("SMV. FIN", normal_font);
            table_detail3.AddCell(cell_detail3);
            table_detail3.AddCell(cell_colon);
            cell_detail3.Border = Rectangle.BOTTOM_BORDER;
            cell_detail3.Phrase = new Phrase($"{viewModel.SMV_Finishing}", normal_font);
            table_detail3.AddCell(cell_detail3);
            cell_detail3.Border = Rectangle.BOTTOM_BORDER;
            cell_detail3.Phrase = new Phrase("SMV. TOT", normal_font);
            table_detail3.AddCell(cell_detail3);
            table_detail3.AddCell(cell_colon);
            cell_detail3.Border = Rectangle.BOTTOM_BORDER | Rectangle.RIGHT_BORDER;
            cell_detail3.Phrase = new Phrase($"{viewModel.SMV_Total}", normal_font);
            table_detail3.AddCell(cell_detail3);

            cell_detail3_colspan8.Phrase = new Phrase("BUDGET COST / PCS" + "".PadRight(5) + $"{Number.ToRupiah(budgetCost)}", normal_font);
            table_detail3.AddCell(cell_detail3_colspan8);
            cell_detail3_colspan8.Phrase = isDollar ? new Phrase($"US$ 1 = {Number.ToRupiah(viewModel.Rate.Value)}" + "".PadRight(10) + $"CONFIRM PRICE : {Number.ToDollar(viewModel.ConfirmPrice)} / PCS", normal_font) : new Phrase($"CONFIRM PRICE : {Number.ToRupiah(viewModel.ConfirmPrice)} / PCS", normal_font);
            table_detail3.AddCell(cell_detail3_colspan8);
            cell_detail3_colspan8.Border = Rectangle.NO_BORDER;
            cell_detail3_colspan8.HorizontalAlignment = Element.ALIGN_CENTER;
            cell_detail3_colspan8.Phrase = new Phrase($"ALLOWANCE >> FABRIC = {viewModel.FabricAllowance}%, ACC = {viewModel.AccessoriesAllowance}%", normal_font);
            table_detail3.AddCell(cell_detail3_colspan8);
            #endregion

            #region Signature
            PdfPTable table_signature = new PdfPTable(5);
            table_signature.TotalWidth = 570f;

            float[] signature_widths = new float[] { 1f, 1f, 1f, 1f, 1f };
            table_signature.SetWidths(signature_widths);

            PdfPCell cell_signature = new PdfPCell()
            {
                Border = Rectangle.NO_BORDER, HorizontalAlignment = Element.ALIGN_CENTER, VerticalAlignment = Element.ALIGN_MIDDLE, Padding = 2
            };

            cell_signature.Phrase = new Phrase("", normal_font);
            table_signature.AddCell(cell_signature);
            cell_signature.Phrase = new Phrase("", normal_font);
            table_signature.AddCell(cell_signature);
            cell_signature.Phrase = new Phrase("", normal_font);
            table_signature.AddCell(cell_signature);
            cell_signature.Phrase = new Phrase("", normal_font);
            table_signature.AddCell(cell_signature);
            cell_signature.Phrase = new Phrase("", normal_font);
            table_signature.AddCell(cell_signature);

            string signatureArea = string.Empty;
            for (int i = 0; i < 5; i++)
            {
                signatureArea += Environment.NewLine;
            }

            cell_signature.Phrase = new Phrase(signatureArea, normal_font);
            table_signature.AddCell(cell_signature);
            table_signature.AddCell(cell_signature);
            table_signature.AddCell(cell_signature);
            table_signature.AddCell(cell_signature);
            table_signature.AddCell(cell_signature);


            cell_signature.Phrase = new Phrase("(................)", normal_font);
            table_signature.AddCell(cell_signature);
            cell_signature.Phrase = new Phrase("(................)", normal_font);
            table_signature.AddCell(cell_signature);
            cell_signature.Phrase = new Phrase("(................)", normal_font);
            table_signature.AddCell(cell_signature);
            cell_signature.Phrase = new Phrase("(................)", normal_font);
            table_signature.AddCell(cell_signature);
            cell_signature.Phrase = new Phrase("(................)", normal_font);
            table_signature.AddCell(cell_signature);

            cell_signature.Phrase = new Phrase("Penjualan", normal_font);
            table_signature.AddCell(cell_signature);
            cell_signature.Phrase = new Phrase("Ka. Sie/Ka. Bag Penjualan", normal_font);
            table_signature.AddCell(cell_signature);
            cell_signature.Phrase = new Phrase("Ka. Bag Pembelian", normal_font);
            table_signature.AddCell(cell_signature);
            cell_signature.Phrase = new Phrase("Ka. Div Produksi Garment", normal_font);
            table_signature.AddCell(cell_signature);
            cell_signature.Phrase = new Phrase("Ka. Div Penjualan", normal_font);
            table_signature.AddCell(cell_signature);
            #endregion

            #region Cost Calculation Material
            PdfPTable table_ccm = new PdfPTable(10);
            table_ccm.TotalWidth = 570f;

            float[] ccm_widths = new float[] { 1f, 3f, 3f, 6f, 2f, 3f, 3f, 2f, 3f, 3f };
            table_ccm.SetWidths(ccm_widths);

            PdfPCell cell_ccm = new PdfPCell()
            {
                Border = Rectangle.TOP_BORDER | Rectangle.LEFT_BORDER | Rectangle.BOTTOM_BORDER | Rectangle.RIGHT_BORDER, HorizontalAlignment = Element.ALIGN_CENTER, VerticalAlignment = Element.ALIGN_MIDDLE, Padding = 2
            };

            cell_ccm.Phrase = new Phrase("NO", bold_font);
            table_ccm.AddCell(cell_ccm);
            cell_ccm.Phrase = new Phrase("CATEGORIES", bold_font);
            table_ccm.AddCell(cell_ccm);
            cell_ccm.Phrase = new Phrase("KODE PRODUK", bold_font);
            table_ccm.AddCell(cell_ccm);
            cell_ccm.Phrase = new Phrase("DESCRIPTION", bold_font);
            table_ccm.AddCell(cell_ccm);
            cell_ccm.Phrase = new Phrase("USAGE", bold_font);
            table_ccm.AddCell(cell_ccm);
            cell_ccm.Phrase = new Phrase("PRICE", bold_font);
            table_ccm.AddCell(cell_ccm);
            cell_ccm.Phrase = new Phrase("QUANTITY", bold_font);
            table_ccm.AddCell(cell_ccm);
            cell_ccm.Phrase = new Phrase("UNIT", bold_font);
            table_ccm.AddCell(cell_ccm);
            cell_ccm.Phrase = new Phrase("AMOUNT", bold_font);
            table_ccm.AddCell(cell_ccm);
            cell_ccm.Phrase = new Phrase("PO NUMBER", bold_font);
            table_ccm.AddCell(cell_ccm);

            float row2Y               = row1Y - table_detail1.TotalHeight - 10;
            float row3Height          = table_detail2.TotalHeight > table_detail3.TotalHeight ? table_detail2.TotalHeight : table_detail3.TotalHeight;
            float row2RemainingHeight = row2Y - 10 - row3Height - printedOnHeight - margin;
            float row2AllowedHeight   = row2Y - printedOnHeight - margin;

            for (int i = 0; i < viewModel.CostCalculationGarment_Materials.Count; i++)
            {
                //NO
                cell_ccm.Phrase = new Phrase((i + 1).ToString(), normal_font);
                table_ccm.AddCell(cell_ccm);

                cell_ccm.HorizontalAlignment = Element.ALIGN_LEFT;

                //CATEGORY
                cell_ccm.Phrase = new Phrase(viewModel.CostCalculationGarment_Materials[i].Category.name, normal_font);
                table_ccm.AddCell(cell_ccm);

                //KODE PRODUK
                cell_ccm.Phrase = new Phrase(viewModel.CostCalculationGarment_Materials[i].Product.code, normal_font);
                table_ccm.AddCell(cell_ccm);

                //DESCRIPTION
                cell_ccm.Phrase = new Phrase(viewModel.CostCalculationGarment_Materials[i].Description, normal_font);
                table_ccm.AddCell(cell_ccm);

                cell_ccm.HorizontalAlignment = Element.ALIGN_RIGHT;

                double usage = viewModel.CostCalculationGarment_Materials[i].Quantity ?? 0;
                cell_ccm.Phrase = new Phrase(usage.ToString(), normal_font);
                table_ccm.AddCell(cell_ccm);

                double price = viewModel.CostCalculationGarment_Materials[i].Price ?? 0;
                cell_ccm.Phrase = new Phrase(String.Format("{0}/{1}", Number.ToRupiahWithoutSymbol(price), viewModel.CostCalculationGarment_Materials[i].UOMPrice.unit), normal_font);
                table_ccm.AddCell(cell_ccm);

                double factor;
                if (viewModel.CostCalculationGarment_Materials[i].Category.name == "FABRIC")
                {
                    factor = viewModel.FabricAllowance ?? 0;
                }
                else
                {
                    factor = viewModel.AccessoriesAllowance ?? 0;
                }
                double totalQuantity = viewModel.Quantity ?? 0;
                double quantity      = (100 + factor) / 100 * usage * totalQuantity;
                cell_ccm.Phrase = new Phrase(quantity.ToString(), normal_font);
                table_ccm.AddCell(cell_ccm);

                cell_ccm.HorizontalAlignment = Element.ALIGN_CENTER;
                cell_ccm.Phrase = new Phrase(viewModel.CostCalculationGarment_Materials[i].UOMQuantity.unit, normal_font);
                table_ccm.AddCell(cell_ccm);

                cell_ccm.HorizontalAlignment = Element.ALIGN_RIGHT;
                double amount = quantity * price;
                cell_ccm.Phrase = new Phrase(Number.ToRupiahWithoutSymbol(amount), normal_font);
                table_ccm.AddCell(cell_ccm);

                cell_ccm.HorizontalAlignment = Element.ALIGN_CENTER;
                cell_ccm.Phrase = new Phrase(viewModel.CostCalculationGarment_Materials[i].PO_SerialNumber, normal_font);
                table_ccm.AddCell(cell_ccm);

                float currentHeight = table_ccm.TotalHeight;
                if (currentHeight / row2RemainingHeight > 1)
                {
                    if (currentHeight / row2AllowedHeight > 1)
                    {
                        PdfPRow headerRow = table_ccm.GetRow(0);
                        PdfPRow lastRow   = table_ccm.GetRow(table_ccm.Rows.Count - 1);
                        table_ccm.DeleteLastRow();
                        table_ccm.WriteSelectedRows(0, -1, 10, row2Y, cb);
                        table_ccm.DeleteBodyRows();
                        this.DrawPrintedOn(now, bf, cb);
                        document.NewPage();
                        table_ccm.Rows.Add(headerRow);
                        table_ccm.Rows.Add(lastRow);
                        table_ccm.CalculateHeights();
                        row2Y = startY;
                        row2RemainingHeight = row2Y - 10 - row3Height - printedOnHeight - margin;
                        row2AllowedHeight   = row2Y - printedOnHeight - margin;
                    }
                }
            }
            #endregion

            #region Draw Others
            table_ccm.WriteSelectedRows(0, -1, 10, row2Y, cb);

            float row3Y = row2Y - table_ccm.TotalHeight - 10;
            float row3RemainigHeight = row3Y - printedOnHeight - margin;
            if (row3RemainigHeight < row3Height)
            {
                this.DrawPrintedOn(now, bf, cb);
                row3Y = startY;
                document.NewPage();
            }

            table_detail2.WriteSelectedRows(0, -1, margin, row3Y, cb);

            table_detail3.WriteSelectedRows(0, -1, margin + table_detail2.TotalWidth + 10, row3Y, cb);

            float signatureY = row3Y - row3Height - 10;
            float signatureRemainingHeight = signatureY - printedOnHeight - margin;
            if (signatureRemainingHeight < table_signature.TotalHeight)
            {
                this.DrawPrintedOn(now, bf, cb);
                signatureY = startY;
                document.NewPage();
            }
            table_signature.WriteSelectedRows(0, -1, margin, signatureY, cb);

            this.DrawPrintedOn(now, bf, cb);
            #endregion

            document.Close();

            byte[] byteInfo = stream.ToArray();
            stream.Write(byteInfo, 0, byteInfo.Length);
            stream.Position = 0;

            return(stream);
        }
Esempio n. 8
0
        public void GenerarFactura(string id)
        {
            var existepuntorecogida = false;
            var clavevalor          = new Dictionary <string, string>();

            contexto.clavevalor.ToList().ForEach(x => clavevalor.Add(x.clave, x.valor));

            var datosreserva = contexto.reservaexcursionactividad.Where(x => x.reserva.codigoqr == id).Select(x => new
            {
                exact_id         = x.calendarioexcursion.exact_id,
                fechatransaccion = x.reserva.fechatransaccion,
                fecha            = x.calendarioexcursion.fecha,
                duracion         = x.calendarioexcursion.excursionactividad.duracion,
                tipo_duracion    = x.calendarioexcursion.excursionactividad.tipoduracion,
                actividad        = x.calendarioexcursion.excursionactividad.configuracion.nombre,
                preciogrupo      = x.calendarioexcursion.excursionactividad.precioporgrupo,
                numadultos       = x.numadultos,
                numninos         = x.numninos,
                numinfantes      = x.numinfantes,
                numjuniors       = x.numjuniors,
                numseniors       = x.numseniors,
                direccion        = x.calendarioexcursion.excursionactividad.configuracion.direccion,
                lat             = x.calendarioexcursion.excursionactividad.configuracion.lat,
                lng             = x.calendarioexcursion.excursionactividad.configuracion.lng,
                localidad       = x.calendarioexcursion.excursionactividad.configuracion.localidad.nombre,
                codigopostal    = x.calendarioexcursion.excursionactividad.configuracion.localidad.cp,
                provincia       = x.calendarioexcursion.excursionactividad.configuracion.localidad.provincia.nombre,
                pais            = x.calendarioexcursion.excursionactividad.configuracion.localidad.provincia.pais.nombre,
                descuento       = x.calendarioexcursion.excursionactividad.descuento == null ? 0 : x.calendarioexcursion.excursionactividad.descuento,
                punto           = x.punto,
                telefono        = x.reserva.proveedor.usuario.PhoneNumber,
                nombre          = x.reserva.cliente.usuario.nombre,
                primerapellido  = x.reserva.cliente.usuario.primerapellido,
                segundoapellido = x.reserva.cliente.usuario.segundoapellido,
                idfactura       = x.reserva.id
            }).FirstOrDefault();

            dynamic precios;

            if (datosreserva.preciogrupo)
            {
                precios = contexto.preciotemporada.Where(c => (c.desde <= datosreserva.fechatransaccion && datosreserva.fechatransaccion <= c.hasta) && c.exact_id == datosreserva.exact_id).Select(x => new
                {
                    preciogrupo = x.pvpgrupo
                }).FirstOrDefault();
            }
            else
            {
                precios = contexto.preciotemporada.Where(c => (c.desde <= datosreserva.fechatransaccion && datosreserva.fechatransaccion <= c.hasta) && c.exact_id == datosreserva.exact_id).Select(x => new
                {
                    precioadulto  = x.pvpadulto,
                    precionino    = x.pvpnino,
                    precioinfante = x.pvpinfante,
                    preciojunior  = x.pvpjunior,
                    preciosenior  = x.pvpsenior,
                    totaladulto   = datosreserva.numadultos * x.pvpadulto,
                    totaljunior   = datosreserva.numjuniors * x.pvpjunior,
                    totalsenior   = datosreserva.numseniors * x.pvpsenior,
                    totalnino     = datosreserva.numninos * x.pvpnino,
                    totalinfante  = datosreserva.numinfantes * x.pvpinfante
                }).FirstOrDefault();
            }

            var facturaitems = contexto.facturaitem_exact.Where(x => x.exact_id == datosreserva.exact_id).Select(x => x.item).ToList();

            if (datosreserva.punto != null)
            {
                existepuntorecogida = true;
            }

            string    filePath = HostingEnvironment.MapPath("~/facturas/" + id + ".pdf");
            Document  doc      = new Document(PageSize.A4);
            PdfWriter writer   = PdfWriter.GetInstance(doc, new FileStream(filePath, FileMode.Create));

            doc.Open();
            iTextSharp.text.Font  fuente       = new iTextSharp.text.Font(iTextSharp.text.Font.TIMES_ROMAN, 12, iTextSharp.text.Font.NORMAL, iTextSharp.text.Color.BLACK);
            iTextSharp.text.Font  bold         = new iTextSharp.text.Font(iTextSharp.text.Font.TIMES_ROMAN, 12, iTextSharp.text.Font.BOLD, iTextSharp.text.Color.BLACK);
            iTextSharp.text.Font  pequeña      = new iTextSharp.text.Font(iTextSharp.text.Font.TIMES_ROMAN, 7, iTextSharp.text.Font.NORMAL, iTextSharp.text.Color.BLACK);
            iTextSharp.text.Image logocabecera = iTextSharp.text.Image.GetInstance(HostingEnvironment.MapPath("~/recursos/imagenes/logo_empresa.png"));
            logocabecera.ScalePercent(15);

            PdfContentByte cb = writer.DirectContent;

            cb.SetColorStroke(iTextSharp.text.Color.BLACK);
            cb.MoveTo(38, 720);
            cb.SetLineWidth(0.5f);
            cb.LineTo(doc.PageSize.Width - 38, 720);
            cb.Stroke();

            #region tabla_empresa

            PdfPTable tabla_cabecera_datos_empresa = new PdfPTable(1);

            PdfPCell celda_tabla_cabecera_datos_empresa_nombre    = new PdfPCell(new Phrase("EcoTurismo Adventures S.L", fuente));
            PdfPCell celda_tabla_cabecera_datos_empresa_cif       = new PdfPCell(new Phrase("B569854122", fuente));
            PdfPCell celda_tabla_cabecera_datos_empresa_telefono  = new PdfPCell(new Phrase("(+34) 922 68 32 44", fuente));
            PdfPCell celda_tabla_cabecera_datos_empresa_email     = new PdfPCell(new Phrase("*****@*****.**", fuente));
            PdfPCell celda_tabla_cabecera_datos_empresa_direccion = new PdfPCell(new Phrase(@"C:\Avenida Bélgica 32006 (Adeje) Santa Cruz de Tenerife, España", fuente));

            tabla_cabecera_datos_empresa.AddCell(celda_tabla_cabecera_datos_empresa_nombre);
            tabla_cabecera_datos_empresa.AddCell(celda_tabla_cabecera_datos_empresa_cif);
            tabla_cabecera_datos_empresa.AddCell(celda_tabla_cabecera_datos_empresa_telefono);
            tabla_cabecera_datos_empresa.AddCell(celda_tabla_cabecera_datos_empresa_email);
            tabla_cabecera_datos_empresa.AddCell(celda_tabla_cabecera_datos_empresa_direccion);
            PdfPCell celda = null;
            foreach (PdfPRow row in tabla_cabecera_datos_empresa.GetRows(0, tabla_cabecera_datos_empresa.Rows.Count))
            {
                foreach (PdfPCell cell in row.GetCells())
                {
                    celda = new PdfPCell(cell.Phrase);
                    celda.HorizontalAlignment = PdfCell.ALIGN_RIGHT;
                    celda.Border = 0;
                    tabla_cabecera_datos_empresa.DeleteRow(0);
                    tabla_cabecera_datos_empresa.AddCell(celda);
                }
            }
            #endregion

            #region tabla_cabecera

            PdfPTable tabla_cabecera = new PdfPTable(2);
            tabla_cabecera.WidthPercentage = 100;
            tabla_cabecera.SetWidths(new float[] { 20, 80 });

            PdfPCell celda_cabecera_logo = new PdfPCell(logocabecera);
            celda_cabecera_logo.Border = 0;

            PdfPCell celda_cabecera_datosempresa = new PdfPCell(tabla_cabecera_datos_empresa);
            celda_cabecera_datosempresa.Border = 0;

            tabla_cabecera.AddCell(celda_cabecera_logo);
            tabla_cabecera.AddCell(celda_cabecera_datosempresa);

            doc.Add(tabla_cabecera);

            #endregion

            #region tabla_precios

            PdfPTable tabla_precios = new PdfPTable(2);
            tabla_precios.WidthPercentage = 100;
            tabla_precios.SetWidths(new float[] { 40, 60 });
            Dictionary <string, string> preciosdic = new Dictionary <string, string>();

            preciosdic.Add(Mensajes.mensaje15, datosreserva.actividad);
            preciosdic.Add(Mensajes.mensaje16, datosreserva.fecha.ToString("dd-MM-yyyy HH:mm:ss"));
            if (datosreserva.tipo_duracion.Equals("flexible"))
            {
                preciosdic.Add(Mensajes.mensaje17, Mensajes.mensaje18);
            }
            else
            {
                var tiempo = "";
                switch (datosreserva.tipo_duracion)
                {
                case "hora":
                    tiempo = Mensajes.mensaje19;
                    break;

                case "minuto":
                    tiempo = Mensajes.mensaje20;
                    break;

                case "dia":
                    tiempo = Mensajes.mensaje21;
                    break;
                }
                preciosdic.Add(Mensajes.mensaje17, datosreserva.duracion.ToString() + " " + tiempo);
            }
            preciosdic.Add(Mensajes.mensaje22, datosreserva.numadultos.ToString());
            preciosdic.Add(Mensajes.mensaje23, datosreserva.numjuniors.ToString());
            preciosdic.Add(Mensajes.mensaje24, datosreserva.numseniors.ToString());
            preciosdic.Add(Mensajes.mensaje25, datosreserva.numninos.ToString());
            preciosdic.Add(Mensajes.mensaje26, datosreserva.numinfantes.ToString());
            if (datosreserva.preciogrupo)
            {
                preciosdic.Add(Mensajes.mensaje40, precios.preciogrupo.ToString());
            }
            else
            {
                preciosdic.Add(Mensajes.mensaje27, precios.precioadulto.ToString());
                preciosdic.Add(Mensajes.mensaje28, precios.preciojunior.ToString());
                preciosdic.Add(Mensajes.mensaje29, precios.preciosenior.ToString());
                preciosdic.Add(Mensajes.mensaje30, precios.precionino.ToString());
                preciosdic.Add(Mensajes.mensaje31, precios.precioinfante.ToString());
                preciosdic.Add(Mensajes.mensaje32, precios.totaladulto.ToString());
                preciosdic.Add(Mensajes.mensaje33, precios.totaljunior.ToString());
                preciosdic.Add(Mensajes.mensaje34, precios.totalsenior.ToString());
                preciosdic.Add(Mensajes.mensaje35, precios.totalnino.ToString());
                preciosdic.Add(Mensajes.mensaje36, precios.totalinfante.ToString());
            }
            PdfPCell cel1 = null;
            foreach (KeyValuePair <string, string> val in preciosdic)
            {
                cel1        = new PdfPCell(new Phrase(val.Key, bold));
                cel1.Border = 0;
                cel1.HorizontalAlignment = PdfCell.ALIGN_LEFT;
                tabla_precios.AddCell(cel1);
                cel1        = new PdfPCell(new Phrase(val.Value, fuente));
                cel1.Border = 0;
                cel1.HorizontalAlignment = PdfCell.ALIGN_LEFT;
                tabla_precios.AddCell(cel1);
            }

            #endregion

            #region tabla_wraper1

            PdfPTable tabla_wraper1 = new PdfPTable(2);
            tabla_wraper1.WidthPercentage = 100;
            tabla_wraper1.SetWidths(new float[] { 70, 30 });
            tabla_wraper1.SpacingBefore       = 40f;
            tabla_wraper1.HorizontalAlignment = 0;
            PdfPCell cel2 = new PdfPCell(new Phrase(Mensajes.menaje37 + " " + datosreserva.nombre + " " + datosreserva.primerapellido + " " + datosreserva.segundoapellido, bold));
            cel2.Colspan       = 2;
            cel2.PaddingBottom = 5;
            tabla_wraper1.AddCell(cel2);
            tabla_wraper1.AddCell(tabla_precios);

            var          qrEncoder = new QrEncoder(ErrorCorrectionLevel.H);
            var          qrCode    = qrEncoder.Encode(id);
            var          renderer  = new GraphicsRenderer(new FixedModuleSize(5, QuietZoneModules.Four), Brushes.Black, Brushes.White);
            MemoryStream stream    = new MemoryStream();
            renderer.WriteToStream(qrCode.Matrix, ImageFormat.Png, stream);
            stream.Position = 0;
            iTextSharp.text.Image output = iTextSharp.text.Image.GetInstance(stream);
            stream.Close();
            tabla_wraper1.AddCell(output);

            doc.Add(tabla_wraper1);

            #endregion

            #region tabla_precio_total

            PdfPTable tabla_precio_total = new PdfPTable(2);
            tabla_precio_total.SetWidths(new float[] { 80, 20 });
            tabla_precio_total.WidthPercentage     = 100;
            tabla_precio_total.SpacingBefore       = 20;
            tabla_precio_total.HorizontalAlignment = 0;

            PdfPCell celda_tabla_precio_total_nombre = null;

            if (datosreserva.descuento == 0)
            {
                celda_tabla_precio_total_nombre = new PdfPCell(new Phrase(Mensajes.menasje38, fuente));
            }
            else
            {
                celda_tabla_precio_total_nombre = new PdfPCell(new Phrase(String.Format(Mensajes.mensaje39, datosreserva.descuento), fuente));
            }

            celda_tabla_precio_total_nombre.HorizontalAlignment = PdfCell.ALIGN_RIGHT;
            celda_tabla_precio_total_nombre.BorderWidthRight    = 0;
            celda_tabla_precio_total_nombre.PaddingBottom       = 5;
            decimal total = 0;
            if (datosreserva.preciogrupo)
            {
                total = (precios.preciogrupo) - (precios.preciogrupo * ((decimal)datosreserva.descuento / 100));
            }
            else
            {
                total = (precios.totaladulto + precios.totalinfante + precios.totalnino + precios.totaljunior + precios.totalsenior) - ((precios.totaladulto + precios.totalinfante + precios.totalnino + precios.totaljunior + precios.totalsenior) * ((decimal)datosreserva.descuento / 100));
            }
            PdfPCell celda_tabla_precio_total_precio = new PdfPCell(new Phrase(Math.Round(total, 2).ToString() + " €", fuente));
            celda_tabla_precio_total_precio.HorizontalAlignment = PdfCell.ALIGN_RIGHT;
            celda_tabla_precio_total_precio.BorderWidthLeft     = 0;
            celda_tabla_precio_total_precio.PaddingBottom       = 5;

            tabla_precio_total.AddCell(celda_tabla_precio_total_nombre);
            tabla_precio_total.AddCell(celda_tabla_precio_total_precio);

            doc.Add(tabla_precio_total);
            #endregion

            if (existepuntorecogida)

            {
                var datospunto = contexto.puntorecogida.Where(x => x.id == datosreserva.punto.id).Select(x => new
                {
                    pais      = x.localidad.provincia.pais.nombre,
                    provincia = x.localidad.provincia.nombre,
                    localidad = x.localidad.nombre,
                    cp        = x.localidad.cp
                }).First();


                #region tabla_punto

                PdfPTable tabla_punto = new PdfPTable(2);
                tabla_punto.WidthPercentage = 100;
                tabla_punto.SetWidths(new float[] { 20, 80 });
                tabla_punto.SpacingBefore = 20;

                Dictionary <string, string> puntodic = new Dictionary <string, string>();
                puntodic.Add(Mensajes.mensaje9, datosreserva.punto.nombre);
                puntodic.Add(Mensajes.mensaje5, datosreserva.punto.direccion);
                puntodic.Add(Mensajes.mensaje11, datospunto.localidad);
                puntodic.Add(Mensajes.mensaje12, datospunto.provincia);
                puntodic.Add(Mensajes.mensaje13, datospunto.pais);
                puntodic.Add(Mensajes.mensaje14, datospunto.cp.ToString());
                puntodic.Add(Mensajes.mensaje6, String.Format(clavevalor["googlemap"], datosreserva.punto.lat, datosreserva.punto.lng));
                PdfPCell cel       = null;
                var      puntotext = new Phrase(Mensajes.mensaje8, bold);
                cel                     = new PdfPCell(puntotext);
                cel.Colspan             = 2;
                cel.HorizontalAlignment = PdfCell.ALIGN_LEFT;
                cel.PaddingBottom       = 10;
                cel.BorderWidthBottom   = 0;
                tabla_punto.AddCell(cel);
                foreach (KeyValuePair <string, string> val in puntodic)
                {
                    cel = new PdfPCell(new Phrase(val.Key, bold));
                    cel.BorderWidthBottom = 0;
                    cel.BorderWidthRight  = 0;
                    cel.BorderWidthTop    = 0;
                    tabla_punto.AddCell(cel);
                    cel = new PdfPCell(new Phrase(val.Value, fuente));
                    cel.BorderWidthBottom = 0;
                    cel.BorderWidthLeft   = 0;
                    cel.BorderWidthTop    = 0;
                    tabla_punto.AddCell(cel);
                }
                PdfPRow last = tabla_punto.GetRow(tabla_punto.Rows.Count - 1);
                last.GetCells()[0].BorderWidthBottom = 0.5F;
                last.GetCells()[0].PaddingBottom     = 5;
                last.GetCells()[1].BorderWidthBottom = 0.5F;
                last.GetCells()[1].PaddingBottom     = 5;
                doc.Add(tabla_punto);


                #endregion
            }
            else
            {
                #region tabla_punto

                PdfPTable tabla_punto = new PdfPTable(2);
                tabla_punto.WidthPercentage = 100;
                tabla_punto.SetWidths(new float[] { 20, 80 });
                tabla_punto.SpacingBefore = 20;

                Dictionary <string, string> puntodic = new Dictionary <string, string>();
                puntodic.Add(Mensajes.mensaje5, datosreserva.direccion);
                puntodic.Add(Mensajes.mensaje11, datosreserva.localidad);
                puntodic.Add(Mensajes.mensaje12, datosreserva.provincia);
                puntodic.Add(Mensajes.mensaje13, datosreserva.pais);
                puntodic.Add(Mensajes.mensaje14, datosreserva.codigopostal.ToString());
                puntodic.Add(Mensajes.mensaje6, String.Format(clavevalor["googlemap"], datosreserva.lat, datosreserva.lng));
                PdfPCell cel       = null;
                var      puntotext = new Phrase(Mensajes.mensaje7, bold);
                cel                     = new PdfPCell(puntotext);
                cel.Colspan             = 2;
                cel.HorizontalAlignment = PdfCell.ALIGN_LEFT;
                cel.PaddingBottom       = 10;
                cel.BorderWidthBottom   = 0;
                tabla_punto.AddCell(cel);
                foreach (KeyValuePair <string, string> val in puntodic)
                {
                    cel = new PdfPCell(new Phrase(val.Key, bold));
                    cel.BorderWidthBottom = 0;
                    cel.BorderWidthRight  = 0;
                    cel.BorderWidthTop    = 0;
                    tabla_punto.AddCell(cel);
                    cel = new PdfPCell(new Phrase(val.Value, fuente));
                    cel.BorderWidthBottom = 0;
                    cel.BorderWidthLeft   = 0;
                    cel.BorderWidthTop    = 0;
                    tabla_punto.AddCell(cel);
                }
                PdfPRow last = tabla_punto.GetRow(tabla_punto.Rows.Count - 1);
                last.GetCells()[0].BorderWidthBottom = 0.5F;
                last.GetCells()[0].PaddingBottom     = 5;
                last.GetCells()[1].BorderWidthBottom = 0.5F;
                last.GetCells()[1].PaddingBottom     = 5;
                doc.Add(tabla_punto);

                #endregion
            }

            #region tabla_importante

            PdfPTable tabla_importante = new PdfPTable(1);
            tabla_importante.WidthPercentage = 100;
            tabla_importante.SpacingBefore   = 20;

            PdfPCell celda_tabla_importante_importante = new PdfPCell(new Phrase(Mensajes.mensaje3, bold));
            celda_tabla_importante_importante.HorizontalAlignment = PdfCell.ALIGN_LEFT;
            celda_tabla_importante_importante.BorderWidthBottom   = 0;
            celda_tabla_importante_importante.PaddingBottom       = 10;

            PdfPCell celda_tabla_importante_desc = null;

            if (existepuntorecogida)
            {
                celda_tabla_importante_desc = new PdfPCell(new Phrase(String.Format(Mensajes.mensaje2, datosreserva.telefono), fuente));
            }
            else
            {
                celda_tabla_importante_desc = new PdfPCell(new Phrase(String.Format(Mensajes.mensaje4, datosreserva.fecha.ToString("dd-MM-yyyy"), datosreserva.fecha.ToString("HH:mm:ss"), datosreserva.telefono), fuente));
            }

            celda_tabla_importante_importante.HorizontalAlignment = PdfCell.ALIGN_LEFT;
            celda_tabla_importante_desc.PaddingBottom             = 5;
            celda_tabla_importante_desc.BorderWidthTop            = 0;

            tabla_importante.AddCell(celda_tabla_importante_importante);
            tabla_importante.AddCell(celda_tabla_importante_desc);

            doc.Add(tabla_importante);

            #endregion

            #region tabla_condiciones

            PdfPTable tabla_items = new PdfPTable(1);
            tabla_items.WidthPercentage = 100;
            tabla_items.SpacingBefore   = 50;

            tabla_items.HorizontalAlignment = 0;

            PdfPCell celda_tabla_items = null;
            foreach (var item in facturaitems)
            {
                celda_tabla_items = new PdfPCell(new Phrase(item.nombre.ToUpper() + ": " + item.descripcion, pequeña));
                celda_tabla_items.HorizontalAlignment = PdfCell.ALIGN_LEFT;
                celda_tabla_items.Border        = 0;
                celda_tabla_items.PaddingBottom = 10;
                tabla_items.AddCell(celda_tabla_items);
            }

            doc.Add(tabla_items);

            #endregion

            doc.Close();
            writer.Close();
        }
Esempio n. 9
0
        public MemoryStream GeneratePdfTemplate(RO_GarmentViewModel viewModel, int offset)
        {
            //set pdf stream
            MemoryStream stream   = new MemoryStream();
            Document     document = new Document(PageSize.A4, 10, 10, 10, 10);
            PdfWriter    writer   = PdfWriter.GetInstance(document, stream);

            writer.CloseStream = false;
            document.Open();
            PdfContentByte cb = writer.DirectContent;
            //set content configuration
            BaseFont bf              = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1250, BaseFont.NOT_EMBEDDED);
            BaseFont bf_bold         = BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1250, BaseFont.NOT_EMBEDDED);
            Font     normal_font     = FontFactory.GetFont(BaseFont.HELVETICA, BaseFont.CP1250, BaseFont.NOT_EMBEDDED, 7);
            Font     bold_font       = FontFactory.GetFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1250, BaseFont.NOT_EMBEDDED, 7);
            Font     bold_font_8     = FontFactory.GetFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1250, BaseFont.NOT_EMBEDDED, 8);
            Font     font_9          = FontFactory.GetFont(BaseFont.HELVETICA, BaseFont.CP1250, BaseFont.NOT_EMBEDDED, 9);
            DateTime now             = DateTime.Now;
            float    margin          = 10;
            float    printedOnHeight = 10;
            float    startY          = 840 - margin;

            #region Header
            cb.BeginText();
            cb.SetFontAndSize(bf, 10);
            cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "PT. DANLIRIS", 10, 820, 0);
            cb.SetFontAndSize(bf_bold, 12);
            cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "RO EKSPOR GARMENT", 10, 805, 0);
            cb.EndText();
            #endregion

            #region Top
            PdfPTable table_top  = new PdfPTable(9);
            float[]   top_widths = new float[] { 1f, 0.1f, 2f, 1f, 0.1f, 2f, 1.2f, 0.1f, 2f };

            table_top.TotalWidth = 500f;
            table_top.SetWidths(top_widths);

            PdfPCell cell_top = new PdfPCell()
            {
                Border = Rectangle.NO_BORDER,
                HorizontalAlignment = Element.ALIGN_LEFT,
                VerticalAlignment   = Element.ALIGN_TOP,
                PaddingRight        = 1,
                PaddingBottom       = 2,
                PaddingTop          = 2
            };

            PdfPCell cell_colon = new PdfPCell()
            {
                Border = Rectangle.NO_BORDER,
                HorizontalAlignment = Element.ALIGN_LEFT,
                VerticalAlignment   = Element.ALIGN_TOP
            };

            PdfPCell cell_top_keterangan = new PdfPCell()
            {
                Border = Rectangle.NO_BORDER,
                HorizontalAlignment = Element.ALIGN_LEFT,
                VerticalAlignment   = Element.ALIGN_TOP,
                PaddingRight        = 1,
                PaddingBottom       = 2,
                PaddingTop          = 2,
                Colspan             = 7
            };

            cell_colon.Phrase = new Phrase(":", normal_font);
            cell_top.Phrase   = new Phrase("NO RO", normal_font);
            table_top.AddCell(cell_top);
            table_top.AddCell(cell_colon);

            cell_top.Phrase = new Phrase($"{viewModel.CostCalculationGarment.RO_Number}", normal_font);
            table_top.AddCell(cell_top);
            cell_top.Phrase = new Phrase("SECTION", normal_font);
            table_top.AddCell(cell_top);
            table_top.AddCell(cell_colon);
            cell_top.Phrase = new Phrase($"{viewModel.CostCalculationGarment.Section}", normal_font);
            table_top.AddCell(cell_top);
            cell_top.Phrase = new Phrase("DATE", normal_font);
            table_top.AddCell(cell_top);
            table_top.AddCell(cell_colon);
            cell_top.Phrase = new Phrase($"{viewModel.CostCalculationGarment.ConfirmDate.AddHours(offset).ToString("dd MMMM yyyy")}", normal_font);
            table_top.AddCell(cell_top);

            cell_top.Phrase = new Phrase("Konveksi", normal_font);
            table_top.AddCell(cell_top);
            table_top.AddCell(cell_colon);
            cell_top.Phrase = new Phrase($"{viewModel.CostCalculationGarment.Unit.Code}", normal_font);
            table_top.AddCell(cell_top);
            cell_top.Phrase = new Phrase("BUYER", normal_font);
            table_top.AddCell(cell_top);
            table_top.AddCell(cell_colon);
            cell_top.Phrase = new Phrase($"{viewModel.CostCalculationGarment.BuyerBrand.Name}", normal_font);
            table_top.AddCell(cell_top);
            cell_top.Phrase = new Phrase("DELIVERY DATE", normal_font);
            table_top.AddCell(cell_top);
            table_top.AddCell(cell_colon);
            cell_top.Phrase = new Phrase($"{viewModel.CostCalculationGarment.DeliveryDate.AddHours(offset).ToString("dd MMMM yyyy")}", normal_font);
            table_top.AddCell(cell_top);

            cell_top.Phrase = new Phrase("DESC", normal_font);
            table_top.AddCell(cell_top);
            table_top.AddCell(cell_colon);
            cell_top.Phrase = new Phrase($"{viewModel.CostCalculationGarment.Description}", normal_font);
            table_top.AddCell(cell_top);
            cell_top.Phrase = new Phrase("QUANTITY", normal_font);
            table_top.AddCell(cell_top);
            table_top.AddCell(cell_colon);
            cell_top.Phrase = new Phrase($"{viewModel.Total.ToString()} {viewModel.CostCalculationGarment.UOM.Unit}", normal_font);
            table_top.AddCell(cell_top);
            cell_top.Phrase = new Phrase("SIZE RANGE", normal_font);
            table_top.AddCell(cell_top);
            table_top.AddCell(cell_colon);
            cell_top.Phrase = new Phrase($"{viewModel.CostCalculationGarment.SizeRange}", normal_font);
            table_top.AddCell(cell_top);

            byte[] imageByte;
            try
            {
                imageByte = Convert.FromBase64String(Base64.GetBase64File(viewModel.CostCalculationGarment.ImageFile));
            }
            catch (Exception)
            {
                var webClient = new WebClient();
                imageByte = webClient.DownloadData("http://babakunyho.eu/img/default-no-image.png");
            }

            Image image = Image.GetInstance(imgb: imageByte);

            if (image.Width > 60)
            {
                float percentage = 0.0f;
                percentage = 60 / image.Width;
                image.ScalePercent(percentage * 100);
            }

            float row1Y  = 800;
            float imageY = 800 - image.ScaledHeight;

            image.SetAbsolutePosition(520, imageY);
            cb.AddImage(image, inlineImage: true);
            table_top.WriteSelectedRows(0, -1, 10, row1Y, cb);
            #endregion

            #region Table Fabric
            //Fabric title
            PdfPTable table_fabric_top = new PdfPTable(1);
            table_fabric_top.TotalWidth = 570f;

            float[] fabric_widths_top = new float[] { 5f };
            table_fabric_top.SetWidths(fabric_widths_top);

            PdfPCell cell_top_fabric = new PdfPCell()
            {
                Border = Rectangle.NO_BORDER,
                HorizontalAlignment = Element.ALIGN_LEFT,
                VerticalAlignment   = Element.ALIGN_MIDDLE,
                PaddingRight        = 1,
                PaddingBottom       = 2,
                PaddingTop          = 2
            };

            cell_top_fabric.Phrase = new Phrase("FABRIC", bold_font);
            table_fabric_top.AddCell(cell_top_fabric);

            float row1Height        = image.ScaledHeight > table_top.TotalHeight ? image.ScaledHeight : table_top.TotalHeight;
            float rowYTittleFab     = row1Y - row1Height - 10;
            float allowedRow2Height = rowYTittleFab - printedOnHeight - margin;
            table_fabric_top.WriteSelectedRows(0, -1, 10, rowYTittleFab, cb);

            //Main fabric table
            PdfPTable table_fabric = new PdfPTable(8);
            table_fabric.TotalWidth = 570f;

            float[] fabric_widths = new float[] { 5f, 5f, 5f, 5f, 5f, 5f, 5f, 5f };
            table_fabric.SetWidths(fabric_widths);

            PdfPCell cell_fabric_center = new PdfPCell()
            {
                Border = Rectangle.TOP_BORDER | Rectangle.LEFT_BORDER | Rectangle.BOTTOM_BORDER | Rectangle.RIGHT_BORDER,
                HorizontalAlignment = Element.ALIGN_CENTER,
                VerticalAlignment   = Element.ALIGN_MIDDLE,
                Padding             = 2
            };

            PdfPCell cell_fabric_left = new PdfPCell()
            {
                Border = Rectangle.TOP_BORDER | Rectangle.LEFT_BORDER | Rectangle.BOTTOM_BORDER | Rectangle.RIGHT_BORDER,
                HorizontalAlignment = Element.ALIGN_LEFT,
                VerticalAlignment   = Element.ALIGN_MIDDLE,
                Padding             = 2
            };

            float rowYFab = rowYTittleFab - table_fabric_top.TotalHeight - 5;
            float allowedRow2HeightFab = rowYFab - printedOnHeight - margin;

            //cell_fabric_center.Phrase = new Phrase("FABRIC", bold_font);
            //table_fabric.AddCell(cell_fabric_center);

            cell_fabric_center.Phrase = new Phrase("PRODUCT CODE", bold_font);
            table_fabric.AddCell(cell_fabric_center);

            cell_fabric_center.Phrase = new Phrase("COMPOSITION", bold_font);
            table_fabric.AddCell(cell_fabric_center);

            cell_fabric_center.Phrase = new Phrase("CONSTRUCTION", bold_font);
            table_fabric.AddCell(cell_fabric_center);

            cell_fabric_center.Phrase = new Phrase("YARN", bold_font);
            table_fabric.AddCell(cell_fabric_center);

            cell_fabric_center.Phrase = new Phrase("WIDTH", bold_font);
            table_fabric.AddCell(cell_fabric_center);

            cell_fabric_center.Phrase = new Phrase("DESCRIPTION", bold_font);
            table_fabric.AddCell(cell_fabric_center);

            cell_fabric_center.Phrase = new Phrase("QUANTITY", bold_font);
            table_fabric.AddCell(cell_fabric_center);

            cell_fabric_center.Phrase = new Phrase("REMARK", bold_font);
            table_fabric.AddCell(cell_fabric_center);

            foreach (var materialModel in viewModel.CostCalculationGarment.CostCalculationGarment_Materials)
            {
                if (materialModel.Category.name == "FABRIC")
                {
                    //cell_fabric_left.Phrase = new Phrase(materialModel.Category.SubCategory != null ? materialModel.Category.SubCategory : "", normal_font);
                    //table_fabric.AddCell(cell_fabric_left);

                    cell_fabric_left.Phrase = new Phrase(materialModel.Product.Code, normal_font);
                    table_fabric.AddCell(cell_fabric_left);

                    cell_fabric_left.Phrase = new Phrase(materialModel.Product.Composition, normal_font);
                    table_fabric.AddCell(cell_fabric_left);

                    cell_fabric_left.Phrase = new Phrase(materialModel.Product.Const, normal_font);
                    table_fabric.AddCell(cell_fabric_left);

                    cell_fabric_left.Phrase = new Phrase(materialModel.Product.Yarn, normal_font);
                    table_fabric.AddCell(cell_fabric_left);

                    cell_fabric_left.Phrase = new Phrase(materialModel.Product.Width, normal_font);

                    table_fabric.AddCell(cell_fabric_left);

                    cell_fabric_left.Phrase = new Phrase(materialModel.Description != null ? materialModel.Description : "", normal_font);
                    table_fabric.AddCell(cell_fabric_left);

                    cell_fabric_left.Phrase = new Phrase(materialModel.Quantity.ToString() != null ? String.Format("{0} " + materialModel.UOMQuantity.Unit, materialModel.Quantity.ToString()) : "0", normal_font);
                    table_fabric.AddCell(cell_fabric_left);

                    cell_fabric_left.Phrase = new Phrase(materialModel.Information != null ? materialModel.Information : "", normal_font);
                    table_fabric.AddCell(cell_fabric_left);
                }
            }
            table_fabric.WriteSelectedRows(0, -1, 10, rowYFab, cb);
            #endregion

            #region Table Accessories
            //Accessories Title
            PdfPTable table_acc_top = new PdfPTable(1);
            table_acc_top.TotalWidth = 570f;

            float[] acc_width_top = new float[] { 5f };
            table_acc_top.SetWidths(acc_width_top);

            PdfPCell cell_top_acc = new PdfPCell()
            {
                Border = Rectangle.NO_BORDER,
                HorizontalAlignment = Element.ALIGN_LEFT,
                VerticalAlignment   = Element.ALIGN_MIDDLE,
                PaddingRight        = 1,
                PaddingBottom       = 2,
                PaddingTop          = 2
            };

            cell_top_acc.Phrase = new Phrase("ACCESSORIES", bold_font);
            table_acc_top.AddCell(cell_top_acc);

            float rowYTittleAcc           = rowYFab - table_fabric.TotalHeight - 10;
            float allowedRow2HeightTopAcc = rowYTittleFab - printedOnHeight - margin;
            table_acc_top.WriteSelectedRows(0, -1, 10, rowYTittleAcc, cb);

            //Main Accessories Table
            PdfPTable table_accessories = new PdfPTable(8);
            table_accessories.TotalWidth = 570f;

            float[] accessories_widths = new float[] { 5f, 5f, 5f, 5f, 5f, 5f, 5f, 5f };
            table_accessories.SetWidths(accessories_widths);

            PdfPCell cell_acc_center = new PdfPCell()
            {
                Border = Rectangle.TOP_BORDER | Rectangle.LEFT_BORDER | Rectangle.BOTTOM_BORDER | Rectangle.RIGHT_BORDER,
                HorizontalAlignment = Element.ALIGN_CENTER,
                VerticalAlignment   = Element.ALIGN_MIDDLE,
                Padding             = 2
            };

            PdfPCell cell_acc_left = new PdfPCell()
            {
                Border = Rectangle.TOP_BORDER | Rectangle.LEFT_BORDER | Rectangle.BOTTOM_BORDER | Rectangle.RIGHT_BORDER,
                HorizontalAlignment = Element.ALIGN_LEFT,
                VerticalAlignment   = Element.ALIGN_MIDDLE,
                Padding             = 2
            };

            float rowYAcc = rowYTittleAcc - table_fabric_top.TotalHeight - 5;
            float allowedRow2HeightAcc = rowYAcc - printedOnHeight - margin;

            //cell_acc_center.Phrase = new Phrase("ACCESSORIES", bold_font);
            //table_accessories.AddCell(cell_acc_center);

            cell_fabric_center.Phrase = new Phrase("PRODUCT CODE", bold_font);
            table_accessories.AddCell(cell_fabric_center);

            cell_fabric_center.Phrase = new Phrase("COMPOSITION", bold_font);
            table_accessories.AddCell(cell_fabric_center);

            cell_fabric_center.Phrase = new Phrase("CONSTRUCTION", bold_font);
            table_accessories.AddCell(cell_fabric_center);

            cell_fabric_center.Phrase = new Phrase("YARN", bold_font);
            table_accessories.AddCell(cell_fabric_center);

            cell_fabric_center.Phrase = new Phrase("WIDTH", bold_font);
            table_accessories.AddCell(cell_fabric_center);

            cell_acc_center.Phrase = new Phrase("DESCRIPTION", bold_font);
            table_accessories.AddCell(cell_acc_center);

            cell_acc_center.Phrase = new Phrase("QUANTITY", bold_font);
            table_accessories.AddCell(cell_acc_center);

            cell_acc_center.Phrase = new Phrase("REMARK", bold_font);
            table_accessories.AddCell(cell_acc_center);

            foreach (var materialModel in viewModel.CostCalculationGarment.CostCalculationGarment_Materials)
            {
                if (materialModel.Category.name != "FABRIC")
                {
                    cell_acc_left.Phrase = new Phrase(materialModel.Product.Code, normal_font);
                    table_accessories.AddCell(cell_acc_left);

                    cell_acc_left.Phrase = new Phrase(materialModel.Product.Composition, normal_font);
                    table_accessories.AddCell(cell_acc_left);

                    cell_acc_left.Phrase = new Phrase(materialModel.Product.Const, normal_font);
                    table_accessories.AddCell(cell_acc_left);

                    cell_acc_left.Phrase = new Phrase(materialModel.Product.Yarn, normal_font);
                    table_accessories.AddCell(cell_acc_left);

                    cell_acc_left.Phrase = new Phrase(materialModel.Product.Width, normal_font);

                    table_accessories.AddCell(cell_acc_left);

                    cell_acc_left.Phrase = new Phrase(materialModel.Description != null ? materialModel.Description : "", normal_font);
                    table_accessories.AddCell(cell_acc_left);

                    cell_acc_left.Phrase = new Phrase(materialModel.Quantity != null ? String.Format("{0} " + materialModel.UOMQuantity.Unit, materialModel.Quantity.ToString()) : "0", normal_font);
                    table_accessories.AddCell(cell_acc_left);

                    cell_acc_left.Phrase = new Phrase(materialModel.Information != null ? materialModel.Information : "", normal_font);
                    table_accessories.AddCell(cell_acc_left);
                }
            }
            table_accessories.WriteSelectedRows(0, -1, 10, rowYAcc, cb);
            #endregion


            #region Table Size Breakdown
            //Title
            PdfPTable table_breakdown_top = new PdfPTable(1);
            table_breakdown_top.TotalWidth = 570f;

            float[] breakdown_width_top = new float[] { 5f };
            table_breakdown_top.SetWidths(breakdown_width_top);

            PdfPCell cell_top_breakdown = new PdfPCell()
            {
                Border = Rectangle.NO_BORDER,
                HorizontalAlignment = Element.ALIGN_LEFT,
                VerticalAlignment   = Element.ALIGN_MIDDLE,
                PaddingRight        = 1,
                PaddingBottom       = 2,
                PaddingTop          = 2
            };

            cell_top_breakdown.Phrase = new Phrase("SIZE BREAKDOWN", bold_font);
            table_breakdown_top.AddCell(cell_top_breakdown);

            float rowYTittleBreakDown        = rowYAcc - table_accessories.TotalHeight - 10;
            float allowedRow2HeightBreakdown = rowYTittleBreakDown - printedOnHeight - margin;
            table_breakdown_top.WriteSelectedRows(0, -1, 10, rowYTittleBreakDown, cb);

            //Main Table Size Breakdown
            PdfPTable table_breakDown = new PdfPTable(2);
            table_breakDown.TotalWidth = 570f;

            float[] breakDown_widths = new float[] { 5f, 10f };
            table_breakDown.SetWidths(breakDown_widths);

            PdfPCell cell_breakDown_center = new PdfPCell()
            {
                Border = Rectangle.TOP_BORDER | Rectangle.LEFT_BORDER | Rectangle.BOTTOM_BORDER | Rectangle.RIGHT_BORDER,
                HorizontalAlignment = Element.ALIGN_CENTER,
                VerticalAlignment   = Element.ALIGN_MIDDLE,
                Padding             = 2
            };

            PdfPCell cell_breakDown_left = new PdfPCell()
            {
                Border = Rectangle.TOP_BORDER | Rectangle.LEFT_BORDER | Rectangle.BOTTOM_BORDER | Rectangle.RIGHT_BORDER,
                HorizontalAlignment = Element.ALIGN_LEFT,
                VerticalAlignment   = Element.ALIGN_MIDDLE,
                Padding             = 2
            };

            PdfPCell cell_breakDown_total = new PdfPCell()
            {
                Border = Rectangle.TOP_BORDER | Rectangle.LEFT_BORDER | Rectangle.BOTTOM_BORDER,
                HorizontalAlignment = Element.ALIGN_LEFT,
                VerticalAlignment   = Element.ALIGN_MIDDLE,
                Padding             = 2
            };

            PdfPCell cell_breakDown_total_2 = new PdfPCell()
            {
                Border = Rectangle.TOP_BORDER | Rectangle.BOTTOM_BORDER | Rectangle.RIGHT_BORDER,
                HorizontalAlignment = Element.ALIGN_LEFT,
                VerticalAlignment   = Element.ALIGN_MIDDLE,
                Padding             = 2
            };

            float rowYbreakDown = rowYTittleBreakDown - table_breakdown_top.TotalHeight - 5;
            float allowedRow2HeightBreakDown   = rowYbreakDown - printedOnHeight - margin;
            var   remainingRowToHeightBrekdown = rowYbreakDown - 5 - printedOnHeight - margin;

            cell_breakDown_center.Phrase = new Phrase("WARNA", bold_font);
            table_breakDown.AddCell(cell_breakDown_center);

            cell_breakDown_center.Phrase = new Phrase("SIZE RANGE", bold_font);
            table_breakDown.AddCell(cell_breakDown_center);

            foreach (var productRetail in viewModel.RO_Garment_SizeBreakdowns)
            {
                if (productRetail.Total != 0)
                {
                    cell_breakDown_left.Phrase = new Phrase(productRetail.Color.Name != null ? productRetail.Color.Name : "", normal_font);
                    table_breakDown.AddCell(cell_breakDown_left);

                    PdfPTable table_breakDown_child = new PdfPTable(3);
                    table_breakDown_child.TotalWidth = 300f;

                    float[] breakDown_child_widths = new float[] { 5f, 5f, 5f };
                    table_breakDown_child.SetWidths(breakDown_child_widths);

                    PdfPCell cell_breakDown_child_center = new PdfPCell()
                    {
                        Border = Rectangle.TOP_BORDER | Rectangle.LEFT_BORDER | Rectangle.BOTTOM_BORDER | Rectangle.RIGHT_BORDER,
                        HorizontalAlignment = Element.ALIGN_CENTER,
                        VerticalAlignment   = Element.ALIGN_MIDDLE,
                        Padding             = 2
                    };

                    PdfPCell cell_breakDown_child_left = new PdfPCell()
                    {
                        Border = Rectangle.TOP_BORDER | Rectangle.LEFT_BORDER | Rectangle.BOTTOM_BORDER | Rectangle.RIGHT_BORDER,
                        HorizontalAlignment = Element.ALIGN_LEFT,
                        VerticalAlignment   = Element.ALIGN_MIDDLE,
                        Padding             = 2
                    };

                    cell_breakDown_child_center.Phrase = new Phrase("KETERANGAN", bold_font);
                    table_breakDown_child.AddCell(cell_breakDown_child_center);

                    cell_breakDown_child_center.Phrase = new Phrase("SIZE", bold_font);
                    table_breakDown_child.AddCell(cell_breakDown_child_center);

                    cell_breakDown_child_center.Phrase = new Phrase("QUANTITY", bold_font);
                    table_breakDown_child.AddCell(cell_breakDown_child_center);

                    foreach (var size in productRetail.RO_Garment_SizeBreakdown_Details)
                    {
                        cell_breakDown_child_left.Phrase = new Phrase(size.Information != null ? size.Information : "", normal_font);
                        table_breakDown_child.AddCell(cell_breakDown_child_left);

                        cell_breakDown_child_left.Phrase = new Phrase(size.Size != null ? size.Size : "", normal_font);
                        table_breakDown_child.AddCell(cell_breakDown_child_left);

                        cell_breakDown_child_left.Phrase = new Phrase(size.Quantity.ToString() != null ? size.Quantity.ToString() : "0", normal_font);
                        table_breakDown_child.AddCell(cell_breakDown_child_left);
                    }

                    cell_breakDown_child_left.Phrase = new Phrase(" ", bold_font);
                    table_breakDown_child.AddCell(cell_breakDown_child_left);

                    cell_breakDown_child_left.Phrase = new Phrase("TOTAL", bold_font);
                    table_breakDown_child.AddCell(cell_breakDown_child_left);

                    cell_breakDown_child_left.Phrase = new Phrase(productRetail.Total.ToString() != null ? productRetail.Total.ToString() : "0", normal_font);
                    table_breakDown_child.AddCell(cell_breakDown_child_left);

                    table_breakDown_child.WriteSelectedRows(0, -1, 10, 0, cb);

                    table_breakDown.AddCell(table_breakDown_child);
                }

                var tableBreakdownCurrentHeight = table_breakDown.TotalHeight;

                if (tableBreakdownCurrentHeight / remainingRowToHeightBrekdown > 1)
                {
                    if (tableBreakdownCurrentHeight / allowedRow2HeightBreakDown > 1)
                    {
                        PdfPRow headerRow = table_breakDown.GetRow(0);
                        PdfPRow lastRow   = table_breakDown.GetRow(table_breakDown.Rows.Count - 1);
                        table_breakDown.DeleteLastRow();
                        table_breakDown.WriteSelectedRows(0, -1, 10, rowYbreakDown, cb);
                        table_breakDown.DeleteBodyRows();
                        this.DrawPrintedOn(now, bf, cb);
                        document.NewPage();
                        table_breakDown.Rows.Add(headerRow);
                        table_breakDown.Rows.Add(lastRow);
                        table_breakDown.CalculateHeights(true);
                        rowYbreakDown = startY;
                        remainingRowToHeightBrekdown = rowYbreakDown - 5 - printedOnHeight - margin;
                        allowedRow2HeightBreakDown   = remainingRowToHeightBrekdown - printedOnHeight - margin;
                    }
                }
            }

            cell_breakDown_total_2.Phrase = new Phrase("TOTAL", bold_font);
            table_breakDown.AddCell(cell_breakDown_total_2);

            cell_breakDown_total_2.Phrase = new Phrase(viewModel.Total.ToString(), bold_font);
            table_breakDown.AddCell(cell_breakDown_total_2);

            table_breakDown.WriteSelectedRows(0, -1, 10, rowYbreakDown, cb);
            #endregion

            #region Table Instruksi
            //Title
            PdfPTable table_instruction  = new PdfPTable(1);
            float[]   instruction_widths = new float[] { 5f };

            table_instruction.TotalWidth = 500f;
            table_instruction.SetWidths(instruction_widths);

            PdfPCell cell_top_instruction = new PdfPCell()
            {
                Border = Rectangle.NO_BORDER,
                HorizontalAlignment = Element.ALIGN_LEFT,
                VerticalAlignment   = Element.ALIGN_MIDDLE,
                PaddingRight        = 1,
                PaddingBottom       = 2,
                PaddingTop          = 2
            };

            PdfPCell cell_colon_instruction = new PdfPCell()
            {
                Border = Rectangle.NO_BORDER,
                HorizontalAlignment = Element.ALIGN_LEFT,
                VerticalAlignment   = Element.ALIGN_MIDDLE
            };

            PdfPCell cell_top_keterangan_instruction = new PdfPCell()
            {
                Border = Rectangle.NO_BORDER,
                HorizontalAlignment = Element.ALIGN_LEFT,
                VerticalAlignment   = Element.ALIGN_MIDDLE,
                PaddingRight        = 1,
                PaddingBottom       = 2,
                PaddingTop          = 2,
                Colspan             = 7
            };

            cell_top_instruction.Phrase = new Phrase("INSTRUCTION", normal_font);
            table_instruction.AddCell(cell_top_instruction);
            table_instruction.AddCell(cell_colon_instruction);
            cell_top_keterangan_instruction.Phrase = new Phrase($"{viewModel.Instruction}", normal_font);
            table_instruction.AddCell(cell_top_keterangan_instruction);

            float rowYInstruction = rowYbreakDown - table_breakDown.TotalHeight - 10;
            float allowedRow2HeightInstruction = rowYInstruction - printedOnHeight - margin;

            table_instruction.WriteSelectedRows(0, -1, 10, rowYInstruction, cb);
            #endregion

            #region RO Image
            var    countImageRo = 0;
            byte[] roImage;

            foreach (var index in viewModel.ImagesFile)
            {
                countImageRo++;
            }


            float rowYRoImage = rowYInstruction - table_instruction.TotalHeight - 10;
            float imageRoHeight;

            if (countImageRo != 0)
            {
                if (countImageRo > 5)
                {
                    countImageRo = 5;
                }

                PdfPTable table_ro_image = new PdfPTable(countImageRo);
                float[]   ro_widths      = new float[countImageRo];

                for (var i = 0; i < countImageRo; i++)
                {
                    ro_widths.SetValue(5f, i);
                }

                if (countImageRo != 0)
                {
                    table_ro_image.SetWidths(ro_widths);
                }

                table_ro_image.TotalWidth = 570f;

                foreach (var imageFromRo in viewModel.ImagesFile)
                {
                    try
                    {
                        roImage = Convert.FromBase64String(Base64.GetBase64File(imageFromRo));
                    }
                    catch (Exception)
                    {
                        var webClient = new WebClient();
                        roImage = webClient.DownloadData("https://bateeqstorage.blob.core.windows.net/other/no-image.jpg");
                    }

                    Image images = Image.GetInstance(imgb: roImage);

                    if (images.Width > 60)
                    {
                        float percentage = 0.0f;
                        percentage = 60 / images.Width;
                        images.ScalePercent(percentage * 100);
                    }

                    PdfPCell imageCell = new PdfPCell(images);
                    imageCell.Border = 0;
                    table_ro_image.AddCell(imageCell);
                }

                PdfPCell cell_image = new PdfPCell()
                {
                    Border = Rectangle.NO_BORDER,
                    HorizontalAlignment = Element.ALIGN_LEFT,
                    VerticalAlignment   = Element.ALIGN_MIDDLE,
                    Padding             = 2,
                };

                foreach (var name in viewModel.ImagesName)
                {
                    cell_image.Phrase = new Phrase(name, normal_font);
                    table_ro_image.AddCell(cell_image);
                }

                imageRoHeight = table_ro_image.TotalHeight;
                table_ro_image.WriteSelectedRows(0, -1, 10, rowYRoImage, cb);
            }
            else
            {
                imageRoHeight = 0;
            }
            #endregion

            #region Signature
            PdfPTable table_signature = new PdfPTable(6);
            table_signature.TotalWidth = 570f;

            float[] signature_widths = new float[] { 1f, 1f, 1f, 1f, 1f, 1f };
            table_signature.SetWidths(signature_widths);

            PdfPCell cell_signature = new PdfPCell()
            {
                Border = Rectangle.NO_BORDER,
                HorizontalAlignment = Element.ALIGN_CENTER,
                VerticalAlignment   = Element.ALIGN_MIDDLE,
                Padding             = 2,
            };

            PdfPCell cell_signature_noted = new PdfPCell()
            {
                Border = Rectangle.NO_BORDER,
                HorizontalAlignment = Element.ALIGN_CENTER,
                VerticalAlignment   = Element.ALIGN_MIDDLE,
                Padding             = 2,
                PaddingTop          = 50
            };

            cell_signature.Phrase = new Phrase("Dibuat", normal_font);
            table_signature.AddCell(cell_signature);
            cell_signature.Phrase = new Phrase("Kasie Merchandiser", normal_font);
            table_signature.AddCell(cell_signature);
            cell_signature.Phrase = new Phrase("R & D", normal_font);
            table_signature.AddCell(cell_signature);
            cell_signature.Phrase = new Phrase("Ka Produksi", normal_font);
            table_signature.AddCell(cell_signature);
            cell_signature.Phrase = new Phrase("Mengetahui", normal_font);
            table_signature.AddCell(cell_signature);
            cell_signature.Phrase = new Phrase("Menyetujui", normal_font);
            table_signature.AddCell(cell_signature);

            cell_signature_noted.Phrase = new Phrase("(                           )", normal_font);
            table_signature.AddCell(cell_signature_noted);
            cell_signature_noted.Phrase = new Phrase("(                           )", normal_font);
            table_signature.AddCell(cell_signature_noted);
            cell_signature_noted.Phrase = new Phrase("(                           )", normal_font);
            table_signature.AddCell(cell_signature_noted);
            cell_signature_noted.Phrase = new Phrase("(                           )", normal_font);
            table_signature.AddCell(cell_signature_noted);
            cell_signature_noted.Phrase = new Phrase("(                           )", normal_font);
            table_signature.AddCell(cell_signature_noted);
            cell_signature_noted.Phrase = new Phrase("(Michelle Tjokrosaputro)", normal_font);
            table_signature.AddCell(cell_signature_noted);

            float table_signatureY = rowYRoImage - imageRoHeight - 10;
            table_signature.WriteSelectedRows(0, -1, 10, table_signatureY, cb);
            #endregion

            this.DrawPrintedOn(now, bf, cb);
            document.Close();

            byte[] byteInfo = stream.ToArray();
            stream.Write(byteInfo, 0, byteInfo.Length);
            stream.Position = 0;
            return(stream);
        }
Esempio n. 10
0
        public string Create(PersonModel person)
        {
            var pdfdoc = new Document();

            pdfdoc.SetPageSize(PageSize.A4);
            pdfdoc.SetMargins(MillimetersToPoints(25), MillimetersToPoints(20), MillimetersToPoints(25), MillimetersToPoints(15));

            var culture = CultureInfo.GetCultureInfo("de-DE");

            var       stream = new MemoryStream();
            PdfWriter writer = PdfWriter.GetInstance(pdfdoc, stream);

            pdfdoc.Open();

            Image logo = Image.GetInstance(Properties.Resources.FegLogo, System.Drawing.Imaging.ImageFormat.Png);

            logo.ScaleToFit(MillimetersToPoints(55f), MillimetersToPoints(500f));
            logo.SetAbsolutePosition(pdfdoc.PageSize.Width - MillimetersToPoints(73f) - MillimetersToPoints(55f), pdfdoc.PageSize.Height - MillimetersToPoints(23f));
            pdfdoc.Add(logo);

            var header = new PdfPTable(1)
            {
                WidthPercentage = 100,
                HeaderRows      = 0
            };

            header.DefaultCell.Border = Rectangle.NO_BORDER;

            var paragraph = new Paragraph(this.CreatePhrase("Zugangsdaten zum Intranet", this.TitleFont))
            {
                Alignment     = Element.ALIGN_CENTER,
                SpacingBefore = 2,
                SpacingAfter  = 2
            };

            header.AddCell(paragraph);
            var cell = header.GetRow(0).GetCells()[0];

            cell.BackgroundColor     = new BaseColor(191, 191, 191);
            cell.HorizontalAlignment = Element.ALIGN_CENTER;
            cell.PaddingTop          = 2;
            cell.PaddingBottom       = 5;

            header.CompleteRow();

            pdfdoc.Add(header);

            pdfdoc.Add(new Paragraph(this.CreatePhrase(person.Briefanrede + ","))
            {
                SpacingBefore = MillimetersToPoints(5)
            });
            pdfdoc.Add(new Paragraph(this.CreatePhrase("die Webadresse der Gemeinde lautet"))
            {
                SpacingBefore = MillimetersToPoints(2)
            });

            paragraph = new Paragraph(this.CreatePhrase("www.feg-giessen.de", StandardBoldFont))
            {
                SpacingBefore = MillimetersToPoints(2),
                Alignment     = Element.ALIGN_CENTER
            };

            pdfdoc.Add(paragraph);

            paragraph = new Paragraph(this.CreatePhrase("Über die öffentlichen Informationen hinaus gibt es auch noch geschützte, interne Bereiche, die weitere nützliche und aktuelle Zusatzinformationen enthalten. Diese sind nur für Gemeindemitglieder gedacht und deshalb nur mit entsprechender Zugangsberechtigung erreichbar, sobald das zugehörige Passwort mitgeteilt wurde. Folgende Schritte sind dann dazu erforderlich:"));
            paragraph.SpacingBefore = MillimetersToPoints(2);
            paragraph.Alignment     = Element.ALIGN_JUSTIFIED;
            pdfdoc.Add(paragraph);

            var table = new PdfPTable(2)
            {
                WidthPercentage = 100,
                HeaderRows      = 0,
                SpacingBefore   = MillimetersToPoints(5),
                SpacingAfter    = MillimetersToPoints(5)
            };

            table.DefaultCell.Border = Rectangle.NO_BORDER;

            table.SetTotalWidth(new[] { 1.2f, 5f });

            table.AddCell(this.CreatePhrase("Benutzername", this.StandardBoldFont));
            table.AddCell(this.CreatePhrase(person.Username));
            table.CompleteRow();
            table.AddCell(this.CreatePhrase("Passwort", this.StandardBoldFont));
            table.AddCell(this.CreatePhrase("(kommt per E-Mail)"));
            table.CompleteRow();

            pdfdoc.Add(table);

            pdfdoc.Add(new Paragraph(this.CreatePhrase("1.) Nach einem Klick rechts oben auf \"Login\" erscheint das Anmeldeformular")));
            pdfdoc.Add(new Paragraph(this.CreatePhrase("2.) Der o.g. Benutzername kann mit Kleinbuchstaben und Punkt in der Mitte eingegeben werden")));
            pdfdoc.Add(new Paragraph(this.CreatePhrase("3.) Das per E-Mail zugeschickte persönliche, geheime Passwort ist einzugeben")));
            pdfdoc.Add(new Paragraph(this.CreatePhrase("4.) Klick auf \"Anmelden\" gewährt den Intranetzugang, erkennbar durch den Login-Namen oben rechts")));

            pdfdoc.Add(new Paragraph(this.CreatePhrase("Und nun freuen wir uns über viele Besucher - und Rückmeldungen, wenn etwas gefällt, etwas fehlt, etwas falsch ist, oder irgendwas noch nicht ganz klappt :-). Das Online-Internet-Team ist unter [email protected] zu erreichen."))
            {
                SpacingBefore = MillimetersToPoints(2), Alignment = Element.ALIGN_JUSTIFIED
            });
            pdfdoc.Add(new Paragraph(this.CreatePhrase("Freunde, Gäste, Sucher und Besucher können natürlich gerne auf die öffentliche Website hingewiesen werden. Dort sind stets die aktuellen Informationen über unsere Gemeinde zu finden."))
            {
                SpacingBefore = MillimetersToPoints(2), SpacingAfter = MillimetersToPoints(5), Alignment = Element.ALIGN_JUSTIFIED
            });

            pdfdoc.Close();
            writer.Flush();

            byte[] data = stream.ToArray();

            string filename = this.fileManager.GetTempFile(person.Username.Replace(".", "-") + ".pdf");

            using (var fs = File.OpenWrite(filename))
            {
                fs.Write(data, 0, (int)data.Length);

                stream.Dispose();
                fs.Flush();
            }

            return(filename);
        }