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.

Was it helpful?

Solution

There is no need to do any single line of code follow this simple step step:

  1. Create first crystal report in Portrait format and this is default format of report.
  2. 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
  3. 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
  4. 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.
  5. Then export that final report to PDF in your code.

OTHER TIPS

  1. Export first page as PDF in Portrait
  2. Export second page as PDF in Landscape
  3. 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
scroll top