You could use the System.Drawing library to create a TIFF image; the library includes text options so you'd just need to work out the right font sizes and positioning.
Once you have a TIFF file created in a stream, you can write that to the document.
I don't have a process that does exactly what you're doing, but I do have bits from other processes that could give you a general idea of the different steps.
I usually target an image size of 2550x3000, which is 8.5″x11″ at 300DPI
Here's an example of code we use to generate stamp images dynamically (you'd want to save it to ImageFormat.Tiff instead of ImageFormat.Bmp so there's some encoder stuff you may want too).
02 | Bitmap bmp = new Bitmap(width, height); |
03 | using (Graphics g = Graphics.FromImage(bmp)) |
06 | StringFormat stringFormat = new StringFormat(); |
10 | stringFormat.Alignment = StringAlignment.Center; |
11 | stringFormat.LineAlignment = StringAlignment.Center; |
14 | g.TextRenderingHint = TextRenderingHint.AntiAlias; |
15 | System.Drawing.Font line1 = new System.Drawing.Font( "Arial" , 60, FontStyle.Bold, GraphicsUnit.Point); |
16 | System.Drawing.Font line2 = new System.Drawing.Font( "Arial" , 30, FontStyle.Bold, GraphicsUnit.Point); |
17 | System.Drawing.Font line3 = new System.Drawing.Font( "Arial" , 25, FontStyle.Bold, GraphicsUnit.Point); |
23 | g.DrawString(type.ToUpper(), line1, Brushes.Black, new Rectangle(0, 0, width, 150), stringFormat); |
24 | g.DrawString(vtoken, line2, Brushes.Black, new Rectangle(0, 165, width, 150), stringFormat); |
25 | g.DrawString(subtext, line3, Brushes.Black, new Rectangle(0, 245, width, 150), stringFormat); |
29 | BitmapData bmpData = bmp.LockBits( new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, PixelFormat.Format1bppIndexed); |
30 | bmp = new Bitmap(width, height, bmpData.Stride, System.Drawing.Imaging.PixelFormat.Format1bppIndexed, bmpData.Scan0); |
33 | MemoryStream ms = new MemoryStream(); |
34 | bmp.Save(ms, ImageFormat.Tiff); |
Here's some code I use to draw a TIFF back to PagePart.Image (we use this in a process that auto-resizes documents that have unnecessarily high dimensions or DPI).
02 | ms.Seek(0, SeekOrigin.Begin); |
05 | using (Stream writer = page.WritePagePart(PagePart.Image, ( int )ms.Length)){ |
06 | byte [] buffer = new byte [0x8000]; |
08 | while ((count = ms.Read(buffer, 0, buffer.Length)) > 0){ |
09 | writer.Write(buffer, 0, count); |
I think ImageFormat.Tiff defaults to LZW or uncompressed, but if you want to force it to use something different, like monochrome for smaller file sizes, then you want something like this:
1 | EncoderParameters encoderParameters = new EncoderParameters(2); |
2 | encoderParameters.Param[0] = new EncoderParameter(Encoder.Compression,compressionType[compressionIndex]); |
3 | encoderParameters.Param[1] = new EncoderParameter(Encoder.Quality,100L); |
6 | ImageCodecInfo tiffEncoder = GetEncoderInfo( "image/tiff" ); |
9 | newImg.Save(ms,tiffEncoder,encoderParameters); |
The compression type arrays it references are defined like so
01 | string [] compressionTypeName = new string [5] { |
04 | "Facsimile - compatible CCITT Group 3" , |
08 | long [] compressionType = new long [5] { |
09 | ( long )EncoderValue.CompressionNone, |
10 | ( long )EncoderValue.CompressionCCITT3, |
11 | ( long )EncoderValue.CompressionCCITT3, |
12 | ( long )EncoderValue.CompressionCCITT4, |
13 | ( long )EncoderValue.CompressionLZW |
And the GetEncoderInfo method is as follows
01 | private static ImageCodecInfo GetEncoderInfo( string mimeType){ |
03 | ImageCodecInfo[] encoders = ImageCodecInfo.GetImageEncoders(); |
04 | for ( int i = 0; i < encoders.Length; i++) |
06 | if (encoders[i].MimeType == mimeType) |
However, in your case, you could probably set that with static values and would probably want Group 4 rather than LZW unless you want/need color.
I only needed dynamic since I'm resizing existing images and the source image codecs can vary.