views:

176

answers:

1

I'm sure I'm making some kind of silly mistake here, but when converting a tiff file to PDF, the colours become reversed. I can't figure out why. Here's my code:

Document document = new Document(PageSize.A4, 50, 50, 50, 50);

PdfWriter writer = PdfWriter.GetInstance(document, new FileStream("Image.pdf", FileMode.Create));

System.Drawing.Bitmap bm = new System.Drawing.Bitmap(@"C:\Temp\338814-00.tif");
int total = bm.GetFrameCount(FrameDimension.Page);                

document.Open();
PdfContentByte cb = writer.DirectContent;

for (int k = 0; k < total; ++k)
{
 bm.SelectActiveFrame(FrameDimension.Page, k);
 MemoryStream ms = new MemoryStream();
 bm.Save(ms, ImageFormat.Tiff);

 Image img = Image.GetInstance(ms.ToArray());

 img.ScalePercent(72f / (float)img.DpiX * 100);
 img.SetAbsolutePosition(0, 0);

 cb.AddImage(img);
 document.NewPage();     
}
document.Close();

Thanks.

A: 

The best solution appears to be to replace the line:

bm.Save(ms, ImageFormat.Tiff);

with

bm.Save(ms, ImageFormat.Png);

Png seems to give the best speed/file size performance for Tiff images. Why this works I still have no idea.

spiderdijon
The most likely reason is that 1-bit TIFF images usually (but not always) include a color map to specify the sense of black and white. In addition, they also have a tag called photometric interpretation which indicates whether 0 is black or 0 is white. Between these two settings, there are 4 opportunities to get it wrong.
plinth