Я использовал iText в Java-приложении Swing - он хорошо работал для создания некоторых базовых PDF-файлов.Код выглядит примерно так:
@Override public void buildPDF(List<Folder> folders) {
Document document = new Document();
String fname = "";
boolean open = false;
try {
// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
fname = filename(getName()) + "-" + filename(getDataset().getName()) + ".pdf";
PdfWriter writer = PdfWriter.getInstance(document,
new FileOutputStream(fname));
for (int i = 0; i < folders.size(); i++ ) {
// grab the folder
LIFolder f = (LIFolder) folders.get(i);
if (f == null) continue;
open = true;
break;
}
// we have no valid folders
if (folders.size() > 0 && !open) {
// they selected an empty row
javax.swing.JOptionPane.showMessageDialog(null, BUNDLE.getString("report-none-selected"),
BUNDLE.getString("report-none-selected-title"),
javax.swing.JOptionPane.ERROR_MESSAGE);
return;
}
if (folders.size() == 0) {
// get all folders from dataset
folders = FolderFinder.findAll(getSession(), getDataset());
}
open = false;
for (int i = 0; i < folders.size(); i++ ) {
// grab the folder
LIFolder f = (LIFolder) folders.get(i);
if (f == null) continue;
if (!open) {
open = true;
document.open();
}
Paragraph p = new Paragraph(BUNDLE.getString("report-heading-summary-main"), getPageHeadingFont());
p.setAlignment("center");
document.add(p);
p = new Paragraph(BUNDLE.getString("report-heading-summary-main-sub"), this.pageHeadingFont1);
p.setAlignment("center");
document.add(p);
blankLine(document);
drawLine(writer);
///////////////////////////////////////////////////////////////////////////////////////////////////
// Primary Statement Details
///////////////////////////////////////////////////////////////////////////////////////////////////
p = new Paragraph("Primary Statement Details", this.pageHeadingFont2);
p.setAlignment("center");
document.add(p);
blankLine(document);
PdfPTable table = new PdfPTable(4);
table.addCell(new BorderlessCell(new Paragraph("Dataset:", getFieldHeadingFont())));
BorderlessCell cell = new BorderlessCell(new Paragraph(getDataset().getName(), getTextFont()));
cell.setColspan(3);
table.addCell(cell);
table.addCell(new BorderlessCell(new Paragraph("Data Entry Clerk:", getFieldHeadingFont())));
cell = new BorderlessCell(new Paragraph(
(f.getDataEntryClerk() != null ? f.getDataEntryClerk().toDescriptionPathString() : emptyIfNull(null)),
getTextFont()));
cell.setColspan(3);
table.addCell(cell);
table.setWidthPercentage(100);
cell = new BorderlessCell(new Paragraph("Statement Number:", getFieldHeadingFont()));
table.addCell(cell);
table.addCell(new BorderlessCell(new Paragraph(f.getReferenceId(), getTextFont())));
table.addCell(new BorderlessCell(new Paragraph("Statement Date:", getFieldHeadingFont())));
String strDate = "";
java.util.Date date = f.getStatementDate();
if (date != null) {
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
strDate = formatter.format(date);
}
table.addCell(new BorderlessCell(new Paragraph(strDate, getTextFont())));
table.addCell(new BorderlessCell(new Paragraph("Statement Location:", getFieldHeadingFont())));
cell = new BorderlessCell(new Paragraph(emptyIfNull(f.getTakerLocation()), getTextFont()));
cell.setColspan(3);
table.addCell(cell);
table.addCell(new BorderlessCell(new Paragraph("Statement keywords:", getFieldHeadingFont())));
cell = new BorderlessCell(new Paragraph(emptyIfNull(f.getKeywords()), getTextFont()));
cell.setColspan(3);
table.addCell(cell);
document.add(table);
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
// Statement Giver's Details
///////////////////////////////////////////////////////////////////////////////////////////////////
LIPerson p01 = null;
Set<Actor> actors = f.getActors();
Iterator iter = actors.iterator();
while (iter.hasNext()) {
Actor actor = (Actor) iter.next();
if (actor instanceof LIPerson) {
LIPerson person = (LIPerson) actor;
if (person.getReferenceId().toString().equalsIgnoreCase("p01") ) {
p01 = person;
break;
}
}
}
blankLine(document);
drawLine(writer);
p = new Paragraph(new Chunk("Statement Giver's Details", this.pageHeadingFont2));
p.setAlignment("center");
document.add(p);
java.util.ArrayList giver = new java.util.ArrayList();
if (p01 != null)
giver.add(p01);
table = new PdfPTable(2);
table.setWidthPercentage(100f);
table.addCell(new BorderlessCell(new Paragraph("Name of Statement Giver:", getFieldHeadingFont())));
table.addCell(new BorderlessCell(new Paragraph(ActorsToString(giver), getTextFont())));
table.addCell(new BorderlessCell(new Paragraph("Sex:", getFieldHeadingFont())));
if (p01 == null)
table.addCell(new BorderlessCell(new Paragraph(emptyIfNull("TBD"), getTextFont())));
else
table.addCell(new BorderlessCell(new Paragraph((p01.getSex() != null ? p01.getSex().toString() : ""), getTextFont())));
table.addCell(new BorderlessCell(new Paragraph("Tribe:", getFieldHeadingFont())));
if (p01 == null)
table.addCell(new BorderlessCell(new Paragraph(emptyIfNull(null), getTextFont())));
else
table.addCell(new BorderlessCell(
new Paragraph((p01.getEthnicityOrTribe() != null ? p01.getEthnicityOrTribe().toDescriptionPathString() : emptyIfNull(null)),
getTextFont())));
table.addCell(new BorderlessCell(new Paragraph("Nationality:", getFieldHeadingFont())));
if (p01 == null)
table.addCell(new BorderlessCell(new Paragraph(emptyIfNull(null), getTextFont())));
else
table.addCell(new BorderlessCell(
new Paragraph((p01.getNationality() != null ? p01.getNationality().toDescriptionPathString() : emptyIfNull(null)),
getTextFont())));
table.addCell(new BorderlessCell(new Paragraph("Marital Status:", getFieldHeadingFont())));
table.addCell(new BorderlessCell(new Paragraph(emptyIfNull(f.getMaritalStatus()), getTextFont())));
table.addCell(new BorderlessCell(new Paragraph("Education Level:", getFieldHeadingFont())));
table.addCell(new BorderlessCell(new Paragraph(emptyIfNull(f.getEducationLevel()), getTextFont())));
table.addCell(new BorderlessCell(new Paragraph("County of Origin:", getFieldHeadingFont())));
table.addCell(new BorderlessCell(new Paragraph(emptyIfNull(f.getCountyOfOrigin()), getTextFont())));
table.addCell(new BorderlessCell(new Paragraph("Mother's Name:", getFieldHeadingFont())));
table.addCell(new BorderlessCell(new Paragraph(emptyIfNull(f.getMothersName()), getTextFont())));
table.addCell(new BorderlessCell(new Paragraph("Father's Name:", getFieldHeadingFont())));
table.addCell(new BorderlessCell(new Paragraph(emptyIfNull(f.getFathersName()), getTextFont())));
table.addCell(new BorderlessCell(new Paragraph("# of Dependents:", getFieldHeadingFont())));
table.addCell(new BorderlessCell(new Paragraph(Integer.toString(f.getNumDependents()), getTextFont())));
table.addCell(new BorderlessCell(new Paragraph("Phone Number:", getFieldHeadingFont())));
table.addCell(new BorderlessCell(new Paragraph(emptyIfNull(f.getPhoneNumber()), getTextFont())));
document.add(table);
///////////////////////////////////////////////////////////////////////////////////////////////////
// Summary
///////////////////////////////////////////////////////////////////////////////////////////////////
blankLine(document);
drawLine(writer);
p = new Paragraph(new Chunk("Summary", this.pageHeadingFont2));
p.setAlignment("center");
document.add(p);
p = new Paragraph(new Chunk(emptyIfNull(f.getSourceSummary()), getTextFont()));
document.add(p);
///////////////////////////////////////////////////////////////////////////////////////////////////
// Incident(s)
///////////////////////////////////////////////////////////////////////////////////////////////////
Set<Act> acts = f.getActs();
if (acts.size() > 0) {
// sort them by id
Hashtable map = new Hashtable();
Iterator it = acts.iterator();
String[] ids = new String[acts.size()];
int x = 0;
while (it.hasNext()) {
LIAct act = (LIAct) it.next();
String index = Integer.toString(act.getId());
map.put(index, act);
ids[x++] = index;
}
java.util.Arrays.sort(ids);
blankLine(document);
drawLine(writer);
p = new Paragraph(new Chunk("Act(s)", this.pageHeadingFont2));
p.setAlignment("center");
document.add(p);
blankLine(document);
table = new PdfPTable(4);
table.setWidthPercentage(100);
for (int y = 0; y < ids.length; y++) {
LIAct act = (LIAct) map.get(ids[y]);
table.addCell(new BorderlessCell(new Paragraph("Act ID:", getFieldHeadingFont())));
cell = new BorderlessCell(new Paragraph(act.getReferenceId(), getTextFont()));
cell.setColspan(3);
table.addCell(cell);
table.addCell(new BorderlessCell(new Paragraph("Start Date:", getFieldHeadingFont())));
table.addCell(new BorderlessCell(new Paragraph((act.getWhen() != null ? act.getWhen().toString():""), getTextFont())));
table.addCell(new BorderlessCell(new Paragraph("End Date:", getFieldHeadingFont())));
table.addCell(new BorderlessCell(new Paragraph((act.getUntil() != null?act.getUntil().toString():""), getTextFont())));
table.addCell(new BorderlessCell(new Paragraph("Location of act:", getFieldHeadingFont())));
p = new Paragraph(emptyIfNull(act.getWhere().toString()), getTextFont());
p.setAlignment(Paragraph.ALIGN_LEFT);
cell = new BorderlessCell(p);
cell.setColspan(3);
table.addCell(cell);
table.addCell(new BorderlessCell(new Paragraph("Precise Location:", getFieldHeadingFont())));
p = new Paragraph(emptyIfNull(act.getPreciseLocation()), getTextFont());
p.setAlignment(Paragraph.ALIGN_LEFT);
cell = new BorderlessCell(p);
cell.setColspan(3);
table.addCell(cell);
table.addCell(new BorderlessCell(new Paragraph("Violation:", getFieldHeadingFont())));
p = new Paragraph(emptyIfNull(act.getViolation().toDescriptionPathString()), getTextFont());
p.setAlignment(Paragraph.ALIGN_LEFT);
cell = new BorderlessCell(p);
cell.setColspan(3);
table.addCell(cell);
table.addCell(new BorderlessCell(new Paragraph("Description:", getFieldHeadingFont())));
p = new Paragraph(emptyIfNull(act.getDescriptionLocation()), getTextFont());
p.setAlignment(Paragraph.ALIGN_LEFT);
cell = new BorderlessCell(p);
cell.setColspan(3);
table.addCell(cell);
// add empty cell
cell = new BorderlessCell(new Paragraph(""));
cell.setColspan(4);
table.addCell(cell);
cell = new BorderlessCell(new Paragraph(""));
cell.setColspan(4);
table.addCell(cell);
}
document.add(table);
}
/////////////////////////////////////////////////////////////////////////////////////////////////
// Victims
/////////////////////////////////////////////////////////////////////////////////////////////////
List<Actor> victims = this.getAllVictims(f);
if (!victims.isEmpty()) {
blankLine(document);
drawLine(writer);
String strVictims = ActorsToString(victims);
p = new Paragraph(new Chunk("Victim(s)", this.pageHeadingFont2));
p.setAlignment("center");
document.add(p);
p = new Paragraph(new Chunk("Name(s) of Victim(s): ", getFieldHeadingFont()));
p.add(new Chunk(strVictims, getTextFont()));
document.add(p);
String gender = ActorsToGender(victims);
p = new Paragraph(new Chunk("Gender: ", getFieldHeadingFont()));
p.add(new Chunk(gender, getTextFont()));
document.add(p);
}
/////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////
// Perps
/////////////////////////////////////////////////////////////////////////////////////////////////
List<Actor> perps = this.getAllPerpetrators(f);
if (!perps.isEmpty()) {
blankLine(document);
drawLine(writer);
p = new Paragraph(new Chunk("Perpetrator(s)", this.pageHeadingFont2));
p.setAlignment("center");
document.add(p);
String strPerps = ActorsToString(perps);
p = new Paragraph(new Chunk("Name(s) of alleged perpetrator(s): ", getFieldHeadingFont()));
p.add(new Chunk(strPerps, getTextFont()));
document.add(p);
}
/////////////////////////////////////////////////////////////////////////////////////////////////
document.newPage();
}
} catch (DocumentException de) {
System.err.println(de.getMessage());
} catch (java.io.IOException ioe) {
System.err.println(ioe.getMessage());
}
// step 5: we close the document
if (open) {
document.close();
if (!fname.equals("")) {
// launch it
Reports.LaunchPDF(fname);
}
}
}
Если вам нужна дополнительная информация, дайте мне знать.Удачи!