Getting the bounding box of each glyph in pdf - iOS - objective-c

I've been trying to highlight text in a pdf. After a lot of research and experiments, it seems like I have to find the bounding box of each glyphs, create an overlay where the actual drawing is happening, and highlight the text by filling the CGRect with the info from bounding box and fill it with color. Now, I am stumped with the bounding box.
I've been using the PDFKitten to search and highlight the text. Now I want to use it to select and highlight the text. What I don't understand is how does it use the bounding box ( as well as other information like ascent, descent, capHeight, etc.) to fill highlight the searched word. When I tried to access the FontDescriptor class to get the info, it displays this:
2012-06-28 16:32:20.626 er[2408:15203] x:-665, y:-325, width:2000, height:1006
2012-06-28 16:32:20.627 er[2408:15203] x:-157, y:-250, width:1126, height:952
2012-06-28 16:32:20.628 er[2408:15203] x:-628, y:-376, width:2000, height:1010
It is very confusing so if anyone can clarify this, it will be very much appreciated.

You cannot use font descriptor information to get bounding box for the glyph.
PDFKitten takes care of finding width and height of the each glyph using RenderingState model.
You can use the same to find the location of the word while scanning the PDF.
Font dictionary provides the widths for the glyph. You can use cid to get the correct width of the glyph.
Try to look into highlighting code of the PDFKitten.

Related

How do you change text color of all the text of a particular color to another in pdf?

My Professor uses a very hard to see white font colour for about half of his lecture notes.
it is very hard to see on the slides, even worse printed out.
Is there a dynamic way to convert all white text in the entire document to black? hopefully there is a solution with Adobe Acrobat Pro DC which is what i am using, but im open to other suggestions.
In the Accessibility preferences, you can change the way text and line art is displayed. This isn't a permanent change but just changes how the PDF is drawn to the screen.

Can't change bold text color, a 'black border' remains

When I change the color of the text I get the following result:
What is creating this black border?
This problem occurs when I'm generating a PDF using flying saucer, with an embedded font, and try to change the color on h1, h2, etc. If I dump the html flying saucer uses to generate the PDF to disk and open it in Chrome the color change is applied correctly, with no weird black border.
A font program contains all the path construction operators that are needed to draw a set of glyphs. When you use a glyph from a font, these drawing instructions are executed along with a path painting operator. By default, the path painting operator is a "fill" operator. No borders are drawn, the shape is just filled using the current fill color. This is called the default render mode.
Other rendermodes are for instance "stroke" which draws the outlines of the glyph using the current stroke color, but doesn't fill it. Or "fill and stroke" which draws the outlines using the default stroke color and fills the path using the default fill color. There's also a render mode "invisible" in which case nothing is stroked, nothing is filled.
Looking at your image, it seems like the render mode that is used fills the glyph in gray and strokes it using black (and the line width seems to be quite thick). If I had to guess, I'd say that you're trying to mimic "bold". The best way to draw text in bold, is to switch from a regular font (e.g. arial.ttf) to a bold font (e.g. arialbd.ttf). Unfortunately, not every font has a bold counterpart (e.g. there's arialuni.ttf, but no arialunibd.ttf). In that case, bold is mimicked by changing the render mode to "fill and stroke" and the line width is changed to give the glyphs a bold appearance. This could be what is happening in your case.
Check your HTML: are you by any chance talking about text that is in bold? If so, maybe you should change the font along with the color. That way, you'll use a true bold font instead of a pseudo false font.
DISCLAIMER: I don't know Flying Saucer and I don't want to know it. Flying Saucer is using iText for PDF generation. iText is a library I wrote, but Flying Saucer is using it without having established a business relationship with iText Group NV, which is a company I run. Hence it's the responsibility of the creators of Flying Saucer to support their software.

How to increase acrofield(textfield) size to fit text in itext?

I have a pdf template having a multi line textfield.While filling up the textfield at run-time using itext, I want to increase the textfield size so that the entire content is visible.
I know similar questions have been posted previously.e.g. the following link suggests a solution but I am too new to itext to make it work.
How to display floating textfield depending on the text content?
I know setting font size 0 automatically adjust the font size to fit in the text, but that is not an option for me since I can't reduce the font size.
So kindly provide me an workable example if possible.

How to extract text from a pdf doc within a specific rectangular region in objective C?

I want to extract the text inside a bounding box specified inside highlight annotation using the PSPDFKit. Can anybody say how to do this?
To get the text that's beneath a PDF highlight annotation, you need to iterate over all rects and contact the affected glyphs. (You can ignore the boundingBox for that) The highlight annotation just has rect data, not text, so it's a bit tricky to get right.
There's also a convenience method on PSPDFHighlightAnnotation called highlightedString, which will do all this work for you.

PDF Highlighting above image / below text

i'm trying to highlight text in a pdf, and have the highlighted rectangle to be drawn under the text.
It works fine on most PDF's, but I jumped into a problem when the text I'm trying to highlight has an image/background under it. The problem is that the highlight rectangle is drawn under the image as well, so it is not visible.
The drawing order I have is this:
draw a blank rectangle with the page size
draw the highlight
draw the pdf using CGContextDrawPDFPage(context, page);
Is there a way to draw the PDF images and text separately? so that I could go
blank rectangle
pdf images/background
highlight
pdf text
Do I have to do something to the pdf / context so that it draws it automatically the way I want it to? I've tried messing with the context but nothing worked so far, it's all drawn entirely under or entirely above the full pdf
Every reader I've seen does this (PDFExpert, GoodReader, iAnnotate to name a few), so it can't be impossible, I just haven't found the solution yet :)
Any help will help, thanks in advance!!
Cheers
My understanding is that these other apps are reading and rendering the PDF themselves (they support selecting text, or adding annotations, for example), so they would be able to much more easily layer things in the way you're mentioning.
The CGPDFDocument you're starting with is an opaque object (in the OO sense, not transparency) that can draw itself, but I don't know of any way to break out and render various sublayers of the document.
As a way forward, you could look at using Core Image (iOS5+) or some other method to blend the highlights layer with the PDF. If you used the right filter (Multiply, maybe), the darker text would still come through and a .3 alpha highlight would blend with any background.