@intranote, @tak
Thanks for your patience.
We have investigated the earlier logged issue and found that first operator of first page in Test111.pdf
was:
0.750000 0.000000 0.000000 -0.750000 0.000000 612.000000 cm
Which was why for all page contents specified transformation matrix applied which rotated contents by 180 degrees. So when image was added it was also rotated.
Please consider following possible solutions:
Determine transformation at the end of contents, and multiply your matrix by reversed current transformation matrix:
public void InsertPngImageIntoDocument(Stream document, Stream image, int pageNumber, double imageHeight,
double imageWidth, double imageLeft, double imageBottom, ref MemoryStream output)
{
if (document == null || image == null)
return;
document.Seek(0, SeekOrigin.Begin);
Aspose.Pdf.Document PdfDocument = new Aspose.Pdf.Document(document);
//Get the page where image needs to be added
Aspose.Pdf.Page Page = PdfDocument.Pages[pageNumber];
//Set coordinates
double LowerLeftX = imageLeft;
double LowerLeftY = imageBottom;
double UpperRightX = imageLeft + imageWidth;
double UpperRightY = imageBottom + imageHeight;
//Add image to Images collection of Page Resources
Page.Resources.Images.Add(image);
//Using GSave operator: this operator saves current graphics state
Page.Contents.Add(new Aspose.Pdf.Operator.GSave());
//Create Rectangle and Matrix objects
Aspose.Pdf.Rectangle Rectangle = new Aspose.Pdf.Rectangle(LowerLeftX, LowerLeftY, UpperRightX, UpperRightY);
Aspose.Pdf.Matrix matrix = new Aspose.Pdf.Matrix(new double[]
{
Rectangle.URX - Rectangle.LLX,
0,
0,
Rectangle.URY - Rectangle.LLY,
Rectangle.LLX,
Rectangle.LLY
});
//this is transformation matrix specified in source document
Matrix m = new Matrix(0.75, 0, 0, -0.75, 0, 612);
//calculate reverse matrix
m = m.Reverse();
//modify our matrix by reversed matrix
matrix = matrix.Multiply(m);
//Using ConcatenateMatrix (concatenate matrix) operator: defines how image must be placed
Page.Contents.Add(new Aspose.Pdf.Operator.ConcatenateMatrix(matrix));
Aspose.Pdf.XImage Ximage = Page.Resources.Images[Page.Resources.Images.Count];
//Using Do operator: this operator draws image
Page.Contents.Add(new Aspose.Pdf.Operator.Do(Ximage.Name));
//Using GRestore operator: this operator restores graphics state
Page.Contents.Add(new Aspose.Pdf.Operator.GRestore());
PdfDocument.Save(output);
}
Please note that in above case you will need to analyze page contents, in order to check which transformation matrix is set after last operator.
Add operators to draw image BEFORE page contents:
public void InsertPngImageIntoDocument(Stream document, Stream image, int pageNumber, double imageHeight,
double imageWidth, double imageLeft, double imageBottom, ref MemoryStream output)
{
if (document == null || image == null)
return;
document.Seek(0, SeekOrigin.Begin);
Aspose.Pdf.Document PdfDocument = new Aspose.Pdf.Document(document);
//Get the page where image needs to be added
Aspose.Pdf.Page Page = PdfDocument.Pages[pageNumber];
//Set coordinates
double LowerLeftX = imageLeft;
double LowerLeftY = imageBottom;
double UpperRightX = imageLeft + imageWidth;
double UpperRightY = imageBottom + imageHeight;
//Add image to Images collection of Page Resources
Page.Resources.Images.Add(image);
Page.Contents.Insert(1, new Aspose.Pdf.Operator.GSave());
//Create Rectangle and Matrix objects
Aspose.Pdf.Rectangle Rectangle = new Aspose.Pdf.Rectangle(LowerLeftX, LowerLeftY, UpperRightX, UpperRightY);
Aspose.Pdf.Matrix matrix = new Aspose.Pdf.Matrix(new double[]
{
Rectangle.URX - Rectangle.LLX,
0,
0,
Rectangle.URY - Rectangle.LLY,
Rectangle.LLX,
Rectangle.LLY
});
//Using ConcatenateMatrix (concatenate matrix) operator: defines how image must be placed
Page.Contents.Insert(2, new Aspose.Pdf.Operator.ConcatenateMatrix(matrix));
Aspose.Pdf.XImage Ximage = Page.Resources.Images[Page.Resources.Images.Count];
//Using Do operator: this operator draws image
Page.Contents.Insert(3, new Aspose.Pdf.Operator.Do(Ximage.Name));
//Using GRestore operator: this operator restores graphics state
Page.Contents.Insert(4, new Aspose.Pdf.Operator.GRestore());
PdfDocument.Save(output);
}
Possible disadvantage of above approach is that, image may be hidden or overlapped by page contents.
Add q - Q (GSave - GResore) operators before and after page contents, so initial matrix (1 0 0 1 0 0 ) will be restored:
public void InsertPngImageIntoDocument(Stream document, Stream image, int pageNumber, double imageHeight,
double imageWidth, double imageLeft, double imageBottom, ref MemoryStream output)
{
if (document == null || image == null)
return;
document.Seek(0, SeekOrigin.Begin);
Aspose.Pdf.Document PdfDocument = new Aspose.Pdf.Document(document);
//Get the page where image needs to be added
Aspose.Pdf.Page Page = PdfDocument.Pages[pageNumber];
//Set coordinates
double LowerLeftX = imageLeft;
double LowerLeftY = imageBottom;
double UpperRightX = imageLeft + imageWidth;
double UpperRightY = imageBottom + imageHeight;
//Add image to Images collection of Page Resources
Page.Resources.Images.Add(image);
//Add GSave and GRestore operators before and after page contents
Page.Contents.Insert(1, new Aspose.Pdf.Operator.GSave());
Page.Contents.Add(new Aspose.Pdf.Operator.GRestore());
//Using GSave operator: this operator saves current graphics state
Page.Contents.Add(new Aspose.Pdf.Operator.GSave());
//Create Rectangle and Matrix objects
Aspose.Pdf.Rectangle Rectangle = new Aspose.Pdf.Rectangle(LowerLeftX, LowerLeftY, UpperRightX, UpperRightY);
Aspose.Pdf.Matrix matrix = new Aspose.Pdf.Matrix(new double[]
{
Rectangle.URX - Rectangle.LLX,
0,
0,
Rectangle.URY - Rectangle.LLY,
Rectangle.LLX,
Rectangle.LLY
});
//Matrix m = new Matrix(0.75, 0, 0, -0.75, 0, 612);
//m = m.Reverse();
//matrix = matrix.Multiply(m);
//Using ConcatenateMatrix (concatenate matrix) operator: defines how image must be placed
Page.Contents.Add(new Aspose.Pdf.Operator.ConcatenateMatrix(matrix));
Aspose.Pdf.XImage Ximage = Page.Resources.Images[Page.Resources.Images.Count];
//Using Do operator: this operator draws image
Page.Contents.Add(new Aspose.Pdf.Operator.Do(Ximage.Name));
//Using GRestore operator: this operator restores graphics state
Page.Contents.Add(new Aspose.Pdf.Operator.GRestore());
PdfDocument.Save(output);
}
Use ImageStamp:
public void InsertPngImageIntoDocument(Stream document, Stream image, int pageNumber, double imageHeight,
double imageWidth, double imageLeft, double imageBottom, ref MemoryStream output)
{
if (document == null || image == null)
return;
document.Seek(0, SeekOrigin.Begin);
Aspose.Pdf.Document PdfDocument = new Aspose.Pdf.Document(document);
ImageStamp stamp = new ImageStamp(image);
stamp.BottomMargin = imageBottom;
stamp.LeftMargin = imageLeft;
stamp.Width = imageWidth;
stamp.Height = imageHeight;
PdfDocument.Pages[1].AddStamp(stamp);
PdfDocument.Save(output);
}
Please use Aspose.PDF for .NET 18.3 while applying any of the above solutions and in case you still face any issue, please feel free to contact us.