iOS draw the table in pdf document - iphone

I will create the pdf from the contents of UIViewController.
When UIViewController has a table region(the same as Excel), I didn't find the method for drawing an table in pdf document.
Does anyone know to solve this problem? Please help me.

There is no easy method of creating a table I know of if you're using a Quartz graphics context (CGContextRef type). I was able to create a table programmatically by drawing lines on the PDF in nested for loops, and by paying careful attention to spacing to make it look right.
Psuedo-code:
for (int i=0; i < numberOfRows; i++)
{
// display each row
// Draw row line
CGPoint horizontalRowDivider[2] = {CGPointMake(x_startPoint, y_startPoint + (i * row_width)), CGPointMake(x_endPoint, y_endPoint + (i * row_width))};
CGContextStrokeLineSegments(pdfContext, newlineItemDivider, 2);
for(int j = 0; j < numberOfColumns; j++)
{
//Draw each column cell
const char *desc_text = "Table value"
CGContextShowTextAtPoint (pdfContext, x_cellDataLoc, y_cellDataLoc, desc_text, strlen(desc_text));
CGPoint verticalLineCellDivider[2] = {CGPointMake(x_startPoint + (j * col_width), y_startPoint), CGPointMake(x_endPoint + (j * col_width), y_endPoint)};
CGContextStrokeLineSegments(pdfContext, verticalLineCellDivider, 2);
}
}
This is a rough example of the kind of logic you might use to draw a table. You'll probably want a rectangle, or a border, around your table to make it look right, among any other kinds of tweaks to make it how you want. You can find the list of methods on how to do that and more through the below link. I know this probably isn't what you had in mind, but I hope this gives you some direction to take if you were totally stuck. Good luck!
You can find the full library of functions available for drawing PDFs in Apple's documentation: http://developer.apple.com/library/ios/#documentation/GraphicsImaging/Reference/CGContext/Reference/reference.html

Related

iText -- How do I get the rendered dimensions of text?

I would like to find out information about the layout of text in a PdfPCell. I'm aware of BaseFont.getWidthPointKerned(), but I'm looking for more detailed information like:
How many lines would a string need if rendered in a cell of a given width (say, 30pt)? What would the height in points of the PdfPCell be?
Give me the prefix or suffix of a string that fits in a cell of a given width and height. That is, if I have to render the text "Today is a good day to die" in a specific font in a PdfPCell of width 12pt and height 20pt, what portion of the string would fit in the available space?
Where does iText break a given string when asked to render it in a cell of a given width?
This is with regard to iText 2.1.6. Thanks.
iText uses the ColumnText class to render content to a cell. This is explained in my book on page 98-99. This means that, just like with ColumnText, you need to make the distinction between text mode and composite mode.
In any case, ColumnText measures the width of the characters and tests if they fit the available width. If not, the text is split. You can change the split behavior in different ways: by introducing hyphenation or by defining a custom split character.
I've written a small proof of concept to show how you could implement custom "truncation" behavior. See the TruncateTextInCell example.
Instead of adding the content to the cell, I have an empty cell for which I define a cell event. I pass the long text "D2 is a cell with more content than we can fit into the cell." to this event.
In the event, I use a fancy algorithm: I want the text to be truncated in the middle and insert "..." at the place where I truncated the text.
BaseFont bf = BaseFont.createFont();
Font font = new Font(bf, 12);
float availableWidth = position.getWidth();
int contentLength = content.length();
int leftChar = 0;
int rightChar = contentLength - 1;
availableWidth -= bf.getWidthPoint("...", 12);
while (leftChar < contentLength && rightChar != leftChar) {
availableWidth -= bf.getWidthPoint(content.charAt(leftChar), 12);
if (availableWidth > 0)
leftChar++;
else
break;
availableWidth -= bf.getWidthPoint(content.charAt(rightChar), 12);
if (availableWidth > 0)
rightChar--;
else
break;
}
String newContent = content.substring(0, leftChar) + "..." + content.substring(rightChar);
PdfContentByte canvas = canvases[PdfPTable.TEXTCANVAS];
ColumnText ct = new ColumnText(canvas);
ct.setSimpleColumn(position);
ct.addElement(new Paragraph(newContent, font));
ct.go();
As you can see, we get the available width from the position parameter and we check how many characters match, alternating between a character at the start and a character at the end of the content.
The result is shown in the resulting PDF: the content is truncated like this: "D2 is a c... the cell."
Your question about "how many lines" can be solved in a similar way. The ColumnText class has a getLinesWritten() method that gives you that information. You can find more info about positioning a ColumnText object in my answer to your other question: Can I tell iText how to clip text to fit in a cell

Add rectangle as inline-element with iText

How do I add a rectangle (or other graphical elements) as inline-elements to an iText PDF?
Example code of what I'm trying to achieve:
foreach (Row r in entrylist)
{
p = new Paragraph();
p.IndentationLeft = 10;
p.SpacingBefore = 10;
p.SpacingAfter = 10;
p.Add(new Rectangle(0, 0, 10, 10)); <<<<<<<<< THAT ONE FAILS
p.Add(new Paragraph(r.GetString("caption"), tahoma12b));
p.Add(new Paragraph(r.GetString("description"), tahoma12));
((Paragraph)p[1]).IndentationLeft = 10;
doc.Add(p);
}
It's something like a column of text-blocks, of which each of them have (only a printed) checkbox.
I've tried various things with DirectContent, but it requires me to provide absolute X and Y values. Which I simply don't have. The elements should be printed at the current position, wherever that may be.
Any clues?
You need a Chunk for which you've defined a generic tag. For instance, in this example listing a number of movies, a snippet of pellicule is drawn around the year a movie was produced and an ellipse was drawn in the background of the link to IMDB.
If you look at the MovieYears example, you'll find out how to use the PdfPageEvent interface and its onGenericTag() method. You're right that you can't add a Rectangle to a Paragraph (IMHO that wouldn't make much sense). As you indicate, you need to draw the rectangle to the direct content, and you get the coordinates of a Chunk by using the setGenericTag() method. As soon as the Chunk is drawn on the page, its coordinates will be passed to the onGenericTag() method.

Image sorting bug in Scrollview - iPad application

We are building a catalog app that has 2 rows of sorted images with varying widths but same height. We draw the artwork on scrollview in sorted order (from A to Z) As per attached image.
We search using a alphabetically ordered bar on top that has letters (A to Z) If i touch on letter J the i want the artwork which starts from j comes first. The code we are using is not working well. E.g. clicking on J takes us to A. Will greatly appreciate your help or advice.
Code is as follows:
NSString *newStr = [currentArtworkTitle substringWithRange:NSMakeRange(0,1)];
if([newStr isEqualToString:self.searchString])
if (scrollViewTopRowWidth > scrollViewBottomRowWidth) {
xCordForSortedView = scrollViewTopRowWidth - c - imgForButton.size.width;//self.touchLengthCount;//50 ;// -10//c scrollViewBottomRowWidth
}
else {
xCordForSortedView = scrollViewBottomRowWidth - c - imgForButton.size.width;// self.touchLengthCount;//40;//scrollViewTopRowWidth
}
// For shifting the screen
if(scrollViewTopRowWidth > scrollViewBottomRowWidth){
int tmpMargin = scrollViewTopRowWidth - xCordForSortedView;
if(tmpMargin < 1024)
scrollView.contentSize = CGSizeMake(scrollViewTopRowWidth+1024,scrollView.bounds.size.height);
}
else{
int tmpMargin = scrollViewBottomRowWidth - xCordForSortedView;
if(tmpMargin < 1024)
scrollView.contentSize = CGSizeMake(scrollViewBottomRowWidth+1024,scrollView.bounds.size.height);
}
[scrollView scrollRectToVisible:CGRectMake(xCordForSortedView, 0,scrollView.frame.size.width, scrollView.frame.size.height) animated:YES];
I won't edit your code but I can give you a hint.
Look, when you populate the scrollwiew, by adding an imageview, add also an entry into a NSMutableDictionary (instance variable), so the key will be the A-Z letter, and the value will be the x position of the image. When you finish populating the scrollview, you will have also the dictionary containing the position of all your images (and you need only the x) and the corresponding letter. Now you tap "J" and hit "Search" - you parse your dictionary to fing the object with the key "J" and read it's value. Use this (x) value to set your srollview's contentOffset. Tada!

set symbol height in highchart

I have a pie chart and I need to change the size of the symbols in the legend, but there's only setSymbolWidth. How do I change the height of the symbol?
I'm using Moxie Group's GWT highchart, but if there's a way in solving this with the javascript highcharts I'm probably able to do it in the GWT version.
I tried this solution http://jsfiddle.net/MEr2n/ but the series property is returning null (don't know why) even after I add a series to the chart. I'm also not sure this would work because it seems this solution gets the SVG element of the legend from the serie and set its property directly, in my case there is one serie and a symbol for each point in the serie.
I solved it. Unfortunately I needed to create a very ugly workaround for this. At least it worked.
My solution was:
NodeList<Element> gElements = chart.getElement().getElementsByTagName('g');
for (int i = 0; i < gElements.getLength(); i++) {
Element gel = gElements.getItem(i);
if (gel.getAttribute("class").equals("highcharts-legend")) {
NodeList<Element> elements = gel.getElementsByTagName("rect");
for (int j = 0; j < elements.getLength(); j++) {
elements.getItem(j).setAttribute("height", "8");
}
break;
}
}
I guess I could have used gwt-jquery to get the g element with the highcharts-legend class, but I don't want to add another API just for this.

What is the right way to manage image assets for J2ME in NetBeans

I'm using NetBeans to develop a J2ME app that runs across many different devices. The app uses a lot of different image assets. Since the devices have different screen sizes, this means that I need to compile multiple binaries, each with different asset sizes.
So far, I've been using a manual process to control the assets. I have a directory consisting of a bunch of subdirectories, each corresponding to assets needed for a particular class of device. For example, I have one directory "320_240", that has assets sized for a 320x240 screen, and another "480_360", that has assets sized for a 480x360 screen. The files names are exactly the same as is the code that loads them. Before I compile, I just copy the proper files into the default package (under src).
This can obviously be improved. I already have different project configurations representing the different screen sizes, so I'd like to make the assets switch automatically, too. As a relative novice for NetBeans, I'm not sure what the best way to do this is.
FWIW, here's the best I've come up with yet:
Create asset. packages under src, where LABEL corresponds to the device class (e.g. "320_240", "480_360")
Put the images for each class into the proper src/asset/ directory
Create a static final String assetDir that gets set to "/asset//" according to the currently selected project config
Load the images using Image.creatImage(assetDir + "image.png")
For each configuration, only include the necessary asset directory in Project->Build->Sources Filtering (I think this is necessary to avoid storing the unused images in the compiled app, correct?)
This still feels a bit hokey, though. This has to be a common problem. Does anyone have a better solution?
Thanks!
If you are using lot of images, then the size of jar file will be increased. You can't install that jar in some low-end devices.
Just use one image and resize the image according to screen width and screen height.
To resize the image, use the below method.
public Image resizeImage(Image src, int screenHeight, int screenWidth) {
int srcWidth = src.getWidth();
int srcHeight = src.getHeight();
Image tmp = Image.createImage(screenWidth, srcHeight);
Graphics g = tmp.getGraphics();
int ratio = (srcWidth << 16) / screenWidth;
int pos = ratio / 2;
//Horizontal Resize
for (int index = 0; index < screenWidth; index++) {
g.setClip(index, 0, 1, srcHeight);
g.drawImage(src, index - (pos >> 16), 0);
pos += ratio;
}
Image resizedImage = Image.createImage(screenWidth, screenHeight);
g = resizedImage.getGraphics();
ratio = (srcHeight << 16) / screenHeight;
pos = ratio / 2;
//Vertical resize
for (int index = 0; index < screenHeight; index++) {
g.setClip(0, index, screenWidth, 1);
g.drawImage(tmp, 0, index - (pos >> 16));
pos += ratio;
}
return resizedImage;
}