3
Vote

Print View

description

Make a printable view of a ticket and it's comments

file attachments

comments

sid351 wrote Apr 2, 2010 at 7:04 PM

If anyone is interested in implementing this feature, I've managed to write one using iTextSharp in version 28536.

Once I've got my head around 41103 I'll upload the two versions here.

sid351 wrote Apr 2, 2010 at 9:09 PM

Here's my code (built for version 28536 (the Version 2 Download)) and a sample of the PDF it prints out.

I've just included the code and the iTextSharp DLL which you will need to include in the BIN directory.

I have commented ALOT of my code so you can understand it as best as possible.

PLEASE NOTE: I am a beginner, therefore it is possible this could be written more effeciently, if you do make improvements, would you mind letting me know what and how to help my experience?

wrote Apr 2, 2010 at 9:09 PM

naspinski wrote Apr 14, 2010 at 12:52 AM

I am just getting into the last 3 weeks of my semester right now, so it's a bit hectic, but as soon as I get some time, I will get to this (along with a bunch of other things I need to get to).

I really appreciate the contributions!

sid351 wrote Apr 14, 2010 at 9:32 AM

I've updated my code to remove the HTML Markup in the details section. Please see "Export to PDF update.txt"

I've not done anything with 41103 yet.

wrote Apr 14, 2010 at 9:32 AM

wrote Feb 21, 2013 at 11:22 PM

wrote Mar 15, 2013 at 7:25 AM

mjvarela wrote Mar 15, 2013 at 7:25 AM

//Updated to include "StringClean" function that removes the HTML markup from the Details in the PDF

//Goes in Ticket.aspx
//I recommend between pnlShowTicket and pnlDisplay
//<asp:Button id="btnExportPdf" runat="server" OnClick="btnExport_Click" Text="Export to PDF"/>

//Namespaces required in addition to defaults:
//using iTextSharp.text;
//using iTextSharp.text.pdf;
//using System.Threading;
//using System.Text.RegularExpressions;

//Goes in Ticket.aspx.cs before the final }


protected void btnExport_Click(object sender, EventArgs e)
{
    //The following code creates a PDF of the Ticket view, with a logo in the top right corner

    //The path for the PDF to be written to
    string strPath = Request.PhysicalApplicationPath.ToString() + "Ticket_" + t.id + ".pdf";
    //The path of the font to be used to create the PDF
    string strFontPath = Request.PhysicalApplicationPath.ToString() + "Fonts\\Calibri.ttf";
    //The path of the Logo to be used in the PDF
    string strLogoPath = Request.PhysicalApplicationPath.ToString() + "Images\\Logo.png";

    //Defining the layout of the sections to be used in the Ticket body
        //It is done this way to simplify the logic of the strTicket definition
    string strTicketDetails = "Details: " + t.details + "\n";
    string strTicketDate = "Date Submitted: " + t.submitted.ToString().Substring(0, 10) + "\n";
    string strTicketPriority = "Priority: " + t.priority1.priority_name + "\n";

    //This strips the HTML out of the Ticket Details
    strTicketDetails = StringClean(strTicketDetails);

    //Defining the Title and Sub-title strings
    string strTitle = "Ticket " + t.id + " - " + t.title;
    string strTitle2 = "Owner: " + t.user.userName.capitalize() + " (" + t.sub_unit1.unit.unit_name + " - " + t.sub_unit1.sub_unit_name + ")" + "\n" + "Status: " + t.statuse.status_name;

    //Defining the Ticket body string
    string strTicket = strTicketDate + strTicketPriority + strTicketDetails;

    //Creation of a document-object
    Document document = new Document();

    try
    {
        //Writer that listens to the document and directs a PDF-stream to a file
        PdfWriter.GetInstance(document, new FileStream(strPath, FileMode.Create));

        //Open the document
        document.Open();

        //Adding fonts
        BaseFont fntBase = BaseFont.CreateFont(strFontPath, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
        Font fntUse = new Font(fntBase, 11, Font.NORMAL);
        Font fntTitle = new Font(fntBase, 16, Font.BOLD);
        Font fntSubTitle = new Font(fntBase, 14, Font.BOLD);
        Font fntBreak = new Font(fntBase, 5, Font.NORMAL);

        //Adding the Logo to the top of the page
        iTextSharp.text.Image imgLogo = iTextSharp.text.Image.GetInstance(strLogoPath);
        imgLogo.Alignment = iTextSharp.text.Image.RIGHT_ALIGN;
        document.Add(imgLogo);

        //Defining the blank paragraph "pBlank"
        Paragraph pBreak = new Paragraph(" ", fntBreak);

        //Adding the title                
        document.Add(new Paragraph(strTitle, fntTitle));
        //Adding the sub-title
        document.Add(new Paragraph(strTitle2, fntSubTitle));
        //Adding a gap between the sub-title and the body text
        document.Add(pBreak);
        //Adding the body
        document.Add(new Paragraph(strTicket, fntUse));
        //Adding a gap
        document.Add(pBreak);

        //Comment building code
        IEnumerable<comment> comments = Tickets.Comments.List(db, t.id);
        foreach (comment c in comments)
        {
            //This changes the Submitted date to a string, and returns the date in dd/mm/yyyy format
            string cDateFull = c.submitted.ToString();
            string cDate = "No Date Given";
            if (cDateFull.Length > 10)
            {
                cDate = cDateFull.Substring(0, 10);
            }

            //Defining the layout of the Comment body section(s)
            string strCommentDetails = c.comment1;
            //This strips the HTML out of the Comment Details
            strCommentDetails = StringClean(strCommentDetails);

            string strComment =
                "Date: " + cDate + "\n" +
                "Priority: " + c.priority.priority_name + "\n" +
                "Details: " + strCommentDetails;

            //Adding the comment to the PDF
            document.Add(new Paragraph(strComment, fntUse));
            //Adding a gap
            document.Add(pBreak);
        }
    }
        //These are included in the iTextSharp Examples
    catch (DocumentException de)
    {
        Console.Error.WriteLine(de.Message);
    }
    catch (IOException ioe)
    {
        Console.Error.WriteLine(ioe.Message);
    }

    //Close the pdf
    document.Close();

    if (File.Exists(strPath))
    {
        //Run a seperate thread that will delete the server copy of the PDF after 5 seconds
        Thread thrWait = new Thread(deleteMe);
        thrWait.Start(strPath);
    }

    //Offer the user a chance to download the PDF
    Response.ClearContent();
    Response.ClearHeaders();
    Response.ContentType = "application/pdf";
    Response.AddHeader
    ("Content-Disposition", "attachment; filename=Ticket_" + t.id + ".pdf");
    Response.TransmitFile(strPath);

}

//This deletes the Sever-side PDF
static void deleteMe(object fileObject)
{
    string filePath = (string)fileObject;
    Thread.Sleep(5000);
    if (File.Exists(filePath))
    {
        File.Delete(filePath);
    }
}

//This is strips the HTML markup from the Ticket & Comment Details
public string StringClean(string text)
{
    char charAmpersand = '&';
    string strAmpersand = charAmpersand.ToString();
    text = text.Replace("&amp;", strAmpersand);
    text = text.Replace("&nbsp;", " ");
    char charQuote = '"';
    string strQuote = charQuote.ToString();
    text = text.Replace("&quot;", strQuote);
    text = Regex.Replace(text, "<.+?>", "");
    text = text.Replace("&lt;", "<");
    text = text.Replace("&gt;", ">");
    text = text.Replace("&amp;", strAmpersand);
    return text;
}[TEXT]([http://www.example.com](http://www.example.com))

naspinski wrote Mar 15, 2013 at 1:59 PM

Added you to the project, feel free to add it in!