CreateTextRun with specific rectangle

Hi,

I have the following code that writes a textrun to a pdf.
I have a rectangle named "rectTextBox" that has a height of 54 and a width of 200.

CGRect rectTextBox = CGRectMake(testX0, testY0, (testX1 - testX0), (testY1 - testY3));
double widthTextBox = rectTextBox.size.width;
double heightTextBox = rectTextBox.size.height;

I'm using the width and the height of the textBox as parameter A and D in the function SetTextMatrix.

Why does it return a text with a fontsize 1 x 54? instead of just filling the rectangle with text ?

How does one calculate the font size if we just have a rectangle for the text?

PTElement *element = [elementBuilder CreateRect: testX3 y: testY3 width: widthTextBox height: heightTextBox];
                    
                    element = [elementBuilder CreateTextRunWithFont: string font: [PTFont Create: [pdfDocument GetSDFDoc] type: e_ptcourier embed: NO] font_sz: 1];
                    
                    [element SetTextMatrix: widthTextBox b: 0 c: 0 d: heightTextBox h: testX3 v: testY3];
                    
                    PTGState *gstate = [element GetGState];
                    
                    [gstate SetFillColorSpace: [PTColorSpace CreateDeviceCMYK]];
                    [gstate SetFillColorWithColorPt: [[PTColorPt alloc] initWithX: 1 y: 0 z: 0 w:0]];
                    [gstate SetFillOpacity: 0.5];
                    
                    [elementWriter WriteElement: element];
                    [elementWriter WriteElement:[elementBuilder CreateTextEnd]];

Hi did you follow this forum post as a basis?
https://groups.google.com/d/msg/pdfnet-sdk/O8PQLisCf1M/lj3zGfXMjO4J

I also don’t see the CreateTextBegin element being added, just a TextRun and TextEnd.
https://www.pdftron.com/pdfnet/samplecode/ElementBuilderTest.m.html

Hello Ryan, yes I did follow that exact post.

I do add the CreateTextBegin.

It just seems like the SetTextMatrix function takes the width and height of the rectangle and multiplies this by the fontsize, being 1.
So I end up with a text larger than the actual width and height of the rectangle.

What I need is the text to be rendered in the rectangle, not just scaling up the font by a factor.
Op dinsdag 13 september 2016 00:47:29 UTC+2 schreef Ryan:

Hi did you follow this forum post as a basis?
https://groups.google.com/d/msg/pdfnet-sdk/O8PQLisCf1M/lj3zGfXMjO4J

I also don’t see the CreateTextBegin element being added, just a TextRun and TextEnd.
https://www.pdftron.com/pdfnet/samplecode/ElementBuilderTest.m.html

There is this old post.
https://groups.google.com/d/msg/pdfnet-sdk/3CLREVIcDxY/zj25Fz21EX4J

However, now you could use the ContentReplacer class, since that class allows you to specify a rectangle area.
https://www.pdftron.com/pdfnet/samplecode/ContentReplacerTest.m.html

Though note, it will delete any pre-existing text that intersects the rectangle.

Another option, is to use annotation flattening. Create a FreeText annotation, and set the contents, and then call Annot.Flatten.
https://www.pdftron.com/pdfnet/samplecode/AnnotationTest.m.html

Finally, if the entire document is being created from scratch, is to start with HTML and use our HTML2PDF converter.
https://www.pdftron.com/pdfnet/samplecode/HTML2PDFTest.m.html