gillardg
Messages postés
3275
Date d'inscription
jeudi 3 avril 2008
Statut
Membre
Dernière intervention
14 septembre 2014
2
4 août 2008 à 17:21
la concatenation de fichiers pdf est possible
requiert ItextSharp.dll
Imports
Microsoft.VisualBasic
Imports
System
Imports
System.Collections.Generic
Imports
System.IO
Imports
iTextSharp.text
Imports
iTextSharp.text.pdf
Namespace
SmartSoft.PdfLibrary
Public
NotInheritable
Class PdfMerger
''' <summary>
''' Merge pdf files.
''' </summary>
''' PDF files being merged.
''' <returns></returns>
Private
Sub
New()
End
Sub
Public
Shared
Function MergeFiles(
ByVal sourceFiles
As List(
Of
Byte()))
As
Byte()
Dim document
As
New Document()
Dim output
As
New MemoryStream()
Try
Try
' Initialize pdf writer
Dim writer As PdfWriter PdfWriter.GetInstance(document, output)writer.PageEvent
New PdfPageEvents()
' Open document to writedocument.Open()
Dim content
As PdfContentByte = writer.DirectContent
' Iterate through all pdf documents
For fileCounter
As
Integer = 0
To sourceFiles.Count - 1
' Create pdf reader
Dim reader
As
New PdfReader(sourceFiles(fileCounter))
Dim numberOfPages
As
Integer = reader.NumberOfPages
' Iterate through all pages
For currentPageIndex
As
Integer = 1
To numberOfPages
' Determine page size for the current pagedocument.SetPageSize(reader.GetPageSizeWithRotation(currentPageIndex))
' Create pagedocument.NewPage()
Dim importedPage
As PdfImportedPage = writer.GetImportedPage(reader, currentPageIndex)
' Determine page orientation
Dim pageOrientation
As
Integer = reader.GetPageRotation(currentPageIndex)
If (pageOrientation = 90)
OrElse (pageOrientation = 270)
Thencontent.AddTemplate(importedPage, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation(currentPageIndex).Height)
Elsecontent.AddTemplate(importedPage, 1f, 0, 0, 1f, 0, 0)
End
If
Next currentPageIndex
Next fileCounter
Catch exception
As Exception
Throw
New Exception(
"There has an unexpected exception occured during the pdf merging process.", exception)
End
Try
Finallydocument.Close()
End
Try
Return output.GetBuffer()
End
Function
End
Class
''' <summary>
''' Implements custom page events.
''' </summary>
Friend
Class PdfPageEvents
Implements IPdfPageEvent#
Region
"members"
Private _baseFont
As BaseFont =
Nothing
Private _content
As PdfContentByte#
End
Region#
Region
"IPdfPageEvent Members"
Public
Sub OnOpenDocument(
ByVal writer
As PdfWriter,
ByVal document
As Document)
Implements IPdfPageEvent.OnOpenDocument_baseFont = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED)
_content = writer.DirectContent
End
Sub
Public
Sub OnStartPage(
ByVal writer
As PdfWriter,
ByVal document
As Document)
Implements IPdfPageEvent.OnStartPage
End
Sub
Public
Sub OnEndPage(
ByVal writer
As PdfWriter,
ByVal document
As Document)
Implements IPdfPageEvent.OnEndPage
' Write header text
Dim headerText
As
String =
"PDF Merger by Smart-Soft"_content.BeginText()
_content.SetFontAndSize(_baseFont, 8)
_content.SetTextMatrix(GetCenterTextPosition(headerText, writer), writer.PageSize.Height - 10)
_content.ShowText(headerText)
_content.EndText()
' Write footer text (page numbers)
Dim text
As
String =
"Page " & writer.PageNumber_content.BeginText()
_content.SetFontAndSize(_baseFont, 8)
_content.SetTextMatrix(GetCenterTextPosition(text, writer), 10)
_content.ShowText(text)
_content.EndText()
End
Sub
Public
Sub OnCloseDocument(
ByVal writer
As PdfWriter,
ByVal document
As Document)
Implements IPdfPageEvent.OnCloseDocument
End
Sub
Public
Sub OnParagraph(
ByVal writer
As PdfWriter,
ByVal document
As Document,
ByVal paragraphPosition
As
Single)
Implements IPdfPageEvent.OnParagraph
End
Sub
Public
Sub OnParagraphEnd(
ByVal writer
As PdfWriter,
ByVal document
As Document,
ByVal paragraphPosition
As
Single)
Implements IPdfPageEvent.OnParagraphEnd
End
Sub
Public
Sub OnChapter(
ByVal writer
As PdfWriter,
ByVal document
As Document,
ByVal paragraphPosition
As
Single,
ByVal title
As Paragraph)
Implements IPdfPageEvent.OnChapter
End
Sub
Public
Sub OnChapterEnd(
ByVal writer
As PdfWriter,
ByVal document
As Document,
ByVal paragraphPosition
As
Single)
Implements IPdfPageEvent.OnChapterEnd
End
Sub
Public
Sub OnSection(
ByVal writer
As PdfWriter,
ByVal document
As Document,
ByVal paragraphPosition
As
Single,
ByVal depth
As
Integer,
ByVal title
As Paragraph)
Implements IPdfPageEvent.OnSection
End
Sub
Public
Sub OnSectionEnd(
ByVal writer
As PdfWriter,
ByVal document
As Document,
ByVal paragraphPosition
As
Single)
Implements IPdfPageEvent.OnSectionEnd
End
Sub
Public
Sub OnGenericTag(
ByVal writer
As PdfWriter,
ByVal document
As Document,
ByVal rect
As Rectangle,
ByVal text
As
String)
Implements IPdfPageEvent.OnGenericTag
End
Sub#
End
Region
Private
Function GetCenterTextPosition(
ByVal text
As
String,
ByVal writer
As PdfWriter)
As
Single
Return writer.PageSize.Width / 2 - _baseFont.GetWidthPoint(text, 8) / 2
End
Function
End
ClassEnd
Namespace
usage
bonjour chez vous!
< body>