' Instantiate a Document object
Dim doc As Aspose.Pdf.Document = New Aspose.Pdf.Document()
doc.PageInfo.Height = Aspose.Pdf.PageSize.PageLetter.Height
doc.PageInfo.Width = Aspose.Pdf.PageSize.PageLetter.Width
doc.PageInfo.Margin.Bottom = 0 : doc.PageInfo.Margin.Top = 0 : doc.PageInfo.Margin.Left = 0 : doc.PageInfo.Margin.Right = 0
' Navigate through the files and add them to the pdf file
For Each myFile As String In FilenameArray
' add page to pages collection of PDF file
Dim asposePage As Page = doc.Pages.Add()
asposePage.PageInfo.Height = Aspose.Pdf.PageSize.PageLetter.Height
asposePage.PageInfo.Width = Aspose.Pdf.PageSize.PageLetter.Width
asposePage.PageInfo.Margin.Bottom = 0 : asposePage.PageInfo.Margin.Top = 0 : asposePage.PageInfo.Margin.Left = 0 : asposePage.PageInfo.Margin.Right = 0
' create Image object
Dim image As New Aspose.Pdf.Image()
' set image file path
image.File = myFile
' add image to paragraphs collection of Page instance
asposePage.Paragraphs.Add(image)
Next myFile
Try
doc.Save(OutFilename)
Catch ex As Exception
End Try
doc.Dispose()
doc = Nothing
GC.Collect() : GC.WaitForPendingFinalizers()
Application.DoEvents()
Dim optimizationOptions As Optimization.OptimizationOptions = Nothing
Try
doc = New Aspose.Pdf.Document(OutFilename)
optimizationOptions = New Aspose.Pdf.Optimization.OptimizationOptions()
optimizationOptions.RemoveUnusedObjects = True
optimizationOptions.RemoveUnusedStreams = True
optimizationOptions.AllowReusePageContent = True
optimizationOptions.LinkDuplcateStreams = True
optimizationOptions.UnembedFonts = True
optimizationOptions.ImageCompressionOptions.CompressImages = True
optimizationOptions.ImageCompressionOptions.Version = Aspose.Pdf.Optimization.ImageCompressionVersion.Fast
optimizationOptions.ImageCompressionOptions.ImageQuality = 50
optimizationOptions.ImageCompressionOptions.ResizeImages = True
Try
doc.OptimizeResources(optimizationOptions)
Catch ex As Exception
Reference 1 Error Invalid image stream (Out of memory.)
End Try
Try
doc.Optimize()
Catch ex As Exception
Reference - I never get error here
End Try
Try
doc.Save(OutFilename)
Catch ex As Exception
Reference 2 Error "The Process cannot access the file ...........pdf because it is being used by another process
End Try
Catch ex As Exception
#If DEBUG Then
Stop
#End If
MsgBox(String.Format(“Optimize error,{0}”, ex.Message), MsgBoxStyle.OkOnly, “OptimizeResources Error”)
Finally
optimizationOptions = Nothing
doc.Dispose()
doc = Nothing
End Try
Information :
All images are letter size Tiff and is 300x300 dpi with lzw compression, 24/32 bit rgb because we handle color documents.
For example : I processed 148 tif images, color which saved a PDF file of 217 MB. I got Reference 1 Error followed by Reference 2 Error.
I opened the PDF with Acrobat and did a “Save As” -> “Reduced Size PDF” This worked and output file was 10.4 MB PDF
These errors always occur when I am processing more than 100 pages/images
When I get the Reference 1 Error, sometimes the code continues and saves a reduced size pdf that appears to be good.
Sometimes I get Reference 1 Error and also get Reference 2 Error which leaves orginal pdf file I saved.
Sometimes I get Reference 2 Error, but NO other errors which leaves orginal pdf file I saved.
Qestions :
1. Is the code above correct?
2. Why do I get Reference 2 Error? Is the doc.OptimizeResources call not clearing his access to the file when I don’t get an error?