SORU
2 EKİM 2009, Cuma


Nasıl PDF MVC tarayıcı dönmek için?

İTextSharp için bu demo kodu var

    Document document = new Document();
    try
    {
        PdfWriter.GetInstance(document, new FileStream("Chap0101.pdf", FileMode.Create));

        document.Open();

        document.Add(new Paragraph("Hello World"));

    }
    catch (DocumentException de)
    {
        Console.Error.WriteLine(de.Message);
    }
    catch (IOException ioe)
    {
        Console.Error.WriteLine(ioe.Message);
    }

    document.Close();

Nasıl tarayıcı için pdf belgeyi geri dönmek için kumanda alabilirim?

DÜZENLEME:

Bu kodu çalıştırmadan aç Acrobat yok ama bir hata mesajı alıyorum "dosya zarar görmüş ve onarılması değil"

  public FileStreamResult pdf()
    {
        MemoryStream m = new MemoryStream();
        Document document = new Document();
        PdfWriter.GetInstance(document, m);
        document.Open();
        document.Add(new Paragraph("Hello World"));
        document.Add(new Paragraph(DateTime.Now.ToString()));
        m.Position = 0;

        return File(m, "application/pdf");
    }

Bu çalışmıyor neden herhangi bir fikir?

CEVAP
2 EKİM 2009, Cuma


FileContentResult bir dönüş. Denetleyicisi eylem son satırda şöyle bir şey olurdu:

return File("Chap0101.pdf", "application/pdf");

Eğer bu dinamik PDF oluşturma, daha iyi MemoryStream, kullanmak ve bellekte belge oluşturma dosya kaydetme yerine olabilir. Kod gibi bir şey olabilir:

Document document = new Document();

MemoryStream stream = new MemoryStream();

try
{
    PdfWriter pdfWriter = PdfWriter.GetInstance(document, stream);
    pdfWriter.CloseStream = false;

    document.Open();
    document.Add(new Paragraph("Hello World"));
}
catch (DocumentException de)
{
    Console.Error.WriteLine(de.Message);
}
catch (IOException ioe)
{
    Console.Error.WriteLine(ioe.Message);
}

document.Close();

stream.Flush(); //Always catches me out
stream.Position = 0; //Not sure if this is required

return File(stream, "application/pdf", "DownloadName.pdf");

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • BiGSH0TROB

    BiGSH0TROB

    7 NİSAN 2011
  • finalcall07

    finalcall07

    11 NİSAN 2008
  • metagamers

    metagamers

    13 Mayıs 2006