0

I'm having some trouble using iTextSharp in MVC3.

I am creating a header/footer with some images and it works fine locally but when i upload it to my server it returns a 401:

The remote server returned an error: (401) Unauthorized. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Net.WebException: The remote server returned an error: (401) Unauthorized.

My header/footer code:

public override void OnEndPage(iTextSharp.text.pdf.PdfWriter writer, iTextSharp.text.Document document)
    {

        //Include the header image
        //System.Web.HttpContext.Current.Server.MapPath("/image.jpg");

        iTextSharp.text.Image HeadImg = iTextSharp.text.Image.GetInstance(HttpContext.Current.Server.MapPath("/image.jpg"));
        HeadImg.ScalePercent(50f);
        HeadImg.SetAbsolutePosition(document.PageSize.Width - 60f - 72f, document.PageSize.Height - 36f - 216.6f);

        //Include the footer image/text
        iTextSharp.text.Image FootImgMS = iTextSharp.text.Image.GetInstance(HttpContext.Current.Server.MapPath("/image.jpg"));
        FootImgMS.ScalePercent(50f);

        iTextSharp.text.Image FootLeftImg = iTextSharp.text.Image.GetInstance(HttpContext.Current.Server.MapPath("/image.jpg"));
        FootLeftImg.ScalePercent(50f);

        Phrase FootText = new Phrase("Phone: " + Tel, FontFactory.GetFont("verdana", 10));

        Paragraph LeftFoot = new Paragraph();
        LeftFoot.Add(new Chunk(FootLeftImg, 0, 0));
        LeftFoot.Add(new Chunk(Chunk.NEWLINE));
        LeftFoot.Add(new Phrase(FootText));

        //Create header table
        PdfPTable PDFTab = new PdfPTable(1);

        //Create footer table
        PdfPTable PDFFootTab = new PdfPTable(2);

        //Create header cell
        PdfPCell PDFCell1 = new PdfPCell(HeadImg);
        PDFCell1.Border = 0;

        //create footer cells
        PdfPCell PDFFootCell1 = new PdfPCell(LeftFoot);
        PDFFootCell1.PaddingTop = 35;
        PdfPCell PDFFootCell2 = new PdfPCell(FootImgMS);
        PDFFootCell1.Border = 0;
        PDFFootCell2.Border = 0;
        PDFFootCell1.HorizontalAlignment = Element.ALIGN_LEFT;
        PDFFootCell2.HorizontalAlignment = Element.ALIGN_RIGHT;

        //Add cells to tables
        PDFTab.AddCell(PDFCell1);
        PDFTab.TotalWidth = document.PageSize.Width;
        PDFFootTab.AddCell(PDFFootCell1);
        PDFFootTab.AddCell(PDFFootCell2);
        PDFFootTab.TotalWidth = document.PageSize.Width - 80;

        //Write out header table
        PDFTab.WriteSelectedRows(0, -1, 0, document.PageSize.Height, writer.DirectContent);

        //Write out footer table
        PDFFootTab.WriteSelectedRows(0, -1, 40, 60, writer.DirectContent);

        //set pdfContent value
        pdfContent = writer.DirectContent;

        pdfContent.MoveTo(30, document.PageSize.Height - 145);
        pdfContent.LineTo(document.PageSize.Width - 40, document.PageSize.Height - 145);

    }

I'm almost certain it has something to do with the images but I just can't work out what it should be.

Any help would be apreciated :)

Alexis Pigeon
  • 7,423
  • 11
  • 39
  • 44
Lex Eichner
  • 1,056
  • 3
  • 10
  • 35
  • Can you access the image directly in a browser without hitting the 401 error? – Alexis Pigeon Jun 11 '12 at 10:57
  • Yes i can. I also tried it without the dynamic variables (Logo and Tel) but I still get the 401 when loading the PDF. – Lex Eichner Jun 11 '12 at 11:16
  • You mean that the 401 error is raised when **downloading** the PDF, not when **creating** the PDF? Is the PDF file correctly created on the server? Are the visibility permissions set correctly on the folder where the file is created? – Alexis Pigeon Jun 11 '12 at 11:24
  • Sorry, I meant when creating the PDF. I don't set the permissions for the server, my colleague does but he assures me it is set correctly (I have gone back to him to double check). I was just wondering if I had done anything glaringly daft with my code. – Lex Eichner Jun 11 '12 at 11:30

1 Answers1

0

Assuming your image is located at the root of your web application, try setting a tilde in front of your application relative paths :

iTextSharp.text.Image HeadImg = iTextSharp.text.Image.GetInstance(HttpContext.Current.Server.MapPath("/image.jpg"));

becomes

iTextSharp.text.Image HeadImg = iTextSharp.text.Image.GetInstance(HttpContext.Current.Server.MapPath("~/image.jpg"));
mathieu
  • 30,974
  • 4
  • 64
  • 90