65.9K
CodeProject 正在变化。 阅读更多。
Home

使用 ASP.NET 合并横向和纵向 PDF

starIconstarIconstarIconstarIconemptyStarIcon

4.00/5 (3投票s)

2007年9月14日

2分钟阅读

viewsIcon

60794

本文展示了如何创建具有不同打印偏好(纵向和横向)的 PDF,并将它们合并到一个 PDF 文件中

Screenshot - mergePDFs.gif

引言

在每个企业的运营中,都会有需要将纵向和横向打印偏好设置组合成一个 PDF 的情况。其中一个关键的业务领域“账单”一直试图实现这一点。本文展示了如何使用 Crystal Report 的“导出为 PDF”功能创建不同打印偏好设置(横向和纵向)的 PDF,并将它们合并成一个单一的 PDF,所有这些都在 ASP.NET 中完成。为了将 PDF 合并成最终报告,我们授权使用了第三方 COM 组件 CutePDF。

背景

账单部门一直将这些账单作为打印账单创建并邮寄给客户。有人提出将这些账单在线提供,但业务用户和赞助者坚持要求在线账单与打印账单完全一致。因此,花费了大量时间使用 Crystal Report 来实现这一点。更糟糕的是,打印账单的第一页以纵向打印,其余页面以横向打印。研究立即开始,而且截止日期一如既往地临近,这对于 IT 部门来说是常态。

Using the Code

第一步是创建 Crystal Report 报告。首先,将页面或页面以纵向格式创建为一个报告。然后,将其他页面或页面以横向格式创建为另一个报告。我们不会在本文章中展示如何创建 Crystal Report 报告。我很快会写另一篇文章。下面的代码块展示了如何以编程方式将数据导出为纵向设置的 PDF。

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 
CrReport.Load(reportPath)

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
CrReport.SetDataSource(dsHeader)

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

' Export the report 
       
CrReport.Export()

此代码块展示了如何以编程方式将数据导出为横向 PDF。

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 

CrReport.Export()
CrReport.Dispose()

在这里,我们展示了如何将 PDF 合并成最终报告。我们使用第三方 COM 组件来实现这一点,但也有免费的开源组件可以完成这项任务。

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

最后,我们展示了如何在浏览器中显示最终 PDF。

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.Clear() 
Response.ClearHeaders() 
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) 
memStream.WriteTo(Response.OutputStream) 
memStream.Close() 
Response.End()

关注点

我们完成的另一小部分工作是在创建最终报告后删除临时 PDF 文件。这节省了硬盘空间,并减轻了支持人员的维护负担。相信我,你希望让支持人员保持快乐。祝您编程愉快!!别忘了享受乐趣。

历史

  • 2007 年 9 月 14 日 -- 发布原始版本
© . All rights reserved.