创建pdf文件时表格行将转到下一页

时间:2018-08-13 14:22:49

标签: c# asp.net pdf itext

我必须在我的网站中创建一个带有标头表和订单表的pdf文件,并使用ItextSharp库创建了该文件,问题是当表中有一行时,可以很好地创建pdf,但是是将整个订单表移至下一页的两行或更多行。 这是代码

    public virtual void PrintDeliverySheetsToPdf(Stream stream, IList<Order> orders, int languageId = 0, int vendorId = 0)
    {
        if (stream == null)
            throw new ArgumentNullException("stream");

        if (orders == null)
            throw new ArgumentNullException("orders");

        var pageSize = PageSize.A4.Rotate();

        if (_pdfSettings.LetterPageSizeEnabled)
        {
            pageSize = PageSize.LETTER;
        }


        var doc = new Document(pageSize);
        var pdfWriter = PdfWriter.GetInstance(doc, stream);
        doc.Open();

        //fonts
        var titleFont = GetFont();
        titleFont.SetStyle(Font.BOLD);
        titleFont.Color = BaseColor.BLACK;
        var font = GetFont();
        var attributesFont = GetFont();
        attributesFont.SetStyle(Font.ITALIC);

        int ordCount = orders.Count;
        int ordNum = 0;
        //by default _pdfSettings contains settings for the current active store
        //and we need PdfSettings for the store which was used to place an order
        //so let's load it based on a store of the current order
        //var pdfSettingsByStore = _settingContext.LoadSetting<PdfSettings>(order.StoreId);


        var lang = _languageService.GetLanguageById(0);
        if (lang == null || !lang.Published)
            lang = _workContext.WorkingLanguage;
        #region Header

        //logo
        var logoPicture = _pictureService.GetPictureById(_pdfSettings.LogoPictureId);
        var logoExists = logoPicture != null;

        //header
        var headerTable = new PdfPTable(logoExists ? 3 : 2);
        headerTable.RunDirection = GetDirection(lang);
        headerTable.DefaultCell.Border = Rectangle.NO_BORDER;

        var cellHeader1 = new PdfPCell(new Phrase(String.Format(_localizationService.GetResource("DeliverySheet.Station"), font)));
        cellHeader1.Phrase.Add(new Phrase(Environment.NewLine));
        cellHeader1.Phrase.Add(new Phrase(Environment.NewLine));
        cellHeader1.Phrase.Add(new Phrase(String.Format(_localizationService.GetResource("DeliverySheet.RiderCode"), font)));
        cellHeader1.Phrase.Add(new Phrase(Environment.NewLine));
        cellHeader1.Phrase.Add(new Phrase(Environment.NewLine));
        cellHeader1.Phrase.Add(new Phrase(String.Format(_localizationService.GetResource("DeliverySheet.RiderName"), font)));
        cellHeader1.Phrase.Add(new Phrase(Environment.NewLine));
        cellHeader1.Phrase.Add(new Phrase(Environment.NewLine));
        cellHeader1.Phrase.Add(new Phrase(String.Format(_localizationService.GetResource("DeliverySheet.RiderSign"), font)));
        cellHeader1.Phrase.Add(new Phrase(Environment.NewLine));
        cellHeader1.Phrase.Add(new Phrase(Environment.NewLine));
        cellHeader1.HorizontalAlignment = Element.ALIGN_LEFT;
        cellHeader1.Border = Rectangle.NO_BORDER;
        headerTable.AddCell(cellHeader1);

        var cellHeader2 = new PdfPCell(new Phrase(String.Format(_localizationService.GetResource("DeliverySheet.DateTime"), font)));
        cellHeader2.Phrase.Add(new Phrase(Environment.NewLine));
        cellHeader2.Phrase.Add(new Phrase(Environment.NewLine));
        cellHeader2.Phrase.Add(new Phrase(String.Format(_localizationService.GetResource("DeliverySheet.DeliveryRoute"))));
        cellHeader2.Phrase.Add(new Phrase(Environment.NewLine));
        cellHeader2.Phrase.Add(new Phrase(Environment.NewLine));
        cellHeader2.Phrase.Add(new Phrase(String.Format(_localizationService.GetResource("DeliverySheet.TimeSlot"))));
        cellHeader2.Phrase.Add(new Phrase(Environment.NewLine));
        cellHeader2.Phrase.Add(new Phrase(Environment.NewLine));
        cellHeader2.HorizontalAlignment = Element.ALIGN_LEFT;
        cellHeader2.Border = Rectangle.NO_BORDER;

        headerTable.AddCell(cellHeader2);

        if (logoExists)
            if (lang.Rtl)
                headerTable.SetWidths(new[] { 0.2f, 0.8f });
        //else
        //    headerTable.SetWidths(new[] { 0.8f, 0.2f });
        headerTable.WidthPercentage = 100f;

        //logo               
        if (logoExists)
        {
            var logoFilePath = _pictureService.GetThumbLocalPath(logoPicture, 0, false);
            var logo = Image.GetInstance(logoFilePath);
            logo.Alignment = GetAlignment(lang, true);
            logo.ScaleToFit(65f, 65f);

            var cellLogo = new PdfPCell();
            cellLogo.Border = Rectangle.NO_BORDER;
            cellLogo.AddElement(logo);
            headerTable.AddCell(cellLogo);
        }
        headerTable.SplitLate = false;
        doc.Add(headerTable);


        var ordersTable = new PdfPTable(12);
        ordersTable.RunDirection = GetDirection(lang);
        ordersTable.WidthPercentage = 100f;
        ordersTable.DefaultCell.Border = Rectangle.BOX;

        ordersTable.SetWidths(new[] { 6, 6, 6, 10, 10, 10, 10, 12, 12, 6, 13, 7 });


        //delivery Number
        var cellDeliveryNo = new PdfPCell(new Phrase(_localizationService.GetResource("DeliverySheet.DeliveryNo", lang.Id), font));
        cellDeliveryNo.BackgroundColor = BaseColor.LIGHT_GRAY;
        cellDeliveryNo.HorizontalAlignment = Element.ALIGN_CENTER;
        ordersTable.AddCell(cellDeliveryNo);


        //order Number
        var orderNo = new PdfPCell(new Phrase(_localizationService.GetResource("DeliverySheet.OrderNo", lang.Id), font));
        orderNo.BackgroundColor = BaseColor.LIGHT_GRAY;
        orderNo.HorizontalAlignment = Element.ALIGN_CENTER;
        ordersTable.AddCell(orderNo);

        //CN
        var cn = new PdfPCell(new Phrase(_localizationService.GetResource("DeliverySheet.CN", lang.Id), font));
        cn.BackgroundColor = BaseColor.LIGHT_GRAY;
        cn.HorizontalAlignment = Element.ALIGN_CENTER;
        ordersTable.AddCell(cn);

        //Shipper Name
        var shipperName = new PdfPCell(new Phrase(_localizationService.GetResource("DeliverySheet.ShipperName", lang.Id), font));
        shipperName.BackgroundColor = BaseColor.LIGHT_GRAY;
        shipperName.HorizontalAlignment = Element.ALIGN_CENTER;
        ordersTable.AddCell(shipperName);

        //Shipper Address 
        var shipperAddress = new PdfPCell(new Phrase(_localizationService.GetResource("DeliverySheet.ShipperAddress", lang.Id), font));
        shipperAddress.BackgroundColor = BaseColor.LIGHT_GRAY;
        shipperAddress.HorizontalAlignment = Element.ALIGN_CENTER;
        ordersTable.AddCell(shipperAddress);

        //Consignee Name
        var consigneeName = new PdfPCell(new Phrase(_localizationService.GetResource("DeliverySheet.ConsigneeName", lang.Id), font));
        consigneeName.BackgroundColor = BaseColor.LIGHT_GRAY;
        consigneeName.HorizontalAlignment = Element.ALIGN_CENTER;
        ordersTable.AddCell(consigneeName);

        //Consignee Address
        var consigneeAddress = new PdfPCell(new Phrase(_localizationService.GetResource("DeliverySheet.ConsigneeAddress", lang.Id), font));
        consigneeAddress.BackgroundColor = BaseColor.LIGHT_GRAY;
        consigneeAddress.HorizontalAlignment = Element.ALIGN_CENTER;
        ordersTable.AddCell(consigneeAddress);

        //orderItems
        var orderItemsCell = new PdfPCell(new Phrase(_localizationService.GetResource("DeliverySheet.OrderItems", lang.Id), font));
        orderItemsCell.BackgroundColor = BaseColor.LIGHT_GRAY;
        orderItemsCell.HorizontalAlignment = Element.ALIGN_CENTER;
        ordersTable.AddCell(orderItemsCell);

        //VAS
        var vas = new PdfPCell(new Phrase(_localizationService.GetResource("DeliverySheet.VAS", lang.Id), font));
        vas.BackgroundColor = BaseColor.LIGHT_GRAY;
        vas.HorizontalAlignment = Element.ALIGN_CENTER;
        ordersTable.AddCell(vas);

        //DateTime
        var dateTime = new PdfPCell(new Phrase(_localizationService.GetResource("DeliverySheet.DateTime", lang.Id), font));
        dateTime.BackgroundColor = BaseColor.LIGHT_GRAY;
        dateTime.HorizontalAlignment = Element.ALIGN_CENTER;
        ordersTable.AddCell(dateTime);

        //Remarks
        var remarks = new PdfPCell(new Phrase(_localizationService.GetResource("DeliverySheet.Remarks", lang.Id), font));
        remarks.BackgroundColor = BaseColor.LIGHT_GRAY;
        remarks.HorizontalAlignment = Element.ALIGN_CENTER;
        ordersTable.AddCell(remarks);

        //Recievings
        var recievings = new PdfPCell(new Phrase(_localizationService.GetResource("DeliverySheet.Receiving", lang.Id), font));
        recievings.BackgroundColor = BaseColor.LIGHT_GRAY;
        recievings.HorizontalAlignment = Element.ALIGN_CENTER;
        ordersTable.AddCell(recievings);

        #endregion

        foreach (var order in orders)
        {

            //Delivery No
            ordersTable.AddCell("");

            //Order Id
            ordersTable.AddCell(order.Id.ToString());

            //Order CN
            ordersTable.AddCell(order.CN == null ? "" : order.CN.ToString());

            //Billing Name
            ordersTable.AddCell(order.BillingAddress.FirstName + " " + order.BillingAddress.LastName);

            //Billing Address
            ordersTable.AddCell(order.BillingAddress.Address1 + " " + order.BillingAddress.LandMark1 + " " + order.BillingAddress.LandMark2 + " " + order.BillingAddress.StateProvince.Name);

            //Consignee Name
            ordersTable.AddCell(order.ShippingAddress.FirstName + " " + order.ShippingAddress.LastName);

            //Consignee Address
            ordersTable.AddCell(order.ShippingAddress.Address1 + " " + order.ShippingAddress.LandMark1 + " " + order.ShippingAddress.LandMark2 + " " + order.ShippingAddress.StateProvince.Name);

            var orderItemProductCell = new PdfPCell(new Phrase());
            foreach (var orderItem in order.OrderItems)
            {

                orderItemProductCell.Phrase.Add(new Phrase(orderItem.Product.Name + " - " + orderItem.Product.Sku + "  Qty: " + orderItem.Quantity, font));
                orderItemProductCell.Phrase.Add(new Phrase(Environment.NewLine));
            }
            ordersTable.AddCell(orderItemProductCell);

            var vasCell = new PdfPCell(new Phrase());
            foreach (var vasName in order.OrderValueAddedServices)
            {
                if (vasName.ValueAddedService != null)
                {
                    vasCell.Phrase.Add(new Phrase(vasName.ValueAddedService.Name, font));
                    vasCell.Phrase.Add(new Phrase(Environment.NewLine));
                }
            }
            ordersTable.AddCell(vasCell);

            //Date Time Cell
            ordersTable.AddCell("");

            //Remarks Cell
            ordersTable.AddCell("");

            //Receiving Cell
            ordersTable.AddCell("");


            ordNum++;
            if (ordNum < ordCount)
            {
                doc.NewPage();
            }
        }
        ordersTable.SplitLate = false;
        doc.Add(ordersTable);
        doc.Close();
    }

0 个答案:

没有答案