Karen Wilson
asked on
Merging PDFs with VB.NET
I am trying to append PDFs to a master PDF and my code is replacing the master PDF with the one I want to attached to it. I need someone to look at my code and tell me what I'm doing wrong.
Many thanks!
Dim jfFile As String = "K:\ConfigMgmt\PDF\POAM\JS CForm\JF20 16_10_20.p df"
Dim merged As Boolean = True
Using Stream As New FileStream(jfFile, FileMode.Create)
Dim document As New Document()
Dim pdf As New PdfCopy(document, Stream)
Dim reader As PdfReader = Nothing
Try
document.Open()
Dim getPDF = (From id In d.tblPOAMdetails _
Where id.closedDate Is Nothing _
And id.nasacsspno = "XXXXX" _
Order By id.UPI Ascending _
Select id.poamLink).ToList
For Each file As String In getPDF
reader = New PdfReader(file)
pdf.AddDocument(reader)
reader.Close()
Next
Catch generatedExceptionName As Exception
merged = False
If reader IsNot Nothing Then
reader.Close()
End If
Finally
If document IsNot Nothing Then
document.Close()
End If
End Try
End Using
Many thanks!
Dim jfFile As String = "K:\ConfigMgmt\PDF\POAM\JS
Dim merged As Boolean = True
Using Stream As New FileStream(jfFile, FileMode.Create)
Dim document As New Document()
Dim pdf As New PdfCopy(document, Stream)
Dim reader As PdfReader = Nothing
Try
document.Open()
Dim getPDF = (From id In d.tblPOAMdetails _
Where id.closedDate Is Nothing _
And id.nasacsspno = "XXXXX" _
Order By id.UPI Ascending _
Select id.poamLink).ToList
For Each file As String In getPDF
reader = New PdfReader(file)
pdf.AddDocument(reader)
reader.Close()
Next
Catch generatedExceptionName As Exception
merged = False
If reader IsNot Nothing Then
reader.Close()
End If
Finally
If document IsNot Nothing Then
document.Close()
End If
End Try
End Using
ASKER
The same thing happened. I forgot to note that I had done that previously as well and reverted it back to create.
ASKER
Also, the master PDF is in landscape while the attachments are in portrait. Could that be causing a problem?
Hi Karen;
I have looked at the code again and nothing else sticks out at me. What library are you using? If I can download it from NuGet I will try and test on my computer.
I have looked at the code again and nothing else sticks out at me. What library are you using? If I can download it from NuGet I will try and test on my computer.
ASKER
I am using iTextSharp. I know, it's crazy!
ASKER
Here is what I am importing as well.
Imports System.IO
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Imports iTextSharp.text.pdf.parser
Thanks for your help.
Imports System.IO
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Imports iTextSharp.text.pdf.parser
Thanks for your help.
ASKER
I can get this code to work.
Dim bytes As Byte() = File.ReadAllBytes("K:\Conf igMgmt\PDF \POAM\JSCF orm\JF2016 _10_20.pdf ")
Dim blackFont As Font = FontFactory.GetFont("Arial ", 9, Font.Bold, BaseColor.BLACK)
Using stream As New MemoryStream()
Dim reader As New PdfReader(bytes)
Using stamper As New PdfStamper(reader, stream)
Dim pages As Integer = reader.NumberOfPages
For i As Integer = 1 To pages
ColumnText.ShowTextAligned (stamper.G etUnderCon tent(i), Element.ALIGN_RIGHT, New Phrase("JSC Form 1218 " & i.ToString(), blackFont), 568.0F, 15.0F, 0)
Next
End Using
bytes = stream.ToArray()
End Using
File.WriteAllBytes("K:\Con figMgmt\PD F\POAM\JSC Form\JF201 6_10_20.pd f", bytes)
Dim bytes As Byte() = File.ReadAllBytes("K:\Conf
Dim blackFont As Font = FontFactory.GetFont("Arial
Using stream As New MemoryStream()
Dim reader As New PdfReader(bytes)
Using stamper As New PdfStamper(reader, stream)
Dim pages As Integer = reader.NumberOfPages
For i As Integer = 1 To pages
ColumnText.ShowTextAligned
Next
End Using
bytes = stream.ToArray()
End Using
File.WriteAllBytes("K:\Con
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks Fernando!
That did it!! I will work my code to remove the "original" one and rename the new one and I am good to go. Thanks so much for your time. It was making me bonkers!
That did it!! I will work my code to remove the "original" one and rename the new one and I am good to go. Thanks so much for your time. It was making me bonkers!
Not a problem Karen, always glad to help.
Try changing this line of code
Open in new window
to thisOpen in new window
FileMode.Create over writes the original file.