2012-06-11 37 views
0

我在使用MVC3中的iTextSharp時遇到了一些麻煩。ItextSharp 401錯誤(圖像問題)

我創造了一些圖像的頁眉/頁腳,它工作在當地罰款,但是當我把它上傳到我的服務器返回一個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.

我的頁眉/頁腳代碼:

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); 

    } 

我幾乎可以肯定它與圖像有關,但我無法弄清楚它應該是什麼。

任何幫助將apreciated :)

+0

你能直接在瀏覽器中訪問圖片而不會遇到401錯誤嗎? –

+0

是的,我可以。我也嘗試過沒有動態變量(Logo和Tel),但是在加載PDF時我還是得到了401。 –

+0

你的意思是**下載** PDF時引發401錯誤,而不是當**創建PDF時引發? PDF文件是否在服務器上正確創建?可見性權限是否在創建文件的文件夾上正確設置? –

回答

0

假設您的圖像位於Web應用程序的根目錄,嘗試在你的應用程序的正面設置了一個波浪線相對路徑:

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

變得

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

感謝您的回覆。我嘗試過,不幸的是得到了同樣的問題。 –