Crystal Report - Exporting to PDF in both Landscape & Portrait
-
14-04-2021 - |
Question
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.
Solution
There is no need to do any single line of code follow this simple step step:
- Create first crystal report in Portrait format and this is default format of report.
- Create a second crystal report in landscape. To do this you should right on the second crystal report Design->page set-up->orientation->choose landscape
- Create a third crystal report that is final report in that add in detail section first and second report as a sub report. Just right click insert->sub report
- In the final report just right click on section export on detail section of second report select section expert->paging->landscape your job is done.
- Then export that final report to PDF in your code.
OTHER TIPS
- Export first page as PDF in Portrait
- Export second page as PDF in Landscape
- Combine the two PDF's using a third party library
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");
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow