如何在itextsharp中打印连续页面大小

时间:2017-11-19 16:39:49

标签: c# asp.net-mvc printing controller itext

我必须打印发票,但是,问题是我需要指定纸张的尺寸,但是,我不知道报告的高度,它将被打印为连续的pos打印机。 / p>

这是我创建文档的方式:

    public async Task<FileResult> CreatePdf(int id)
    {
        MemoryStream workStream = new MemoryStream();
        StringBuilder status = new StringBuilder("");
        DateTime dTime = DateTime.Now;
        //file name to be created   
        string strPDFFileName = string.Format("SamplePdf" + dTime.ToString("yyyyMMdd") + "-" + ".pdf");
        Rectangle envelope = new Rectangle(216, 14400);//margenes
       // Document doc = new Document(envelope, 0, 0, 0, 0);
        Document doc = new Document(envelope, 0, 0, 0, 0);
        //Document doc = new Document();
        doc.SetMargins(0f, 0f, 0f, 0f);
        //Create PDF Table with 5 columns  
        PdfPTable tableLayout = new PdfPTable(5);
        doc.SetMargins(0f, 0f, 0f, 0f);
        //Create PDF Table  

        //file will created in this path  
        string strAttachment = Server.MapPath("~/Downloadss/" + strPDFFileName);


        PdfWriter.GetInstance(doc, workStream).CloseStream = false;
        doc.Open();

        //Add Content to PDF   
        doc.Add(await Add_Content_To_PDF(tableLayout, id));

        // Closing the document  
        doc.Close();

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

        //
        // Summary:
        //     Creates a FileStreamResult object using the Stream object, the content type,
        //     and the target file name.
        //
        // Parameters:
        //   fileStream:
        //     The stream to send to the response.
        //
        //   contentType:
        //     The content type (MIME type)
        //
        //   fileDownloadName:
        //     The file name to use in the file-download dialog box that is displayed in the
        //     browser.
        //
        // Returns:
        //     The file-stream result object.
        //protected internal virtual FileStreamResult File(Stream fileStream, string contentType, string fileDownloadName);
        return File(workStream, "application/pdf", strPDFFileName);

    }
    public async Task<int> GetAuthorId()
    {
        var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
        var currentUser = manager.FindById(User.Identity.GetUserId());

        return await UsersHelper.GetAuthorId(currentUser.Email);
    }
    protected async Task<PdfPTable> Add_Content_To_PDF(PdfPTable tableLayout, int id)
    {
       // float[] headers = { 50, 24, 45, 35, 50 }; //Header Widths 
        float[] headers = { 105, 45, 45,0,0 }; //Header Widths  
        tableLayout.SetWidths(headers); //Set the pdf headers  
        tableLayout.WidthPercentage = 100; //Set the PDF File witdh percentage  
        tableLayout.HeaderRows = 1;
        //Add Title to the PDF file at the top  

        var salesDetails =   _db.SalesDetails.ToList<SalesDetail>();
        var salesDetails2 = await _db.SalesDetails.FirstOrDefaultAsync(x=> x.SalesId==id);
        var authorid = await GetAuthorId();

        var report = await _db.Reports.FirstOrDefaultAsync(x => x.AuthorId == authorid);

        if (report == null)
        {
            // return RedirectToAction($"Create/{id}");
        }
        //HEADER BEGINS
        tableLayout.AddCell(new PdfPCell(new Phrase(report.Header1, new Font(Font.FontFamily.HELVETICA, 8, 1, new iTextSharp.text.BaseColor(0, 0, 0))))
        {
            Colspan = 12,
            Border = 0,
            PaddingBottom = 5,
            HorizontalAlignment = Element.ALIGN_CENTER
        });
        tableLayout.AddCell(new PdfPCell(new Phrase(report.Header2, new Font(Font.FontFamily.HELVETICA, 8, 1, new iTextSharp.text.BaseColor(0, 0, 0))))
        {
            Colspan = 12,
            Border = 0,
            PaddingBottom = 5,
            HorizontalAlignment = Element.ALIGN_CENTER
        });
        tableLayout.AddCell(new PdfPCell(new Phrase(report.Header3, new Font(Font.FontFamily.HELVETICA, 8, 1, new iTextSharp.text.BaseColor(0, 0, 0))))
        {
            Colspan = 12,
            Border = 0,
            PaddingBottom = 5,
            HorizontalAlignment = Element.ALIGN_CENTER
        });
        //HEADER ENDS
        //Sales Details Begins
        tableLayout.AddCell(new PdfPCell(new Phrase(" _________________", new Font(Font.FontFamily.HELVETICA, 8, 1, new iTextSharp.text.BaseColor(0, 0, 0))))
        {
            Colspan = 12,
            Border = 0,
            PaddingBottom = 5,
            HorizontalAlignment = Element.ALIGN_CENTER
        });
        tableLayout.AddCell(new PdfPCell(new Phrase(" No. Factura......:  " + salesDetails2.Sales.SalesInvoiceNo, new Font(Font.FontFamily.HELVETICA, 8, 1, new iTextSharp.text.BaseColor(0, 0, 0))))
        {
            Colspan = 12,
            Border = 0,
            PaddingBottom = 5,
            HorizontalAlignment = Element.ALIGN_LEFT
        });
        tableLayout.AddCell(new PdfPCell(new Phrase(" Cliente..........:  " + salesDetails2.Sales.Remarks, new Font(Font.FontFamily.HELVETICA, 8, 1, new iTextSharp.text.BaseColor(0, 0, 0))))
        {
            Colspan = 12,
            Border = 0,
            PaddingBottom = 5,
            HorizontalAlignment = Element.ALIGN_LEFT
        });
        tableLayout.AddCell(new PdfPCell(new Phrase(" Estatus Factura..:  " + salesDetails2.Sales.Status.Name, new Font(Font.FontFamily.HELVETICA, 8, 1, new iTextSharp.text.BaseColor(0, 0, 0))))
        {
            Colspan = 12,
            Border = 0,
            PaddingBottom = 5,
            HorizontalAlignment = Element.ALIGN_LEFT
        });
        tableLayout.AddCell(new PdfPCell(new Phrase(" Fecha Compra.....:  " + salesDetails2.Sales.SalesDate, new Font(Font.FontFamily.HELVETICA, 8, 1, new iTextSharp.text.BaseColor(0, 0, 0))))
        {
            Colspan = 12,
            Border = 0,
            PaddingBottom = 5,
            HorizontalAlignment = Element.ALIGN_LEFT
        });
        //Sales Details Ends
        tableLayout.AddCell(new PdfPCell(new Phrase(" __________________", new Font(Font.FontFamily.HELVETICA, 8, 1, new iTextSharp.text.BaseColor(0, 0, 0))))
        {
            Colspan = 12,
            Border = 0,
            PaddingBottom = 5,
            HorizontalAlignment = Element.ALIGN_CENTER
        });

        ////Add header  
         AddCellToHeader(tableLayout, "Producto");
         AddCellToHeader(tableLayout, "Cantidad");
         AddCellToHeader(tableLayout, "Precio");
          AddCellToHeader(tableLayout, "");
        AddCellToHeader(tableLayout, "");
        //tableLayout.AddCell(new PdfPCell(new Phrase("Producto", new Font(Font.FontFamily.HELVETICA, 8, 1, iTextSharp.text.BaseColor.BLACK)))
        //{
        //    HorizontalAlignment = Element.ALIGN_LEFT,
        //    Padding = 5,
        //    BackgroundColor = new iTextSharp.text.BaseColor(255, 255, 255)
        //});
        //tableLayout.AddCell(new PdfPCell(new Phrase("Cantidad", new Font(Font.FontFamily.HELVETICA, 8, 1, iTextSharp.text.BaseColor.BLACK)))
        //{
        //    HorizontalAlignment = Element.ALIGN_LEFT,
        //    Padding = 5,
        //    BackgroundColor = new iTextSharp.text.BaseColor(255, 255, 255)
        //});
        //tableLayout.AddCell(new PdfPCell(new Phrase("Precio", new Font(Font.FontFamily.HELVETICA, 8, 1, iTextSharp.text.BaseColor.BLACK)))
        //{
        //    HorizontalAlignment = Element.ALIGN_LEFT,
        //    Padding = 5,
        //    BackgroundColor = new iTextSharp.text.BaseColor(255, 255, 255)
        //});
        //tableLayout.AddCell(new PdfPCell(new Phrase("Precio", new Font(Font.FontFamily.HELVETICA, 8, 1, iTextSharp.text.BaseColor.BLACK)))
        //{
        //    HorizontalAlignment = Element.ALIGN_LEFT,
        //    Padding = 5,
        //    BackgroundColor = new iTextSharp.text.BaseColor(255, 255, 255)
        //});
        //tableLayout.AddCell(new PdfPCell(new Phrase("Precio", new Font(Font.FontFamily.HELVETICA, 8, 1, iTextSharp.text.BaseColor.BLACK)))
        //{
        //    HorizontalAlignment = Element.ALIGN_LEFT,
        //    Padding = 5,
        //    BackgroundColor = new iTextSharp.text.BaseColor(255, 255, 255)
        //});
        ////Add body  

        foreach (var emp in salesDetails)
        {

             //AddCellToBody(tableLayout, emp.SalesDetailId.ToString());
             AddCellToBody(tableLayout, emp.Product.Name);
            AddCellToBody(tableLayout, emp.Quantity.ToString());
             AddCellToBody(tableLayout, emp.Product.SellPrice.ToString());
            AddCellToBody(tableLayout, "");
            AddCellToBody(tableLayout, "");
            //tableLayout.AddCell(new PdfPCell(new Phrase(emp.Product.Name, new Font(Font.FontFamily.HELVETICA, 8, 1, iTextSharp.text.BaseColor.BLACK)))
            //{
            //    HorizontalAlignment = Element.ALIGN_LEFT,
            //    Padding = 5,
            //    BackgroundColor = new iTextSharp.text.BaseColor(255, 255, 255)
            //});
            //tableLayout.AddCell(new PdfPCell(new Phrase(emp.Quantity.ToString(), new Font(Font.FontFamily.HELVETICA, 8, 1, iTextSharp.text.BaseColor.BLACK)))
            //{
            //    HorizontalAlignment = Element.ALIGN_LEFT,
            //    Padding = 5,
            //    BackgroundColor = new iTextSharp.text.BaseColor(255, 255, 255)
            //});
            //tableLayout.AddCell(new PdfPCell(new Phrase(emp.Product.SellPrice.ToString(), new Font(Font.FontFamily.HELVETICA, 8, 1, iTextSharp.text.BaseColor.BLACK)))
            //{
            //    HorizontalAlignment = Element.ALIGN_LEFT,
            //    Padding = 5,
            //    BackgroundColor = new iTextSharp.text.BaseColor(255, 255, 255)
            //});
            //  AddCellToBody(tableLayout, emp.SubTotal.ToString());

        }

        tableLayout.AddCell(new PdfPCell(new Phrase(" Sub Total........:  " + salesDetails2.Sales.SubTotal, new Font(Font.FontFamily.HELVETICA, 8, 1, new iTextSharp.text.BaseColor(0, 0, 0))))
        {
            Colspan = 12,
            Border = 0,
            PaddingBottom = 5,
            HorizontalAlignment = Element.ALIGN_LEFT
        });
        tableLayout.AddCell(new PdfPCell(new Phrase(" Descuentos.......:  " + salesDetails2.Sales.TotalDiscount, new Font(Font.FontFamily.HELVETICA, 8, 1, new iTextSharp.text.BaseColor(0, 0, 0))))
        {
            Colspan = 12,
            Border = 0,
            PaddingBottom = 5,
            HorizontalAlignment = Element.ALIGN_LEFT
        });
        tableLayout.AddCell(new PdfPCell(new Phrase(" Cargos...........:  " + salesDetails2.Sales.TotalCharges, new Font(Font.FontFamily.HELVETICA, 8, 1, new iTextSharp.text.BaseColor(0, 0, 0))))
        {
            Colspan = 12,
            Border = 0,
            PaddingBottom = 5,
            HorizontalAlignment = Element.ALIGN_LEFT
        });
        tableLayout.AddCell(new PdfPCell(new Phrase(" Total a Pagar....:  " + salesDetails2.Sales.Total, new Font(Font.FontFamily.HELVETICA, 8, 1, new iTextSharp.text.BaseColor(0, 0, 0))))
        {
            Colspan = 12,
            Border = 0,
            PaddingBottom = 5,
            HorizontalAlignment = Element.ALIGN_LEFT
        });
        tableLayout.AddCell(new PdfPCell(new Phrase(" Monto Pagado.....:  " + salesDetails2.Sales.PaidAmount, new Font(Font.FontFamily.HELVETICA, 8, 1, new iTextSharp.text.BaseColor(0, 0, 0))))
        {
            Colspan = 12,
            Border = 0,
            PaddingBottom = 5,
            HorizontalAlignment = Element.ALIGN_LEFT
        });
        tableLayout.AddCell(new PdfPCell(new Phrase(" Monto Adeudado...:  " + salesDetails2.Sales.DebtAmount, new Font(Font.FontFamily.HELVETICA, 8, 1, new iTextSharp.text.BaseColor(0, 0, 0))))
        {
            Colspan = 12,
            Border = 0,
            PaddingBottom = 5,
            HorizontalAlignment = Element.ALIGN_LEFT
        });
        tableLayout.AddCell(new PdfPCell(new Phrase(report.Footer1, new Font(Font.FontFamily.HELVETICA, 8, 1, new iTextSharp.text.BaseColor(0, 0, 0))))
        {
            Colspan = 12,
            Border = 0,
            PaddingBottom = 5,
            HorizontalAlignment = Element.ALIGN_CENTER
        });
        tableLayout.AddCell(new PdfPCell(new Phrase(report.Footer2, new Font(Font.FontFamily.HELVETICA, 8, 1, new iTextSharp.text.BaseColor(0, 0, 0))))
        {
            Colspan = 12,
            Border = 0,
            PaddingBottom = 5,
            HorizontalAlignment = Element.ALIGN_CENTER
        });
        tableLayout.AddCell(new PdfPCell(new Phrase(report.Footer3, new Font(Font.FontFamily.HELVETICA, 8, 1, new iTextSharp.text.BaseColor(0, 0, 0))))
        {
            Colspan = 12,
            Border = 0,
            PaddingBottom = 5,
            HorizontalAlignment = Element.ALIGN_CENTER
        });
        return tableLayout;
    }
    // Method to add single cell to the Header  
    private static void AddCellToHeader(PdfPTable tableLayout, string cellText)
    {

        tableLayout.AddCell(new PdfPCell(new Phrase(cellText, new Font(Font.FontFamily.HELVETICA, 8, 1, iTextSharp.text.BaseColor.YELLOW)))
        {
            HorizontalAlignment = Element.ALIGN_LEFT,
            Padding = 5,
            BackgroundColor = new iTextSharp.text.BaseColor(128, 0, 0)
        });
    }

    // Method to add single cell to the body  
    private static void AddCellToBody(PdfPTable tableLayout, string cellText)
    {
        tableLayout.AddCell(new PdfPCell(new Phrase(cellText, new Font(Font.FontFamily.HELVETICA, 8, 1, iTextSharp.text.BaseColor.BLACK)))
        {
            HorizontalAlignment = Element.ALIGN_LEFT,
            Padding = 5,
            BackgroundColor = new iTextSharp.text.BaseColor(255, 255, 255)
        });
    }

我在官方文档中没有看到任何相关内容,所以,如果有人可以提供帮助。

这是我的其余代码,如果它适用于某人:

Answer

0 个答案:

没有答案