【问题标题】:ItextSharp 401 error (Image issue)ItextSharp 401 错误(图像问题)
【发布时间】:2012-06-11 10:45:56
【问题描述】:

我在 MVC3 中使用 iTextSharp 时遇到了一些问题。

我正在创建一个带有一些图像的页眉/页脚,它在本地运行良好,但是当我将它上传到我的服务器时,它返回一个 401:

远程服务器返回错误:(401) Unauthorized。 说明:执行当前 Web 请求期间发生未处理的异常。 请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

异常详情:System.Net.WebException:远程服务器返回错误:(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);

    }

我几乎可以肯定它与图像有关,但我无法弄清楚它应该是什么。

任何帮助将不胜感激:)

【问题讨论】:

  • 您可以直接在浏览器中访问图片而不会出现 401 错误吗?
  • 是的,我可以。我也尝试过不使用动态变量(徽标和电话),但加载 PDF 时仍然得到 401。
  • 您的意思是 下载 PDF 时引发 401 错误,而不是 创建 PDF 时? PDF 文件是否在服务器上正确创建?是否在创建文件的文件夹上正确设置了可见性权限?
  • 抱歉,我的意思是在创建 PDF 时。我没有设置服务器的权限,我的同事做了,但他向我保证设置正确(我已经回到他那里仔细检查)。我只是想知道我是否对我的代码做了任何明显愚蠢的事情。

标签: asp.net asp.net-mvc-3 itextsharp http-status-code-401


【解决方案1】:

假设您的图像位于 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"));

【讨论】:

  • 感谢您的回复。我试过了,不幸的是遇到了同样的问题。
猜你喜欢
  • 2014-02-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多