Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to enhance this code further ??

 
Ashwin Kumar
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Please help me in enhancing the below code further. The below code creates 2frames and prints a table in the first frame which flows into the second page also. I would like to add another table in the second frame which should get displayed in the first page. If i uncomment the comments(code) in the below code, the first table flow is getting displayed in both the frames of the first page and second table is getting displayed in the second page.

How to resovle this issue and control the flow so that the first page displays both tables in both the frames.


package examples.pagelayout;
import com.klg.jclass.page.*;
import com.klg.jclass.page.adobe.pdf.JCPDFPrinter;
import com.klg.jclass.page.html.JCHTMLPrinter;
import javax.swing.*;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.StringReader;
import java.util.List;

public class PdfPage {
public JCDocument document;
public JCTextStyle standard_style;

private static String template =
"<?xml version=\"1.0\"?>\n" +
"<!DOCTYPE JCPAGETEMPLATE SYSTEM \"JCPageTemplate.dtd\">\n" +
"<JCPAGETEMPLATE TITLE=\"PDFIssue1.srep\">\n" +
"<PAGE NAME=\"PDFIssue1.srep_0\" UNIT=\"points\">\n" +
"<LOCATION X=\"0\" Y=\"0\"/>\n" +
"<SIZE WIDTH=\"600\" HEIGHT=\"750\"/>\n" +
"<FRAME NAME=\"0\" UNIT=\"points\" COLOR=\"grey\">\n" +
"<LOCATION X=\"76\" Y=\"75\"/>\n" +
"<SIZE WIDTH=\"200\" HEIGHT=\"660\"/>\n" +
"</FRAME>\n" +
"<FRAME NAME=\"1\" UNIT=\"points\" COLOR=\"pink\">\n" +
"<LOCATION X=\"354\" Y=\"75\"/>\n" +
"<SIZE WIDTH=\"200\" HEIGHT=\"500\"/>\n" +
"</FRAME>\n" +
"<FLOWFRAME NAME=\"0\" />\n" +
//"<FLOWFRAME NAME=\"1\" />\n" +
"<FLOWPAGE NAME=\"PDFIssue1.srep_0\" />\n" +
"<FLOWSECTION NAME=\"PDFIssue1.srep_0\" />\n" +
"</PAGE>\n" +
/*"<PAGE NAME=\"PDFIssue1.srep_1\" UNIT=\"points\">\n" +
"<LOCATION X=\"0\" Y=\"0\"/>\n" +
"<SIZE WIDTH=\"600\" HEIGHT=\"750\"/>\n" +
"<FRAME NAME=\"0\" UNIT=\"points\" COLOR=\"grey\">\n" +
"<LOCATION X=\"76\" Y=\"75\"/>\n" +
"<SIZE WIDTH=\"200\" HEIGHT=\"800\"/>\n" +
"</FRAME>\n" +
"<FLOWFRAME NAME=\"0\" />\n" +
"<FLOWPAGE NAME=\"PDFIssue1.srep_1\" />\n" +
"<FLOWSECTION NAME=\"PDFIssue1.srep_1\" /> \n" +
"</PAGE>\n" +*/
"</JCPAGETEMPLATE>\n";


public PdfPage(JCPrinter printer, List templates)
{
// Create the document and load the page templates
document = new JCDocument(printer, templates);

createTable(document);
//createTable1(document);

}

public JCPageTable createTable(JCDocument doc) {

String data[][] = {{"pdf","doc"},{"pdf","doc"},
{"pdf","doc"},{"pdf","doc"},{"pdf","doc"},{"pdf","doc"},{"pdf","doc"},{"pdf","doc"},{"pdf","doc"}
,{"pdf","doc"},{"pdf","doc"},{"pdf","doc"},{"pdf","doc"},{"pdf","doc"},{"pdf","doc"},{"pdf","doc"}
,{"pdf","doc"},{"pdf","doc"},{"pdf","doc"},{"pdf","doc"},{"pdf","doc"},{"pdf","doc"},{"pdf","doc"}
,{"pdf","doc"},{"pdf","doc"},{"pdf","doc"},{"pdf","doc"},{"pdf","doc"},{"pdf","doc"},{"pdf","doc"}
,{"pdf","doc"},{"pdf","doc"},{"pdf","doc"},{"pdf","doc"},{"pdf","doc"},{"pdf","doc"},{"pdf","doc"}};
String col[] = {"ext1","ext2"};

JFrame frame = new JFrame("Creating JTable Component Example!");
JPanel panel = new JPanel();
JTable table = new JTable(data,col);

panel.add(table);

frame.add(panel);
//frame.setSize(300,200);
frame.setVisible(true);

return JCPageTableFromJTable.createTable(doc,table,true);
}
//2nd table
/*
public JCPageTable createTable1(JCDocument doc) {

String data[][] = {{"txt","xls"},{"txt","xls"},
{"txt","xls"},{"txt","xls"},{"txt","xls"},{"txt","xls"},{"txt","xls"},{"txt","xls"},{"txt","xls"}
,{"txt","xls"},{"txt","xls"},{"txt","xls"},{"txt","xls"},{"txt","xls"},{"txt","xls"},{"txt","xls"}};
String col[] = {"ext3","ext4"};

JFrame frame = new JFrame("Creating JTable Component Example!");
JPanel panel = new JPanel();
JTable table1 = new JTable(data,col);

panel.add(table1);

frame.add(panel);
//frame.setSize(300,200);
frame.setVisible(true);

return JCPageTableFromJTable.createTable(doc,table1,true);
}*/

//---
public PdfPage(){

}



public JCDocument getDocument()
{

return(document);

}

public static void main(String args[]) {




PdfPage pf= new PdfPage();
PdfPage pf1= new PdfPage();



List templates = null;
try {
templates = JCPageTemplate.loadTemplates(new StringReader(template));
}
catch (Exception e) {
System.err.println("Error loading template = " + e);
System.exit(1);
}

FileOutputStream outfile;
try{
outfile = new FileOutputStream("C:\\eclipse3.2/eclipse/workspace/temp/Page1.pdf");
}catch(FileNotFoundException e)
{
System.out.println("no file");
return;
}

// Get a printer of the correct type
JCPrinter printer = new JCPDFPrinter(outfile);
//JCPrinter printer = PrintManager.getPrinter(args, templates);
if (printer == null) {
System.exit(0);
}
else if (printer instanceof JCHTMLPrinter) {
System.out.println("This example will not work with an HTML printe");
System.exit(0);
}


JCDocument document = new JCDocument(printer,templates);

// instantiate a flow object on the document
JCFlow flow = new JCFlow(document);

JCPageTable table =pf.createTable(document);
//JCPageTable table1 =pf1.createTable1(document);




//JCDocument document = new JCDocument(printer, template);
// Get the contents frame from the first page template
JCPage page1 = document.stringToTemplate("PDFIssue1.srep_0");
//JCPage page2 = document.stringToTemplate("PDFIssue1.srep_1");



JCFrame frame1 = page1.stringToFrame("0");
//JCFrame frame2 = page1.stringToFrame("1");


JCTextStyle style = (JCTextStyle) JCTextStyle.stringToStyle("default text").clone();
style.setFontFamily("TimesRoman");


flow.print(table);

//flow.print(table1);



// Create the pdf and print it

PrintManager.printDocument(document, printer);

System.exit(0);
}
}

Thanks...

[ October 01, 2008: Message edited by: Ashwin Kumar ]

[ October 01, 2008: Message edited by: Ashwin Kumar ]
[ October 02, 2008: Message edited by: Ashwin Kumar ]
 
Rob Spoor
Sheriff
Pie
Posts: 20606
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could start by Using Code Tags. You can use the edit button to change your post.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic