コード例 #1
0
        /// <summary>
        /// Adds the line message.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="doc">The document.</param>
        public static void Add_Line_Message(string message, Document doc)
        {
            PdfPTable table = new PdfPTable(1);

            table            = new PdfPTable(1);
            table.TotalWidth = 500f;
            table.SetWidths(new float[] { 1f });
            table.SpacingBefore = 20f;

            Phrase phrase = new Phrase();

            phrase.Add(new Chunk(message, fontMessage));
            PdfPCell cell = PdfExport.PhraseCell(phrase, PdfPCell.ALIGN_LEFT);

            cell.VerticalAlignment = PdfPCell.ALIGN_TOP;
            table.AddCell(cell);
            doc.Add(table);
        }
コード例 #2
0
        /// <summary>
        /// Exports the PDF.
        /// </summary>
        /// <param name="headers">The headers.</param>
        /// <param name="dataTable">The data table.</param>
        /// <param name="pathImage">The path image.</param>
        /// <param name="CurrentLanguage">The current language.</param>
        /// <param name="fontPath">The font path.</param>
        /// <returns>System.Byte[].</returns>
        public static byte [] exportPDF(List <string> headers, List <List <string> > dataTable,
                                        string pathImage, string CurrentLanguage, string fontPath)
        {
            //start the pdf
            MemoryStream  workStream = new MemoryStream();
            StringBuilder status     = new StringBuilder("");

            //file name to be created

            Document doc = new Document();

            doc.SetMargins(0f, 0f, 0f, 0f);

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

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

            //Add Image to PDF
            iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(pathImage);
            PdfExport.Add_Image_To_PDF(image, doc);
            PdfExport.Add_Line_Message(
                string.Format(
                    ResourceServices.GetString(CurrentLanguage, "Export", "ExportDate"),
                    DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss")), doc);

            // Add Content to PDF
            //Create PDF Table with the count of header columns

            PdfPTable table = new PdfPTable(headers.Count);

            table = new PdfPTable(headers.Count);
            table.HorizontalAlignment = Element.ALIGN_LEFT;
            //table.SetWidths(new float[] { 0.3f, 1f });
            table.SpacingBefore = 20f;
            table.TotalWidth    = 400f;

            float[] headersWidth = new float[headers.Count];// { 20, 20, 20, 20, 20, 20, 20 };  //Header Widths
            for (int i = 0; i < headers.Count; i++)
            {
                headersWidth[i] = 20;
            }
            table.SetWidths(headersWidth);     //Set the pdf headers

            table.WidthPercentage = 100;       //Set the PDF File witdh percentage
            table.HeaderRows      = 1;

            if (CurrentLanguage.Equals("ar"))
            {
                // for arabic language:
                table.DefaultCell.NoWrap = false;
                table.RunDirection       = PdfWriter.RUN_DIRECTION_RTL;
            }
            BaseFont bf = BaseFont.CreateFont(fontPath, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);

            ////Add header
            for (int i = 0; i < headers.Count; i++)
            {
                PdfExport.AddCellToHeader(table, headers[i].ToString(), bf);// );
            }

            //Add body
            for (int i = 0; i < dataTable.Count; i++)
            {
                for (int j = 0; j < dataTable[i].Count; j++)
                {
                    PdfExport.AddCellToBody(table, dataTable[i][j], bf);
                }
            }

            doc.Add(table);

            // Closing the document
            doc.Close();

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

            return(workStream.ToArray());
            //return File(workStream.ToArray(), "application/pdf", strPDFFileName);
        }