0

我正在将不确定长度的文本文件写入 PDF 页面。如果文件的文本超出页面的容量,我需要添加第二个 PDF 页面。但是,我似乎无法让绘图/图形上下文开始在下一页上绘图。它生成一个新页面就好了,但它保持空白。

这是我的代码:

public static FileInfo writeTextToPDF(FileInfo input) {
    if(!input.Exists)
        return default(FileInfo); //Return null if there is no log to be written

    UIGraphics.BeginPDFContext (input.FullName + ".pdf", RectangleF.Empty, default(NSDictionary));

    UIGraphics.BeginPDFPage ();

    using (CGContext g = UIGraphics.GetCurrentContext()) {
        const int marginTop = 12; //Margin to leave at the top of the page
        const int marginLeft = 12; //Margin to leave at the left of the page
        const int lineSpacing = 4; //Space between lines
        const int fontSize = 8; //Change this to change the font size

        var yOffset = -marginTop - fontSize; //This is easier for drawing than translating a CTM all the time

        g.ScaleCTM (1, -1);

        //Write the title
        g.SelectFont ("Helvetica", fontSize * 2, CGTextEncoding.MacRoman);
        g.ShowTextAtPoint (marginLeft, yOffset, input.Name);
        yOffset -= fontSize * 2 + lineSpacing * 2;

        g.SelectFont ("Helvetica", fontSize, CGTextEncoding.MacRoman);
        using (var fs = input.OpenText()) {
            string text = fs.ReadLine ();
            while(text != null) {
                    g.ShowTextAtPoint (marginLeft, yOffset, text); //Draw it 12 points from the left
                yOffset -= fontSize + lineSpacing; //Move down another line
                text = fs.ReadLine (); //Get the next line

                if(-yOffset >= UIGraphics.PDFContextBounds.Height) {
                    // Start a new page if needed
                    //g.EndPage ();
                    UIGraphics.BeginPDFPage ();

                    yOffset = -marginTop - fontSize;
                }
            }
        }
    }

    UIGraphics.EndPDFContent ();

    return new FileInfo (input.FullName + ".pdf");
}

我究竟做错了什么?

4

2 回答 2

1

添加两行:

if(-yOffset >= UIGraphics.PDFContextBounds.Height) {
    // Start a new page if needed
    //g.EndPage ();
    UIGraphics.BeginPDFPage ();

    **g = UIGraphics.GetCurrentContext();
    g.ScaleCTM (1, -1);**

    yOffset = -marginTop - fontSize;
}
于 2013-08-29T23:26:32.767 回答
0

https://github.com/mapo80/iTextSharp-Monotouch

不为 Xamarin.IOS 使用 ITextSharp?

我在取笑,但这是一个可能的答案。:)

于 2013-08-26T01:53:09.597 回答