Using itextsharp and MVC to display a range of PDF pages on a web page

I used this site to copy examples and ask different people for help, so I decided to share my attempt at putting things together with others who might be interested.

Below is a series of pages from an existing PDF file and displays the result in an iframe or new tab. It uses [itextsharp] [1]

The code contains a lot of new things, but at least it works. There is absolutely no point in asking me any questions, because I almost certainly don't know the answer.

If anyone would like to point out what improvements can be made, I would be very grateful.

VIEW

<input id="btnIframe" type="button" value="Iframe" />
<input id="btnNewTab" type="button" value="New Tab" />

<div id="pdfDiv"></div>

<script type="text/javascript">
  $(function () {

    $("#btnIframe").click(function () {
      var filename = "Test1";
      var startPage = 1;
      var endPage = 3;

      var pParams = filename + "¬" + startPage + "¬" + endPage;
      var url = '/PDFTest/GetPdfPages/' + pParams;
      var html = "<iframe src=" + url + " style='width: 100%; height: 400px' ></iframe>";

      $('#pdfDiv').html(html);
    });

    $("#btnNewTab").click(function () {
      var filename = "Test1";
      var startPage = 1;
      var endPage = 3;

      var pParams = filename + "¬" + startPage + "¬" + endPage;
      var url = '/PDFTest/GetPdfPages/' + pParams;

      window.open(url, "_blank");
    });

  });
</script>

      

CONTROLLER

public FileStreamResult GetPdfPages(string id)
{
  var pParams = id.Split('¬');

  var fileName = pParams[0];
  var start = Convert.ToInt32(pParams[1]);
  var end = Convert.ToInt32(pParams[2]);

  var inputFile = Server.MapPath(@"~/PDFFiles/" + fileName + ".pdf");

  var inputPdf = new PdfReader(inputFile);

  int pageCount = inputPdf.NumberOfPages;

  if (end < start || end > pageCount)
  {
    end = pageCount;
  }

  var inputDoc =
    new Document(inputPdf.GetPageSizeWithRotation(1));

  using (MemoryStream ms = new MemoryStream())
  {

    var outputWriter = PdfWriter.GetInstance(inputDoc, ms);
    inputDoc.Open();
    var cb1 = outputWriter.DirectContent;

    for (var i = start; i <= end; i++)
    {
      inputDoc.SetPageSize(inputPdf.GetPageSizeWithRotation(i));
      inputDoc.NewPage();

      var page =
        outputWriter.GetImportedPage(inputPdf, i);
      int rotation = inputPdf.GetPageRotation(i);

      if (rotation == 90 || rotation == 270)
      {
        cb1.AddTemplate(page, 0, -1f, 1f, 0, 0,
                        inputPdf.GetPageSizeWithRotation(i).Height);
      }
      else
      {
        cb1.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
      }
    }

    inputDoc.Close();

    Response.ContentType = "application/pdf";
    Response.AddHeader("Content-Disposition", "inline;test.pdf");
    Response.Buffer = true;
    Response.Clear();
    Response.OutputStream.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length);
    Response.OutputStream.Flush();
    Response.End();

    return new FileStreamResult(Response.OutputStream, "application/pdf");
  }

}

      

+1


source to share





All Articles