Canarys | IT Services

Blogs

Displaying Postscript text and Vector images on PDF in C#.net using Imageglue

Date:
Author:
Share

Postscript is a language used for creating vector Images,Vector images are made up of many scalable objects  and they are  defined  by mathematical equations rather than the pixels ,so they always renders higher quality images.

The Advantages of Vector Images over Bitmap images

1.Vector images are resolution independent, how much ever we resize the image the quality remains the same.

VectorBitmapExample.png

2.Vector Images images does not contains rectangular background as the bitmap images.

vectorbitmap.gif

Rasterization

Conversion of vector images to bitmap image is referred as Rasterization.

 When you convert a vector image to a bitmap, you can specify the output resolution of the final bitmap for whatever size you need. It’s always important to save a copy of your original vector image in its native format (EPS, SVG) before converting it to a bitmap; once it has been converted to a bitmap, the image loses all the wonderful qualities it had in its vector state. If you convert a vector to a bitmap at a size of 100 by 100 pixels and then decide you need the image to be larger, you’ll need to go back to the original vector file and export the image again. Also keep in mind that opening a vector image in a bitmap editing program usually destroys the vector qualities of the image and converts it to raster data.

The most common reason for wanting to convert a vector to a bitmap would be for use on the Web.

Drawing text using postscript

      MyScript = “/” + Font name of text + ” findfont ” + Font size of text in double+ ” scalefont setfont ” + red + ” ” + green + ” ” + blue + ” setrgbcolor ” + x Position of text in double + ” ” + y Position of text in double + ” translate ” + Angle of rotation in integer+ ” rotate ” + x Scale factor in double+ ” ” + y Scale factor in double+ ” scale newpath 0 0 moveto (” + Text to be drawn + “) show “

Drawing a line using postscript

MyScript = “” + Thickness of underline in Double + ” setlinewidth ” + red + ” ” + green + ” ” + blue + ” setrgbcolor ” + Starting X position + ” ” + Starting Y position + ” ” + ” moveto ” + Final X position + ” ” + Final Y position + ” lineto stroke  show”;

Displaying postscript text and Vector images on pdf using C#.net.

The .Net framework does not support the drawing of Vector graphics or displaying of vector graphics. To overcome by this we have used a third party control by name IMAGEGLUE (www.websupergoo.com/imageglue-1.htm).

Steps to draw text and vector images on pdf using Imageglue and c#.net

1. Create a pdf file and save it in a temporary folder as below.

string strFileName = “VectorImage.pdf”;

string path = Server.MapPath(“.”) + “\PDFFiles\”; //saves temporary to this folder

2.Create a object of the class Canvas of the Imageglue .

This Canvas object provides a space for drawing and painting, It has various properties such as size and color and methods for drawing a variety of objects.

Pass the Width,Height  and color of the canvas as the parameter  as below.

Canvas canvas = new Canvas(intdivdropWidth, intdivdropHeight, new XColor(objCommonFunctions.GetColorforHEXCode(“#FFFFFF”)));

3.Draw the text on canvas as below

String MyScript;

MyScript = “/” + Fontname of text + ” findfont ” + Fontsize of text in double+ ” scalefont setfont ” + red + ” ” + green + ” ” + blue + ” setrgbcolor ” + x Position of text in double + ” ” + y Position of text in double + ” translate ” + Angle of rotation in integer+ ” rotate ” + x Scalefactor in double+ ” ” + y Scalefactor in double+ ” scale newpath 0 0 moveto (” + Text to be drawn + “) show “;

canvas.DrawPostscript(MyScript, new DrawOptions());

4. Draw the Image on canvas as below

String FileName = Server.UrlDecode(FileName);//Fetch the raster or vector image(EPS,SVG)

String  strWidth = “20”; //Width of the Image

String  strHeight = “20”; //Height of the Image

Int   intX=50; //X position  of the Image

Int   intY=50; //Y position  of the Image

WebSupergoo.ImageGlue7.XImage  image = null;

WebSupergoo.ImageGlue7.DrawOptions drawOpts = new DrawOptions(canvas);

drawOpts.ImageFit = DrawOptions.ImageFitType.Stretch;

drawOpts.Transparency = true;

image = XImage.FromFile(FileName);

drawOpts.Transform.Translate(double.Parse(intX.ToString()), double.Parse(intY.ToString()));

canvas.DrawImage(image, double.Parse(strWidth), double.Parse(strHeight), drawOpts);

image.Dispose(); //Releasing the resources

5.Saving  the Canvas as PDF

XExport objexport = new XExport();

 objexport.Pdf.CropBox = new XRect(intdivdropWidth, intdivdropHeight);

canvas.SaveAs(Server.MapPath(“~/PDFFiles/” + strFileName), objexport);

The below image shows the output, you can observe the quality of the vector image of tiger and the raster image of the tiger but the quality of the raster image(left side) is poor compare to vector image(right side). If you draw text using DrawPostscript method of Imageglue the quality of the text is good compare to drawing the text by using DrawText method of Imageglue.

compare

Leave a Reply

Your email address will not be published. Required fields are marked *

Reach Us

With Canarys,
Let’s Plan. Grow. Strive. Succeed.