We need to convert multiple 32bpp TIF sources - all are provided via MemoryStream TIFs - to a single CCITT4 1bpp grayscale TIF MemoryStream which is then returned.
All TIFs may contain one or more frames.
The object may also be converted to grayscale PDF MemoryStream - but not all the time.
The conversion algorithms and built-in types I have found in Aspose drop the gray-scaled logo on the cover sheet completely (built-in specifying IsBlackWhite=true and also using WinAPIIndexBitmapConverter()).
We need to convert to standard G4 black and white grayscaled image - not either black or white.
The method I am writing is called inside BizTalk and speed is important.
Ideally the ultimate solution would be methods like these two returning 1bpp MemoryStreams:
MemoryStream Convert32bppTIFtoG4GrayscaleTIF(MemoryStream ms32bppTIFIn)
which could then be used separately and by:
MemoryStream Convert32bppTIFArraytoG4GrayscaleTIF(MemoryStream [] msArrayOf32bppTIFIn)
There is an older post requesting this - but no update or info how to with current resources (https://forum.aspose.com/t/128073)
(Our company has ordered Aspose.Total - so all will be available)
Our current code - using samples found on various pages on your forum and documentation does work for strictly Black and White - but I need to do Grayscale conversions.
public static MemoryStream GetStreamOfReportAndImages(...)
{
// Get the cover page sheet as a TIFF image MemoryStream
// Note that this is a 32 bit tiff and will require conversion
MemoryStream report = GetReport(reportName); // returns stream of CoverPage
//Instantiate Pdf instance by calling its empty constructor
Aspose.Pdf.Generator.Pdf pdf1 = new Aspose.Pdf.Generator.Pdf();
//Add a section into the pdf document
Aspose.Pdf.Generator.Section sec = pdf1.Sections.Add();
//Create an image object in the section
Aspose.Pdf.Generator.Image imageht = new Aspose.Pdf.Generator.Image(sec);
//Set the type of image using ImageFileType enumeration
imageht.ImageInfo.ImageFileType = Aspose.Pdf.Generator.ImageFileType.Tiff;
imageht.ImageInfo.TiffFrame = -1;
//imageht.ImageInfo.IsBlackWhite = true;
// Specify the image source of initial pdf as MemoryStream
imageht.ImageInfo.ImageStream = report;
//Add image object into the Paragraphs collection of the section
sec.Paragraphs.Add(imageht);
// Add TIFs from imagePaths array
for (int i = 0; i < imagePaths.Length; i++)
{
// Specify the image source as MemoryStream
using (Stream imageFile = new FileStream(imagePaths[i], FileMode.Open, FileAccess.Read))
{
byte[] tmpBytes = new byte[imageFile.Length];
imageFile.Read(tmpBytes, 0, Convert.ToInt32(imageFile.Length));
MemoryStream copiedStream = new MemoryStream(tmpBytes);
copiedStream.Seek(0, SeekOrigin.Begin);
System.Drawing.Bitmap b = new System.Drawing.Bitmap(copiedStream);
//Create a new section in the Pdf document
sec = new Aspose.Pdf.Generator.Section(pdf1);
// Set margins so image will fit, etc.
sec.PageInfo.Margin.Top = 2;
sec.PageInfo.Margin.Bottom = 2;
sec.PageInfo.Margin.Left = 2;
sec.PageInfo.Margin.Right = 2;
sec.PageInfo.PageWidth = (b.Width / b.HorizontalResolution) * 72;
sec.PageInfo.PageHeight = (b.Height / b.VerticalResolution) * 72;
//Add the section in the sections collection of the Pdf document
pdf1.Sections.Add(sec);
//Create an image object
imageht = new Aspose.Pdf.Generator.Image(sec);
//Add the image into paragraphs collection of the section
imageht.ImageInfo.ImageFileType = Aspose.Pdf.Generator.ImageFileType.Tiff;
// set IsBlackWhite property to true for performance improvement
imageht.ImageInfo.IsBlackWhite = true;
// import ALL pages of TIFF
imageht.ImageInfo.TiffFrame = -1;
//Set the ImageStream to a MemoryStream object
imageht.ImageInfo.ImageStream = copiedStream;
//Set desired image scale
imageht.ImageScale = 1.0F; // 0.95F;
//Add image object into the Paragraphs collection of the section
sec.Paragraphs.Add(imageht);
if (b != null)
b.Dispose();
}
}
// Create base PDF for future processing
Aspose.Pdf.Document newPdf = new Aspose.Pdf.Document(pdf1);
//newPdf.Save(@"C:\Temp\newPdf_test.pdf"); // /test save all pages to PDF file
pageCount = newPdf.Pages.Count;
//create TiffSettings object
TiffSettings tiffSettings = new TiffSettings();
tiffSettings.Compression = CompressionType.CCITT4;
tiffSettings.Depth = ColorDepth.Format1bpp;
//create TIFF device
TiffDevice tiffDevice = new TiffDevice(PageSize.PageLetter, tiffSettings, new WinAPIIndexBitmapConverter());
//convert a particular page and save the image to stream
tiffDevice.Process(newPdf, mergedImage);
// Output to TIF MemoryStream
if (format == ReportFormatType.PDF)
{
// output requested is PDF - reconvert new 1bpp TIF to PDF MemoryStream
Aspose.Pdf.Generator.Pdf pdfout = new Aspose.Pdf.Generator.Pdf();
Aspose.Pdf.Generator.Section secout = new Aspose.Pdf.Generator.Section(pdfout);
// Set margins so image will fit, etc.
secout.PageInfo.Margin.Top = 0;
secout.PageInfo.Margin.Bottom = 0;
secout.PageInfo.Margin.Left = 0;
secout.PageInfo.Margin.Right = 0;
pdfout.Sections.Add(secout);
//Create an image object in the section
Aspose.Pdf.Generator.Image imageout = new Aspose.Pdf.Generator.Image(secout);
//Set the type of image using ImageFileType enumeration
imageout.ImageInfo.ImageFileType = Aspose.Pdf.GeneratorImageFileType.Tiff;
imageout.ImageInfo.TiffFrame = -1;
//imageout.ImageInfo.IsBlackWhite = true;
imageout.ImageScale = 1.0F;
;/Add image object into the Paragraphs collection of the section
secout.Paragraphs.Add(imageout);
// Specify the image source as MemoryStream
imageout.ImageInfo.ImageStream = mergedImage;
//pdfout.Save(@"C:\Temp\TifToPDFBW.pdf"); // uncomment and comment below Save to test
// Output to PDF MemoryStream
pdfout.Save(mergedImage);
}
newPdf.Dispose();
return mergedImage;
}