The attached code will create a text-only calendar for year 2004. Since I mentioned it in my "XML to PDF" message, I thought that someone can use the code. I also have couple of other formats. I'll try to clean up the code and will send it to everybody.
[code] import java.awt.Color; import java.awt.Point; // Java related import java.io.*; import java.text.DecimalFormat;
// iText related import com.lowagie.text.*; import com.lowagie.text.pdf.*;
/**
* This class will create a text-only calendar for year 2004.
*
* @author Jones Henry
*/
public class test
{
// Month Index
private final static int JANUARY = 0;
private final static int FEBRUARY = 1;
private final static int MARCH = 2;
private final static int APRIL = 3;
private final static int MAY = 4;
private final static int JUNE = 5;
private final static int JULY = 6;
private final static int AUGUST = 7;
private final static int SEPTEMBER = 8;
private final static int OCTOBER = 9;
private final static int NOVEMBER = 10;
private final static int DECEMBER = 11; // Month itself
private final static int[] month =
{
JANUARY, FEBRUARY, MARCH, APRIL,
MAY, JUNE, JULY, AUGUST,
SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER
}; // Month strings
private final static String[] months =
{
"January 2004", "February 2004", "March 2004", "April 2004",
"May 2004", "June 2004", "July 2004", "August 2004",
"September 2004", "October 2004", "November 2004", "December 2004"
}; // Day heading
private final static String[] dayHeading =
{
"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"
};// Months heading for small month
private final static String[] smallMonths =
{
" DECEMBER\n\n",
" JANUARY\n\n", " FEBRUARY\n\n", " MARCH\n\n", " APRIL\n\n",
" MAY\n\n", " JUNE\n\n", " JULY\n\n", " AUGUST\n\n",
" SEPTEMBER\n\n", " OCTOBER\n\n", " NOVEMBER\n\n", " DECEMBER\n\n",
" JANUARY\n\n"
};
// Day of each month. 0 means not a valid day. -1 means next week. -2 means next month.
private final static int[][] smallDay =
{
{0, 1, 2, 3, 4, 5, 6, -1, 7, 8, 9, 10, 11, 12, 13, -1, 14, 15, 16, 17, 18, 19, 20, -1, 21, 22, 23, 24, 25, 26, 27, -1, 28, 29, 30, 31, 0, 0, 0, -2},
{0, 0, 0, 0, 1, 2, 3, -1, 4, 5, 6, 7, 8, 9, 10, -1, 11, 12, 13, 14, 15, 16, 17, -1, 18, 19, 20, 21, 22, 23, 24, -1, 25, 26, 27, 28, 29, 30, 31, -2},
{1, 2, 3, 4, 5, 6, 7, -1, 8, 9, 10, 11, 12, 13, 14, -1, 15, 16, 17, 18, 19, 20, 21, -1, 22, 23, 24, 25, 26, 27, 28, -1, 29, 0, 0, 0, 0, 0, 0, -2},
{0, 1, 2, 3, 4, 5, 6, -1, 7, 8, 9, 10, 11, 12, 13, -1, 14, 15, 16, 17, 18, 19, 20, -1, 21, 22, 23, 24, 25, 26, 27, -1, 28, 29, 30, 31, 0, 0, 0, -2},
{0, 0, 0, 0, 1, 2, 3, -1, 4, 5, 6, 7, 8, 9, 10, -1, 11, 12, 13, 14, 15, 16, 17, -1, 18, 19, 20, 21, 22, 23, 24, -1, 25, 26, 27, 28, 29, 30, 0, -2},
{30, 31, 0, 0, 0, 0, 1, -1, 2, 3, 4, 5, 6, 7, 8, -1, 9, 10, 11, 12, 13, 14, 15, -1, 16, 17, 18, 19, 20, 21, 22, -1, 23, 24, 25, 26, 27, 28, 29, -2},
{0, 0, 1, 2, 3, 4, 5, -1, 6, 7, 8, 9, 10, 11, 12, -1, 13, 14, 15, 16, 17, 18, 19, -1, 20, 21, 22, 23, 24, 25, 26, -1, 27, 28, 29, 30, 0, 0, 0, -2},
{0, 0, 0, 0, 1, 2, 3, -1, 4, 5, 6, 7, 8, 9, 10, -1, 11, 12, 13, 14, 15, 16, 17, -1, 18, 19, 20, 21, 22, 23, 24, -1, 25, 26, 27, 28, 29, 30, 31, -2},
{1, 2, 3, 4, 5, 6, 7, -1, 8, 9, 10, 11, 12, 13, 14, -1, 15, 16, 17, 18, 19, 20, 21, -1, 22, 23, 24, 25, 26, 27, 28, -1, 29, 30, 31, 0, 0, 0, 0, -2},
{0, 0, 0, 1, 2, 3, 4, -1, 5, 6, 7, 8, 9, 10, 11, -1, 12, 13, 14, 15, 16, 17, 18, -1, 19, 20, 21, 22, 23, 24, 25, -1, 26, 27, 28, 29, 30, 0, 0, -2},
{31, 0, 0, 0, 0, 1, 2, -1, 3, 4, 5, 6, 7, 8, 9, -1, 10, 11, 12, 13, 14, 15, 16, -1, 17, 18, 19, 20, 21, 22, 23, -1, 24, 25, 26, 27, 28, 29, 30, -2},
{0, 1, 2, 3, 4, 5, 6, -1, 7, 8, 9, 10, 11, 12, 13, -1, 14, 15, 16, 17, 18, 19, 20, -1, 21, 22, 23, 24, 25, 26, 27, -1, 28, 29, 30, 0, 0, 0, 0, -2},
{0, 0, 0, 1, 2, 3, 4, -1, 5, 6, 7, 8, 9, 10, 11, -1, 12, 13, 14, 15, 16, 17, 18, -1, 19, 20, 21, 22, 23, 24, 25, -1, 26, 27, 28, 29, 30, 31, 0, -2},
{30, 31, 0, 0, 0, 0, 1, -1, 2, 3, 4, 5, 6, 7, 8, -1, 9, 10, 11, 12, 13, 14, 15, -1, 16, 17, 18, 19, 20, 21, 22, -1, 23, 24, 25, 26, 27, 28, 29, -2}
};
// Fonts for main month
private static Font monthHeaderFont =
FontFactory.getFont( FontFactory.TIMES_ROMAN, 28, Font.BOLD | Font.ITALIC );
private static Font dayHeaderFont =
FontFactory.getFont( FontFactory.TIMES_ROMAN, 14, Font.BOLD );
private static Font dayFont =
FontFactory.getFont( FontFactory.TIMES_ROMAN, 32, Font.BOLD );
// Fonts for small month. Used fixed width font so that the alignment looks good (and easy to code)
private static Font monthSmall =
FontFactory.getFont( FontFactory.COURIER, 10, Font.BOLD );
private static Font daySmall =
FontFactory.getFont( FontFactory.COURIER, 6, Font.BOLD );
/**
* The main program for the test class to create the calendar
*
* @param args The command line arguments
*/
public static void main( String[] args )
{
// A4 size Landscape
Document document = new Document( PageSize.A4.rotate() );try
{
// Want to create a PDF
PdfWriter.getInstance( document, new FileOutputStream( "test.pdf" ) );
document.open();
// For each month
for ( int m = 0; m < month.length; m++ )
{
// Generate the big month heading with two small months
generateMonth( month[m], document ); // Now create the current month days
Table table = new Table( 7, 7 );
table.setPadding( 2 );// Create the day heading row
for ( int i = 0; i < dayHeading.length; i++ )
{
Paragraph p = new Paragraph( 14, dayHeading[i], dayHeaderFont );
Cell cell = new Cell( p );
cell.setBackgroundColor( new Color( 0xee, 0xee, 0xee ) );
cell.setHorizontalAlignment( ElementTags.ALIGN_CENTER );
table.addCell( cell );
}
// Create the rest of the days
int[] day = smallDay[month[m]];
int cnt = 0; // For each day
for ( int i = 0; i < day.length; i++ )
{
String d = "";
// if weekend or monthend, just go to next day
if ( day[i] == -1 || day[i] == -2 )
{
continue;
}
// if not a valid day, simply put a space in there
else if ( day[i] == 0 )
{
d = " ";
}
// we got the day
else
{
d = String.valueOf( day[i] );
}
// find the position of the day and print the day
int row = ( cnt / 7 ) + 1;
int col = ( cnt % 7 );
Paragraph p2 = new Paragraph( 70, d, dayFont );
Cell cell = new Cell( p2 );
cell.setHorizontalAlignment( ElementTags.ALIGN_RIGHT );
table.addCell( cell, new Point( row, col ) );
cnt++;
} // finally done for a month, then next month
document.add( table );
document.newPage();
}
}
catch ( DocumentException de )
{
System.out.println( de.getMessage() );
}
catch ( IOException ioe )
{
System.out.println( ioe.getMessage() );
}document.close(); }
/**
* This procedure creates the big month heading with 2 small months
*
* @param month Month index
* @param document Document itself
* @exception DocumentException Exception thrown
*/
private static void generateMonth( int month, Document document )
throws DocumentException
{ // Create an empty paragraph to give some space
Paragraph empty = new Paragraph( 10, " " );
document.add( empty ); // Create a table with 3 columns to put the header information
PdfPTable tableHeader = new PdfPTable( 3 );
float widths[] = {11.5f, 57.0f, 11.5f};
tableHeader.setWidths( widths );
tableHeader.setWidthPercentage( 80 ); // First cell will be the previous month
generateSmallMonth( month, tableHeader ); // Put the month heading in the middle cell
generateMonthHeading( month, tableHeader ); // Third cell will be the next month
generateSmallMonth( month + 2, tableHeader );document.add( tableHeader ); }
/**
* This method creates the small month
*
* @param month Description of the Parameter
* @param table Description of the Parameter
* @exception DocumentException Description of the Exception
*/
private static void generateSmallMonth( int month, PdfPTable table )
throws DocumentException
{ Phrase phraseMonth = new Phrase( smallMonths[month], monthSmall );
StringBuffer dayString = new StringBuffer( "" ); // build the day string for the entire month
for ( int i = 0; i < smallDay[month].length; i++ )
{
// if not a valid day, just print the space
if ( smallDay[month][i] == 0 )
{
dayString.append( " " );
}
// if weekend, print in the next line
else if ( smallDay[month][i] == -1 )
{
dayString.append( "\n" );
}
// if monthend, print two empty line
else if ( smallDay[month][i] == -2 )
{
dayString.append( "\n\n" );
}
// convert the valid day to string of 3 characters
else
{
String ls = String.valueOf( smallDay[month][i] );
if ( ls.length() == 1 )
{
dayString.append( " " + ls );
}
else if ( ls.length() == 2 )
{
dayString.append( " " + ls );
}
}
} //System.out.println ( "dayString: " + dayString.toString ( ) );
Phrase phraseDay = new Phrase( dayString.toString(), daySmall );
phraseMonth.add( phraseDay );
PdfPCell cellMonth = new PdfPCell( phraseMonth );
cellMonth.setHorizontalAlignment( Element.ALIGN_JUSTIFIED );
cellMonth.setPadding( 3 );
table.addCell( cellMonth );
}/**
* This method generates the big month heading
*
* @param month Month index
* @param table Table
* @exception DocumentException Exception thrown
*/
private static void generateMonthHeading( int month, PdfPTable table )
throws DocumentException
{
Paragraph paraMonth = new Paragraph( 14, months[month], monthHeaderFont );
paraMonth.setAlignment( ElementTags.ALIGN_CENTER );
PdfPCell cellMonth = new PdfPCell( paraMonth );
cellMonth.setHorizontalAlignment( Element.ALIGN_CENTER );
cellMonth.setVerticalAlignment( Element.ALIGN_MIDDLE );
cellMonth.setBorderWidth( 0 );
cellMonth.setPadding( 3 );
table.addCell( cellMonth );
}
} [/code]
Jones.
_________________________________________________________________
Let the new MSN Premium Internet Software make the most of your high-speed experience. http://join.msn.com/?pgmarket=en-us&page=byoa/prem&ST=1
------------------------------------------------------- The SF.Net email is sponsored by EclipseCon 2004 Premiere Conference on Open Tools Development and Integration See the breadth of Eclipse activity. February 3-5 in Anaheim, CA. http://www.eclipsecon.org/osdn _______________________________________________ iText-questions mailing list [EMAIL PROTECTED] https://lists.sourceforge.net/lists/listinfo/itext-questions
