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
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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();
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0