We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Opimize error PDF

	' 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?

@elumicor

Thanks for contacting support.

The code you have shared seems fine and should not produce any issue. In case it is generating some issue, we need to test the scenario in our environment to address it accordingly. For the purpose, we may need sample PDF document with which you are facing these issues. It would really be helpful if you can please share a sample console application along with sample file(s). We will further proceed to help you accordingly.

PS: Please make sure to use/try Aspose.PDF for .NET 19.5 which is latest version.