Post by RobertoAPost by Andrea (Work)O scarichi visual studio gratuito e con itextsharp e 20 righe di codice ti
fai il tuo programma da usare a riga di comando.
Esplicati mellio, peppiacere
Qualcosa del genere
| Sub AddPdf(ByVal sInFilePath As String, ByRef oPdfDoc As iTextSharp.text.Document, ByRef oPdfWriter As PdfWriter)
|
| Dim oDirectContent As iTextSharp.text.pdf.PdfContentByte = oPdfWriter.DirectContent
| Dim oPdfReader As iTextSharp.text.pdf.PdfReader = New iTextSharp.text.pdf.PdfReader(sInFilePath)
| Dim iNumberOfPages As Integer = oPdfReader.NumberOfPages
| Dim iPage As Integer = 0
|
| Do While (iPage < iNumberOfPages)
| iPage += 1
|
| Dim iRotation As Integer = oPdfReader.GetPageRotation(iPage)
| Dim oPdfImportedPage As iTextSharp.text.pdf.PdfImportedPage = oPdfWriter.GetImportedPage(oPdfReader, iPage)
|
| oPdfDoc.SetPageSize(oPdfReader.GetPageSizeWithRotation(iPage))
| oPdfDoc.NewPage()
|
| If (iRotation = 90) Or (iRotation = 270) Then
| oDirectContent.AddTemplate(oPdfImportedPage, 0, -1.0F, 1.0F, 0, 0, oPdfReader.GetPageSizeWithRotation(iPage).Height)
| Else
| oDirectContent.AddTemplate(oPdfImportedPage, 1.0F, 0, 0, 1.0F, 0, 0)
| End If
|
| Loop
|
| End Sub
| Sub Main()
| Dim workFolder As String = System.AppDomain.CurrentDomain.BaseDirectory
| Dim oFolderInfo As New System.IO.DirectoryInfo(workFolder)
| Dim sOutFilePath As String = workFolder & "\" & oFolderInfo.Name & ".pdf"
|
|
| If IO.File.Exists(sOutFilePath) Then
| Try
| IO.File.Delete(sOutFilePath)
| Catch ex As Exception
| Console.WriteLine("Output file already exists: " & sOutFilePath & " and could not be deleted.")
| Exit Sub
| End Try
| End If
|
| Dim oFiles As String() = Directory.GetFiles(workFolder, "*.PDF")
|
| ' esco se non ci sono files
| If oFiles.Length = 0 Then Exit Sub
|
| Dim oPdfDoc As New iTextSharp.text.Document()
| Dim oPdfWriter As PdfWriter = PdfWriter.GetInstance(oPdfDoc, New FileStream(sOutFilePath, FileMode.Create))
|
| oPdfDoc.Open()
|
| System.Array.Sort(Of String)(oFiles)
|
| For i As Integer = 0 To oFiles.Length - 1
| Dim sFromFilePath As String = oFiles(i)
| Dim oFileInfo As New FileInfo(sFromFilePath)
|
| Try
|
| AddPdf(sFromFilePath, oPdfDoc, oPdfWriter)
|
| Catch ex As Exception
| Console.WriteLine(sFromFilePath & vbTab & ex.Message)
| End Try
|
|
| Next
|
| Try
| oPdfDoc.Close()
| oPdfWriter.Close()
| Catch ex As Exception
| Console.WriteLine(ex.Message)
| Try
| IO.File.Delete(sOutFilePath)
| Catch ex2 As Exception
| End Try
| End Try
|
|
|
| End Sub