Hi,
As can be seen in the title, after updating the Aspose Words 14.2 to 15.7, we noticed some issues when converting files to tiff. This only goes for existing tif / tiff images and files we’ve extracted from a .zip or .7z file.
When inputting an image the outputs are as follow:
- Words 14.2: image equal to input
- Words 15.7: image is shrunk to the top left corner of the page, about a quarter of the page.
Keep in mind that the piece of code below is used for multiple types of images, not just tiff. (Including bmp, png, jpeg, tiff, and gif)
Let’s just focus on the tiff to tiff for now - one may want to convert this to e.g. lower the resolution, but for now we input a tiff and want the same image as an output.
Code
Where:
- “Data” is a MemoryStream containing the file.
- "MAX_PAGE_ … " equals 1584, as taken somewhere from the Aspose documentation. The CalculateImageSize is never reached in this example though.
// create the document - no data
document = new Document();
// we use the builder to fill the document
DocumentBuilder builder = new DocumentBuilder(document);
// load the input image from the stream
using (System.Drawing.Image image = System.Drawing.Image.FromStream(Data))
{
// Find which dimension the frames in this image represent. For example
// the frames of a BMP or TIFF are "page dimension" whereas frames of a GIF image are "time dimension".
FrameDimension dimension = new FrameDimension(image.FrameDimensionsList[0]);
// Get the number of frames in the image.
FrameDimension fd = this.Type == FileType.GIF ? FrameDimension.Time : FrameDimension.Page;
int framesCount = image.GetFrameCount(fd);
// Loop through all frames.
for (int frameIdx = 0; frameIdx < framesCount; frameIdx++)
{
// Insert a section break before each new page, in case of a multi-frame TIFF.
if (frameIdx != 0)
builder.InsertBreak(BreakType.SectionBreakNewPage);
// Select active frame.
image.SelectActiveFrame(dimension, frameIdx);
double imageWidth = ConvertUtil.PixelToPoint(image.Width, image.HorizontalResolution);
double imageHeight = ConvertUtil.PixelToPoint(image.Height, image.VerticalResolution);
// If the image is larger than the maximum size for word document: shrink the image.
if (imageWidth >= Words.MAX_PAGE_WIDTH || imageHeight >= Words.MAX_PAGE_HEIGHT)
// Shrink the image
CalculateImageSize(image, Words.MAX_PAGE_HEIGHT, Words.MAX_PAGE_WIDTH, out imageHeight, out imageWidth);
// We want the size of the page to be the same as the size of the image.
// Convert pixels to points to size the page to the actual image size.
PageSetup ps = builder.PageSetup;
ps.PageWidth = imageWidth;
ps.PageHeight = imageHeight;
// Insert the image into the document and position it at the top left corner of the page.
builder.InsertImage(
image,
RelativeHorizontalPosition.Page,
0,
RelativeVerticalPosition.Page,
0,
ps.PageWidth,
ps.PageHeight,
WrapType.None);
}
}
ImageSaveOptions options = new ImageSaveOptions(SaveFormat.Tiff);
// any resolution
// any compression
document.Save(*Some other Stream *, options);
End Code
If there are any questions, let me know.
Cheers,
Knots