Line numbering in PDFBox - java

at the moment I evaluate some libraries for PDF creations in Java. PDFBox is at the moment one of the libraries I want to use. In my use case I want to give every chapter a new line numbering. I tried and searched a lot around, but I really have no idea how I can archieve this. Does anyone has some suggestions? Thank you very much for everything!

Maybe it will help. Here's how to generate a menu and sub-menu (along with main menu and sub-menu numbering):
Use this class:
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
public class PdfExample
{
private String pdfFileName = "";
public PdfExample(String pdfFileName)
{
this.pdfFileName=pdfFileName;
//creating new document:
PDDocument document = new PDDocument();
PDPage blancPage = new PDPage();
document.addPage(blancPage);
PDFont font_TIMES_ROMAN = PDType1Font.TIMES_ROMAN;
try
{
PDPageContentStream fileStream = new PDPageContentStream(document,blancPage);
fileStream.setFont( font_TIMES_ROMAN, 12 );
fileStream.beginText();
fileStream.newLineAtOffset(50,735);
fileStream.showText("Chapter numbering example:");
fileStream.endText();
//veeery simple positioning configuration:
int mainMenuPosition_Y = 700;
int mainMenuPosition_X = 50;
int lineSpacing = 30;
int numberOfMainMenus = 6;
int subMenuLeftMargin = mainMenuPosition_X+20;
HashMap<Integer, ArrayList> subMenu = new HashMap<Integer,ArrayList>();
//I'm adding some sample submenu's:
subMenu.put(1,new ArrayList<String>(Arrays.asList("Item 1","Item 2","Item 3"))); // firstOne Main Menu
subMenu.put(2,new ArrayList<String>(Arrays.asList("Item 1","Item 2","Item 3"))); // second Main Menu
subMenu.put(5,new ArrayList<String>(Arrays.asList("Item 1","Item 2"))); // 5th
for(int i = 1 ; i <= numberOfMainMenus ; i++)
{
fileStream.beginText();
fileStream.newLineAtOffset(mainMenuPosition_X,mainMenuPosition_Y);
fileStream.showText(i+". Main Menu");
fileStream.endText();
if(subMenu.get(i)!=null)
{
for(int w = 0; w < subMenu.get(i).size();w++)
{
System.out.println(subMenu.get(i).get(w));
fileStream.beginText();
fileStream.newLineAtOffset(subMenuLeftMargin,(mainMenuPosition_Y-=15));
fileStream.showText(i+"."+(w+1)+" "+subMenu.get(i).get(w));
fileStream.endText();
}
}
mainMenuPosition_Y-=lineSpacing;
}
fileStream.close();
document.save(pdfFileName+".pdf");
document.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}//end
}
and then just:
PdfExample document = new PdfExample("SimpleExample");
This code will generate something like this:
SimpleExample
And remember about 'project structure libraries' in your IDE:
Project Structure Libraries
I hope I helped a little bit ;]

Related

Oracle Apex, Apache PDFBox java stored procedure issues

Some background: in my app, there are some pdf reports. But, these pdf reports need to be "image" based and I was told that the report server is unable to do this. The call to the report server is done from a pl/sql procedure and the result is a blob, so now all I have at my disposal to try to do this conversion is a java stored procedure. Here is what I came up with (using Apache PDFBox):
create or replace and compile java source named "APDFUtil"
as
import oracle.sql.*;
import oracle.jdbc.driver.*;
import java.sql.*;
import oracle.sql.BLOB;
import java.sql.Blob;
import javax.imageio.ImageIO;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.*;
import java.util.*;
import javax.imageio.ImageIO;
import org.apache.pdfbox.pdmodel.*;
import org.apache.pdfbox.rendering.*;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.graphics.image.LosslessFactory;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
import org.apache.pdfbox.rendering.PDFRenderer;
import org.apache.pdfbox.tools.imageio.ImageIOUtil;
import java.awt.image.BufferedImage;
public class APDFUtil{
static OracleDriver ora = new OracleDriver();
static Connection conn;
static ByteArrayOutputStream out;
static {
try {
conn = ora.defaultConnection();
} catch (Exception ex) {}
}
public static oracle.sql.BLOB flattenPDF (oracle.sql.BLOB value) throws Exception {
if (conn == null) conn = ora.defaultConnection();
BLOB retBlob = BLOB.createTemporary(conn, true, oracle.sql.BLOB.DURATION_SESSION);
/*BEGIN TO_JPG*/
InputStream inputStream = value.getBinaryStream();
PDDocument document = PDDocument.load(inputStream);
PDFRenderer pdfRenderer = new PDFRenderer(document);
int noOfPages = document.getNumberOfPages();
BufferedImage[] pdfJPEG = new BufferedImage[noOfPages];
for (int page = 0; page < noOfPages; ++page) {
BufferedImage bim = pdfRenderer.renderImageWithDPI(page, 300, ImageType.RGB);
pdfJPEG[page] = bim;
}
/*write images to new pdf*/
PDDocument documentOut = new PDDocument();
for (int page = 0; page < noOfPages;++page) {
/*get page from old document to determine width and height*/
PDPage oldPage = document.getPage(page);
Float pw = oldPage.getMediaBox().getWidth();
Float ph = oldPage.getMediaBox().getHeight();
PDRectangle rec = new PDRectangle(pw,ph);
PDPage newPage = new PDPage(rec);
documentOut.addPage(newPage);
PDImageXObject pdImage = LosslessFactory.createFromImage(documentOut, pdfJPEG[page]);
PDPageContentStream contents = new PDPageContentStream(documentOut, newPage);
contents.drawImage(pdImage, 0, 0,pw,ph);
contents.close();
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
documentOut.save(out);
documentOut.close();
document.close();
/*END OF TO_JPG*/
/*out - we used to get this back from TO_JPG*/
try {
java.io.OutputStream outStr = retBlob.setBinaryStream(0);
outStr.write(out.toByteArray());
outStr.flush();
} finally {
out.close();
}
return retBlob;
}
}
the pdfbox jars have been loaded into the database
database is oracle 19c standard edition 2 release 19.0.0.0.0
I tried this code as a standalone java project with the exception that the pdf file is being read from the disk, and new file written to the disk and there it works flawlessly.
The issue:
I believe the problem starts at this line: BufferedImage bim = pdfRenderer.renderImageWithDPI(page, 300, ImageType.RGB);
but I don't know what is causing the error or how to debug it (I came to this conclusion by a painstaking process of elimination of code and throwing exceptions) especially since it works in a standalone project. Java stored procedures are not a specialty of mine and this code was pieced together from many different sources online.

Importing URLs for JSOUP to Scrape via Spreadsheet

I finally got IntelliJ to work. I'm using the code below. It works perfect. I need it to loop over and over and pull links from a spreadsheet to find the price over and over again on different items. I have a spreadsheet with a few sample URLs located in column C starting at row 2. How can I have JSOUP use the URLs in this spreadsheet then output to column D?
public class Scraper {
public static void main(String[] args) throws Exception {
final Document document = Jsoup.connect("examplesite.com").get();
for (Element row : document.select("#price")) {
final String price = row.select("#price").text();
System.out.println(price);
}
}
Thanks in advance for any help!
Eric
You can use JExcel library to read and edit sheets: https://sourceforge.net/projects/jexcelapi/ .
When you download the zip file with library there's also very useful tutorial.html.
Explanation in comments:
import java.io.File;
import java.io.IOException;
import jxl.Cell;
import jxl.CellType;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
public class StackoverflowQuestion51577491 {
private static final int URL_COLUMN = 2; // Column C
private static final int PRICE_COLUMN = 3; // Column D
public static void main(final String[] args) throws Exception {
// open worksheet with URLs
Workbook originalWorkbook = Workbook.getWorkbook(new File("O:/original.xls"));
// create editable copy
WritableWorkbook workbook = Workbook.createWorkbook(new File("O:/updated.xls"), originalWorkbook);
// close read-only workbook as it's not needed anymore
originalWorkbook.close();
// get first available sheet
WritableSheet sheet = workbook.getSheet(0);
// skip title row 0
int currentRow = 1;
Cell cell;
// iterate each cell from column C until we find an empty one
while (!(cell = sheet.getCell(URL_COLUMN, currentRow)).getType().equals(CellType.EMPTY)) {
// raed cell contents
String url = cell.getContents();
System.out.println("parsing URL: " + url);
// parse and get the price
String price = parseUrlWithJsoupAndGetProductPrice(url);
System.out.println("found price: " + price);
// create new cell with price
Label cellWithPrice = new Label(PRICE_COLUMN, currentRow, price);
sheet.addCell(cellWithPrice);
// go to next row
currentRow++;
}
// save and close file
workbook.write();
workbook.close();
}
private static String parseUrlWithJsoupAndGetProductPrice(String url) throws IOException {
// download page and parse it to Document
Document doc = Jsoup.connect(url).get();
// get the price from html
return doc.select("#priceblock_ourprice").text();
}
}
before:
after:

Program freezes, multithread? Locksupport.park method

I'm trying to extract establishments data from GooglePlaces API. It used to work initially, but after making a specific method for extracting the places (instead of in the main method), the program crushes. When debbuged it, it gets stuck in a java method called park (under LockSupport class from java). Reading about it, it says that this happens when there is more than 1 thread and there are sync problems. I'm very new at this and I don't know how to solve this in my code. In my mind, there is only 1 thread in this code, but I'm pretty sure I'm wrong. Please help. It crashes in a "for" commented below. Thanks so much!
package laundry;
import java.util.ArrayList;
import java.util.List;
import se.walkercrou.places.GooglePlaces;
import se.walkercrou.places.Param;
import se.walkercrou.places.Place;
import java.io.FileWriter; //add to import list
import java.io.IOException;
import java.io.Writer;
import static java.lang.Math.sqrt;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.microedition.location.Coordinates;
import se.walkercrou.places.exception.GooglePlacesException;
import se.walkercrou.places.exception.RequestDeniedException;
public class Laundry {
public static void main(String[] args) throws IOException {
List<Place> detailedPlaces = new ArrayList<>();
List<double[]> circlesPoint = new ArrayList<>();
double radio = 10;
Coordinates startingPoint = new Coordinates (-38.182476,144.552079,0);//geelong south west corner of the grid
Coordinates finalPoint = new Coordinates(-37.574381,145.415879,0); //north east of melbourne
GooglePlaces cliente = new GooglePlaces("keyof googlplaces");
MyResult result1=exploreGrid(startingPoint,finalPoint, radio, detailedPlaces, circlesPoint,cliente);
writeResultsCircles(result1.getPoints(),"c:\\outputCircles.txt" );
writeResultsPlaces(result1.getPlaces(), "c:\\outputPlaces.txt");
}
private static MyResult exploreGrid(Coordinates SWpoint,Coordinates NEpoint,double rad, List<Place> lugares, List<double[]> points,GooglePlaces client){
int iterationRow=0;
Coordinates workingPoint = new Coordinates(SWpoint.getLatitude(),SWpoint.getLongitude(),(float) 0.0);
List<Place> places = new ArrayList<>();
while (workingPoint.getLatitude()<NEpoint.getLatitude()){
while (workingPoint.getLongitude()<NEpoint.getLongitude()){
try {
places = client.getNearbyPlaces(workingPoint.getLatitude(), workingPoint.getLongitude(), rad*1000,GooglePlaces.MAXIMUM_RESULTS ,Param.name("types").value("laundry"));
if (places.size()==60){//si se llega al tope de resultados de getNearbyPlaces
iterationRow=1;
}
for (Place place : places) {
lugares.add(place.getDetails());//here is where it crashes
}
}catch (GooglePlacesException ex) {
System.out.println(ex.getCause());
}
double[] prePoint = {workingPoint.getLatitude(),workingPoint.getLongitude(),rad};
points.add(prePoint);
workingPoint.setLongitude(workingPoint.getLongitude()+rad*sqrt(3)*0.01134787);
}
iterationRow++;
if (isEven(iterationRow)){
workingPoint.setLongitude(SWpoint.getLongitude());
} else {
workingPoint.setLongitude(SWpoint.getLongitude()+rad*sqrt(3)*0.01134787/2);
}
workingPoint.setLatitude(workingPoint.getLatitude()+rad*3/2*0.00899416);
}
return new MyResult(lugares,points);
}
}

Error: Could not find or load main class excel2JSON.excel

package excel2JSON;
import java.io.File;
import java.io.FileInputStream;
import java.util.Iterator;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.json.JSONArray;
import org.json.JSONObject;
public class excel {
public static void main(String[] args)
{
String str = ExcelToJSON(new File ("C:\\workbook.xlsx"));
System.out.println("JSON = " + str);
}
private static String ExcelToJSON(File file_open)
{
JSONObject json = null;
try {
FileInputStream input = new FileInputStream(file_open);
Workbook workbook = WorkbookFactory.create(input);
Sheet sheet = workbook.getSheetAt(0);
json = new JSONObject();
JSONArray rows = new JSONArray();
for (Iterator<Row> rows_it = sheet.rowIterator(); rows_it.hasNext();) {
Row row = rows_it.next();
JSONObject jason_row = new JSONObject();
JSONArray cells = new JSONArray();
for (Iterator<Cell> cells_it = row.cellIterator(); cells_it.hasNext();) {
Cell cell = cells_it.next();
//Check the cell type and format accordingly
switch (cell.getCellType())
{
case Cell.CELL_TYPE_NUMERIC:
double numeric=cell.getNumericCellValue();
cells.put(String.valueOf(numeric));
break;
case Cell.CELL_TYPE_STRING:
cells.put(cell.getStringCellValue());
break;
}
}
jason_row.put("cell", cells);
rows.put(jason_row);
}
// Create the JSON.
json.put("rows", rows);
} catch (Exception e) {
e.printStackTrace();
}
// Get the JSON text.
return json.toString();
}
}
getting this error:
Error: Could not find or load main class excel2JSON.excel
I don't know why i am getting this error. I am new to Java and i need help to fix this issue. I am trying to convert Excel file to JSON document. This is the code i wrote for that but i am getting issue when i run the program in Eclipse.
The most common cause of this error in eclipse is when you have moved the class around, Eclipse will be trying to use an outdated run configuration which will not work.
Click the down arrow next to the green play button in Eclipse, select Run Configurations.
Delete the references to this class, then close that window, select the excel class. Click the down arrow next to the play button again and select run as Java Application.

Graph does not render as expected

This is the entire source code for the java file.
package gephifyer;
import java.awt.Color;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.gephi.data.attributes.api.AttributeColumn;
import org.gephi.data.attributes.api.AttributeController;
import org.gephi.data.attributes.api.AttributeModel;
import org.gephi.graph.api.DirectedGraph;
import org.gephi.graph.api.GraphController;
import org.gephi.graph.api.GraphModel;
import org.gephi.io.exporter.api.ExportController;
import org.gephi.io.importer.api.Container;
import org.gephi.io.importer.api.EdgeDefault;
import org.gephi.io.importer.api.ImportController;
import org.gephi.io.importer.spi.FileImporter;
import org.gephi.io.processor.plugin.DefaultProcessor;
import org.gephi.partition.api.Partition;
import org.gephi.partition.api.PartitionController;
import org.gephi.partition.plugin.NodeColorTransformer;
import org.gephi.preview.api.PreviewController;
import org.gephi.preview.api.PreviewModel;
import org.gephi.preview.api.PreviewProperty;
import org.gephi.preview.types.DependantOriginalColor;
import org.gephi.project.api.ProjectController;
import org.gephi.project.api.Workspace;
import org.gephi.ranking.api.Ranking;
import org.gephi.ranking.api.RankingController;
import org.gephi.ranking.plugin.transformer.AbstractSizeTransformer;
import org.gephi.statistics.plugin.Modularity;
import org.openide.util.Lookup;
import org.gephi.layout.plugin.force.StepDisplacement;
import org.gephi.layout.plugin.force.yifanHu.YifanHu;
import org.gephi.layout.plugin.force.yifanHu.YifanHuLayout;
import org.gephi.layout.plugin.openord.*;
public class Gephifyer {
public void doStuff(String[] args)
{
String filename = new String();
try{
filename = args[0];
} catch (ArrayIndexOutOfBoundsException ex) {
System.out.println("Supply the subreddit name as the argument.");
System.exit(0);
}
ProjectController pc = Lookup.getDefault().lookup(ProjectController.class);
pc.newProject();
Workspace workspace = pc.getCurrentWorkspace();
ImportController importController = Lookup.getDefault().lookup(ImportController.class);
Container container;
try{
File file = new File(filename + ".csv");
//File file = new File(getClass().getResource("askscience.csv").toURI());
container = importController.importFile(file);
container.getLoader().setEdgeDefault(EdgeDefault.DIRECTED);
container.setAllowAutoNode(false); // don't create missing nodes
} catch (Exception ex) {
ex.printStackTrace();
return;
}
// Append imported data to graph api
importController.process(container, new DefaultProcessor(), workspace);
GraphModel graphModel = Lookup.getDefault().lookup(GraphController.class).getModel();
DirectedGraph directedGraph = graphModel.getDirectedGraph();
// Now let's manipulate the graph api, which stores / serves graphs
System.out.println("Nodes: " + directedGraph.getNodeCount() + "\nEdges: " + directedGraph.getEdgeCount());
//Run OpenOrd.
//OpenOrdLayout layout = new OpenOrdLayout(null);
YifanHuLayout layout = new YifanHuLayout(null, new StepDisplacement(0.95f));
layout.setGraphModel(graphModel);
layout.resetPropertiesValues();
layout.initAlgo();
layout.goAlgo();
while (layout.canAlgo()) // This is only possible because OpenOrd has a finite number of iterations.
{
layout.goAlgo();
}
AttributeModel attributemodel = Lookup.getDefault().lookup(AttributeController.class).getModel();
// Get modularity for coloring
Modularity modularity = new Modularity();
modularity.setUseWeight(true);
modularity.setRandom(true);
modularity.setResolution(1.0);
modularity.execute(graphModel, attributemodel);
// Partition with modularity
AttributeColumn modcol = attributemodel.getNodeTable().getColumn(Modularity.MODULARITY_CLASS);
PartitionController partitionController = Lookup.getDefault().lookup(PartitionController.class);
Partition p = partitionController.buildPartition(modcol, directedGraph);
NodeColorTransformer nodeColorTransformer = new NodeColorTransformer();
nodeColorTransformer.randomizeColors(p);
partitionController.transform(p, nodeColorTransformer);
// Ranking
RankingController rankingController = Lookup.getDefault().lookup(RankingController.class);
Ranking degreeRanking = rankingController.getModel().getRanking(Ranking.NODE_ELEMENT, Ranking.INDEGREE_RANKING);
AbstractSizeTransformer sizeTransformer = (AbstractSizeTransformer) rankingController.getModel().getTransformer(Ranking.NODE_ELEMENT, org.gephi.ranking.api.Transformer.RENDERABLE_SIZE);
sizeTransformer.setMinSize(5.0f);
sizeTransformer.setMaxSize(40.0f);
rankingController.transform(degreeRanking,sizeTransformer);
// Finally, the preview model
PreviewController previewController = Lookup.getDefault().lookup(PreviewController.class);
PreviewModel previewModel = previewController.getModel();
previewModel.getProperties().putValue(PreviewProperty.SHOW_NODE_LABELS, Boolean.TRUE);
previewModel.getProperties().putValue(PreviewProperty.NODE_LABEL_COLOR, new DependantOriginalColor(Color.BLACK));
previewModel.getProperties().putValue(PreviewProperty.NODE_LABEL_FONT, previewModel.getProperties().getFontValue(PreviewProperty.NODE_LABEL_FONT).deriveFont(8));
previewModel.getProperties().putValue(PreviewProperty.EDGE_CURVED, Boolean.FALSE);
previewModel.getProperties().putValue(PreviewProperty.EDGE_OPACITY, 50);
previewModel.getProperties().putValue(PreviewProperty.EDGE_RADIUS, 10f);
previewModel.getProperties().putValue(PreviewProperty.BACKGROUND_COLOR, Color.TRANSLUCENT);
previewController.refreshPreview();
System.out.println("starting export");
ExportController ec = Lookup.getDefault().lookup(ExportController.class);
try{
ec.exportFile(new File(filename + ".svg"));
}
catch (IOException ex){
ex.printStackTrace();
return;
}
System.out.println("Done.");
}
public static void main(String[] args)
{
Gephifyer g = new Gephifyer();
g.doStuff(args);
}
}
At its heart, it's the various demos' code cobbled together to do what I want it to do.
I expect a graph that looks like this svg file, but the result is this svg file. That is, the problem is that the above code yields a graph where the arrows aren't fully connected to the nodes, making it look a bit messy. I can't for my life tell where in the code that is happening, though I guess it would be in the preview model part.
previewModel.getProperties().putValue(PreviewProperty.EDGE_RADIUS, 10f); sets the distance of the arrows from the node.

Categories