Getting a large PDF when Concatenate is used to combine two PDF�s �

I am using the ASPOSE.PDF.KIT to do the following.

  1. Open a PDF add four lines of text to the PDF and a very small 1kb JPEG image.
  2. Open another PDF add four lines of text to the PDF and a very small 1kb JPEG image.
  3. Use the PdfFileEditor.Concatenate function to combine the two PDF files.
  4. Set the security on the resulting file.

The file size of the two PDF files before this is done is: 71 kb and 831 kb. However the size of the combined files is 2048 kb. This is odd because the changes that are made are very small and should not result in over a MB being added to the Concatenate file. Is there something that can be done with the PDF.KIT to compress thee new file

MK

Hi,

Thank you for considering Aspose.

Can you please provide the pdf files and code? We will check this issue and reply to you soon.

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

Hi,

I have checked your code and found that concatenation of Pdf is not taking much memory but addition of Text and sample image might be taking more memory. But I won't take it as a bug nevertheless I will discuss this with the developer and we will let you know as soon as solution is found.

Thanks.

Thanks. Let me know what the solution is so that I can start to get smaller files.

Here is some more information:

- The Image that is created and added to the PDF file has an averages size of 714 Bytes Under 1 KB.

Hi,

After discussing with the developers I am afraid that we can't support/fix this feature in the near future. We apologise for inconvenience.

Thanks.

So there is not a way to get a smaller sized PDF when adding text and a Image to a PDF file? Is there a way I could run the stream through the ASPOSE.PDF to compress the stream with the text and image added?

MK

It looks like the files size doubles when I set the Privilages with the following code.

Dim doc As Aspose.Pdf.Kit.PdfFileSecurity = New Aspose.Pdf.Kit.PdfFileSecurity(oPDF3, oPDF3)

Dim docP As DocumentPrivilege = DocumentPrivilege.AllowAll

docP.ChangeAllowLevel = 0

doc.SetPrivilege(docP)

I needed to run the above code to get rid of the message that was coming up in Acrobat “Stating that the end user needs to fill in the form. You can not save changes to the form.”

This message was bothersome because the PDF files that are being combined are not forms type of documents with fields to complete rather they are just plain text PDF’s.

Is there a better way to create the resulting PDF not as a form type of PDF? Or is there a way to set the Privilages with out having the file double in size?

Ifigured out what was doubling the files size.

When constructing the Pdf.Kit.PdfFileSecurity object my instream and outstream are the same stream. However I change my out stream to be a new stream then the file size does not double. is this a known issue?

Hi,

Yes, sometime Pdf engine writes Pdf to its original Pdf and hence the size of file increases. This is a known but complex issue and hence it is difficult to resolve in near future. we apologise for inconvenience.

Thanks.