I've a crystal report which is having two pages; one in Portrait and the second one is in Landscape format. In report it looks fine but when I exporting it to a PDF, both pages got exported in Portrait format loosing some of the data. Can anyone solve my problem? Many thanks in advance.
It's a bit tricky, if you're using ExportToStream because the main public one is
report.ExportToStream(ExportFormatType formatType)
which ignores FormatOptions. Instead you will need to use
report.FormatEngine.ExportToStream(ExportRequestContext reqContext)
This code should do what you want (using PdfSharp to combine):
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;
...
using (var report = new ReportClass { FileName = Server.MapPath("/Crystal/Reports/DosBatches.rpt") })
{
report.Load();
/* set data source connections */
/* set parameters */
// export the first page
report.PrintOptions.PaperOrientation = PaperOrientation.Portrait;
var exportOptions1 = new ExportOptions
{
ExportFormatType = ExportFormatType.PortableDocFormat,
FormatOptions = new PdfFormatOptions { UsePageRange = true, FirstPageNumber = 1, LastPageNumber = 1 }
};
exportRequestContext.ExportInfo = exportOptions1;
var stream1 = report.FormatEngine.ExportToStream(exportRequestContext);
stream1.Seek(0, SeekOrigin.Begin);
// export the second page
report.PrintOptions.PaperOrientation = PaperOrientation.Landscape;
var exportOptions2 = new ExportOptions
{
ExportFormatType = ExportFormatType.PortableDocFormat,
FormatOptions = new PdfFormatOptions { UsePageRange = true, FirstPageNumber = 2, LastPageNumber = 2 }
};
exportRequestContext.ExportInfo = exportOptions2;
var stream2 = report.FormatEngine.ExportToStream(exportRequestContext);
stream2.Seek(0, SeekOrigin.Begin);
// merge the two PDF streams
var combinedPdf = new PdfDocument();
foreach (PdfPage page in PdfReader.Open(stream1, PdfDocumentOpenMode.Import).Pages)
combinedPdf.AddPage(page);
foreach (PdfPage page in PdfReader.Open(stream2, PdfDocumentOpenMode.Import).Pages)
combinedPdf.AddPage(page);
// probably not the most efficient, but works
var output = new MemoryStream();
combinedPdf.Save(output, false);
return new FileStreamResult(output, "application/pdf");
}
There is no need to do any single line of code follow this simple step step:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.