Aspose.Pdf - Old-style JPEG compression for TIFF file

We’re trying to convert tiff files to pdf using Aspose.Pdf. But some of our TIFF files were compressed using “Old-style JPEG” compression method. We get this error “Invalid image stream (Parameter is not valid.)” when hitting this line of code: page.Resources.Images.Add(imageStream). Does that mean your product does not support old style jpg compression method for TIFF file?

Hi Hoang,

Thank you for using our products.

Can you please share some details regarding sample source code and template documents you are using or create a sample application to show the issue. This will help us to figure out the issue and reply back to you soon.

We apologize for your inconvenience.

Thanks & Regards,

I cannot give you the TIF file, but it needs to compressed with "Old-style JPEG" method. Do you guys support that?

Here is the code:

//open document

Document pdfDocument = new Document();

//set coordinates

int lowerLeftX = 100;

int lowerLeftY = 100;

int upperRightX = 200;

int upperRightY = 200;

//get the page where image needs to be added

Aspose.Pdf.Page page = pdfDocument.Pages.Add();

//load image into stream

FileStream imageStream = new FileStream(@"C:\Development\Projects\Misc\TestWebsite\7302390.TIF", FileMode.Open);

//add image to Images collection of Page Resources

page.Resources.Images.Add(imageStream);

//using q operator: this operator saves current graphics state

page.Contents.Add(new Operator.q());

//create Rectangle and Matrix objects

Aspose.Pdf.Rectangle rectangle = new Aspose.Pdf.Rectangle(lowerLeftX, lowerLeftY, upperRightX, upperRightY);

Matrix matrix = new Matrix(new double[] { rectangle.URX - rectangle.LLX, 0, 0, rectangle.URY - rectangle.LLY, rectangle.LLX, rectangle.LLY });

//using cm (concatenate matrix) operator: defines how image must be placed

page.Contents.Add(new Operator.cm(matrix));

XImage ximage = page.Resources.Images[page.Resources.Images.Count];

//using Do operator: this operator draws image

page.Contents.Add(new Operator.Do(ximage.Name));

//using Q operator: this operator restores graphics state

page.Contents.Add(new Operator.Q());

//save updated document

pdfDocument.Save(Response.OutputStream);

Hi Hoang,

First of all, sorry for the delayed in response and thank you for sharing the source code.

I have tested the scenario using updated code mentioned below with Aspose.Pdf v6.6 and it works fine. Kindly download the latest version of Aspose.Pdf v6.6 and check if it fits your need. However, I am unable to produce the issue at my end and this in not possible for us to identify the issue without source file. If your file have confidential data you can mark this thread as private. By using this option, this link will be available only for you and Aspose Team.

[C#]
//open document
Document pdfDocument = new Document();

//set coordinates
int lowerLeftX = 100;
int lowerLeftY = 100;
int upperRightX = 200;
int upperRightY = 200;

//get the page where image needs to be added
Aspose.Pdf.Page page = pdfDocument.Pages.Add();

//load image into stream
FileStream imageStream = new FileStream(@"d:\pdffiles\adobe.tif", FileMode.Open);

//add image to Images collection of Page Resources
page.Resources.Images.Add(imageStream);

//using q operator: this operator saves current graphics state
page.Contents.Add(new Operator.GSave());

//create Rectangle and Matrix objects
Aspose.Pdf.Rectangle rectangle = new Aspose.Pdf.Rectangle(lowerLeftX, lowerLeftY, upperRightX, upperRightY);
Matrix matrix = new Matrix(new double[] { rectangle.URX - rectangle.LLX, 0, 0, rectangle.URY - rectangle.LLY, rectangle.LLX, rectangle.LLY });

//using cm (concatenate matrix) operator: defines how image must be placed
page.Contents.Add(new Operator.ConcatenateMatrix(matrix));
XImage ximage = page.Resources.Images[page.Resources.Images.Count];

//using Do operator: this operator draws image
page.Contents.Add(new Operator.Do(ximage.Name));

//using Q operator: this operator restores graphics state
page.Contents.Add(new Operator.GRestore());

//save updated document
pdfDocument.Save(@"d:\pdffiles\test.pdf");

Please try the changes (marked in bold) in your code.

Also, we will update our documentation with the latest update soon.

Sorry for the inconvenience,

Thanks & Regards,