I would be glad to provide the PDF files can I get a email address to send them to? The files need to be treated as confidential information.
I am running Aspose.Pdf.Kit version 2.6.3 .NET 2.0
Here is the code snippet:
Imports Aspose.Pdf.Kit
Imports System.IO
Imports System.Drawing
Imports System.Drawing.Imaging
Module Module1
Private mstrFormName As String
Private mstrNumber As String
Private LeftX As Single
Private LeftY As Single
Private RightX As Single
Private RightY As Single
Sub Main()
Dim oPDF1 As MemoryStream = Nothing
Dim oPDF2 As MemoryStream = Nothing
Dim oPDF3 As New MemoryStream
'create the first PDF file
LeftX = 255
LeftY = 676
RightX = 408
RightY = 694
mstrFormName = "ORT4372"
mstrNumber = "MXSF--08000103"
oPDF1 = GetJacketPDF()
'create the second PDF PDF file
LeftX = 250
LeftY = 660
RightX = 400
RightY = 685
mstrFormName = "ORT3990"
mstrNumber = "HPT-08001685"
oPDF2 = GetJacketPDF()
If oPDF1 IsNot Nothing AndAlso oPDF2 IsNot Nothing Then
'combine the files
Dim oPdfEditor As New PdfFileEditor
oPdfEditor.Concatenate(oPDF1, oPDF2, oPDF3)
'set the file props so that the user doesn't see the file as being read only.
Dim pdfProperties As New MemoryStream
Dim doc As Aspose.Pdf.Kit.PdfFileSecurity = New Aspose.Pdf.Kit.PdfFileSecurity(oPDF3, oPDF3)
Dim docP As DocumentPrivilege = DocumentPrivilege.AllowAll
'0 is the most restrictive security level.
'By setting it to this we get ride of the form edit message on save.
docP.ChangeAllowLevel = 0
doc.SetPrivilege(docP)
'write to the output file
If File.Exists("c:temp\results.PDF") Then
File.Delete("c:temp\results.PDF")
End If
Dim fs As New FileStream("c:\temp\results.PDF", FileMode.Create)
Dim w As New BinaryWriter(fs)
'convert the stream to a byt()
Dim returnValue As Byte()
returnValue = oPDF3.GetBuffer
w.Write(returnValue, 0, CInt(returnValue.Length))
w.Flush()
w.Close()
fs.Close()
fs.Dispose()
End If
oPDF1.Dispose()
oPDF2.Dispose()
oPDF3.Dispose()
End Sub
Private Function GetJacketPDF() As Object
'use the unc path or path to load it in as a byte array.
Dim fs As FileStream = File.Open(String.Format("{0}{1}.pdf", "C:\temp\", mstrFormName), FileMode.Open, FileAccess.Read)
Dim aFileByteArray As Byte()
ReDim aFileByteArray(fs.Length)
Dim br As BinaryReader = New BinaryReader(fs)
br.Read(aFileByteArray, 0, CInt(fs.Length))
br.Close()
fs.Close()
fs.Dispose()
'this code unlocks the PDF control to remove the water mark.
Dim license As Aspose.Pdf.Kit.License = New Aspose.Pdf.Kit.License
license.SetLicense("C:\work\ASPOSE test\CombinePDF\CombinePDF\bin\Debug\Aspose.Custom.lic")
'load the pdf document into memory.
Dim pdfStream As MemoryStream = New MemoryStream(aFileByteArray)
Dim outStream As MemoryStream = New MemoryStream
Dim imageStream As New MemoryStream
Dim ImageCodecInfoJPG As ImageCodecInfo
ImageCodecInfoJPG = GetEncoderInfo("image/jpeg")
'set it to be low quality and color depth to get a small image.
Dim EP As New EncoderParameters(2)
EP.Param(0) = New EncoderParameter(Encoder.Quality, 0)
EP.Param(1) = New EncoderParameter(Encoder.ColorDepth, 0)
'create number background image to cover any prefix information
'that exists on the page.
CreateImage(mstrNumber).Save(imageStream, ImageCodecInfoJPG, EP)
'create a ASPOSE object from the stream.
Dim mend As PdfFileMend = New PdfFileMend(pdfStream, outStream)
'insert background image on the pdf.
mend.AddImage(imageStream, 1, LeftX, CSng(LeftY - 10), RightX, RightY)
'close pdf editor.
mend.Close()
'holds the pdf with the number background added.
Dim polStream As New MemoryStream
mend = New PdfFileMend(outStream, polStream)
Dim ofont As FontColor = New FontColor()
'add number.
Dim fText As FormattedText = New FormattedText(mstrNumber, New FontColor(0, 0, 0), Aspose.Pdf.Kit.FontStyle.HelveticaBold, EncodingType.Winansi, True, 18)
mend.AddText(fText, 1, LeftX, LeftY, RightX, RightY)
'close pdf editor.
mend.Close()
Dim userInfoStream As New MemoryStream
mend = New PdfFileMend(polStream, userInfoStream)
'create a formatted text object.
Dim ContactText As FormattedText = New FormattedText("Policy Issuer:", New FontColor(0, 0, 0), Aspose.Pdf.Kit.FontStyle.HelveticaBold, EncodingType.Winansi, True, 10)
'add Contact Info.
With ContactText
.AddNewLineText("TRI Source TEST Company")
.AddNewLineText("425 MY Lane Test NORTH")
.AddNewLineText(String.Format("{0}, {1} {2}", _
"DUBLIN,", _
"OH", _
"43017"))
.AddNewLineText(String.Format("PHONE: {0}", "555-555-5555"))
End With
mend.AddText(ContactText, 1, LeftX + 85, LeftY + 55) ', RightX + 200, RightY + 50)
'close pdf editor.
mend.Close()
outStream.Close()
pdfStream.Close()
imageStream.Close()
polStream.Close()
'Return userInfoStream
Return userInfoStream
End Function
Private Function CreateImage(ByVal imageText As String) As System.Drawing.Image
'create an empty image object.
Dim oBitmap As Bitmap = New Bitmap(1, 1)
'creates a graphic object to draw on.
Dim oGraphic As Graphics = Graphics.FromImage(oBitmap)
oGraphic.SmoothingMode = Drawing2D.SmoothingMode.HighSpeed
'set the font type.
Dim oFont As New Font("Helvetica", 18, Drawing.FontStyle.Bold)
'measure the width and height of the text.
Dim width As Integer = oGraphic.MeasureString(imageText, oFont).Width
Dim height As Integer = oGraphic.MeasureString(imageText, oFont).Height
're-create the image with the correct width and height.
oBitmap = New Bitmap(oBitmap, New Size(width, height))
oGraphic = Graphics.FromImage(oBitmap)
oGraphic.SmoothingMode = Drawing2D.SmoothingMode.HighSpeed
'create text image.
With oGraphic
.Clear(Color.White)
.TextRenderingHint = Text.TextRenderingHint.AntiAliasGridFit
.DrawRectangle(Pens.White, New Rectangle(0, 0, width, height))
End With
oGraphic.Flush()
oGraphic.Dispose()
Return oBitmap
End Function
Private Function GetEncoderInfo(ByVal mimeType As String) As ImageCodecInfo
Dim j As Integer
Dim encoders() As ImageCodecInfo
encoders = ImageCodecInfo.GetImageEncoders()
For j = 0 To encoders.Length - 1 Step 1
If (encoders(j).MimeType = mimeType) Then
Return encoders(j)
End If
Next
Return Nothing
End Function
End Module