Is there a way to set the dimensions of a paragraph in itext7? - java-11

Trying to set the height along with setting the rotation on a Paragraph object results in the following error:
ERROR c.i.layout.renderer.BlockRenderer - Rotation was not correctly processed for ParagraphRenderer
Also, the height is sent to infinity resulting in an apparently empty page which I'm not sure if it's the expected behavior or not.
If I don't set the height, the text shows up rotated (which is what I want). But my task is to place the text at certain coordinates with certain dimensions and rotation. The placement is fine, but not being able to set the height results in assigning the rotation points to the wrong values and in turn displacing the final position of the text.
Paragraph paragraph = new Paragraph(text);
paragraph.setHeight(height);
paragraph.setMargins(0, 0, 0, 0);
paragraph.setProperty(ROTATION_POINT_X, x);
paragraph.setProperty(ROTATION_POINT_Y, y);
paragraph.setProperty(ROTATION_ANGLE, asFloat(toRadians(rotation))));
document.add(paragraph);
Setting the margins to 0 doesn't seem to have any influence either. The text looks to keep some sort of margin anyway.
So is there a way to control what the dimensions of the object displayed are? I could also settle for a way to make the rotation pivot from the center of the object.

You should set both width and height at the same time for rotated objects.
Here is an example code:
Paragraph paragraph = new Paragraph("Hello world");
paragraph.setWidth(200);
paragraph.setHeight(20);
paragraph.setBackgroundColor(ColorConstants.RED);
paragraph.setMargins(0, 0, 0, 100);
paragraph.setProperty(ROTATION_POINT_X, paragraph.getWidth().getValue() / 2);
paragraph.setProperty(ROTATION_POINT_Y, paragraph.getHeight().getValue() / 2);
paragraph.setRotationAngle(Math.PI / 180 * 45);
document.add(paragraph);
Visual result:

Related

iText Vertical Alignment in Text inside a Canvas isn't working

I'm trying to set my text which is inside a canvas to get vertically aligned with VerticalAlignment.BOTTOM property value, but whatever I put doesn't change the visual result.
retangulo = New iText.Kernel.Geom.Rectangle(122, AlturaPag - 208, 235, 27)
PdfCanvas = New iText.Kernel.Pdf.Canvas.PdfCanvas(page).SetLineWidth(1).Rectangle(retangulo).Stroke()
canvas = New iText.Layout.Canvas(PdfCanvas, retangulo)
texto = (New iText.Layout.Element.Text(Npagador).SetFontSize(11).SetBold)
' VerticalAlingment = BOTTOM \/
paragrafo = (New iText.Layout.Element.Paragraph().Add(texto).SetTextAlignment(TextAlignment.LEFT).SetVerticalAlignment(VerticalAlignment.BOTTOM))
canvas.Add(paragrafo)
You are trying to align the contents of the paragraph to the bottom of the paragraph itself, and the paragraph when laying itself out simply uses the minimal necessary height, so whatever alignment (bottom or top) you set, the result it just the same simply because paragraph packs itself to the minimal height.
To make sure setVerticalAlignment has an effect, you need to give the paragraph more vertical space by setting its height. Here is an example where I set paragraph height to match the height of the canvas you add it to, since it's the only element in the canvas. I also set the margins of the paragraph to zero so that you don't get additional spacing around the text.
The code is in Java but you will be able to convert it back to VB.NET as easily as I converted your code to Java:
Paragraph p = new Paragraph().add("Test").setTextAlignment(TextAlignment.LEFT)
.setVerticalAlignment(VerticalAlignment.BOTTOM).setHeight(retangulo.getHeight());
p.setMargin(0);

Text Formatting To Avoid Overwritting

I already saw this question but it doesn’t help me.
The problem is that I can write the text to the PDF file, but if I do that with a new gfx.DrawString it put the second text on the first...
like this....
Here is my code I used to set a new line:
gfx.DrawString(lbname.Text + " " + lbnamei.Text, font, XBrushes.Black, new XRect(0, 0, page.Width.Point, page.Height.Point), XStringFormats.TopLeft);
gfx.DrawString(lbvorname.Text + " " + lbvornamei.Text, font, XBrushes.Black, new XRect(0, 0, page.Width.Point, page.Height.Point), XStringFormats.TopLeft);
I think it has something to do with the numbers in the first argument (new XRect(0, 0,) but it is unclear how to set them.
I can't remember which parameter does which, but this will help you discover for yourself:
Set one of the numbers to 20 on one of the pieces of text; observe the effect. Then change it back and change a different number; observe the effect.
An XRect has the four parameters x, y, width, height. x is the position from the left, y is the position from the top.
Width and height have no effect as long as XStringFormats.TopLeft is used. Therefore it is enough with the code shown above to increment y to draw text in a new line.
Width must be set to have text that is right-aligned or horizontally centered.
Height must be set to have text that is bottom-aligned or vertically centered.

How to determine size of text drawn in rectangle

I am outputting text on a printed page and using drawstring to draw the text in a rectangle so that it will word wrap using a call such as:
ev.Graphics.DrawString(textToOutput, printFont, myBrush, New RectangleF(leftMargin, yPosition, pagewidth - leftMargin - rightmargin, 400))
This works fine. What I am trying to determine is what the Y position would be after the drawscreen call (in other words, what was the height of the text after it was wrapped in the rectangle). I am trying to print variable length strings from a database and they will frequently exceed the page width. I need to know where the vertical start of the next paragraph will be.
You can use Graphics.MeasureString and perhaps this overload of it. Part of the example from MSDN:
Dim stringSize As New SizeF
stringSize = e.Graphics.MeasureString(measureString, stringFont, stringWidth)
Your calculated height would be stored in SizeF.Height.

VBA Excel 2010 CreateObject ExtendedProperty get image width attribute

I'm using similar kind of functions to get some property attributes like Filename, bit depth, dimensions, size etc by using some vba code. The last line of that function would be something like the following:
PicSize = CreateObject("Shell.Application").Namespace(vPth2).Parsename(sPic2).ExtendedProperty("size")
Now, I want to get the height and width of that image file. I've put height and width in the ExtendedProperty attribute value but it's not giving me the height and width of that file.
Note that, in the property dialog box of that image file, in the summary tab, it is showing Bit depth but putting this did not work. Then I put bitdepth and it worked successfully. The names are not case sensitive.
Okay! Now all I want to get the height and width of that selected image file. How can I do that? I mean what to put inside ExtendedProperty() to get height and width.
An early reply with solution would be highly appreciated.
Can't help with why Height and Width don't work, but Dimensions seems to work, returns a string like ?493 x 376?
I suggest the work around method
replace size in your code
PicSize=CreateObject("Shell.Application").Namespace(vPth2).Parsename(sPic2).ExtendedProperty("size")
to Dimensions
you will get something like ?493 x 376?
now declare
a string t = "?493 x 376?"
'search the index of "x"
search_x = InStr(t, "x")
'we would got width and height by
width = Mid(t, 2, search_x - 2)
height = Mid(t, search_x + 2, Len(t) - 8)
It is unknow why but I found that start at "2" in width and "-8" in height like that would give a number

Ready An Existing PDF Page Size (ex. 8.5 x 11, 11 x 17) VB.Net

Like the title says i'd like to read an existing pdf page size with VB.Net. I've been working with Itext.Sharp, and the Acrobat.dll. Is this possible??
There are a number of different "Boxes" a given page can have:
Media Box (required): The initial page size when printing viewing.
Crop Box (optional): Supersedes the media box. Defaults to match the media box. Must be a subset or match the media box.
There's also art/trim/bleed boxes, but they don't matter as much and are much less common.
So, the page size:
PdfReader reader = new PdfReader(myPath);
// gets the MEDIA BOX
Rectangle pageRect = reader.getPageSize(1); // 1 -> first page
// gets the crop box if present, or the media box if not.
Rectangle cropRect = reader.getCropBox(1);
// and finally
Rectangle artBox = reader.getBoxSize( 1, "art");
// could be "art", "bleed", "crop", "media", or "trim"
I'd go with getCropBox().
I also recommend checking out the JavaDoc for things like this. At the very least you would have come up with getPageSize() on your own. No, it's not C#. Yes, it's very useful.
http://api.itextpdf.com/
Also note that these Rectangles need not be based on 0,0 (which would be the lower left corner on an unrotated page).
Further, you should check the page's rotation, getPageRotation(int), and swap height and width if the rotation is 90 or 270. There is getPageSizeWithRotation(int), but it only works with the media box, so I'd do it yourself if I were you. It's only a few extra lines of code:
// rotation has to be 0, 90, 180, or 270. "360" isn't kosher IIRC.
if (reader.getPageRotation(pageNum) % 180 != 0) {
float tmp = width;
width = height;
height = tmp;
}