0
   $(document).off("click", "#btnPreviewAndDownload").on("click", "#btnPreviewAndDownload", function (e) {
    var QuestionPaperID = 1;
    var SubjectCode = '@Model.Model.SubjectPapperCode';
   window.location = "@Url.Action("PreviewAndDownloadQuestionPaper", "TeamCreation")?QuestionPaperID=" + QuestionPaperID + "&SubjectPapperCode=" + SubjectCode;
});
  private FileResult CreateQuestionPaperPDF(List<CustomMainQuestionDTO> questionDTOList, byte[] byteDoc, MemoryStream memStream)
    {
        Document doc = new Document(iTextSharp.text.PageSize.A4, 70, 40, 70, 40);
        PdfWriter writer = PdfWriter.GetInstance(doc, memStream);

        Font fontH1 = new Font(Font.FontFamily.HELVETICA, 11, Font.BOLD);

        doc.Open();
        doc.NewPage();


        Paragraph Heading = new Paragraph(questionDTOList[0].SubjectName, fontH4);
        Heading.Alignment = Element.ALIGN_CENTER;
        doc.Add(Heading);


        foreach (var questionDTO in questionDTOList)
        {
            doc.Add(Chunk.NEWLINE);
            if (String.IsNullOrEmpty(questionDTO.QuestionParentQuestionMappingID.ToString()))
            {
                MainQuestion(questionDTO, fontH2, doc, fontH1Bold);
            }
            else
            {
                SubQuestion(questionDTO, fontH2, fontH1, doc);
            }
        }
         doc.Close();
        byteDoc = memStream.GetBuffer();
        string fileName = "QuestionPaper - " + questionDTOList[0].SubjectName + ".pdf";
        return File(byteDoc, System.Net.Mime.MediaTypeNames.Application.Octet, fileName);
    }

i have created and aligned the question paper format using Itextsharp and downloaded it. it is downloading in window but unable to open in new tab? How i can open the generated PDF in new tab?

Ahaliya
  • 57
  • 1
  • 14
  • Probably you looking for this similar question https://stackoverflow.com/questions/2827637/how-can-i-open-a-link-in-a-new-window and https://stackoverflow.com/questions/14950269/jquery-open-in-new-tab-blank –  Oct 12 '17 at 09:01
  • data is not shown to the pdf – Ahaliya Oct 12 '17 at 09:19

2 Answers2

1

You can try using following code to generate your pdf it will display on your browser.

string filenm = "test";
        Document doc = new Document(iTextSharp.text.PageSize.A4, 70, 40, 70, 40);

        var fpath = Server.MapPath("~/PDFFiles/FunctionProspectus/");
        PdfWriter.GetInstance(doc, new FileStream(fpath + filenm, FileMode.Create));

        Font fontH1 = new Font(Font.FontFamily.HELVETICA, 11, Font.BOLD);

        doc.Open();
        doc.NewPage();

        Paragraph Heading = new Paragraph("Test");
        Heading.Alignment = Element.ALIGN_CENTER;
        doc.Add(Heading);

        doc.Add(Chunk.NEWLINE);               

        doc.Close();

        string contentType = "application/pdf";
        return File(Server.MapPath("~/PDFFiles/FunctionProspectus/") + filenm, contentType);

In one of my project i have used this and is working well.

Manish Kumar
  • 184
  • 2
  • 9
  • but i have to create and open the pdf – Ahaliya Oct 12 '17 at 10:05
  • @Ahilya ..the code is creating and opening pdf in the same window. If you want it in a new windows just try to call it from a new window. Try your code with this one and you will get the result. – Manish Kumar Oct 12 '17 at 10:13
0

If I'm reading the question correctly, you are able to create and return the PDF just fine, but it is causing the browser to navigate to the document instead of opening separately. The reason for this would be because you're changing window.location to the address of the action producing the PDF. One way to avoid this and simplify the code would be to use a hyperlink with the "target" attribute set to "_blank" instead of using JavaScript to open it. You could also use window.open instead of setting the location if you wanted to keep using the JavaScript solution. Both of these will either open in a new tab or new browser window depending on browser and user preference settings.

Kyle Burns
  • 1,164
  • 7
  • 16