enicolasgomez#28397
Hello,
wrote the following code to produce text. This is a module in a MVC controller which is called from a webpage that sends all of those parameters using ajax and renders back to a Canvas element. I have two problems with the following approach:
Is there any way to improve this approach?
wrote the following code to produce text. This is a module in a MVC controller which is called from a webpage that sends all of those parameters using ajax and renders back to a Canvas element. I have two problems with the following approach:
- Using PdfWriter (which is the only vector based graphic interface) so I need to convert it to SVG before sending back to Canvas
- Writes to an actual physicall file which makes the process slow, would like to know if I can do this in a memory stream or any other similar in-memory data structure
Is there any way to improve this approach?
var aFont = "Arial";
var aFontSize = 24f;
var aText = "Graphics Mill by Aurigma";
var aColor = System.Drawing.Color.Blue;
System.Drawing.RectangleF blackBox;
var dpi = 150f;
using (var bitmap = new Bitmap(1, 1, PixelFormat.Format24bppRgb))
{
bitmap.DpiX = dpi;
bitmap.DpiY = dpi;
using (var lGraphics = bitmap.GetAdvancedGraphics())
{
var lFont = lGraphics.CreateFont(aFont, aFontSize);
var lBrush = new SolidBrush(aColor);
var lText = new PlainText(aText, lFont, lBrush);
lText.Position = new System.Drawing.PointF(0f, 0f);
blackBox = lText.GetBlackBox();
}
}
using (var lPdfWriter = new PdfWriter(@"c:\eps\out.pdf"))
using (var lGraphics = lPdfWriter.GetGraphics())
{
lPdfWriter.AddPage((int)Math.Ceiling(blackBox.Width), (int)Math.Ceiling(blackBox.Height), dpi, dpi);
{
var lFont = lGraphics.CreateFont(aFont, aFontSize);
var lBrush = new SolidBrush(aColor);
var lText = new PlainText(aText, lFont, lBrush)
{
Position = new System.Drawing.PointF(0, - blackBox.Y)
};
lGraphics.DrawText(lText);
}
}