PdfSharp and iText7 - itext7

I am replacing PdfSharp with iText7 and I am not sure why when I use the same x and y coordinates I am getting different results. I am using 735 and 520 on both but they are printing on different location on the pdf file. Any help with this would be great. PdfSharp is using a double and iText7 is using float but they are exactly the same, under the hood.
the origin (0, 0) is top left and coordinates grow right and down. The unit of measure is always point (1/72 inch).
http://www.pdfsharp.net/wiki/Graphics.ashx
PdfSharp
PdfSharp.Drawing.XGraphics gfx = PdfSharp.Drawing.XGraphics.FromPdfPage(page);
dfSharp.Drawing.Layout.XTextFormatter tf = new XTextFormatter(gfx);
pnt = new XPoint(735, 520);
gfx.DrawString("Text Enter", font, XBrushes.White, pnt);
iText7
iText.Kernel.Geom.Rectangle rectangle = new iText.Kernel.Geom.Rectangle(735, 520, 100, 100);
Canvas canvas = new Canvas(pdfCanvas, rectangle);
Style normal = new Style();
PdfFont font = PdfFontFactory.CreateFont(StandardFonts.HELVETICA_BOLD);
normal.SetFont(font).SetFontSize(34).SetFontColor(ColorConstants.WHITE);
Paragraph p = new Paragraph()
.Add(_versionNumber)
.SetFontSize(34)
.SetFontColor(ColorConstants.WHITE)
.SetFont(font);

paragraph.SetFixedPosition(0, 800, 200) will put you at the Top left edge of the page, 200 is the width. Itext7 PDF coordinates start at the bottom left. (0,0) is the bottom left corner of the document normally.

Related

Extra pixels at top and bottom of label with image in tkinter

In Python 3.2.5 and tkinter, I keep getting 1 or 2 "extra" border-like pixels at the top and bottom of a middle label (of three stacked labels) containing an image (the sides do not have a white border). No matter how I shrink the window or designate no borderwidth, I get a couple of pixels beyond the top and bottom of the label(s).
Here's the code....
root.withdraw()
LoginErrorMsg = tk.Toplevel()
LoginErrorMsg.title('ERROR! Unauthorized Login Attempt')
# pick .gif file
# load the file and covert it to a tkinter image object
imageFile = "ErrorLogin.gif"
image1 = tk.PhotoImage(file=imageFile)
#set size of Error Message Window
width = 348
height = 480
#get size of the whole screen
xmax = LoginErrorMsg.winfo_screenwidth()
ymax = LoginErrorMsg.winfo_screenheight()
#calculate centered window coordinates and position it
x0 = xmax/2 - width/2
y0 = ymax/2 - height/2
LoginErrorMsg.geometry("%dx%d+%d+%d" % (width, height, x0, y0))
# make the root window the size of the image
#root.geometry("%dx%d+%d+%d" % (325, 475, x+100, y+100))
# remove tk icon
LoginErrorMsg.wm_iconbitmap('Ethicsicon.ico')
#if user closes Error Message Window, clean up memory
LoginErrorMsg.protocol("WM_DELETE_WINDOW", handler)
# root has no image argument, so use a label as a panel
panel1 = tk.Label(LoginErrorMsg, text="\nYou have exceeded three (3) login attempts.", background = "white", borderwidth = 0)
#panel1.configure(background = 'white')
panel1.pack(side='top', fill='both', expand='yes')
panel2 = tk.Label(LoginErrorMsg, image=image1, background = "white", borderwidth = 0)
panel2.pack(side='top', fill='both', expand='yes')
panel3 = tk.Label(LoginErrorMsg, text="", borderwidth = 0)
panel3.pack(side='top', fill= 'both', expand='yes')
# save the panel's image from 'garbage collection'
panel1.image = image1
# put a button on the image panel
button2 = tk.Button(panel3, text = ' OK ', command = LeaveSystemCallback)
button2.pack(side='top')
LoginErrorMsg.update()
Try changing the highlightthickness attribute of each label to zero.

Cropped Image is not aligned in center

I am developing a Photo App in Windows phone 7.
When I crop the image by a Rectangle area, the Cropped image aligned to the top-left corner of the image control. I want to align the image in center and zoom/stretch it to the full image control.
See the figures.
1st Image shows Before Crop and 2nd Image shows after crop, the image aligned top-left corner. the cropped image should be aligned in center. And plz also see the cropped image is not fully come in figure-2, I mean the Portion of Boot of Player is also cropped, but not showing in the cropped image, why this?
Code is:
void ClipImage()
{
RectangleGeometry geo = new RectangleGeometry();
r = (Rectangle)(from c in ImageLayout.Children where c.Opacity == .5 select
c).First();
GeneralTransform gt = r.TransformToVisual(ImageLayout);
Point p = gt.Transform(new Point(0, 0));
geo.Rect = new Rect(p.X, p.Y, r.Width, r.Height);
ImageMain.Clip = geo;
r.Visibility = System.Windows.Visibility.Collapsed;
TranslateTransform t = new TranslateTransform();
t.X = -p.X;
t.Y = -p.Y;
ImageMain.RenderTransform = t;
}
Here in code, I think some values should be changed, to align the image in center. This is just a cropped image code, many other functions also used inside, but is not concerned I think.
If you want to center some XAML element, use HorizontaAlignment.Center and VerticalAlignment.Center.

Fill & Stoke width issue in html 5 canvas

I want to draw a closed shape(Using paths) & my stroke width is 10.
Now,i want to fill that shape,i can fill it using fill() function of context.
But,when i want to change alpha of my shape,then stroke & fill area overlap at border of shape.
I want only fill the area of shape that remains black after my stroke.
I have attached image of explaining my problem.
Click here to show shape with stork & fill bug.
As you can see in jsfiddle,
-- Color of overlapping area are composite color. That i don't want.
I want it to be exactly same as in border(or stroke color with alpha).
-- i am not enable to specify fill area of closed path.(there is no method of contexx.)
-- I can't use "glabalCompositeOperation",because i am drawing more than 1 shapes in 1 canvas in my application.
The effect you are getting seems to be a property of how canvas draws lines round a shape. Half the thickness of the line is drawn inside the shape and half outside the shape. One way round it is to draw the filled shape and the border as seperate paths. The changes to do this for your example are shown below. This will be more difficult with irregular shapes.
var canvas = document.getElementById("myCanvas");
var context = canvas.getContext("2d");
var topLeftCornerX = 188;
var topLeftCornerY = 50;
var width = 200;
var height = 100;
var linewidth = 10;
context.globalAlpha = 0.5;
context.beginPath();
context.moveTo(topLeftCornerX, topLeftCornerY);
context.lineTo(topLeftCornerX+width,topLeftCornerY);
context.lineTo(topLeftCornerX+width,topLeftCornerY+height);
context.lineTo(topLeftCornerX,topLeftCornerY+height);
context.closePath();
context.fillStyle = "#FF0000";
context.fill();
context.beginPath();
context.moveTo(topLeftCornerX-linewidth/2, topLeftCornerY-linewidth/2);
context.lineTo(topLeftCornerX+width+linewidth/2,topLeftCornerY-linewidth/2);
context.lineTo(topLeftCornerX+width+linewidth/2,topLeftCornerY+height+linewidth/2);
context.lineTo(topLeftCornerX-linewidth/2,topLeftCornerY+height+linewidth/2);
context.closePath();
context.lineWidth = linewidth;
context.strokeStyle = "#00FF00";
context.stroke();

Resize image for Live Tile - WriteableBitmapEx

**
Found the solution
Because of the fact this is a tile, the image will always be strechted to 173 by 173!
To avoid this first create a dummy 173 by 173 and merge this with the resized one!
Rect rect = new Rect(0.0, 0.0, width, height);
WriteableBitmap bitmapDummy = new WriteableBitmap(173, 173);
bitmapDummy.Blit(rect, resized, rect, WriteableBitmapExtensions.BlendMode.None);
**
Well I have created a Background agent to update the live tile of my WP7 app.
But no matter what I try to resize it, I'm not getting a good result!
Any tips? Currently I have following code, but I also tried 135 by 173 and also the other Interpolation.
WriteableBitmap writeableBitmap = new WriteableBitmap(bitmapImage);
var resized = writeableBitmap.Resize(173, 173, System.Windows.Media.Imaging.WriteableBitmapExtensions.Interpolation.Bilinear);
There is also a small rectangle added below to show the title of the app! It's 40px in height, would be great if image would be cropped above.
The actual image is always 250 by 321px
Your problem is that you're not calculating the width/heights to a correct Aspect ratio.
So to get a 1:1 proportions, you would need a width of 134.735 pixels, for a 173 pixel height.
This can be done by first determining what side is the largest
var aspect = Math.Max(bitmapImage.Width, bitmapImage.Height)
var ratio = largest / 173;
var width = width / ratio;
var height = height / ratio;
var resizedImage = writeableBitmap.Resize(width, height, System.Windows.Media.Imaging.WriteableBitmapExtensions.Interpolation.Bilinear);
And remember to set Stretch="Uniform" to avoid stretching the image to unnecessary proportions.
To create a 173x173 pixel image, with the other image applied on top, use the Blit function from WriteableBitmapEx
var tileImage = new WriteableBitmap(173, 173, ...)
tileImage.Blit(new Rect(width, height), resizedImage, new Rect(width, height), BlendMode.None);

HTML5 Canvas Image Rotation

I have a canvas where i have drawn a series of images onto it that can be dragged and dropped around, each image is a seperate entity.
I need to, depending on where the image is dropped rotate that image so that it looks appropiate, imaging dragging a triangle around a circle, the base line needs to always point outwards.
I thought i would be able to rotate the image and then draw that on the canvas, but what i seem to find when searching on the internet is that the canvas rotates each time - is it possible to rotate the image only and then place it on the canvas?
Many Thanks!
Yes you can, but it's not as simple as setting a rotation attribute. The way canvas works you can't rotate individual items specifically, only the whole canvas.
To rotate a single item, you'll need to rotate the canvas, place the item, and then rotate the canvas back to it's original orientation.
This question has a few examples: How do I rotate a single object on an html 5 canvas?
Before placing the image on the canvas, create a buffer canvas, place image there, rotate it and then port to main canvas.
var canvas = document.getElementById('test_canvas');
var ctx = canvas.getContext('2d');
var buffer = document.createElement('canvas');
buffer.width = buffer.height = 60;
var bctx = buffer.getContext('2d');
bctx.translate(30, 30);
bctx.rotate(0.5);
bctx.fillStyle = 'rgb(255, 0, 0)';
bctx.fillRect(-15, -15, 30, 30);
ctx.fillStyle = 'rgb(0, 255, 0)';
ctx.fillRect(30, 30, 30, 30);
ctx.drawImage(buffer, 50, 50);
You can check this code at JSFiddle I've setup - http://jsfiddle.net/dzenkovich/UdaUA/2/
Note you'll need to keep a close eye at the rotation point and buffer canvas size for this to work.
For more info check out this article I've found http://creativejs.com/2012/01/day-10-drawing-rotated-images-into-canvas/

Resources