1

i'm Beginner in asp.net, i create pdf file with PdfRpt. i write this code in class

namespace PdfReportSamples.CustomPriceNumber
{
    public class CustomPriceNumberPdfReport
    {
        public IPdfReportData CreatePdfReport()
        {
            using (var memoryStream = new MemoryStream())
            {
                var ii= new PdfReport().DocumentPreferences(doc =>
                {
                    doc.RunDirection(PdfRunDirection.LeftToRight);
                    doc.Orientation(PageOrientation.Portrait);
                    doc.PageSize(PdfPageSize.A4);
                    doc.DocumentMetadata(new DocumentMetadata { Author = "Vahid", Application = "PdfRpt", Keywords = "Test", Subject = "Test Rpt", Title = "Test" });
                })
           .DefaultFonts(fonts =>
           {
               fonts.Path(Environment.GetEnvironmentVariable("SystemRoot") + "\\fonts\\arial.ttf",
                                 Environment.GetEnvironmentVariable("SystemRoot") + "\\fonts\\verdana.ttf");
           })
           .PagesFooter(footer =>
           {
               footer.DefaultFooter(DateTime.Now.ToString("MM/dd/yyyy"));
           })
           .PagesHeader(header =>
           {
               header.DefaultHeader(defaultHeader =>
               {
                   defaultHeader.RunDirection(PdfRunDirection.LeftToRight);

               });
           })
           .MainTableTemplate(template =>
           {
               template.BasicTemplate(BasicTemplate.SilverTemplate);
           })
           .MainTablePreferences(table =>
           {
               table.ColumnsWidthsType(TableColumnWidthType.Relative);
           })
           .MainTableDataSource(dataSource =>
           {
               var listOfRows = new List<Transaction>();
               for (int i = 0; i < 200; i++)
               {
                   listOfRows.Add(new Transaction
                   {
                       Product = "Item " + i,
                       Description = "Desc. " + i,
                       SalePrice = 1000 * i
                   });
               }
               dataSource.StronglyTypedList(listOfRows);
           })

           .MainTableColumns(columns =>
           {
               columns.AddColumn(column =>
               {
                   column.PropertyName("rowNo");
                   column.IsRowNumber(true);
                   column.CellsHorizontalAlignment(HorizontalAlignment.Center);
                   column.IsVisible(true);
                   column.Order(0);
                   column.Width(1);
                   column.HeaderCell("#");
               });

               columns.AddColumn(column =>
               {
                   column.PropertyName<Transaction>(x => x.Product);
                   column.CellsHorizontalAlignment(HorizontalAlignment.Center);
                   column.IsVisible(true);
                   column.Order(1);
                   column.Width(2);
                   column.HeaderCell("Product");
               });

               columns.AddColumn(column =>
               {
                   column.PropertyName<Transaction>(x => x.Description);
                   column.CellsHorizontalAlignment(HorizontalAlignment.Center);
                   column.IsVisible(true);
                   column.Order(2);
                   column.Width(3);
                   column.HeaderCell("Description");
               });

               columns.AddColumn(column =>
               {
                   column.PropertyName<Transaction>(x => x.SalePrice);
                   column.CellsHorizontalAlignment(HorizontalAlignment.Center);
                   column.IsVisible(true);
                   column.Order(3);
                   column.Width(3);
                   column.HeaderCell("Sale Price");
                   column.ColumnItemsTemplate(template =>
                   {
                       template.CustomTemplate(new CustomPriceCell());
                   });
                   column.AggregateFunction(aggregateFunction =>
                   {
                       aggregateFunction.NumericAggregateFunction(AggregateFunction.Sum);
                       aggregateFunction.DisplayFormatFormula(obj => obj == null ? string.Empty : string.Format("{0:n0}", obj));
                   });
               });

           })
           .MainTableEvents(events =>
           {
               events.DataSourceIsEmpty(message: "There is no data available to display.");
           })
           .Export(export =>
           {
               export.ToExcel();
           })
           .Generate(data => data.AsPdfStream(memoryStream));


       }


        }
    }
}

i Want when user click in button send this file (into memoryStream ) for download. but i don't know how to write this code. please help me. thanks expert

Pouya
  • 1,908
  • 17
  • 56
  • 78
  • 1
    It's *really* unclear what you mean by "send this file (into memoryStream ) for download". You're creating a `MemoryStream`, but then completely ignoring it, because you're returning immediately from the method. It looks like the code creating the report should *not* be serializing it - it should just return the report to the calling code, which can then save it to a stream. Unfortunately we don't have enough context to help much more than that. – Jon Skeet Nov 20 '12 at 13:05
  • @ Jon Skeet: first i create pdf file in MemoryStream, example i put button into form , i want when user click button this file download. – Pouya Nov 20 '12 at 13:11
  • Right, so as I suggested, you need to separate out "creating and returning the report" from "writing to a memory stream". The way you've put them together at the moment means the memory stream is created in code which doesn't want to use it. – Jon Skeet Nov 20 '12 at 13:14

1 Answers1

11
byte[] bytes = memoryStream.GetBuffer();
Response.Buffer = true;
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment; filename=report.pdf");
Response.BinaryWrite(bytes);
Response.Flush();
Boris Gappov
  • 2,483
  • 18
  • 23
  • 1
    That will write more than it should do - either use `ToByteArray` or limit the `BinaryWrite` call based on the length of the memory stream. – Jon Skeet Nov 20 '12 at 13:13
  • Note that filename should be encoded with `Server.UrlEncode(filename)`. – Fred Feb 24 '14 at 13:28