Ken Huysmans posted an updated version of iTextSharp's MergeFile subroutine. Thank you, Ken.
Some of us VB.netter's, though, need to 'transpose' the code to VB. So I thought I'd post my VB.net version and, perhaps, save others a little time.
To quote Ken's post:
iText# (iTextSharp) is a port of the iText open source java library written entirely in C# for the .NET platform. iText# is a library that allows you to generate PDF files on the fly. It is implemented as an assembly.
The code of the class I've written uses iText# and is based on the example code (Console Application) that can be found on http://itextsharp.sourceforge.net/examples/Concat.cs . However, this code seems to target an out of date version of iText# and can't be compiled without fixing some lines...
This VB sub will allow you to merge multiple PDF's to one big PDF-file:
The dateTime stamp of this (tested) code is: November 30, 2007 @ 1:30pm PST.
I'll test it in the next couple of days and fix any issues (and/or fix any issues others may bring to my attention).
One thing to ensure is that the application you've written has read/write priviledges to the folder it will use.
Option Explicit On
Option Strict On
Imports System.IO
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Public Class PDFClass
Public Shared Sub ContactPDFs(ByVal destFile As String, ByVal sourceFiles As String())
If sourceFiles.Length <>
Console.Error.WriteLine("This tools needs at least 3 parameters:" & Microsoft.VisualBasic.Chr(10) & "java Concat destfile file1 file2 [file3 ...]")
Else
Dim f As Integer = 1
Dim reader As PdfReader = New PdfReader(destFile)
Dim n As Integer = reader.NumberOfPages
Dim doc As Document = New Document(reader.GetPageSizeWithRotation(1))
Dim writer As PdfWriter
Try
writer = PdfWriter.GetInstance(doc, New FileStream(destFile, FileMode.Create))
Console.WriteLine("There are " & n.ToString & " pages in the original file.")
doc.Open()
Dim cb As PdfContentByte = writer.DirectContent
Dim page As PdfImportedPage
Dim rotation As Integer
While f <>
Dim i As Integer = 0
While i <>
System.Math.Min(System.Threading.Interlocked.Increment(i), i - 1)
doc.SetPageSize(reader.GetPageSizeWithRotation(i))
doc.NewPage()
page = writer.GetImportedPage(reader, i)
rotation = reader.GetPageRotation(i)
If rotation = 90 OrElse rotation = 270 Then
cb.AddTemplate(page, 0, -1.0F, 1.0F, 0, 0, reader.GetPageSizeWithRotation(i).Height)
Else
cb.AddTemplate(page, 1.0F, 0, 0, 1.0F, 0, 0)
End If
Console.WriteLine("Processed page " & i.ToString)
End While
System.Math.Min(System.Threading.Interlocked.Increment(f), f - 1)
If f <>
reader = New PdfReader(sourceFiles(f))
n = reader.NumberOfPages
'Console.WriteLine("There are " + n.ToString + " pages in the original file.")
End If
End While
Catch ex As Exception
uErr.LastException = ex
Finally
doc.Close()
End Try
End If
End Sub
End Class
Subscribe to:
Post Comments (Atom)
3 comments:
Good example, however I cannot get Visual Studio Pro 2005 to recognize the iTextSharp dll at all. It is in the Bin folder. I even recompiled the source to make sure the DLL was in the same version. No luck. Seems the web development does not like the DLL. Worse it appears that you have to create a class to access the dll class as code behind pages will not allow the Imports option. but the class will not import the dll.
Bob Curtice
The VS Complaint is:
Namespace or type specified in the Imports 'iTextSharp.text.SimpleTable' doesn't contain any public member or cannot be found. Make sure the namespace or the type is defined and contains at least one public member. Make sure the imported element name doesn't use any aliases.
Any ideas?
Here is the link for you to vb.net .net merge pdf files. Hope this gives you a start on rasteredge page http://www.rasteredge.com/how-to/vb-net-imaging/pdf-merge/
Post a Comment