sivarajankumar / alivepdf

Automatically exported from code.google.com/p/alivepdf
0 stars 0 forks source link

PDF.getY() method #177

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
Hello Thibault,

First let me say that I love what you done with AlivePDF, it certainly 
helps me a great deal and I apologize if I am posting this as an issue but 
it is for me.

I am using the PDF class to make a calendar from a Flex app. As I compose 
the calendar I ran into the situation where some of the information 
overflow to the next page that AlivePDF automatically creates for you in 
such case.
This is great, but now as I retrieve the y coordinate, I realize that I 
don't know what page the getY() pulls the info or at least I am unable to 
tell the page... (the currentPage().Page property is always 0)
This is not a problem the first time around because I can see that the 
totalPages property has changed, but it is if the next day has a longer 
text than that one.

Below is an extract of my code, it probably will make more sense than my 
blabing.

private function ProcessCalendar(columnCount:Number):void
{
    var verticalY:Number;
    var cellContentWidth:Number;
    var verticalOffset:Number;

    var offset:int;

    var title:String = null;
    var titleAlign:String = null;
    var content:String = null;
    var contentAlign:String = null;

    var hasNewPage:Boolean = false;
    var newPageVerticalY:Number = 0;
    var currentPage:int = 1;

    var lineIndex:int = 0;
    var lineIndexStart:int = 0;
    var lineIndexEnd:int = 0;

    try
    {
        xCoordinates = new Array();
        yCoordinates = new Array();
        xCoordinates.push(pdfFile.getX());
        yCoordinates.push(pdfFile.getY());

        cellContentWidth = pdfPageContentWidth/columnCount;
        offset = 0;
        verticalY = yCoordinates[offset];
        verticalOffset = 2;

        //Header
        pdfFile.setFont(FontFamily.ARIAL,Style.BOLD,10);
        pdfFile.textStyle(new RGBColor(0x9f0000));
        for(var j:int=0;j<calendar[0].length;j++)
        {
            content = ParseHTML(calendar[0]
[j].Content.Characters);
            contentAlign = calendar[0][j].Content.TextAlign;
            contentAlign = contentAlign.substr(0,1).toUpperCase
();
            pdfFile.addMultiCell
(cellContentWidth,5,content,0,contentAlign);
            xCoordinates.push(xCoordinates[j] + 
cellContentWidth);
            if(verticalY < pdfFile.getY())
            {
                verticalY = pdfFile.getY();
            }
            pdfFile.setXY(xCoordinates[j+1],yCoordinates
[offset]);
        }
        verticalY = verticalY + verticalOffset;
        yCoordinates.push(verticalY);
        offset++;
        verticalY = verticalY + verticalOffset;
        yCoordinates.push(verticalY);
        offset++;
        pdfFile.setXY(xCoordinates[0],yCoordinates[offset]);

        //Grid
        pdfFile.setFont(FontFamily.ARIAL,Style.NORMAL,8);

        for(var i:int=1;i<calendar.length;i++)
        {
            for(var k:int=0;k<calendar[i].length;k++)
            {
                content = ParseHTML(calendar[i]
[k].Content.Characters);
                contentAlign = calendar[i]
[k].Content.TextAlign;
                contentAlign = contentAlign.substr
(0,1).toUpperCase();

                if(calendar[i][k].Title != null)
                {
                    title = ParseHTML(calendar[i]
[k].Title.Characters);
                    titleAlign = calendar[i]
[k].Title.TextAlign;
                    titleAlign = titleAlign.substr
(0,1).toUpperCase();
                    pdfFile.textStyle(new RGBColor
(0x000000));
                    pdfFile.addMultiCell
(cellContentWidth,5,title,0,titleAlign);
                    pdfFile.setX(xCoordinates[k]);
                    pdfFile.addMultiCell
(cellContentWidth,5,content,0,contentAlign);
                }
                else
                {
                    pdfFile.textStyle(new RGBColor
(0x999999));
                    pdfFile.addMultiCell
(cellContentWidth,5,content,0,contentAlign);
                }

                if(hasNewPage)
                {

                }
                else
                {
                    if(currentPage == 
pdfFile.totalPages)
                    {
                        if(verticalY < pdfFile.getY
())
                        {
                            verticalY = 
pdfFile.getY();
                        }
                    }
                    else
                    {
                        if(pdfFile.totalPages > 
currentPage + 1)
                        {
                            while
(pdfFile.totalPages != currentPage + 1)
                            {

    pdfFile.removePage(pdfFile.totalPages);
                            }
                        }
                        hasNewPage = true;
                        if(newPageVerticalY < 
pdfFile.getY())
                        {
                            newPageVerticalY = 
pdfFile.getY();
                        }
                        pdfFile.gotoPage
(currentPage);
                    }
                }

                pdfFile.setXY(xCoordinates
[k+1],yCoordinates[offset]);
            }

            if(hasNewPage)
            {
                currentPage++;
                pdfFile.gotoPage(currentPage);
                hasNewPage = false;
                verticalY = newPageVerticalY;
                newPageVerticalY = 0;
            }

            verticalY = verticalY + verticalOffset;
            yCoordinates[offset] = verticalY;
            verticalY = verticalY + verticalOffset;
            yCoordinates.push(verticalY);
            offset++;
            pdfFile.setXY(xCoordinates[0],yCoordinates
[offset]);
        }

        pdfFile.lineStyle(new RGBColor(0xdfdfdf),0.3);
        currentPage = 1;
        lineIndex = 1;

        do
        {
            pdfFile.gotoPage(currentPage);
            lineIndexStart = lineIndex;

            do
            {
                pdfFile.moveTo(xCoordinates[0],yCoordinates
[lineIndex]);
                pdfFile.lineTo(xCoordinates
[xCoordinates.length-1],yCoordinates[lineIndex]);
                lineIndex++;

                if(lineIndex < yCoordinates.length - 1)
                {
                    if(yCoordinates[lineIndex - 1] > 
yCoordinates[lineIndex])
                    {
                        break;
                    }
                }
            }while(lineIndex<yCoordinates.length - 1)

            currentPage++;
            if(currentPage <= pdfFile.totalPages)
            {
                for(var l:int=0;l<xCoordinates.length;l++)
                {
                    pdfFile.moveTo(xCoordinates
[l],yCoordinates[lineIndexStart]);
                    pdfFile.lineTo(xCoordinates
[l],pdfPageContentHeight);
                }
                pdfFile.end();
                pdfFile.gotoPage(currentPage);
            }
            else
            {
                for(var m:int=0;m<xCoordinates.length;m++)
                {
                    pdfFile.moveTo(xCoordinates[m],10);
                    pdfFile.lineTo(xCoordinates
[m],yCoordinates[yCoordinates.length - 1] - verticalOffset);
                }
            }

        }while(currentPage <= pdfFile.totalPages)
    }
    catch(e:Error)
    {
        Alert.show(e.message);
    }
}

Thanks for any suggestion that you may have

Original issue reported on code.google.com by cnbli...@gmail.com on 4 Dec 2009 at 9:21