CreateTextRun with specific rectangle

115 views
Skip to first unread message

Falk Beerten

unread,
Sep 12, 2016, 12:02:21 PM9/12/16
to PDFTron PDFNet SDK
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]];

Ryan

unread,
Sep 12, 2016, 6:47:29 PM9/12/16
to PDFTron PDFNet SDK
Hi did you follow this forum post as a basis?

I also don't see the CreateTextBegin element being added, just a TextRun and TextEnd.

Falk Beerten

unread,
Sep 13, 2016, 12:26:30 PM9/13/16
to PDFTron PDFNet SDK
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:

Ryan

unread,
Sep 15, 2016, 7:14:38 PM9/15/16
to PDFTron PDFNet SDK
There is this old post.

However, now you could use the ContentReplacer class, since that class allows you to specify a rectangle area.

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.

Finally, if the entire document is being created from scratch, is to start with HTML and use our HTML2PDF converter.
Reply all
Reply to author
Forward
0 new messages