How set rotation angle a page in itext 7 - itext

How can i rotate page at a specified angle(for example 25 degrees)?
PdfCanvas content = new PdfCanvas(pdfDoc.addNewPage());
for (int i =1 ; i <= srcDoc.getNumberOfPages(); i++) {
PdfFormXObject page = srcDoc.getPage(i).copyAsFormXObject(pdfDoc);
content.add(page...);
}
Can i set RotationAngle to work with PdfFormXObject ? Or is there another way?

The easiest way is by using an AffineTransform (from com.itextpdf.kernel.geom):
PdfCanvas content = new PdfCanvas(pdfDoc.addNewPage());
PageSize pageSize = pdfDoc.getDefaultPageSize();
PdfFormXObject page = srcDoc.getPage(1).copyAsFormXObject(pdfDoc);
AffineTransform transform = AffineTransform.getRotateInstance(25 * Math.PI / 180, (pageSize.getLeft() + pageSize.getRight())/2, (pageSize.getBottom() + pageSize.getTop())/2);
content.concatMatrix(transform);
content.addXObject(page, 0, 0);
(RotatePageXObject test testAddPage25Degree)
AffineTransform.getRotateInstance is documented to
* Get an affine transformation representing a counter-clockwise rotation over the passed angle,
* using the passed point as the center of rotation
so we feed it the angle (transformed to radians) and the coordinates of the page center.
Applied to this source PDF it creates this result:

Related

how to copy a part of a raw image

I want to copy selected part of a raw image to another image
I get start and end position as percentage and by that I can calculate the start and end position in width
how can I copy that selected part to another raw image?
Assuming it's a Texture2D, you can do the following:
Calculate A texture start/end X (dX)
Create a new Texture2D (B), sized as dX and full Y
Call A.GetPixels()
Iterate on array copying pixels to new texture
Apply on new texture
Pseudo code:
var aPixels = aTexture.GetPixels();
var bWidth = endX - startX;
var bTexture = new Texture2D(bWidth, endY);
var bPixels = bTexture.GetPixels();
for (int x = startX; x < endX; x++)
{
for (int y = 0; y < endY; y++)
{
var aIndex = x + y * A.width;
var bIndex = (x - startX) + y * bWidth;
bPixels[bIndex] = aPixels[aIndex];
}
}
bTexture.Apply();
Note that my code quite possibly won't work; as I'm typing this on a mobile phone.
Usually, Image Processing is an expensive process for CPUs, so I don't recommend it in Unity,
But anyway, For your image and in this special case, I think you can crop your image by changing the Size and Offset of texture in material.
Update:
This is an example of what I mentioned:
You can calculate Tile and Offset based on the dragged mouse position on Texture. (Check Here)
I found this.
you can set start coordinates and width and height to GetPixels();
void Start () {
public Texture2D mTexture;
Color[] c = mTexture.GetPixels (startX, startY, width, height);
Texture2D m2Texture = new Texture2D (width, height);
m2Texture.SetPixels (c);
m2Texture.Apply ();
gameObject.GetComponent<MeshRenderer> ().material.mainTexture = m2Texture;
}
```

What is the size limit of the gameobjects?

I am building an AR application with unity and Mapbox. I have a point to represents a building. I can geolocate the point through Mapbox. I want to see this object everywhere. So, I change the size of objects according to distance.
Firstly, the code is working. But, I could not see the point really far away, 5 km away.
// Update is called once per frame
void Update()
{
// Get user location
// Latitude
x = getLocation.x1.ToString();
user_lat = Convert.ToDouble(x);
user_lat_rad = Math.PI * user_lat / 180.0; // Radian
// Longitude
y = getLocation.y1.ToString();
user_lon = Convert.ToDouble(y);
user_lon_rad = Math.PI * user_lon / 180.0; // Radian
// Change POIs sizes
distances = distance(user_lat_rad, user_lon_rad);
double s = 0.3; // size of the poi
double d = 50f; // specific distance to point (reference distance)
double size = (distances * s) / d;
float size2 = Convert.ToSingle(size);
temp = transform.localScale;
temp.x = size2;
temp.y = size2;
temp.z = size2;
transform.localScale = temp;
}
public double distance(double lat2, double lon2)
{
// Haversine Formula
// Lat2,Lon2 = User Location
// Lat1,Lon1 = POI Location
double dist1 = Sqrt((Pow(Sin((lat2 - lat1) / 2), 2)) + Cos(lat2) * Cos(lat2) * (Pow(Sin((lon2 - lon1) / 2), 2)));
double distance = 2 * r * Asin(dist1);
return distance;
}
Why I couldn't see the point far away even though object size is change? Is there any limitation for this?
As said in the comments the issue most probably is a too small value for the Camera's farClipPlane.
The furthest point relative to the camera that drawing will occur.
Any object/triangle that is further away from the Camera will not be rendered.
In the Inspector it is configured on the Camera component &rightarrow; Clipping Planes &rightarrow; Far
or using code
cameraReference.farClipPlane = XYZ;

ITextSharp - Draw Rectangle top left corner

I'm trying to draw a rectangle to the very top left of a page using ITextSharp (5.5.13). I want to draw in the page margins. However, the rectangle is around 25 pixels too low. How can I draw the rectangle in the top left corner?
Below is how I'm adding the rectangle to the page:
using (PdfReader reader = new PdfReader(inputPdf.FullName))
using (PdfStamper stamper = new PdfStamper(reader, new FileStream(outputPdf.FullName, FileMode.Create)))
{
PdfContentByte contentByte = stamper.GetOverContent(1);
PdfDocument doc = contentByte.PdfDocument;
float X = 0.0f;
float Y = 0.0f;
float Height = Utilities.InchesToPoints(0.50f);
float Width = Utilities.InchesToPoints(0.50f);
float llx = (doc.Left - doc.LeftMargin) + X;
float lly = (doc.Top - doc.TopMargin) - (Height + Y);
float urx = (doc.Left - doc.LeftMargin) + Width + X;
float ury = (doc.Top - doc.TopMargin) - Y;
Rectangle rectangle = new Rectangle(llx, lly, urx, ury)
{
BackgroundColor = BaseColor.BLACK
};
contentByte.Rectangle(rectangle);
}
Below are the debug values for each aforementioned variable:
Whenever you use a PdfStamper, the PdfDocument you can retrieve from its parts does not contain sensible information, it merely is a dummy object.
Thus, don't try to determine the page size from that PdfDocument doc, instead use the appropriate methods or properties of your PdfReader reader, e.g.
/** Gets the crop box without taking rotation into account. This
* is the value of the /CropBox key. The crop box is the part
* of the document to be displayed or printed. It usually is the same
* as the media box but may be smaller. If the page doesn't have a crop
* box the page size will be returned.
* #param index the page number. The first page is 1
* #return the crop box
*/
virtual public Rectangle GetCropBox(int index)

EaselJS shape x,y properties confusion

I generate a 4x4 grid of squares with below code. They all draw in correct position, rows and columns, on canvas on stage.update(). But the x,y coordinates for all sixteen of them on inspection are 0,0. Why? Does each shape has it's own x,y coordinate system? If so, if I get a handle to a shape, how do I determine where it was drawn originally onto the canvas?
The EaselJS documentation is silent on the topic ;-). Maybe you had to know Flash.
var stage = new createjs.Stage("demoCanvas");
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
var square = new createjs.Shape();
square.graphics.drawRect(i*100, j*100, 100, 100);
console.log("Created square + square.x + "," + square.y);
stage.addChild(square);
}
}
You are drawing the graphics at the coordinates you want, instead of drawing them at 0,0, and moving them using x/y coordinates. If you don't set the x/y yourself, it will be 0. EaselJS does not infer the x/y or width/height based on the graphics content (more info).
Here is an updated fiddle where the graphics are all drawn at [0,0], and then positioned using x/y instead: http://jsfiddle.net/0o63ty96/
Relevant code:
square.graphics.beginStroke("red").drawRect(0,0,100,100);
square.x = i * 100;
square.y = j * 100;

how to know if a point is inside the playable area on isometric maps?

Using cocos2d tiled maps, i need to know if a touch is done inside the isometric map. Isometric maps have a diamond shape.
When i set a new position i would use a min,max but as it is in diamond shape... can not be sure is inside the map..
This would be the validator if the space was a square, but as this is diamond it won't work.
-(CGPoint)validateInsideMap:(CGPoint)position{
int x,y;
x = MAX(_tileMap.mapSize.width * _tileMap.tileSize.width , position.x);
x = MIN(0, x);
y = MAX(_tileMap.mapSize.height * _tileMap.tileSize.height , position.y);
y = MIN(0, y);
return ccp(x, y);
}
any suggestion?
In pixels was a little complicated to calculate if the touch was inside the map, so i just needed to convert the position in pixels to the tile system, then just validate that tile was inside the map bounds, else return the closest one.
Thats made with this algorithm:
-(CGPoint) tilePosFromLocation:(CGPoint)location{
float mapHeight = _tileMap.mapSize.height;
float tileWidth = _tileMap.tileSize.width;
float tileHeight = _tileMap.tileSize.height;
CGPoint tilePosDiv = CGPointMake(location.x/tileWidth, location.y/tileHeight);
float invereseTileY = mapHeight - tilePosDiv.y;
float posX = (int)(invereseTileY + tilePosDiv.x - (_tileMap.mapSize.width*0.5));
float posY = (int)(invereseTileY - tilePosDiv.x + (_tileMap.mapSize.width*0.5));
posX=MAX(0, posX);
posX=MIN(_tileMap.mapSize.width - 1, posX);
posY=MAX(0, posY);
posY=MIN(_tileMap.mapSize.height - 1, posY);
return CGPointMake(posX, posY);
}
This way there always be a tile inside the bounds of the map retourned, so no problem if the touch was outside