How to extend SpringAir to support E-ticket Generation?

The steps below assumes that you have good knowledge of developing web applications using Microsoft Visual Studio and C#.NET. If you are totally new to these things you can learn ASP.NET and download relevant material from http://www.asp.net

Pre-requisites

  1. Make sure you have already installed Visual Studio 2010 or 2012
  2. Download the latest source code for Spring.NET from https://github.com/spring-projects/spring-net/
  3. Download latest Dlls of Aspose.Words and Aspose.BarCode for .NET from http://www.aspose.com/community/files/default.aspx

SpringAir E-ticket Generation

Please follow the following steps to extend SpringAir with Aspose.Words and Aspose.BarCode to generate e-ticket

  1. Unzip the downloaded Source code of Spring.NET and open “SpringAir.2010.sln” located in examples\Spring\SpringAir folder.
  2. Build the project to make sure there are no build errors. If you find any missing dll reference errors you can refer them from “src\Spring” folder.
  3. Set ‘SpringAir.Web.2010’ as Startup Project and press F5 to run SpringAir project in a browser.
  4. By default tracing is enabled in ‘SpringAir.Web.2010’ project. If you don’t want to use it you can turn it off in Web.config by setting
    
    
    <trace enabled="false" />
    
        
  5. You can explore SpringAir by clicking on “book a trip” and following the screens shown afterwards
  6. Add Aspose.Words.dll and Aspose.BarCode.dll as reference to ‘SpringAir.Web.2010’ project.
  7. Now we will add two Buttons/Links on /Web/BookTrip/ReservationConfirmationPage.aspx page so that we can generate e-ticket from the Itinerary shown on the screen.
     
             
        <table cellspacing="0" cellpadding="0" width="90%" align="center" border="0">
            <tr>
                <td class="buttonBar">
                    <br />
                    <asp:Button runat="server" Text="Save as Pdf" ID="SavePdfButton" OnClick="SavePdfButton_Click"></asp:Button>
                    &nbsp;&nbsp;&nbsp;
                    <asp:Button runat="server" Text="Save as Word" ID="SaveWordButton" OnClick="SaveWordButton_Click"></asp:Button>
                </td>
            </tr>
        </table>
    
        
  8. Add the following to the code-behind in using section
    
    using Aspose.Words;
    using Aspose.Words.Drawing;
    using Aspose.Words.Tables;
    using Aspose.BarCode;
    
        
    Add the following methods to support E-ticket generation
    
    protected void SavePdfButton_Click(object sender, EventArgs e)
        {
            GenerateOutPutDocument(confirmation.ConfirmationNumber + ".pdf");
        }
    
        protected void SaveWordButton_Click(object sender, EventArgs e)
        {
            GenerateOutPutDocument(confirmation.ConfirmationNumber + ".docx");
        }
    
        protected void GenerateOutPutDocument(string outFileName)
        {
            //Open the template document
            Document doc = new Document(Server.MapPath("~/App_Data/DocumentBuilderDemo.docx"));
    
            //Once the builder is created, its cursor is positioned at the beginning of the document.
            DocumentBuilder builder = new DocumentBuilder(doc);
    
            builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
            BarCodeBuilder barCode = CreateBarCode();
            builder.InsertImage(barCode.BarCodeImage);
    
            builder.MoveToDocumentStart();
    
            System.Drawing.Image image = System.Drawing.Image.FromFile(Server.MapPath("~/Web/Images/spring-air-logo-header.jpg"));
            builder.InsertImage(image);
    
            builder.InsertParagraph();
            builder.ParagraphFormat.ClearFormatting();
            builder.Font.ClearFormatting();
    
            builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
            builder.ParagraphFormat.Shading.ForegroundPatternColor = System.Drawing.Color.White;
            builder.ParagraphFormat.Shading.Texture = TextureIndex.TextureSolid;
            builder.ParagraphFormat.LeftIndent = ConvertUtil.InchToPoint(0.3);
            builder.ParagraphFormat.SpaceBefore = 12;
            builder.ParagraphFormat.SpaceAfter = 12;
    
            builder.Font.Name = "Arial";
            builder.Font.Size = 9;
            builder.Write("ELECTRONIC TICKET - PASSENGER ITINERARY/RECEIPT");
            builder.InsertBreak(BreakType.LineBreak);
            builder.Writeln("CUSTOMER COPY - Powered by ASPOSE");
            builder.ParagraphFormat.ClearFormatting();
    
            builder.InsertHtml("<hr>");
    
            BuildBookingTable(builder);
    
            builder.MoveToDocumentEnd();
            builder.InsertBreak(BreakType.LineBreak);
    
            builder.InsertHtml("<hr>");
    
            builder.InsertParagraph();
            builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
            builder.InsertImage(barCode.BarCodeImage);
    
    
            doc.Save(Response, outFileName, ContentDisposition.Inline, null);
            Response.End();
        }
    
        public void BuildBookingTable(DocumentBuilder builder)
        {
            //Reset to default font and paragraph formatting.
            builder.Font.ClearFormatting();
            builder.ParagraphFormat.ClearFormatting();
    
            builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
            builder.InsertParagraph();
            builder.Writeln(confirmationLabel.Text);
    
            builder.Font.Color = System.Drawing.Color.Black;
            builder.Font.Size = 10;
            builder.Font.Name = "Tahoma";
    
            builder.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
    
            BorderCollection borders = builder.CellFormat.Borders;
            borders.LineStyle = LineStyle.Single;
            borders.Color = System.Drawing.Color.Black;
    
            builder.StartTable();
            builder.Font.Bold = true;
            builder.InsertCell();
    
            builder.Write(GetMessage("flightNumber"));
            builder.CellFormat.Shading.BackgroundPatternColor = System.Drawing.Color.LightGray;
    
            builder.InsertCell();
            builder.Write(GetMessage("departureDate"));
            builder.CellFormat.Shading.BackgroundPatternColor = System.Drawing.Color.LightGray;
    
            builder.InsertCell();
            builder.Write(GetMessage("departureAirport"));
            builder.CellFormat.Shading.BackgroundPatternColor = System.Drawing.Color.LightGray;
    
            builder.InsertCell();
            builder.Write(GetMessage("destinationAirport"));
            builder.CellFormat.Shading.BackgroundPatternColor = System.Drawing.Color.LightGray;
    
            builder.InsertCell();
            builder.Write(GetMessage("aircraft"));
            builder.CellFormat.Shading.BackgroundPatternColor = System.Drawing.Color.LightGray;
            builder.EndRow();
    
            builder.CellFormat.ClearFormatting();
            builder.Font.ClearFormatting();
    
            foreach (Flight flight in confirmation.Reservation.Itinerary.Flights)
            {
                builder.InsertCell();
                builder.Write(flight.FlightNumber);
    
                builder.InsertCell();
                builder.Write(flight.DepartureTime.ToString());
    
                builder.InsertCell();
                builder.Write(flight.DepartureAirport.Description);
    
                builder.InsertCell();
                builder.Write(flight.DestinationAirport.Description);
    
                builder.InsertCell();
                builder.Write(flight.Aircraft.Model);
                builder.EndRow();
            }
    
            builder.EndTable();
        }
    
        private BarCodeBuilder CreateBarCode()
        {
            // Instantiate a LinearBarCode object
            BarCodeBuilder barCode = new BarCodeBuilder();
            barCode.SymbologyType = Symbology.Code128;
            barCode.CodeText = confirmation.ConfirmationNumber;
            barCode.CodeLocation = CodeLocation.Below;
            barCode.ImageQuality = ImageQualityMode.Default;
    
            return barCode;
        }
            

Last edited Feb 28, 2014 at 1:01 PM by asposemarketplace, version 11