Click here to Skip to main content
13,801,054 members
Click here to Skip to main content
Add your own
alternative version


34 bookmarked
Posted 14 Sep 2007

Merge Landscape and Portrait PDFs using ASP.NET

, 14 Sep 2007
Rate this:
Please Sign up or sign in to vote.
This article shows how to create PDFs in the different printing preferences of Landscape and Portrait and merge them into one single PDF
Screenshot - mergePDFs.gif


There are situations in every business where it would be nice to have a PDF that is a combination of Portrait and Landscape printing preferences. One of the key business areas, "Billing," is always attempting to achieve this. This article shows how to create PDFs in the different printing preferences of Landscape and Portrait using the Crystal Report "Export to PDF" function, merging them into one single PDF in ASP.NET. A 3rd party COM component called CutePDF was licensed to merge the PDFs to one final report.


The billing department had a history of creating these bills as printed bills and mailing them to the customers. An idea was tossed around to make these bills available online, but the business users and sponsors insisted that online bills match z to z to the printed bill. Needless to say, an excessive amount of time was spent to achieve this using Crystal Report. To add to the horror, the first page of the printed bill was printed in Portrait and the rest of the pages in Landscape. The research started immediately and the deadline was near, like always, in the IT department.

Using the Code

The first step is to create Crystal reports. First, make the page or the pages in Portrait as one report. Make the other page or pages in Landscape as another report. We are not going to show how to create Crystal reports in this article. I'll work on another article soon. The code block below shows how to programmatically export the data to PDF in portrait settings.

Dim CrReport As ReportDocument

' Report Name to set the printing options 
Dim CrPrintOptions As PrintOptions

'set the export options of the report

Dim CrExportOptions As ExportOptions

Dim CrFormatTypeOptions As New PdfRtfWordFormatOptions()

'define the folder to save the reports 
Dim CrDiskFileDestinationOptions As DiskFileDestinationOptions

' Localise the filepath and load the report 
Dim sPDFName As String = CStr(Now().Millisecond())

Dim sPath As String = "C:\report\" 
'first page of the report 
Dim sPDFFile1 As String = sPath + sPDFName + "_a.pdf"

'Other Pages report 
Dim sPDFFile2 As String = sPath + sPDFName + "_b.pdf"

'the final report name 
Dim sPDFFinal As String =   sPath  + sPDFName + ".pdf"< BR   >       
CrReport = New ReportDocument() 
'load the report 

CrDiskFileDestinationOptions = New DiskFileDestinationOptions()

'first page report file name is set 
CrDiskFileDestinationOptions.DiskFileName = sPDFFile1

' Specify a page range (optional) 
CrFormatTypeOptions.FirstPageNumber = 1

' Start Page in the Report 
CrFormatTypeOptions.LastPageNumber = 1

' End Page in the Report 
CrFormatTypeOptions.UsePageRange = True

' Set export options 
CrExportOptions = CrReport.ExportOptions

With CrExportOptions 
    ' Set the destination to a disk file 
    .ExportDestinationType = ExportDestinationType.DiskFile

    ' Set the format to PDF 
    .ExportFormatType = ExportFormatType.PortableDocFormat

    ' Set the destination options to DiskFileDestinationOptions object 
    .DestinationOptions = CrDiskFileDestinationOptions

    .FormatOptions = CrFormatTypeOptions 
End With

'load the dataset 
Dim dsHeader As DataSet

CrPrintOptions = CrReport.PrintOptions 
'set the paper orientation to portrait for the first page 
With CrPrintOptions 
    .PaperOrientation = PaperOrientation.Portrait 
End With

' Export the report 

This code block shows how to programmatically export the data to PDF in Landscape.

With CrPrintOptions 
    'for multi page reports set the page orientation landscape from 3rd page 
    .PaperOrientation = PaperOrientation.Landscape 
    CrFormatTypeOptions.FirstPageNumber = 3 
End With

'set the report name for the 2nd pdf file 
CrDiskFileDestinationOptions.DiskFileName = sPDFFile2

'set maximum page number for lastPageNumber property because 
'it's unknown at the design time 
CrFormatTypeOptions.LastPageNumber = 2000

' End Page in the Report 
CrFormatTypeOptions.UsePageRange = True 


Here we show how to merge PDFs into a final report. We use a 3rd party COM component to achieve this, but there are free open source ones to accomplish this as well.

Dim ocutePDF As CutePDFDocument 
'create a new instance of CutePDFDocument 
ocutePDF = New CutePDFDocument

If System.IO.File.Exists(sPDFFile1) Then 
    'merge the files to one final report 
    ocutePDF.mergePDF(sPDFFile1, sPDFFile2, sFinalReport) 
End If

Finally, we show how to display the final PDF in a browser.

Dim client As System.IO.FileStream 
Dim objFile As System.IO.FileInfo = New System.IO.FileInfo(sFile)

' open the final billing report to read 
client = New System.IO.FileStream(sFile, IO.FileMode.Open)

Dim buffer As Byte() = New Byte(CInt(client.Length)) {}

'read the file into byte array 
client.Read(buffer, 0, buffer.Length) 
Response.ContentType = "application/octet-stream" 
Response.AppendHeader("Content-Disposition", _
    "attachment; filename=" + objFile.Name) 

Response.AppendHeader("Expires", "0") 

Response.AppendHeader("Content-Description", "File Transfer") 
Dim memStream As New IO.MemoryStream(buffer) 

Points of Interest

One more little piece we worked on was to delete the temporary PDF files after creating a final report. This saved hard disk space and made maintenance to support staff less of a headache. Believe me, you want to keep the support staff happy. Happy programming!! And don't forget to have fun.


  • 14 September, 2007 -- Original version posted


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Web Developer
United States United States
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralMy vote of 5 Pin
Manoj Kumar Choubey9-Feb-12 23:14
professionalManoj Kumar Choubey9-Feb-12 23:14 
Generalthank you Pin
mohamed antar3-Oct-10 2:21
membermohamed antar3-Oct-10 2:21 
Generalreference Pin
JernejR24-Feb-10 21:23
memberJernejR24-Feb-10 21:23 
GeneralSeparate but related question Pin
HoyaSaxa9315-Sep-07 2:11
memberHoyaSaxa9315-Sep-07 2:11 
GeneralRe: Separate but related question Pin
puchi00717-Sep-07 4:50
memberpuchi00717-Sep-07 4:50 
GeneralRe: Separate but related question Pin
HoyaSaxa9317-Sep-07 10:27
memberHoyaSaxa9317-Sep-07 10:27 
QuestionSource Code?!?!!? Pin
barbod_blue14-Sep-07 20:44
memberbarbod_blue14-Sep-07 20:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web06 | 2.8.181215.1 | Last Updated 14 Sep 2007
Article Copyright 2007 by puchi007
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid