C# iTextSharp stream PDF to browser and control zoom in IE

邮差的信 提交于 2019-12-11 12:19:21

问题


I am having trouble streaming my PDF to the browser and displaying the correct size. It actually works fine in Firefox, Chrome, Safari but for some reason IE wants to set my zoom level to 662%. If I manually set it to "Actual Fit" it looks like it does in all the other browsers. IE is setting to "Fit Width" I am not sure if I am missing something and that is why IE is doing it's own thing. My code is as follows:

using (MemoryStream memoryStream = new MemoryStream())
{
    Document oDocument = new Document(PageSize.LETTER, 0f, 0f, 0f, 0f);

    PdfWriter oWriter = PdfWriter.GetInstance(oDocument, memoryStream);
    Image oImage = Image.GetInstance(GetBadge(context.Request["badge"]));

    float width = oImage.Width / 4f;
    float height = oImage.Height / 4f;

    oImage.ScaleToFit(width,height);
    oDocument.SetPageSize(new Rectangle(width, height));

    oDocument.Open();
    oDocument.Add(oImage);
    oDocument.Close();

    byte[] bytes = memoryStream.ToArray();
    memoryStream.Close();

    context.Response.Buffer = true;
    context.Response.Clear();
    context.Response.ClearContent();
    context.Response.ClearHeaders();
    context.Response.ContentType = "application/pdf";
    context.Response.AddHeader("Content-Disposition", string.Format("inline; filename=badge_{0}", DateTime.Now.ToShortDateString()));
    context.Response.AddHeader("Content-Length", bytes.Length.ToString());
    context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
    context.Response.BinaryWrite(bytes);
    context.Response.Flush();
}

The GetBadge function on Image is just getting a varbinary field from the database.

来源:https://stackoverflow.com/questions/30623068/c-sharp-itextsharp-stream-pdf-to-browser-and-control-zoom-in-ie

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!