Skip to content

Commit 4bbaff1

Browse files
authored
Pdf Export - enhance output (#167)
* make functions private ; remove redundant initializers * remove Chartinfo coding - it was always null * make pdfheight class variable again - used in other functions too * remove non-used variables/assignments * Copyright / Imports * make global myparser --> OSParser instead of AllParser to use getHostName etc .. there is no other ParserType than OSParser * PDF document properties * IndexPage will show Hostname, OsType, Date, Timeline, * On Each Page - reposition page number to avoid overlap in footer - show hostname and date in header * checkStype import
1 parent 798e224 commit 4bbaff1

File tree

2 files changed

+55
-32
lines changed

2 files changed

+55
-32
lines changed

src/main/java/net/atomique/ksar/export/FilePDF.java

+53-30
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
/*
2-
* Copyright 2017 The kSAR Project. All rights reserved.
2+
* Copyright 2018 The kSAR Project. All rights reserved.
33
* See the LICENSE file in the project root for more information.
44
*/
55

66
package net.atomique.ksar.export;
77

8+
import static com.itextpdf.text.FontFactory.COURIER;
9+
import static com.itextpdf.text.FontFactory.getFont;
10+
811
import com.itextpdf.awt.PdfGraphics2D;
912
import com.itextpdf.text.BaseColor;
1013
import com.itextpdf.text.Document;
1114
import com.itextpdf.text.DocumentException;
1215
import com.itextpdf.text.ExceptionConverter;
13-
import com.itextpdf.text.FontFactory;
1416
import com.itextpdf.text.PageSize;
1517
import com.itextpdf.text.pdf.BaseFont;
1618
import com.itextpdf.text.pdf.PdfContentByte;
@@ -19,6 +21,7 @@
1921
import com.itextpdf.text.pdf.PdfPageEventHelper;
2022
import com.itextpdf.text.pdf.PdfTemplate;
2123
import com.itextpdf.text.pdf.PdfWriter;
24+
2225
import net.atomique.ksar.Config;
2326
import net.atomique.ksar.VersionNumber;
2427
import net.atomique.ksar.graph.Graph;
@@ -27,14 +30,16 @@
2730
import net.atomique.ksar.ui.ParentNodeInfo;
2831
import net.atomique.ksar.ui.SortedTreeNode;
2932
import net.atomique.ksar.ui.TreeNodeInfo;
30-
import org.jfree.chart.ChartRenderingInfo;
33+
3134
import org.jfree.chart.JFreeChart;
35+
3236
import org.slf4j.Logger;
3337
import org.slf4j.LoggerFactory;
3438

3539
import java.awt.Graphics2D;
3640
import java.awt.geom.Rectangle2D;
3741
import java.awt.geom.Rectangle2D.Double;
42+
3843
import java.io.FileNotFoundException;
3944
import java.io.FileOutputStream;
4045
import java.io.IOException;
@@ -46,7 +51,7 @@ public class FilePDF extends PdfPageEventHelper implements Runnable {
4651

4752
private static final Logger log = LoggerFactory.getLogger(FilePDF.class);
4853

49-
public FilePDF(String filename, kSar hissar) {
54+
private FilePDF(String filename, kSar hissar) {
5055
pdffilename = filename;
5156
mysar = hissar;
5257
}
@@ -80,7 +85,7 @@ public void run() {
8085
break;
8186
}
8287

83-
float pdfheight = document.getPageSize().getHeight();
88+
pdfheight = document.getPageSize().getHeight();
8489
pdfwidth = document.getPageSize().getWidth();
8590
pageheight = pdfheight - (2 * pdfmargins);
8691
pagewidth = pdfwidth - (2 * pdfmargins);
@@ -95,8 +100,8 @@ public void run() {
95100

96101
// document parameter before open
97102
document.addTitle("kSar Grapher");
98-
//document.addSubject("Sar output of " + mysar.hostName);
99-
//document.addKeywords("http://ksar.atomique.net/ ");
103+
//document.addSubject("SAR Statistics of " + mysar.hostName);
104+
//document.addKeywords("https://github.com/vlsi/ksar");
100105
//document.addKeywords(mysar.hostName);
101106
//document.addKeywords(mysar.myOS.sarStartDate);
102107
//document.addKeywords(mysar.myOS.sarEndDate);
@@ -108,7 +113,7 @@ public void run() {
108113
pdfcb = writer.getDirectContent();
109114
PdfOutline root = pdfcb.getRootOutline();
110115

111-
IndexPage(writer, document);
116+
IndexPage(document);
112117

113118
export_treenode(mysar.graphtree, root);
114119

@@ -121,8 +126,7 @@ public void run() {
121126

122127
}
123128

124-
125-
public void export_treenode(SortedTreeNode node, PdfOutline root) {
129+
private void export_treenode(SortedTreeNode node, PdfOutline root) {
126130
int num = node.getChildCount();
127131
if (num > 0) {
128132
Object obj1 = node.getUserObject();
@@ -143,7 +147,7 @@ public void export_treenode(SortedTreeNode node, PdfOutline root) {
143147
TreeNodeInfo tmpnode = (TreeNodeInfo) obj1;
144148
Graph nodeobj = tmpnode.getNode_object();
145149
if (nodeobj.isPrintSelected()) {
146-
root = new PdfOutline(root, new PdfDestination(PdfDestination.FIT), nodeobj.getTitle());
150+
new PdfOutline(root, new PdfDestination(PdfDestination.FIT), nodeobj.getTitle());
147151
update_ui();
148152
addchart(writer, nodeobj);
149153
document.newPage();
@@ -163,49 +167,68 @@ private void update_ui() {
163167

164168
public void onEndPage(PdfWriter writer, Document document) {
165169
try {
166-
String text = "Page " + writer.getPageNumber() + "/" + total_pages;
170+
171+
int pageNumber = writer.getPageNumber();
172+
String text = "Page " + pageNumber + "/" + total_pages;
173+
String hostName = mysar.myparser.gethostName();
174+
String date = mysar.myparser.getDate();
167175

168176
pdfcb.beginText();
169177
pdfcb.setFontAndSize(bf, 10);
170178
pdfcb.setColorFill(new BaseColor(0x00, 0x00, 0x00));
171-
pdfcb.showTextAligned(PdfContentByte.ALIGN_RIGHT, text, ((pdfwidth - pdfmargins) - 10),
172-
10 + pdfmargins, 0);
179+
180+
if ( pageNumber > 1) {
181+
pdfcb.showTextAligned(PdfContentByte.ALIGN_LEFT, hostName, pdfmargins, pdfheight - pdfmargins, 0);
182+
pdfcb.showTextAligned(PdfContentByte.ALIGN_RIGHT, date, pdfwidth - pdfmargins, pdfheight - pdfmargins, 0);
183+
}
184+
185+
pdfcb.showTextAligned(PdfContentByte.ALIGN_RIGHT, text,pdfwidth - pdfmargins,pdfmargins - 5 ,0);
173186
pdfcb.endText();
174187
} catch (Exception e) {
175188
throw new ExceptionConverter(e);
176189
}
177190
}
178191

179-
public int addchart(PdfWriter writer, Graph graph) {
192+
private void addchart(PdfWriter writer, Graph graph) {
180193
JFreeChart chart =
181194
graph.getgraph(mysar.myparser.getStartOfGraph(), mysar.myparser.getEndOfGraph());
182195
PdfTemplate pdftpl = pdfcb.createTemplate(pagewidth, pageheight);
183196
Graphics2D g2d = new PdfGraphics2D(pdftpl, pagewidth, pageheight);
184197
Double r2d = new Rectangle2D.Double(0, 0, pagewidth, pageheight);
185-
chart.draw(g2d, r2d, chartinfo);
198+
chart.draw(g2d, r2d);
186199
g2d.dispose();
187200
pdfcb.addTemplate(pdftpl, pdfmargins, pdfmargins);
188201
try {
189202
writer.releaseTemplate(pdftpl);
190203
} catch (IOException ioe) {
191204
log.error("Unable to write to : {}", pdffilename);
192205
}
193-
return 0;
194206
}
195207

196-
public void IndexPage(PdfWriter writer, Document document) {
208+
private void IndexPage(Document document) {
197209
try {
210+
float pdfCenter = ((pdfwidth - pdfmargins) / 2 );
211+
212+
String title = "SAR Statistics";
213+
String t_date = "on " + mysar.myparser.getDate();
214+
String hostName = "for " + mysar.myparser.gethostName();
215+
String osType = mysar.myparser.getOstype();
216+
String graphStart = mysar.myparser.getStartOfGraph().toString();
217+
String graphEnd = mysar.myparser.getEndOfGraph().toString();
198218

199-
String title = "Statistics";
200-
String t_date = "On " + mysar.myparser.getDate();
201219
pdfcb.beginText();
202-
pdfcb.setFontAndSize(bf, 48);
220+
pdfcb.setFontAndSize(bf, 40);
203221
pdfcb.setColorFill(new BaseColor(0x00, 0x00, 0x00));
204-
pdfcb.showTextAligned(PdfContentByte.ALIGN_CENTER, title, ((pdfwidth - pdfmargins) / 2), 500,
205-
0);
206-
pdfcb.setFontAndSize(bf, 36);
207-
pdfcb.showTextAligned(PdfContentByte.ALIGN_CENTER, t_date, ((pdfwidth - pdfmargins) / 2), 300,
208-
0);
222+
pdfcb.showTextAligned(PdfContentByte.ALIGN_CENTER, title, pdfCenter, 500, 0);
223+
224+
pdfcb.setFontAndSize(bf, 32);
225+
pdfcb.showTextAligned(PdfContentByte.ALIGN_CENTER, hostName + " (" + osType + ")", pdfCenter, 400,0);
226+
pdfcb.showTextAligned(PdfContentByte.ALIGN_CENTER, t_date, pdfCenter, 300,0);
227+
228+
pdfcb.setFontAndSize(bf, 20);
229+
pdfcb.showTextAligned(PdfContentByte.ALIGN_CENTER, graphStart, pdfCenter, 200,0);
230+
pdfcb.showTextAligned(PdfContentByte.ALIGN_CENTER, graphEnd, pdfCenter, 150,0);
231+
209232
pdfcb.endText();
210233
document.newPage();
211234

@@ -216,17 +239,17 @@ public void IndexPage(PdfWriter writer, Document document) {
216239

217240
private int progress_info = 0;
218241
private float pdfwidth;
242+
private float pdfheight;
219243
private int pdfmargins = 10;
220244
private float pageheight;
221245
private float pagewidth;
222246
private int total_pages = 1; // page 1 (index)
223-
private String pdffilename = null;
247+
private String pdffilename;
224248
private Document document = null;
225249
private PdfWriter writer = null;
226250
private PdfContentByte pdfcb;
227-
private kSar mysar = null;
228-
private BaseFont bf = FontFactory.getFont(FontFactory.COURIER).getCalculatedBaseFont(false);
251+
private kSar mysar;
252+
private BaseFont bf = getFont(COURIER).getCalculatedBaseFont(false);
229253
private JProgressBar progress_bar = null;
230254
private JDialog dialog = null;
231-
private ChartRenderingInfo chartinfo = null;
232255
}

src/main/java/net/atomique/ksar/kSar.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public int parse(BufferedReader br) {
120120
Class classtmp = GlobalOptions.getParser(ParserType);
121121
if (classtmp != null) {
122122
if (myparser == null) {
123-
myparser = (AllParser) classtmp.newInstance();
123+
myparser = (OSParser) classtmp.newInstance();
124124
myparser.init(this, current_line);
125125

126126
continue;
@@ -241,7 +241,7 @@ public boolean isParsing() {
241241
private String reload_action = "Empty";
242242
private Thread launched_action = null;
243243
private boolean action_interrupted = false;
244-
public AllParser myparser = null;
244+
public OSParser myparser = null;
245245
private boolean Parsing = false;
246246
public int total_graph = 0;
247247
public SortedTreeNode graphtree = new SortedTreeNode("kSar");

0 commit comments

Comments
 (0)