I am new to iText and am using version 1.2.
I'm very impressed with iText so far: congratulations to Bruno, Paulo and
the other folks that have helped make this a great package!
I spent a few hours generating a PDF and everything worked pretty much
exactly as I wanted. However, when I tried to generate the same information
in an RTF(2) document using largely the same techniques, except that I am
using Table and Cell in my RTF rather than PdfPTable and PdfPCell, I found
that spacing behaves very differently.
The best way to illustrate this problem is for you to run my program, which
is supplied below. All you need to do is this:
1. Change line 48, which sets OUTPUT_FILE_PATH_RTF, to write the file to a
path of your choice.
2. Ensure that line 35, which sets EXTRA_SPACING, sets the variable to
false.
3. Save the program and run it. This will produce the RTF using only the
standard iText methods to control spacing.
4. Change line 35 to set EXTRA_SPACING to true.
5. Save the program and run it. This will produce the same RTF but will add
extra spacing beyond those created by the standard iText methods.
As you will see, when you run the program with EXTRA_SPACING set to false,
the document is rather cramped and the standard methods like
Paragraph.setSpacingAfter() and Table.setPadding() seem to be completely
ignored. There are no spaces after each paragraph (beyond the fact that the
next paragraph starts on a new line) and there is no visible spacing within
or between the cells of the Table, despite using Table.setPadding(),
Table.setSpacing(), Table.setSpaceInsideCell(), and
Table.setSpaceBetweenCells(). In addition, list items for Recent Education,
Employability, and Experience are all run together on a single line instead
of each being on a line by themselves. Also, no bullets appear in those
lists.
If you run the program with EXTRA_SPACING set to true, the document improves
considerably in terms of the spacing between paragraphs. Also, list items
appear on separate lines, as they should. But no list bullets appear. Also,
I haven't been able to figure out how to change the spacing between the
tables. I'd like the first table to appear on the line immediately below the
EMPLOYMENT HISTORY title, then to have one or two blank lines between each
table, then to have one or two blank lines after the last table.
Also, I'm curious about Table.setWidth(). Initially, I set it to 100,
assuming that it would use the full width of the page. However, it only used
about 2/3 of the available page width. I got it to use the full width of the
page (and maybe even make the page wider?) by using a value of 150. Is that
working the way it is supposed to? When I used Table.setWidth(100) on the
PDF document, it used the full page width. 100 seems like the logical value
for that purpose. In fact, I would think that the setWidth() value should
never be greater than 100, unless perhaps a value of 100 means "use all
space between the Margins" while a value greater than 100 means "use all
space between the edges of the page, ignorning Margins".
Sorry for the long list of questions but they all seem related so I thought
it made more sense to ask them all at once rather than formulate several
smaller questions.
Here is the source code for my program:
=================================================================
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.ResourceBundle;
import resume.ResumeConstants;
import com.lowagie.text.Cell;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.List;
import com.lowagie.text.ListItem;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.Rectangle;
import com.lowagie.text.Table;
import com.lowagie.text.rtf.RtfWriter2;
/**
* Class SimpleRft is a batch program that creates a simple RTF document.
*
* @author Rhino
* @version 1.0
*
*/
public class SimpleRft implements ResumeConstants {
/**
* If true, causes document to contain additional spaces beyond those
generated by the
* standard iText methods for spacing. If false, only the standard iText
methods are used.
*/
static final boolean EXTRA_SPACING = false;
/**
* If true, this flag enables extra diagnostic messages.
*/
static final boolean DEBUG = true;
/**
* The name of the class containing this program.
*/
final String CLASS_NAME = getClass().getName();
/**
* The path to the PDF output directory.
*/
static final String OUTPUT_FILE_PATH_RTF =
"e:/eclipse/3.0.1/eclipse/workspace/Resume/rtf/";
/**
* The indentation which should be used in the lists.
*/
static final int LIST_INDENTATION = 20;
/**
* The character to use for the list bullets.
*/
static final char LIST_BULLET = '\u2022';
/**
* The spacing to use between paragraphs.
*/
static final int STANDARD_VERTICAL_SPACE = 10;
/**
* An instance of a List ResourceBundle.
*/
ResourceBundle locList = null;
/**
* The main() method creates the application.
*/
public static void main(String[] args) {
new SimpleRft();
System.exit(0);
}
/**
* Generate the various files that are produced by this program.
*/
public SimpleRft() {
/* Write a simple Rtf document. */
writeSimpleRtf();
return;
}
/**
* Generate a simple RTF document.
*
*/
private void writeSimpleRtf() {
String METHOD_NAME = "writeSimpleRtf()";
/**
* The font used for the most important text.
*/
Font H1_FONT = new Font(Font.HELVETICA, 18, Font.BOLD,
PALETTE_CONTRAST_DARK_COLOUR);
/**
* The font used for the second most important text.
*/
Font H2_FONT = new Font(Font.HELVETICA, 14, Font.BOLD,
PALETTE_CONTRAST_DARK_COLOUR);
/**
* The font used for the 'detail' data, i.e. the data that goes in
each paragraph except for
* the table.
*/
Font DETAIL_FONT = new Font(Font.HELVETICA, 11, Font.NORMAL,
PALETTE_TEXT_DARK_COLOUR);
/**
* The font used for a special paragraph.
*/
Font EXAMPLES_FONT = new Font(Font.TIMES_ROMAN, 16, Font.BOLD +
Font.ITALIC, PALETTE_CLASH_COLOUR);
/**
* The font used for a special phrase.
*/
Font ADDRESS_FONT = new Font(Font.HELVETICA, 14, Font.ITALIC,
PALETTE_TEXT_DARK_COLOUR);
/**
* The font used for the data in the table.
*/
Font TABLE_FONT = new Font(Font.HELVETICA, 9, Font.NORMAL,
PALETTE_TEXT_DARK_COLOUR);
/* Identify the output file. */
String strOutfile = OUTPUT_FILE_PATH_RTF + "simple.rtf";
/*
* Create a new instance of a document. Make the document 8.5 X 11
inches.
* Set the background colour for the document.
*/
int pageWidth = new Double(8.5 * 72).intValue();
int pageHeight = 11 * 72;
Rectangle pageSize = new Rectangle(pageWidth, pageHeight);
pageSize.setBackgroundColor(PALETTE_OPPOSITE_COLOUR);
Document document = new Document(pageSize);
/* Create an RTF writer. */
try {
RtfWriter2.getInstance(document, new
FileOutputStream(strOutfile));
} catch (FileNotFoundException fnf_excp) {
System.err.println(fnf_excp.getMessage());
}
/* Open the document. */
document.open();
/*
* Add content to the document.
*/
/*
* Create the candidate paragraph, which consists of the candidate
name, the
* candidate's address, and the candidate's job target(s).
*/
Paragraph candidateParagraph = new Paragraph();
Chunk candidateName = new Chunk("Santa Clause", H1_FONT);
candidateParagraph.setSpacingAfter(STANDARD_VERTICAL_SPACE);
candidateParagraph.add(candidateName);
candidateParagraph.add("\n");
String[] CandidateAddressData = new String[]
{"[EMAIL PROTECTED]"};
for (int ix = 0; ix < CandidateAddressData.length; ix++) {
Chunk addressData = new Chunk(CandidateAddressData[ix],
ADDRESS_FONT);
candidateParagraph.add(addressData);
}
candidateParagraph.add("\n");
Chunk targetTitle = new Chunk("Job Target" + ": ", H2_FONT);
candidateParagraph.add(targetTitle);
Chunk targetData = new Chunk("Jolly old elf", DETAIL_FONT);
candidateParagraph.add(targetData);
candidateParagraph.setAlignment(Element.ALIGN_CENTER);
if (EXTRA_SPACING) candidateParagraph.add("\n");
try {
document.add(candidateParagraph);
} catch (DocumentException d_excp) {
System.err.println(CLASS_NAME + "." + METHOD_NAME + " -
Exception encountered while writing Candidate paragraph. Details: " +
d_excp.getMessage());
}
/*
* Create the Employment History paragraph, which consists of the
heading and a
* table for each job. Each job table consists of four columns
(from/to, employer,
* role, main tools) in the first row and one column (description)
in the second row.
*/
Paragraph historyParagraph = new Paragraph();
historyParagraph.setSpacingAfter(STANDARD_VERTICAL_SPACE/2);
Chunk historyTitle = new Chunk("Employment History".toUpperCase(),
H2_FONT);
historyParagraph.add(historyTitle);
try {
document.add(historyParagraph);
} catch (DocumentException d_excp) {
System.err.println(CLASS_NAME + "." + METHOD_NAME + " -
Exception encountered while writing Employment History paragraph. Details: "
+ d_excp.getMessage());
}
/*
* Display the employment history data. For each job, write a table
that consists of
* four columns (from/to, employer, role, main tools) in the first
row and one column
* (description) in the second row.
*/
Object[][] EmploymentHistoryData = new Object[][] {
{"1800-present", "North Pole Inc.", "CEO", "candy canes",
"Head honcho. Blah blah blah blah blah blah blah blah blah blah blah blah
blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah
blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah
blah blah."},
{"1600-1800", "European Toy Company", "Vice President",
"kind words", "Ran the day-to-day operations, advised CEO on strategic
issues."},
{"400-1600", "Smith's Toys", "Manager", "gentle
supervision", "Oversaw daily operations of successful toy shop."}
};
for (int ix = 0; ix < EmploymentHistoryData.length; ix++) {
try {
Table jobTable = new Table(4);
jobTable.setPadding(2);
jobTable.setSpacing(20);
jobTable.setSpaceInsideCell(5);
jobTable.setSpaceBetweenCells(10);
jobTable.setWidth(150);
jobTable.endHeaders();
Phrase fromToData = new Phrase((String)
EmploymentHistoryData[ix][0], TABLE_FONT);
Cell fromToDataCell = new Cell(fromToData);
jobTable.addCell(fromToDataCell);
Phrase employerData = new Phrase((String)
EmploymentHistoryData[ix][1], TABLE_FONT);
Cell employerDataCell = new Cell(employerData);
jobTable.addCell(employerDataCell);
Phrase roleData = new Phrase((String)
EmploymentHistoryData[ix][2], TABLE_FONT);
Cell roleDataCell = new Cell(roleData);
jobTable.addCell(roleDataCell);
Phrase toolsUsedData = new Phrase((String)
EmploymentHistoryData[ix][3], TABLE_FONT);
Cell toolsUsedDataCell = new Cell(toolsUsedData);
jobTable.addCell(toolsUsedDataCell);
Phrase descriptionData = new Phrase((String)
EmploymentHistoryData[ix][4], TABLE_FONT);
Cell descriptionCell = new Cell(descriptionData);
descriptionCell.setColspan(4);
jobTable.addCell(descriptionCell);
document.add(jobTable);
} catch (DocumentException d_excp) {
System.err.println(CLASS_NAME + "." + METHOD_NAME + " -
Exception encountered while writing Job Table (" + ix + "). Details: " +
d_excp.getMessage());
}
} //end for
/* Create the paragraph for the candidate's formal education. */
Paragraph educationParagraph = new Paragraph();
Chunk educationTitle = new Chunk("Formal Education".toUpperCase(),
H2_FONT);
Chunk educationData = new Chunk("none", DETAIL_FONT);
educationParagraph.add(educationTitle);
educationParagraph.add("\n");
educationParagraph.add(educationData);
educationParagraph.setSpacingAfter(STANDARD_VERTICAL_SPACE);
if (EXTRA_SPACING) educationParagraph.add("\n");
try {
document.add(educationParagraph);
} catch (DocumentException d_excp) {
System.err.println(CLASS_NAME + "." + METHOD_NAME + " -
Exception encountered while writing Formal Education paragraph. Details: " +
d_excp.getMessage());
}
/* Create the paragraph for the candidate's recent education. */
Paragraph recentParagraph = new Paragraph();
Chunk recentTitle = new Chunk("Recent Education".toUpperCase(),
H2_FONT);
recentParagraph.add(recentTitle);
recentParagraph.add("\n");
List recentList = new List(false, LIST_INDENTATION);
recentList.setListSymbol(new Chunk(LIST_BULLET,
FontFactory.getFont(FontFactory.HELVETICA, 20)));
String[] RecentEducationData = new String[] {"Motivating Your Work
Force (seminar)", "Managing Seasonal Workers in a Non-union Environment
(seminar)"};
for (int ix = 0; ix < RecentEducationData.length; ix++) {
Phrase recentData = new Phrase(RecentEducationData[ix],
DETAIL_FONT);
ListItem recentItem = new ListItem(recentData);
recentList.add(recentItem);
if (EXTRA_SPACING) recentList.add("\n");
}
recentParagraph.add(recentList);
recentParagraph.setSpacingAfter(STANDARD_VERTICAL_SPACE);
if (EXTRA_SPACING) recentParagraph.add("\n");
try {
document.add(recentParagraph);
} catch (DocumentException d_excp) {
System.err.println(CLASS_NAME + "." + METHOD_NAME + " -
Exception encountered while writing Recent Education paragraph. Details: " +
d_excp.getMessage());
}
/* Create the paragraph for the candidate's languages/employability
information. */
Paragraph employabilityParagraph = new Paragraph();
Chunk employabilityTitle = new Chunk("Employability".toUpperCase(),
H2_FONT);
employabilityParagraph.add(employabilityTitle);
employabilityParagraph.add("\n");
List employabilityList = new List(false, LIST_INDENTATION);
employabilityList.setListSymbol(new Chunk(LIST_BULLET,
FontFactory.getFont(FontFactory.HELVETICA, 20)));
String[] EmployabilityData = new String[] {"Fluent in all
languages", "Able to work freely in the EC", "Green Card for USA"};
for (int ix = 0; ix < EmployabilityData.length; ix++) {
Phrase employabilityPhrase = new Phrase(EmployabilityData[ix],
DETAIL_FONT);
ListItem employabilityItem = new ListItem(employabilityPhrase);
employabilityList.add(employabilityItem);
if (EXTRA_SPACING) employabilityList.add("\n");
}
employabilityParagraph.add(employabilityList);
employabilityParagraph.setSpacingAfter(STANDARD_VERTICAL_SPACE);
if (EXTRA_SPACING) employabilityParagraph.add("\n");
try {
document.add(employabilityParagraph);
} catch (DocumentException d_excp) {
System.err.println(CLASS_NAME + "." + METHOD_NAME + " -
Exception encountered while writing Languages and Employability paragraph.
Details: " + d_excp.getMessage());
}
/* Start the Addendum on a new page. */
try {
document.newPage();
} catch (DocumentException d_excp) {
System.err.println(CLASS_NAME + "." + METHOD_NAME + " -
Exception encountered while skipping to new page. Details: " +
d_excp.getMessage());
}
/* Create the paragraph that contains the Addendum title. */
Paragraph addendumParagraph = new Paragraph();
Chunk addendumTitle = new Chunk("Addendum".toUpperCase(), H1_FONT);
addendumParagraph.add(addendumTitle);
addendumParagraph.setSpacingAfter(STANDARD_VERTICAL_SPACE);
if (EXTRA_SPACING) addendumParagraph.add("\n");
try {
document.add(addendumParagraph);
} catch (DocumentException d_excp) {
System.err.println(CLASS_NAME + "." + METHOD_NAME + " -
Exception encountered while writing Addendum heading. Details: " +
d_excp.getMessage());
}
/* Create the paragraph that contains the operating system list. */
Paragraph osParagraph = new Paragraph();
Chunk osTitle = new Chunk("Operating Systems", H2_FONT);
Chunk osData = new Chunk("Windows, Linux, OS/2", DETAIL_FONT);
osParagraph.add(osTitle);
osParagraph.add("\n");
osParagraph.add(osData);
osParagraph.setSpacingAfter(STANDARD_VERTICAL_SPACE);
if (EXTRA_SPACING) osParagraph.add("\n");
try {
document.add(osParagraph);
} catch (DocumentException d_excp) {
System.err.println(CLASS_NAME + "." + METHOD_NAME + " -
Exception encountered while writing Operating Systems paragraph. Details: "
+ d_excp.getMessage());
}
/* Create the paragraph that contains the computer languages list.
*/
Paragraph computerLanguagesParagraph = new Paragraph();
Chunk computerLanguagesTitle = new Chunk("Computer Languages",
H2_FONT);
Chunk computerLanguagesData = new Chunk("COBOL, Java", DETAIL_FONT);
computerLanguagesParagraph.add(computerLanguagesTitle);
computerLanguagesParagraph.add("\n");
computerLanguagesParagraph.add(computerLanguagesData);
computerLanguagesParagraph.setSpacingAfter(STANDARD_VERTICAL_SPACE);
if (EXTRA_SPACING) computerLanguagesParagraph.add("\n");
try {
document.add(computerLanguagesParagraph);
} catch (DocumentException d_excp) {
System.err.println(CLASS_NAME + "." + METHOD_NAME + " -
Exception encountered while writing Computer Languages paragraph. Details: "
+ d_excp.getMessage());
}
/* Create the paragraph that contains the databases list. */
Paragraph databasesParagraph = new Paragraph();
Chunk databasesTitle = new Chunk("Databases", H2_FONT);
Chunk databasesData = new Chunk("DB2, MySQL, IMS", DETAIL_FONT);
databasesParagraph.add(databasesTitle);
databasesParagraph.add("\n");
databasesParagraph.add(databasesData);
databasesParagraph.setSpacingAfter(STANDARD_VERTICAL_SPACE);
if (EXTRA_SPACING) databasesParagraph.add("\n");
try {
document.add(databasesParagraph);
} catch (DocumentException d_excp) {
System.err.println(CLASS_NAME + "." + METHOD_NAME + " -
Exception encountered while writing Databases paragraph. Details: " +
d_excp.getMessage());
}
/* Create the paragraph that contains the word processor list. */
Paragraph wordProcParagraph = new Paragraph();
Chunk wordProcTitle = new Chunk("Word Processors", H2_FONT);
Chunk wordProcData = new Chunk("Lotus WordPro, Word Perfect",
DETAIL_FONT);
wordProcParagraph.add(wordProcTitle);
wordProcParagraph.add("\n");
wordProcParagraph.add(wordProcData);
wordProcParagraph.setSpacingAfter(STANDARD_VERTICAL_SPACE);
if (EXTRA_SPACING) wordProcParagraph.add("\n");
try {
document.add(wordProcParagraph);
} catch (DocumentException d_excp) {
System.err.println(CLASS_NAME + "." + METHOD_NAME + " -
Exception encountered while writing Word Processors paragraph. Details: " +
d_excp.getMessage());
}
/* Create the paragraph that contains the editor list. */
Paragraph editorsParagraph = new Paragraph();
Chunk editorsTitle = new Chunk("Editors and IDEs", H2_FONT);
Chunk editorsData = new Chunk("PFE (Programmer's File Editor),
Eclipse, Visual Age Java", DETAIL_FONT);
editorsParagraph.add(editorsTitle);
editorsParagraph.add("\n");
editorsParagraph.add(editorsData);
editorsParagraph.setSpacingAfter(STANDARD_VERTICAL_SPACE);
if (EXTRA_SPACING) editorsParagraph.add("\n");
try {
document.add(editorsParagraph);
} catch (DocumentException d_excp) {
System.err.println(CLASS_NAME + "." + METHOD_NAME + " -
Exception encountered while writing Editors paragraph. Details: " +
d_excp.getMessage());
}
/* Create the paragraph that contains the tools list. */
Paragraph toolsParagraph = new Paragraph();
Chunk toolsTitle = new Chunk("Other Tools", H2_FONT);
Chunk toolsData = new Chunk("Internet Explorer, Firefox, Opera,
Usenet, Google", DETAIL_FONT);
toolsParagraph.add(toolsTitle);
toolsParagraph.add("\n");
toolsParagraph.add(toolsData);
toolsParagraph.setSpacingAfter(STANDARD_VERTICAL_SPACE);
if (EXTRA_SPACING) toolsParagraph.add("\n");
try {
document.add(toolsParagraph);
} catch (DocumentException d_excp) {
System.err.println(CLASS_NAME + "." + METHOD_NAME + " -
Exception encountered while writing Tools paragraph. Details: " +
d_excp.getMessage());
}
/* create the paragraph that lists the other computer experience. */
Paragraph experienceParagraph = new Paragraph();
Chunk experienceTitle = new Chunk("Experience", H2_FONT);
experienceParagraph.add(experienceTitle);
experienceParagraph.add("\n");
experienceParagraph.setSpacingAfter(STANDARD_VERTICAL_SPACE/5);
List experienceList = new List(false, LIST_INDENTATION);
experienceList.setListSymbol(new Chunk(LIST_BULLET,
FontFactory.getFont(FontFactory.HELVETICA, 20)));
String[] OtherExperienceData = new String[] {"Structured Analysis",
"Structured Design", "Data Modelling", "Database Design"};
for (int ix = 0; ix < OtherExperienceData.length; ix++) {
Phrase experienceData = new Phrase(OtherExperienceData[ix],
DETAIL_FONT);
ListItem experienceItem = new ListItem(experienceData);
experienceList.add(experienceItem);
if (EXTRA_SPACING) experienceList.add("\n");
}
experienceParagraph.add(experienceList);
if (EXTRA_SPACING) experienceParagraph.add("\n");
try {
document.add(experienceParagraph);
} catch (DocumentException d_excp) {
System.err.println(CLASS_NAME + "." + METHOD_NAME + " -
Exception encountered while writing Other Experience paragraph. Details: " +
d_excp.getMessage());
}
/* Write the examples note. */
Paragraph examplesParagraph = new Paragraph();
Chunk examples = new Chunk("Merry Christmas!", EXAMPLES_FONT);
examplesParagraph.add(examples);
examplesParagraph.setSpacingBefore(STANDARD_VERTICAL_SPACE);
try {
document.add(examplesParagraph);
} catch (DocumentException d_excp) {
System.err.println(CLASS_NAME + "." + METHOD_NAME + " -
Exception encountered while writing Examples paragraph. Details: " +
d_excp.getMessage());
}
/* Close the document. */
document.close();
/* Tell the user where the file was written. */
System.out.println("The RTF document was written to file " +
strOutfile + ".");
}
}
=================================================================
Rhino
---
rhino1 AT sympatico DOT ca
"There are two ways of constructing a software design. One way is to make it
so simple that there are obviously no deficiencies. And the other way is to
make it so complicated that there are no obvious deficiencies." - C.A.R.
Hoare
-------------------------------------------------------
The SF.Net email is sponsored by: Beat the post-holiday blues
Get a FREE limited edition SourceForge.net t-shirt from ThinkGeek.
It's fun and FREE -- well, almost....http://www.thinkgeek.com/sfshirt
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions