private Phrase ComposePhrase(String text, BaseFont ufont, BaseColor color, float fontSize)
        {
            Phrase phrase = null;

            if (extensionFont == null && (substitutionFonts == null || substitutionFonts.Count == 0))
            {
                phrase = new Phrase(new Chunk(text, new Font(ufont, fontSize, 0, color)));
            }
            else
            {
                FontSelector fs = new FontSelector();
                fs.AddFont(new Font(ufont, fontSize, 0, color));
                if (extensionFont != null)
                {
                    fs.AddFont(new Font(extensionFont, fontSize, 0, color));
                }
                if (substitutionFonts != null)
                {
                    foreach (BaseFont bf in substitutionFonts)
                    {
                        fs.AddFont(new Font(bf, fontSize, 0, color));
                    }
                }
                phrase = fs.Process(text);
            }
            return(phrase);
        }
// ---------------------------------------------------------------------------
    public void Write(Stream stream) {
      // step 1
      using (Document document = new Document(PageSize.A4)) {
        // step 2
        PdfWriter.GetInstance(document, stream);
        // step 3
        document.Open();
        // step 4
        FontSelector selector = new FontSelector();
        selector.AddFont(FontFactory.GetFont(FontFactory.TIMES_ROMAN, 12));
        selector.AddFont(FontFactory.GetFont(
          "MSung-Light", "UniCNS-UCS2-H", BaseFont.NOT_EMBEDDED
        ));
        Phrase ph = selector.Process(TEXT);
        document.Add(new Paragraph(ph));
      }
    }
Beispiel #3
0
// --------------------------------------------------------------------------- 
    public void Write(Stream stream) {
      using (ZipFile zip = new ZipFile()) {
        zip.AddFile(RESOURCE, "");       
        using (MemoryStream ms = new MemoryStream()) {
          using (Document document = new Document(PageSize.A4.Rotate())) {
            // step 2
            PdfWriter.GetInstance(document, ms);
            // step 3
            document.Open();
            // step 4
            fs = new FontSelector();
            for (int i = 0; i < FONTS.Length; i++) {
              fs.AddFont(FontFactory.GetFont(
                FONTS[i][0], FONTS[i][1], BaseFont.EMBEDDED
              ));
            }
            table = new PdfPTable(3);
            table.DefaultCell.Padding = 3;
            table.DefaultCell.UseAscender = true;
            table.DefaultCell.UseDescender = true;
            
            using (XmlReader xr = XmlReader.Create(RESOURCE)) {
              xr.MoveToContent();
              while (xr.Read()) {
                switch (xr.NodeType) {
                  case XmlNodeType.Element:
                    StartElement(xr);
                    break;
                  case XmlNodeType.Text:
                    buf.Append(xr.Value.Trim());
                    break;
                  case XmlNodeType.EndElement:
                    EndElement(xr.Name);
                    break;
                }
              }
            }        
            document.Add(table); 
          }
          zip.AddEntry(RESULT, ms.ToArray());
        }
        zip.Save(stream);             
      }
    }
 private FontSelector getFontSelector()
 {
     var fontSelector = new FontSelector();
     foreach (var font in Fonts)
     {
         fontSelector.AddFont(font);
     }
     return fontSelector;
 }
    protected void ExportToPDF(Recordset Recordset, bool LandScape, string StationID)
    {
        try { 

        DataTable MasterCouponTable = new DataTable("MasterCoupon");
        MasterCouponTable.Columns.Add("日期 / 時間", typeof(String));
        MasterCouponTable.Columns.Add("更期", typeof(String));
        MasterCouponTable.Columns.Add("禮券編號", typeof(String));
        MasterCouponTable.Columns.Add("類型", typeof(String));
        MasterCouponTable.Columns.Add("產品", typeof(String));
        MasterCouponTable.Columns.Add("升數", typeof(String));
        MasterCouponTable.Columns.Add("銀碼", typeof(String));
        MasterCouponTable.Columns.Add("車牌", typeof(String));
        while (!(Recordset.Eof))
        {

            //MasterCouponTable.Rows.Add(Recordset.Fields["Present_Date"].Value);
            //MasterCouponTable.Rows.Add(Recordset.Fields["Period"].Value);
            DataRow trow = MasterCouponTable.NewRow();
            trow[0] = Recordset.Fields["Present_Date"].Value;
          //  trow[1] = Recordset.Fields["Period"].Value;
            if (Convert.ToInt32(Recordset.Fields["Period"].Value) == 11)
            {
               // Response.Write("早");
                trow[1] = "早";
            }
            else if (Convert.ToInt32(Recordset.Fields["Period"].Value) == 12)
            {
               // Response.Write("中");
                trow[1] = "中";
            }
            else if (Convert.ToInt32(Recordset.Fields["Period"].Value) == 13)
            {
                //Response.Write("晚");
                trow[1] = "晚";
            }

            trow[2] = Recordset.Fields["Coupon_Number"].Value;
            trow[3] = Recordset.Fields["Coupon_type"].Value;
            trow[4] = Recordset.Fields["Product_Type"].Value;
            trow[5] = String.Format("{0:0.00}", Recordset.Fields["SaleLitre"].Value);
            trow[6] = Recordset.Fields["SaleAmount"].Value;
            trow[7] = Recordset.Fields["Car_ID"].Value;
           

            MasterCouponTable.Rows.Add(trow);
            Recordset.MoveNext();
        }

        GridView gvReport = new GridView();

        gvReport.DataSource = MasterCouponTable;
        gvReport.DataBind();
      

        int noOfColumns = 0, noOfRows = 0;
        DataTable tbl = new DataTable();



        
        tbl = gvReport.DataSource as DataTable;
        noOfColumns = tbl.Columns.Count;
        noOfRows = gvReport.Rows.Count;
        

        float HeaderTextSize = 8;
        float ReportNameSize = 10;
        float ReportTextSize = 8;
        float ApplicationNameSize = 7;

        // Creates a PDF document
        Document document = null;
        FontSelector selector = new FontSelector();
        selector.AddFont(FontFactory.GetFont(FontFactory.TIMES_ROMAN, 12));
        selector.AddFont(FontFactory.GetFont("MSung-Light", "UniCNS-UCS2-H", BaseFont.NOT_EMBEDDED));

        string fontpath =
        System.Web.HttpContext.Current.Request.PhysicalApplicationPath +
        "include\\fonts\\SimSun.ttf";
        //"simsun.ttf" file was downloaded from web and placed in the folder
        BaseFont bf = BaseFont.CreateFont(fontpath,
                         BaseFont.IDENTITY_H, BaseFont.EMBEDDED);

        //create new font based on BaseFont
        Font fontContent = new Font(bf, 11);
        Font fontHeader = new Font(bf, 12);


        Font simsunBold = new Font(bf, 14, Font.BOLD, Color.BLACK);
        Font simsunNormal = new Font(bf, 11, Font.NORMAL, Color.BLACK);
      
        if (LandScape == true)
        {
            // Sets the document to A4 size and rotates it so that the orientation of the page is Landscape.
            document = new Document(PageSize.A4.Rotate(), 0, 0, 15, 5);
        }
        else
        {
            document = new Document(PageSize.A4, 0, 0, 15, 5);
        }

        // Creates a PdfPTable with column count of the table equal to no of columns of the gridview or gridview datasource.
        iTextSharp.text.pdf.PdfPTable mainTable = new iTextSharp.text.pdf.PdfPTable(noOfColumns);

        // Sets the first 4 rows of the table as the header rows which will be repeated in all the pages.
        mainTable.HeaderRows = 4;

        // Creates a PdfPTable with 2 columns to hold the header in the exported PDF.
        iTextSharp.text.pdf.PdfPTable headerTable = new iTextSharp.text.pdf.PdfPTable(2);

        // Creates a phrase to hold the application name at the left hand side of the header.
        Phrase phApplicationName =  new Phrase(DateTime.Now.Date.ToString("dd/MM/yyyy"), FontFactory.GetFont("Arial", ApplicationNameSize, iTextSharp.text.Font.NORMAL)); 

        // Creates a PdfPCell which accepts a phrase as a parameter.
        PdfPCell clApplicationName = new PdfPCell(phApplicationName);
        // Sets the border of the cell to zero.
        clApplicationName.Border = PdfPCell.NO_BORDER;
        // Sets the Horizontal Alignment of the PdfPCell to left.
        clApplicationName.HorizontalAlignment = Element.ALIGN_LEFT;

        // Creates a phrase to show the current date at the right hand side of the header.
        Phrase phDate = new Phrase("油站 : " + StationID, simsunNormal);

        // Creates a PdfPCell which accepts the date phrase as a parameter.
        PdfPCell clDate = new PdfPCell(phDate);
        // Sets the Horizontal Alignment of the PdfPCell to right.
        clDate.HorizontalAlignment = Element.ALIGN_RIGHT;
        // Sets the border of the cell to zero.
        clDate.Border = PdfPCell.NO_BORDER;

        // Adds the cell which holds the application name to the headerTable.
        headerTable.AddCell(clApplicationName);
        // Adds the cell which holds the date to the headerTable.
        headerTable.AddCell(clDate);
        // Sets the border of the headerTable to zero.
        headerTable.DefaultCell.Border = PdfPCell.NO_BORDER;

        // Creates a PdfPCell that accepts the headerTable as a parameter and then adds that cell to the main PdfPTable.
        PdfPCell cellHeader = new PdfPCell(headerTable);
        cellHeader.Border = PdfPCell.NO_BORDER;
        // Sets the column span of the header cell to noOfColumns.
        cellHeader.Colspan = noOfColumns;
        // Adds the above header cell to the table.
        mainTable.AddCell(cellHeader);

        // Creates a phrase which holds the file name.
        Phrase phHeader = new Phrase("禮券驗證系統",simsunNormal);
        PdfPCell clHeader = new PdfPCell(phHeader);
        clHeader.Colspan = noOfColumns;
        clHeader.Border = PdfPCell.NO_BORDER;
        clHeader.HorizontalAlignment = Element.ALIGN_CENTER;
        mainTable.AddCell(clHeader);

        // Creates a phrase for a new line.
        Phrase phSpace = new Phrase("\n");
        PdfPCell clSpace = new PdfPCell(phSpace);
        clSpace.Border = PdfPCell.NO_BORDER;
        clSpace.Colspan = noOfColumns;
        mainTable.AddCell(clSpace);
        PdfPCell hc;
        PdfPCell fc;

        // Sets the gridview column names as table headers.
        for (int i = 0; i < noOfColumns; i++)
        {
            Phrase ph = null;
            

            if (gvReport.AutoGenerateColumns)
            {
                ph = new Phrase(tbl.Columns[i].ColumnName, simsunNormal);
            }
            else
            {
                ph = new Phrase(gvReport.Columns[i].HeaderText, simsunNormal);
                  
            }

            hc = new PdfPCell(new Phrase(ph));
            hc.Top = 5f;
          
            hc.FixedHeight = 20f;
            hc.HorizontalAlignment =1 ;
            hc.BackgroundColor = iTextSharp.text.Color.LIGHT_GRAY;// new BaseColor(221,221,221,1);  

           // BaseColor objBaseColor = new BaseColor(System.Drawing.ColorTranslator.FromHtml("#008000"));
            
            


            mainTable.AddCell(hc);
        }

        // Reads the gridview rows and adds them to the mainTable
        for (int rowNo = 0; rowNo < noOfRows; rowNo++)
        {
            for (int columnNo = 0; columnNo < noOfColumns; columnNo++)
            {
                if (gvReport.AutoGenerateColumns)
                {
                    string s = gvReport.Rows[rowNo].Cells[columnNo].Text.Trim();
                    Phrase ph = new Phrase(s, simsunNormal);
                    hc = new PdfPCell(new Phrase(ph));
                    hc.Left =2f;
                    mainTable.AddCell(hc);
                }
                else
                {
                    if (gvReport.Columns[columnNo] is TemplateField)
                    {
                        DataBoundLiteralControl lc = gvReport.Rows[rowNo].Cells[columnNo].Controls[0] as DataBoundLiteralControl;
                        string s = lc.Text.Trim();
                        Phrase ph = new Phrase(s, simsunNormal);
                        hc = new PdfPCell(new Phrase(ph));
                        hc.Top = 0f;
                        hc.FixedHeight = 7f;
                        hc.HorizontalAlignment = 1;
                        hc.BackgroundColor = iTextSharp.text.Color.LIGHT_GRAY;
                        ((Chunk)(hc.Phrase[0])).Font = new iTextSharp.text.Font(((Chunk)(hc.Phrase[0])).Font.Family, 5f);
                        mainTable.AddCell(hc); 
                       
                    }
                    else
                    {
                        string s = gvReport.Rows[rowNo].Cells[columnNo].Text.Trim();
                        Phrase ph = new Phrase(s, simsunNormal);
                        hc = new PdfPCell(new Phrase(ph));
                        hc.Left = 2f;
                        mainTable.AddCell(hc);
                    }
                }
            }

            // Tells the mainTable to complete the row even if any cell is left incomplete.
            mainTable.CompleteRow();
        }

        // Gets the instance of the document created and writes it to the output stream of the Response object.
        PdfWriter.GetInstance(document, Response.OutputStream);

        // Creates a footer for the PDF document.
        HeaderFooter pdfFooter = new HeaderFooter(new Phrase(), true);
        pdfFooter.Alignment = Element.ALIGN_CENTER;
        pdfFooter.Border = iTextSharp.text.Rectangle.NO_BORDER;

        // Sets the document footer to pdfFooter.
        document.Footer = pdfFooter;
        // Opens the document.
        document.Open();
        // Adds the mainTable to the document.
        document.Add(mainTable);
        // Closes the document.
        document.Close();

         
       
            }
        catch(Exception ex)
        { 
            
        }

    }
Beispiel #6
0
 private Phrase ComposePhrase(String text, BaseFont ufont, BaseColor color, float fontSize) {
     Phrase phrase = null;
     if (extensionFont == null && (substitutionFonts == null || substitutionFonts.Count == 0))
         phrase = new Phrase(new Chunk(text, new Font(ufont, fontSize, 0, color)));
     else {
         FontSelector fs = new FontSelector();
         fs.AddFont(new Font(ufont, fontSize, 0, color));
         if (extensionFont != null)
             fs.AddFont(new Font(extensionFont, fontSize, 0, color));
         if (substitutionFonts != null) {
             foreach (BaseFont bf in substitutionFonts) {
                 fs.AddFont(new Font(bf, fontSize, 0, color));
             }
         }
         phrase = fs.Process(text);
     }
     return phrase;
 }
Beispiel #7
0
 private Anchor getAnchor(FontSelector fontSelector, CellAttributes attributes)
 {
     var text = getText(attributes);
     var url = getUrl(attributes);
     var anchor = new Anchor(fontSelector.Process(text.ToSafeString())) { Reference = url };
     return anchor;
 }