Reputation: 1210
I'm writing a text file of indeterminate length to a PDF page. If the file has more text than will fit on the page, I need to add a second PDF page. However, I can't seem to get the drawing/graphics context to start drawing on the next page. It generates a new page just fine, but it stays blank.
Here's my code:
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");
}
What am I doing wrong?
Upvotes: 0
Views: 725
Reputation: 26
Add two lines:
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;
}
Upvotes: 1
Reputation: 1110
https://github.com/mapo80/iTextSharp-Monotouch
Not using ITextSharp for Xamarin.IOS?
I'm teasing, but it is a possible answer. : )
Upvotes: 0