Skip to content

Commit eba84ab

Browse files
Aspose.Pdf Java for Struts 1.3
Signed-off-by: AdeelIlyas2014 <adeel.ilyas@aspose.com>
1 parent 1d1f048 commit eba84ab

File tree

15 files changed

+669
-0
lines changed

15 files changed

+669
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" output="target/classes" path="src/main/java">
4+
<attributes>
5+
<attribute name="optional" value="true"/>
6+
<attribute name="maven.pomderived" value="true"/>
7+
</attributes>
8+
</classpathentry>
9+
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
10+
<attributes>
11+
<attribute name="maven.pomderived" value="true"/>
12+
</attributes>
13+
</classpathentry>
14+
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
15+
<attributes>
16+
<attribute name="optional" value="true"/>
17+
<attribute name="maven.pomderived" value="true"/>
18+
</attributes>
19+
</classpathentry>
20+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5">
21+
<attributes>
22+
<attribute name="maven.pomderived" value="true"/>
23+
</attributes>
24+
</classpathentry>
25+
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
26+
<attributes>
27+
<attribute name="maven.pomderived" value="true"/>
28+
</attributes>
29+
</classpathentry>
30+
<classpathentry kind="output" path="target/classes"/>
31+
</classpath>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>Aspose.Pdf for Struts</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.wst.common.project.facet.core.builder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
<buildCommand>
14+
<name>org.eclipse.jdt.core.javabuilder</name>
15+
<arguments>
16+
</arguments>
17+
</buildCommand>
18+
<buildCommand>
19+
<name>org.eclipse.m2e.core.maven2Builder</name>
20+
<arguments>
21+
</arguments>
22+
</buildCommand>
23+
</buildSpec>
24+
<natures>
25+
<nature>org.eclipse.jdt.core.javanature</nature>
26+
<nature>org.eclipse.m2e.core.maven2Nature</nature>
27+
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
28+
</natures>
29+
</projectDescription>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Aspose.Pdf Java for Struts 1.3
2+
Aspose.Pdf Java for Struts is a Maven based struts 1.3 web project that demonstrates the Aspose.Pdf for Java API usage example within Struts 1.3 and Maven framework.
3+
4+
The project is initially Eclipse based but can be built through mvn command line without any IDE support.
5+
6+
The project can also be easily imported in any IDE i.e IntelliJ IDEA and NetBeans etc.
7+
8+
You should have apache tomcat installed. After building the project .war file, copy to webapp folder.
9+
10+
For most complete documentation of the project, check Aspose.Pdf Java for Struts confluence wiki
11+
12+
http://www.aspose.com/docs/display/pdfjava/3.+Aspose.Pdf+Java+For+Struts
13+
14+
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package com.books;
2+
3+
import java.util.List;
4+
import java.util.Map;
5+
6+
import javax.servlet.ServletContext;
7+
import javax.servlet.ServletOutputStream;
8+
9+
import aspose.pdf.BorderInfo;
10+
import aspose.pdf.MarginInfo;
11+
import aspose.pdf.Pdf;
12+
import aspose.pdf.Row;
13+
import aspose.pdf.Section;
14+
import aspose.pdf.Table;
15+
16+
17+
/**
18+
*
19+
* @author Adeel
20+
*
21+
*/
22+
public class AsposeAPIHelper {
23+
24+
/**
25+
* Creates pdf ocument from list of book provided from grid.
26+
*
27+
* @param out
28+
* the current scope OutputStream.
29+
* @param books
30+
* books list as map containing attributes.
31+
* @param context
32+
* the App ServletContext
33+
* @see aspose.pdf.Pdf
34+
*/
35+
public static void createAsposePdf(ServletOutputStream out,
36+
List<Map> books, ServletContext context) throws Exception {
37+
try {
38+
39+
// Create PDF document
40+
Pdf pdf1 = new Pdf();
41+
// Add a section into the PDF document
42+
Section sec1 = pdf1.getSections().add();
43+
44+
// Add a text paragraph into the section
45+
Table table = new Table(sec1);
46+
MarginInfo margin2 = new MarginInfo();
47+
sec1.getParagraphs().add(table);
48+
table.setColumnWidths("80 80 100 80");
49+
MarginInfo margin = new MarginInfo();
50+
margin.setLeft(5f);
51+
margin.setRight(5f);
52+
margin.setTop(5f);
53+
margin.setBottom(5f);
54+
// Set the default cell padding to the MarginInfo object
55+
table.setDefaultCellPadding(margin);
56+
table.setDefaultCellBorder(new BorderInfo(
57+
com.aspose.pdf.BorderSide.All, 0.1F));
58+
table.setBorder(new BorderInfo(com.aspose.pdf.BorderSide.All, 1F));
59+
60+
Row row1 = table.getRows().add();
61+
62+
row1.getCells().add("Book Id");
63+
row1.getCells().add("Book Name");
64+
row1.getCells().add("AuthorName");
65+
row1.getCells().add("Book Cost");
66+
for (Map book : books) {
67+
String bookId = book.get("BookId").toString();
68+
String bookName = book.get("BookName").toString();
69+
String bookAuthorName = book.get("AuthorName").toString();
70+
String bookCost = book.get("BookCost").toString();
71+
Row rows = table.getRows().add();
72+
rows.getCells().add(bookId);
73+
rows.getCells().add(bookName);
74+
rows.getCells().add(bookAuthorName);
75+
rows.getCells().add(bookCost);
76+
}
77+
78+
pdf1.save(out);
79+
80+
} catch (Exception e) {
81+
throw new Exception(
82+
"Aspose: Unable to export to pdf format.. some error occured",
83+
e);
84+
85+
}
86+
}
87+
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package com.books;
2+
3+
import org.apache.struts.action.ActionForm;
4+
import org.apache.struts.action.ActionMapping;
5+
import org.apache.struts.action.ActionForward;
6+
import org.apache.struts.actions.DispatchAction;
7+
8+
import javax.servlet.http.HttpServletRequest;
9+
import javax.servlet.http.HttpServletResponse;
10+
11+
import java.util.List;
12+
import java.util.Map;
13+
14+
/**
15+
*
16+
* @author Adeel
17+
*
18+
*/
19+
20+
public class BookActions extends DispatchAction {
21+
public ActionForward AddBook(ActionMapping mapping, ActionForm form,
22+
HttpServletRequest request, HttpServletResponse response) {
23+
System.out.println("Add Book Page");
24+
return mapping.findForward("addBook");
25+
}
26+
27+
public ActionForward EditBook(ActionMapping mapping, ActionForm form,
28+
HttpServletRequest request, HttpServletResponse response) {
29+
System.out.println("Edit Book Page");
30+
int bookId = Integer.parseInt(request.getParameter("bookId"));
31+
32+
Books b = Books.getInstance();
33+
Map bookDet = b.searchBook(bookId);
34+
35+
// Used form bean class methods to fill the form input elements with
36+
// selected book values.
37+
BookForm bf = (BookForm) form;
38+
bf.setBookName(bookDet.get("BookName").toString());
39+
bf.setAuthorName(bookDet.get("AuthorName").toString());
40+
bf.setBookCost((Integer) bookDet.get("BookCost"));
41+
bf.setBookId((Integer) bookDet.get("BookId"));
42+
return mapping.findForward("editBook");
43+
}
44+
45+
public ActionForward SaveBook(ActionMapping mapping, ActionForm form,
46+
HttpServletRequest request, HttpServletResponse response) {
47+
System.out.println("Save Book");
48+
// Used form bean class methods to get the value of form input elements.
49+
BookForm bf = (BookForm) form;
50+
String bookName = bf.getBookName();
51+
String authorName = bf.getAuthorName();
52+
int bookCost = bf.getBookCost();
53+
54+
Books b = Books.getInstance();
55+
b.storeBook(bookName, authorName, bookCost);
56+
return new ActionForward("/showbooks.do", true);
57+
}
58+
59+
public ActionForward UpdateBook(ActionMapping mapping, ActionForm form,
60+
HttpServletRequest request, HttpServletResponse response) {
61+
System.out.println("Update Book");
62+
BookForm bf = (BookForm) form;
63+
String bookName = bf.getBookName();
64+
String authorName = bf.getAuthorName();
65+
int bookCost = bf.getBookCost();
66+
int bookId = bf.getBookId();
67+
68+
Books b = Books.getInstance();
69+
b.updateBook(bookId, bookName, authorName, bookCost);
70+
return new ActionForward("/showbooks.do", true);
71+
}
72+
73+
public ActionForward DeleteBook(ActionMapping mapping, ActionForm form,
74+
HttpServletRequest request, HttpServletResponse response) {
75+
System.out.println("Delete Book");
76+
int bookId = Integer.parseInt(request.getParameter("bookId"));
77+
Books b = Books.getInstance();
78+
b.deleteBook(bookId);
79+
return new ActionForward("/showbooks.do", true);
80+
}
81+
82+
/**
83+
* Returns PDF file that can be downloaded locally.
84+
* @see AsposeAPIHelper
85+
*/
86+
public ActionForward ExportToPdf(ActionMapping mapping, ActionForm form,
87+
HttpServletRequest request, HttpServletResponse response) {
88+
System.out.println("Aspose export pdf");
89+
90+
Books b = Books.getInstance();
91+
92+
List<Map> books = b.getBookList();
93+
response.setContentType("application/pdf");
94+
response.setHeader("Content-Disposition",
95+
"attachment;filename=AsposeExportBooksList.pdf");
96+
for (Map book : books) {
97+
try {
98+
AsposeAPIHelper.createAsposePdf(response.getOutputStream(),
99+
books, request.getServletContext());
100+
} catch (Exception e) {
101+
e.printStackTrace();
102+
103+
}
104+
105+
}
106+
107+
return null;
108+
}
109+
110+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.books;
2+
3+
import org.apache.struts.action.ActionForm;
4+
5+
public class BookForm extends ActionForm {
6+
7+
private String bookName;
8+
private String authorName;
9+
private int bookCost;
10+
private int bookId;
11+
12+
public BookForm() {
13+
super();
14+
}
15+
16+
public String getBookName() {
17+
return bookName;
18+
}
19+
20+
public void setBookName(String bookName) {
21+
this.bookName = bookName;
22+
}
23+
24+
public String getAuthorName() {
25+
return authorName;
26+
}
27+
28+
public void setAuthorName(String authorName) {
29+
this.authorName = authorName;
30+
}
31+
32+
public int getBookCost() {
33+
return bookCost;
34+
}
35+
36+
public void setBookCost(int bookCost) {
37+
this.bookCost = bookCost;
38+
}
39+
40+
public int getBookId() {
41+
return bookId;
42+
}
43+
44+
public void setBookId(int bookId) {
45+
this.bookId = bookId;
46+
}
47+
}

0 commit comments

Comments
 (0)