iOS在pdf文档中绘制表格

时间:2011-10-19 12:10:56

标签: iphone ios pdf

我将从UIViewController的内容创建pdf。 当UIViewController有一个表区域(与Excel相同)时,我没有找到在pdf文档中绘制表格的方法。 有谁知道解决这个问题?请帮帮我。

1 个答案:

答案 0 :(得分:4)

如果您使用Quartz图形上下文(CGContextRef类型),则没有简单的方法来创建我知道的表。我能够通过在嵌套for循环中在PDF上绘制线条来编程地创建表格,并通过仔细注意间距使其看起来正确。

伪代码:

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);  
    }


}

这是您可能用于绘制表格的逻辑的一个粗略示例。您可能希望在桌子周围使用矩形或边框使其看起来正确,以及任何其他类型的调整,以使其达到您想要的效果。您可以通过以下链接找到有关如何执行此操作的方法列表。我知道这可能不是你的想法,但我希望如果你完全陷入困境,这会给你一些指导。祝你好运!

您可以在Apple的文档中找到可用于绘制PDF的完整函数库:http://developer.apple.com/library/ios/#documentation/GraphicsImaging/Reference/CGContext/Reference/reference.html