Ryan
Ryan

Reputation: 5546

How can one create paragraphs without specifing the exact coordinate of each in SharpPDF?

Can I add paragraphs with SharpPDF, without having to specify the exact coordinates? Can't I just place paragraphs one under the other?

Please tell me if you used the library.

Upvotes: 2

Views: 346

Answers (1)

Jack B Nimble
Jack B Nimble

Reputation: 5087

It is not possible to just add paragraphs one after another without specifying coordinates, however I did write this sample which will move the paragraphs down the page and create a new page when necessary. In this want you could write out text, paragraphs, drawing, and always know the position of the "cursor."

const int WIDTH = 500;
const int HEIGHT = 792;

pdfDocument myDoc;
pdfPage currentPage;

private void button1_Click(object sender, EventArgs e)
{
    int height = 0;

    myDoc = new pdfDocument("TUTORIAL", "ME");
    currentPage = myDoc.addPage(HEIGHT, WIDTH);

    string paragraph1 = "All the goats live in the land of the trees and the bushes, " 
        + " when a person lives in the land of the trees and the bushes they wonder about the sanity" 
        + " of it all. Whatever.";

    string paragraph2 =  "Redwood National and State Parks is located in northernmost coastal "
        + "California — about 325 miles north of San Francisco, Calif. Roughly 50 miles long, the parklands"
        + "stretch from near the Oregon border in the north to the Redwood Creek watershed southeast of"
        + "Orick, Calif. Five information centers are located along this north-south corrdior. Park "
        + "Headquarters is located in Crescent City, Calif. (95531) at 1111 Second Street.";

    int iYpos = HEIGHT;

    for (int ix = 0; ix < 10; ix++)
    {
        height = GetStringHeight(paragraph1, new Font("Helvetica", 12), WIDTH);
        iYpos = CheckHeight(height, iYpos);
        currentPage.addParagraph(paragraph1, 0, iYpos, sharpPDF.Enumerators.predefinedFont.csHelvetica, 12, WIDTH);
        iYpos -= height;

        height = GetStringHeight(paragraph2, new Font("Helvetica", 12), WIDTH);
        iYpos = CheckHeight(height, iYpos);
        currentPage.addParagraph(paragraph2, 0, iYpos, sharpPDF.Enumerators.predefinedFont.csHelvetica, 12, WIDTH);
        iYpos -= height;
    }

    string tmp = Path.GetFileNameWithoutExtension(Path.GetTempFileName()) + ".pdf";
    myDoc.createPDF(tmp);
}

private int GetStringHeight(string text, Font font, int width)
{
    Bitmap b = new Bitmap(WIDTH, HEIGHT);
    Graphics g = Graphics.FromImage((Image)b);
    SizeF size = g.MeasureString(text, font, (int)Math.Ceiling((float)width / 72F * g.DpiX));
    return (int)Math.Ceiling(size.Height)
}

private int CheckHeight(int height, int iYpos)
{
    if (height > iYpos)
    {
        currentPage = myDoc.addPage(HEIGHT, WIDTH);
        iYpos = HEIGHT;
    }
    return iYpos;
}

The Y is backwards in this API, so 792 is the TOP and 0 is the BOTTOM. I use a Graphics object to measure the height of the string, since the Graphics is in pixels and the Pdf is in points I do an estimation to make them similar. I then subtract the height from my remaining Y value.

In this example I keep adding paragraph1 and paragraph2 over and over, updating my Y position as I go along. When I get to the bottom of the page I create a new page and reset my Y position.

This project hasn't seen any updates for a number of years, but the source code is available, using something similar to what I've done you could make your own function that allows you to successively add paragraphs which will keep track of a CURSOR position of where it thinks something should go next.

Upvotes: 1

Related Questions