iTextSharp - Scaling and positioning page - itext

I am reading an existing PDF file into a new PDF file, and scaling the first page based on the page dimensions. The page gets scaled to 90-95% in order to make room for some footer text. The problem I have is that the resulting scaled page leaves white space at the top of the page, NOT the bottom. Is there a way to control the results so that they start in the top left of the page and not the bottom left?
Here's my code that adds the scaled page:
cb.AddTemplate(importedPage, scaleSize, 0, 0, scaleSize, 5, 20);
Object "cb" is a PDFContentByte class, while "scaleSize" is a float set to .9 or .95. The scaling works great, but the positioning is an issue. Thanks!

figured it out: the value of "20" for the final parameter was incorrect. A value of 100 is far more suitable for my purposes, and now I understand far better the coordinate system in PDF/iText. For those interested, the 0, 0 point for a PDF document is the lower left corner.

Related

Coordinate system in Crystal Reports

I'm working with CR version 13.0.25 - I believe the actual latest version as of this date is 13.0.26, so, basically the latest version of CR. What I am working with is re-positioning elements on the report dynamically from the C# code.
There are a couple of the reports where the original rpt (designed a number of years ago) spans multiple pages. These in particular are giving me issues, but they are not the only ones. The issue here focuses on the y (vertical) dimension.
CR documents use a coordinate system that is based in the upper left corner of the document, and is also based on twips (1440 twips/in). So, in theory, I should be able to set the Top value for a ReportObject to 1440 and that element should be printed 1" from some benchmark (either top of the page or bottom of the margin - I'm fairly sure it's bottom of the margin).
My results do not seem to reflect this - at least not exactly.
When I set the Top value, it appears that with some of the documents, the y dimension is scaled. In fact, I can apply a constant scaling factor (it varies according to the document) and I can make the fields appear with a relative 1" spacing all of the way to the bottom of the page. However, that scaling factor varies from document to document.
So, besides experimenting in our production code, I have also done the following:
I created a C# console app that opens an rpt file, grabs some of the elements on it, and re-positions them, then sends it to the printer. I designed a new rpt file with headers and footers sized as small as I could get them, and put 10 text fields in the details section. I iterate through the fields (via ReportDocument and ReportObjects and re-position them at 0, 1440 (1"), 2880 (2") etc.
This works perfectly with a document that has the default margins (.167 inches by default on each side). The first element has its top butted against the bottom of the margin, the second element is 1" from the top of the first element to the top of the second element, and so on down the page. If, however, I reduce the margins to zero - either programmatically or in the design, I get this scaling effect. The elements are all now about 7/8" or so apart, instead of the 1" that I would expect.
Does anyone know of any magic formula(s) or documentation on why the coordinate system appears to be changing? I could understand that if I change the margin that could offset everything on the page (assuming that the margin is our baseline) but it would seem to me that the inter-element spacing, being 1440 should still render at 1".
I believe Y is the vertical distance from the top of that section, not from the top of the page.

Unity3d. UI elements snapping/anchoring

I have canvas with vertical layout and 2 elements within (in fact it's element with only recttransform on it, let's call it container). So these 2 containers take a half of the screen by height and stretched by width, ok. How can I place an text element in above container and snap it to the bottom of this container? I tried press bottom button in recttransform widget (also with shift and alt) and it seems it doesn't affect my transform at all
P.s. May be I can use some free plugin instead of default unity components of UI layout?
There are different ways of placing your UI elements
Simply drag and drop it to the bottom where you want it
Use the anchor widget to set the anchoring to bottom with horizontal stretch and hold shift to also set pivot. Then set Pos Y to 0. Set Left and Right to 0.
Assuming you also want other elements in your containers, place a Vertical Layout Group on each container and make sure that your text element is the last child of the container in the hierarchy.
I would also advise you to seek out tutorials on Unity UI anchoring, positioning, scaling, and layout. You need a deeper understanding of how these things interact than you are likely to get from Stack Overflow. Otherwise you will suddenly find that your UI behaves in unexpected ways when rearranged or displayed on a different aspect ratio.
It's fairly easy with Unity UI system. You just need to get used to it. Here are simple steps to accomplish what you want:
Create Text element as a child of that container.
Select your newly created element and edit its RectTransform component values:
2.1. Set both Y axis anchors (min and max) to 0.
2.2. Set pivot value to 0 as well.
2.3. Set Pos Y value to 0 as well.
Now your Text element is anchored at the bottom of the container and its position (and height) is measured from the bottom of the Text element itself.

iText: Do margins apply when using PdfContentByte?

I created a new document with 36 points margin all around:
Rectangle rect = new Rectangle(1224f, 792f);
Document doc = new Document(rect, 36f, 36f, 36f, 36f);
I drew a rectangle at (0, 0) using PdfContentByte:
cb.rectangle(0, 0, 100, 100);
cb.fill();
The rectangle gets drawn at the lower-left hand corner of the page. It ignores the margins completely. Is this the expected result?
Is this the expected result?
Yes, it is. The margins only are taken into account by operations adding content via the Document.
Whenever you access the content directly (DirectContent, UnderContent,OverContent), you can draw wherever you want.
When creating footers or headers, you actually rely on that behavior.
Furthermore, the margins have no influence on the coordinate system. As the coordinate system is configurable, one could have chosen 0,0 to be the lower left of the margin border. Actually you can do so by defining a page size rectangle that way. But that is not the normal usage of iText.

StretchableImageWithLeftCapWidth stretching wrong portions

I am trying to use a UIImage with stretchableImageWithLeftCapWidth to set the image in my UIImageView but am encountering a strange scaling bug. Basically picture my image as an oval that is 31 pixels wide. The left and right 15 pixels are the caps and the middle single pixel is the scaled portion.
This works fine if I set the left cap to 15. However, if I set it to, say, 4. I would expect to get a 'center' portion that is a bit curved as it spans the center while the ends are a little pinched.
What I get is the left cap seemingly correct, followed by a long middle portion that is as if I scaled the single pixel at pixel 5, then a portion at the right of the image where it expands and closes over a width about twice the width of the original image. The resulting image is like a thermometer bulb.
Has anyone seen odd behavior like this and might know what's going on?
Your observation is correct, Joey. StretchableImageWithLeftCapWidth does NOT expand the whole center of the image as you would expect. It only expands the pixel column just right of the left cap and the pixel row just below the top cap!
Use UIView's contentStretch property instead, and your problem will be solved. Another advantage to this is that contentStretch can also shrink a graphic properly, whereas stretchableImageWithLeftCapWidth only works when making the graphic larger.
Not sure if I got you right, but LeftCapWidth etc is made for rounded corners, with everything in the rectangle within the rounding radius is stretched to fit the space between the 'caps' on the destination button or such.
So if your oval is taller or wider than 4 x 2 = 8, whatever is in the middle rectangle will be stretched. And yours is, so it would at least look at bit ugly! But if it's not even symmetrical, something has affected the stretch. Maybe something to do with origin or frame, or when it's set, or maybe it's set twice, or you have two different stretched images on top of each other giving the thermometer look.
I once created two identical buttons in the same place, using the same retained object - of course throwing away the previous button. Then I wondered why the heck the button didn't disappear when I set alpha to 0... But it did, it's just that there was a 'dead' identical button beneath it :)

Vista and Windows 7 borders cut off form content

Forms on Windows 7 and Vista have thicker borders than those of XP. As a result, some content that spans the entire height or width of a form will be cut off. Is there a simple way to fix this, or do all the forms of my application need to be resized to accommodate for this?
Update: It appears to be the fault of the form size and not the borders that are causing the problem. See the following images for an example. Notice how the controls have the correct location at the top-right corners, but they reach the each of the form in Windows 7 while a border is retained in XP. The same code is used to resize and position the controls.
Here's what a form looks like in XP:
alt text http://img504.yfrog.com/img504/1328/bordersxp.png
You should fix the size of the form programatically, to make sure the client size is big enough to fit everything. You can easily calculate the difference between the current size and client size of the form, and increase/decrease by the right amount.
You probably want to perform this inside Form_Load.
In VB6, the client size and width are referred to as ScaleWidth and ScaleHeight for forms. Setting these values just messes up the scaling, rather than resizing the form, so you instead have to do calculations so you can set the normal Width and Height properties.
BorderSize = Me.Width - Me.ScaleWidth
Me.Width = BorderSize + CorrectScaleWidth
//Same for height!