iText/Swing: Breaking text in a box - java

I am using iText to generate some PDF documents. It is quite easy to draw a colored rectangle. But I would like to add some text fitting into that box. Actually the main problem is to know where to break the text. Actually it will be the equivalent problem in Swing.
With a monotype font it will be quite easy, but without? Are there any well known algorithms or other ways?

See the ColumnText class.
This sort of thing is covered extensively (along with a million other things) in the iText in Action book - I highly recommend picking up a copy.

Related

How to count color pages in a PDF/Word doc using Java

I am looking to develop a desktop application using Java to count the number of colored pages in a PDF or Word file. This will be used as part of an overall system to help calculate the cost of printing a document in terms of how many pages there are (color/B&W).
Ideally, the user of the application would use a file dialog to select the desired PRF/Word file, the application could then count and output the number of colored pages, allowing the system to automatically calculate document cost accordingly.
i.e
if A4 colored pages cost 50c per page to print,
and B&W cost 10c per page,
calculate the total cost of the document per colored/B&W pages.
I am aware of the existing software Rapid PDF Count http://www.traction-software.co.uk/rapidpdfcount/, but would be unsuitable as part on integration into a new system. I have also tried using GhostScript/Python as per this solution: http://root42.blogspot.de/2012/10/counting-color-pages-in-pdf-files.html, however this takes too long (5mins to count a 100 page pdf), and would be difficult to implement into a desktop app.
Is there any method of counting the number of colored pages in a PDF or Word file using Java (or alternative language)
Thanks
Although it might sound easy, the task is rather complicated.
One option would be to use a program such as iText to walk every single token in the PDF, look for tokens that support color and compare that to your definition of "black". However, this will only get you basic text and drawing commands. Images are a completely different beast so you'll probably need to find an image parser or grab a copy of each spec and then walk each of those.
One of the downsides of token walking is you need to properly handle tokens that reference other things and further walk those tokens.
Another downside is that things can overlap each other so you'd probably want be aware of their coordinates, z-index, transparency and such.
There will be many more bumps in the road but that's a good start. What's most interesting is that if you accomplish this, you'll actually have found that you've partially built a PDF renderer!
Next, you'll need to define "black". Off the top of my head there's RGB black, CMYK black, Grey black and maybe Lab black along with some Pantones. That shouldn't be too hard but if I were to build this I'd want to know "blank ink usage" which could also be shades of grey. There's also "rich blank" that you might need to deal with, too!
So, all that said, I think that the GhostScript option you found is really the best bet. It literally renders the PDF and calculates the ink coverage from an RGB standpoint. You still should handle grey's, too, but that shouldn't be too hard, here's a good starting point.
Wanting to know what the click-charge is going to be is a pretty common problem, but it's not easy to solve at all. As already indicated by the answer Chris Haas gave, but I want to put another spin on it.
First of all, you have to wonder whether you really want to support both Word and PDF documents. Analysing Word files is less useful than you might think because that Word file is probably going to be converted into something else before it's going to be printed. And because of the fact that you're starting from Word, the chance that your nice RGB black text in Word gets converted to less-than-perfect 4 color black in PDF is very high. In other words, even though you might count a page of black text in Word as a 'cheap' page, it might turn into an expensive color page after conversion from Word to something that can be printed.
Let's consider the PDF case then. PDF supports a whole host of color spaces (gray, RGB, CMYK, the same with an ICC Profile attached, spot color and a few multi-spot color variants, CalGray and CalRGB and Lab. Besides that there is a whole range of very tricky features such as transparency, overprint, shades, images, masks... that you all have to take into account. The only truly good way to calculate what you need is to do essentially the same work as your printer will do; convert the PDF into one image per page and examine the pixels.
Because of what you want to do, the best way to progress would be to:
1) Convert any word files into PDF
2) Convert any PDF files into CMYK
3) Render each page of that CMYK file into an image.
Once you've done that you can examine the image and see whether you have any colors left. There are a number of potential technologies you can use for this. GhostScript is definitely one, but there are commercial solutions too that would certainly be more expensive but potentially faster.

Is it possible to use MathML to describe data tables?

I'm using JEuclid library for rendering MathML script. I want to display data tables using MathML.
I googled it but all the results describe mtable with examples showing describing matrices.
So, I'm puzzled whether it is possible. In our current framework, it's not possible to use any other resources except graphics object of JPanel or MathML script.
So, please point me in the right direction. Any links to examples would be highly appreciated.
mtable is not specifically for matrices (in particular it does not add parenthesis around the data) It is used for all vertical alignment layouts such as tables and aligned equations.

GWTCanvas or gwt-graphics for interactive drawing?

I want to develop an app where the user should be able to draw objects (circles/elipses), connect them via lines/arrows and drag/drop them, add text to them etc.
Of course I also have to write eventHandling.
What is most suitable for that? Would you rather go for gwt-graphics or GWTCanvas?
Based on that what i know, GWT-Graphics is based on SVG. while GWTCanvas(which is actually part of GWT main line) is based on canvas element.
now, quick comparsion.
For all drag and drop thing, SVG was much easier and quicker (when o was testing drag and drop when i had large abount of elements, my program based on canvas was much slower than similar stuff based on SVG
Other advantage of SVG based library is canvas is not supported y all browsers (actualy older versions of ie imight have some issues)
from the other hand,
canvas is much quicker while you need to draw massive number of objects, canvas is definietly your choice.
I would personally go with gwt-graphics. It is the one I know better, it gives as much control over graphics as was needed to me (and I think you too) and most of the events are already implemented, so the logic you will need to write will be minimal.
On the other hand SVG is not well supported by IExplorer and if the vectors become numerous you will have compatibility problems.
The Canvas if HTML 5 and will give you pixel level control - which I assume is not the case in your project - but will also lead to a - no workarounds - approach which is a pro.
In conclusion, I think it depends on the level of control you want the user to have while using the canvas and the level of work you want to do on your part.

Android fuzzy / faded fonts possible?

So I am developing a very simple app, mostly for personal use, am am looking for a simple solution to a simple problem.
In its simplest form I am looking for a way to have a line of text with just one or two words blurred out. Basically I am looking to blur text beyond readability but still hinting at what is hidden. Kind of a knowledge / memory app to help memorize some definitions by prompting with a few key words.
I am having issues finding a simple way to accomplish this. Am I just missing an attribute to blur text?
I have thought about:
overriding say the textview onDraw but that seems overkill and I am unsure if there are any methods available to easily blur text.
using the toHtml and trying out the new CSS3 blur effects but I don't think that that is a reasonable solution and I am not sure that the Android platform supports all the CSS3 format, if any.
the simplest and most desirable solution in my book was to find a font (ttf, off, etc) file, derived from a common font, that is already blurred as I described, and use that alternating with the non blurred version of that font to achieve the desired effect.
make the described font but that just plain requires too much time on my part and the outcome is not necessarily good :)
I have thought about some alternative ways to simulate this effect but they all result in fading the text, which is undesirable, since I want to have some visual prompts to indicate the obscured texts length.
Any ideas? It's been years since I have developed in Java and I am unsure what is available and what the Android OS supports.
I haven't looked into using these properties for only part of the text, but TextView has some possibly useful properties related to text shadows. Using something like the following XML attributes, you could hide the actual text and just show a blurred shadow.
android:textColor - #0000 (fully transparent so that the crisp text is not shown)
android:shadowColor - #FFFF (or whatever color you want to appear)
android:shadowDx - 0 (the shadow is in the same horizontal position as the text)
android:shadowDy - 0 (the shadow is in the same vertical position as the text)
android:shadowRadius - Depends on how much you want to blur. A very small non-zero value, such as 0.001, will be sharp. Larger values blur more, and at some point the shadow becomes illegible.

Advice needed: A programmatic way for creating vector graphics with heavy usage of text

I need a way for render a tree-like structures, similar to flowcharts.
Surprisingly, I can't find(or I'm doing wrong) a suitable tool.
First, I looked at SVG. But I couldn't find a way to draw a bounding box around the text
without using ECMAScript: I tried to do a simple thing drawing two text surrounded by boxes and linked by a line, centered by sides(some thing like O-O, where O is a box with text).And when you use ECMAScript, you heavily limit the tools that can be used for SVG rendering(for example you can't convert corrently such SVG to png or pdf with ImageMagick).
Second, I tried Asymptote, but it is quite heavy when you start manipulating with text(you need an LaTeX system installed and configured).
I look for a tool in which you can:
Programmatically access to font properties: baseline, ascent, descent, height
Computing height/width of a string(or the bounding box)
basic vector graphics functionality like drawing lines, shapes etc.
I don't think that's a hard stuff. For example, all such functionality exists for example in Java2D. Sure, I can use it as last resort and get raster graphics, but may be there is something handy to use?
Have you looked at GraphViz (http://www.graphviz.org/)? It does not really match your requirements since you give up some control and instead let the tool do the layout and rendering based on a declarative a description of a graph or tree, but I have found it to be the easiest way to generate tree-like output.
Not sure if it should be free?
Here's a commercial solution with an extensive API
http://www-01.ibm.com/software/integration/visualization/java/

Categories

Resources