【问题标题】:TableView to PDF add Padding to PageTableView 到 PDF 添加填充到页面
【发布时间】:2015-01-06 05:05:40
【问题描述】:

我使用此代码从我的 UITableView 创建一个 PDF 文件:

var priorBounds:CGRect = self.tableView.bounds;

var fittedSize:CGSize = self.tableView.sizeThatFits(CGSizeMake(priorBounds.size.width, self.tableView.contentSize.height))
self.tableView.bounds = CGRectMake(0, 0, fittedSize.width, fittedSize.height);

var pdfPageBounds:CGRect = CGRectMake(0, 0, 792, 612); // Change this as your need
var pdfData = NSMutableData()

UIGraphicsBeginPDFContextToData(pdfData, pdfPageBounds, nil)

for var pageOriginY:CGFloat = 0; pageOriginY < fittedSize.height; pageOriginY += pdfPageBounds.size.height {

    UIGraphicsBeginPDFPageWithInfo(pdfPageBounds, nil);       
    CGContextSaveGState(UIGraphicsGetCurrentContext());      
    CGContextTranslateCTM(UIGraphicsGetCurrentContext(), 0, -pageOriginY)
    self.tableView.layer.renderInContext(UIGraphicsGetCurrentContext())
}

UIGraphicsEndPDFContext();

self.tableView.bounds = priorBounds; // Reset the tableView

let path = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as NSString
var pdfFileName = path.stringByAppendingPathComponent("testfilewnew.pdf")

pdfData.writeToFile(pdfFileName, atomically: true)

它工作正常,但 PDF 填写了我的 PDF 的整个页面。我想为每个页面添加一个填充,例如标题(在第一页上)和页脚。

我是使用此 PDF 导出的新手,我无法找到解决该问题的方法。谁能帮助我,或带我走向正确的方向?

任何帮助将不胜感激。

【问题讨论】:

  • 你有办法解决这个问题吗?

标签: ios uitableview pdf swift


【解决方案1】:

您希望通过创建宽度和高度略小于表格的表格来创建适合页面的表格。 试试这个:

let paperA4 = CGRect(x: -25, y: 25, width: 612, height: 892);
let pageWithMargin = CGRect(x: 0, y: 0, width: paperA4.width-50, height: paperA4.height-50);

        var fittedSize:CGSize = self.tableView.sizeThatFits(CGSizeMake(pageWithMargin.width, self.tableView.contentSize.height))
        self.tableView.bounds = CGRectMake(0, 0, fittedSize.size.width, fittedSize.height);

        var pdfData = NSMutableData()


        UIGraphicsBeginPDFContextToData(pdfData, paperA4, nil)

        for var pageOriginY:CGFloat = 0; pageOriginY < fittedSize.height; pageOriginY += paperA4.size.height {

            UIGraphicsBeginPDFPageWithInfo(paperA4, nil);

            CGContextSaveGState(UIGraphicsGetCurrentContext());

            CGContextTranslateCTM(UIGraphicsGetCurrentContext(), 0, -pageOriginY)

            self.tableView.layer.renderInContext(UIGraphicsGetCurrentContext())
        }

        UIGraphicsEndPDFContext();

        self.tableView.bounds = pageWithMargin; // Reset the tableView


        let path = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as NSString
        var pdfFileName = path.stringByAppendingPathComponent("testfilewnew.pdf")

        pdfData.writeToFile(pdfFileName, atomically: true)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-06
    • 1970-01-01
    • 2012-01-11
    • 2010-11-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多