Text Formatting is changed while Sharing data: Android - java

I want to share diamond detail in table format(Plain text not HTML)
I have made this using this library but its prints the data properly using the system.out but while I sharing it, its format is changed:
Below is my code:
List<String> headersList = Arrays.asList("", "");
List<List<String>> rowsList = Arrays.asList(
Arrays.asList("Stone Id :", details[0]),
Arrays.asList("Lab", details[1]),
Arrays.asList("Shape", details[2]),
Arrays.asList("Carat", details[3]),
Arrays.asList("Clarity-Color", details[4]),
Arrays.asList("Cut-Pol-Sym-Flou", details[5]));
Board board = new Board(75);
Table table = new Table(board, 75, headersList, rowsList);
table.invalidate().setGridMode(Table.GRID_NON).setRowsList(rowsList);
List<Integer> colWidthsList = Arrays.asList(30, 14);
table.setColWidthsList(colWidthsList);
Block tableBlock = table.tableToBlocks();
board.setInitialBlock(tableBlock);
board.build();
String preview1 = board.getPreview();
System.out.print(preview1);
sharingIntent.putExtra(Intent.EXTRA_TEXT,preview1);

The console uses monospaced font which takes exactly same width for all characters. But your view isn't using it, so it looks messed up.
Use a monospaced font.
Or use a tabular format. Perhaps a ListView with each row having two text views side by side having fixed width.

Related

how to get the content of a JTextPane with its styled format

I am trying to make a simple word processor that edits the text to make it bold, italic, underline, background color and foreground color. The problem is I want to set the contents/text of the JTextPane with all its edited attributes to a single object to save it to another class as a data field which have other data fields like date created and the name of the document given by the user.
I think the best approach its using html as content type for the Text Pane and string builders.
for example,
TextPane tp = new JTextPane();
tp.setContentType("text/html");
StringBuilder sb = new StringBuilder();
sb.append("<span style=\"color:red\">" + Hello red + "</span>");
sb.append("<span style=\"color:blue\">" + Hello blue + "</span>");
...
tp.setText(sb); // will print text with the style
works the same in the other way,
String txt = tp.getText();
System.print(txt); //wil show html code
You can reference http://www.java2s.com/Tutorials/Java/Swing_How_to/JTextPane/Style_JTextPane_with_HTML_and_CSS.htm

PDFBOX - header in all pages using easytable

I am using pdfbox and easytable https://github.com/vandeseer/easytable for creating dynamic pages which works great. But I do want header to be added in alL pages. I faced/tried below things.
1) Tablebuilder is created before writing rows so we can create a perfect tablebuilder since rows are dynamic.
2) Tried to insert header in middle while creating tablebuilder which again is not perfect since TableDrawer makes the rows to suffice according to row height
Any idea/help would be appreciated.
Need output similar to this project - https://github.com/eduardohl/Paginated-PDFBox-Table-Sample . only problem here being the content is not dynamic like easytable.
As an addition to #mkl's answer and its comments: In current versions of the library there is a class of its own for this very requirement.
So your code basically boils down to something like:
try (final PDDocument document = new PDDocument()) {
RepeatedHeaderTableDrawer.builder()
.table(createTable())
.startX(50)
.startY(100F)
.endY(50F) // note: if not set, table is drawn over the end of the page
.build()
.draw(() -> document, () -> new PDPage(PDRectangle.A4), 50f);
document.save("your-awesome-document.pdf");
}
This answer had been written at an earlier time when easytable had not yet supported repeating table headers. Meanwhile it does, see the answer by philonous, the easytable author.
easytable does not support repeating table headers or footers. Not yet I should say because this feature actually is easy to implement.
It is difficult, though, to implement on top of easytable because that library (like many others) suffers from excessive data hiding: many interesting member variables and methods are private, so extending the classes is not a viable option.
But what you can do is handle the header rows as a separate table which you draw again and again! The downside is a bit of duplicity of settings.
In case of the test code TwoPagesTableTest you referred to, it can be changed like this:
final Table.TableBuilder tableHeaderBuilder = Table.builder()
.addColumnOfWidth(200)
.addColumnOfWidth(200);
CellText dummyHeaderCell = CellText.builder()
.text("Header dummy")
.backgroundColor(Color.BLUE)
.textColor(Color.WHITE)
.borderWidth(1F)
.build();
tableHeaderBuilder.addRow(
Row.builder()
.add(dummyHeaderCell)
.add(dummyHeaderCell)
.build());
Table tableHeader = tableHeaderBuilder.build();
final Table.TableBuilder tableBuilder = Table.builder()
.addColumnOfWidth(200)
.addColumnOfWidth(200);
CellText dummyCell = CellText.builder()
.text("dummy")
.borderWidth(1F)
.build();
for (int i = 0; i < 50; i++) {
tableBuilder.addRow(
Row.builder()
.add(dummyCell)
.add(dummyCell)
.build());
}
TableDrawer drawer = TableDrawer.builder()
.table(tableBuilder.build())
.startX(50)
.endY(50F) // note: if not set, table is drawn over the end of the page
.build();
final PDDocument document = new PDDocument();
float startY = 100F;
do {
TableDrawer headerDrawer = TableDrawer.builder()
.table(tableHeader)
.startX(50)
.build();
PDPage page = new PDPage(PDRectangle.A4);
document.addPage(page);
try (PDPageContentStream contentStream = new PDPageContentStream(document, page)) {
headerDrawer.startY(startY);
headerDrawer.contentStream(contentStream).draw();
drawer.startY(startY - tableHeader.getHeight());
drawer.contentStream(contentStream).draw();
}
startY = page.getMediaBox().getHeight() - 50;
} while (!drawer.isFinished());
document.save("twoPageTable-repeatingHeader.pdf");
document.close();
(RepeatingTableHeaders test createTwoPageTableRepeatingHeader)
As you see, the code first creates a separate Table tableHeader containing only the header row. This table then is added first on each page and a part of the body rows table is added thereafter.
The result: table headers on each page...
A word of warning: This is a proof of concept, I have only tested with the table generation code from TwoPagesTableTest. For production code you should apply further tests.

Printing out an AttributedString

I am using an attributed string to bold the heading names, and I would like to print out in a Text object in javafx, but I cannot figure out how. I've looked a fair amount of places online, including the java docs but nothing seems to go over this... Here is what I am trying to do:
AttributedString boldName = new AttributedString("Name: ");
boldName.addAttribute(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD);
String name = innerNode.name;
Text info = new Text(**boldName.something()?** + name);
The result should be this: Name: name, something quite simple I feel but how??
You cannot use the AttributedString in JavaFX. Instead use,
On JavaFX 2.2 and earlier:
Text name = new Text("Name: ");
name.setFill(Color.BLUE);
name.setFont(Font.font("Helvetica", FontWeight.BOLD, 12));
Text info = new Text(innerNode.name);
HBox hbox = new HBox(1);
hbox.getChildren().addAll(name, info);
On JavaFX 8:
Text name = new Text("Name: ");
name.setFill(Color.BLUE);
name.setFont(Font.font("Helvetica", FontWeight.BOLD, 12));
Text info = new Text(innerNode.name);
TextFlow textFlow = new TextFlow(name, info);
Reference:
Using Text and Text Effects in JavaFX
javafx.scene.text.TextFlow

JTextPane multi-line to single-line conversion with HTML

I have a piece in my code that formats a string, appends html/css tags and then adds the text to a JTextPane. I create the textPane in some panel's constructor with the following:
public PnlSmartCommands(ServerLogFormatter formatter, ServerCommandsComponent container){
setLayout( new java.awt.BorderLayout() );
this.container = container;
txtServerCommands = new JTextPane();
txtServerCommands.setContentType("text/html");
scpServerCommands = new JScrollPane( );
this.formatter = formatter;
scpServerCommands.setViewportView( txtServerCommands );
scpServerCommands.getVerticalScrollBar().setUnitIncrement(16);
scpServerCommands.getHorizontalScrollBar().setUnitIncrement(50);
add( scpServerCommands, java.awt.BorderLayout.CENTER );
txtServerCommands.setEditable(false);
loadRules(txtServerCommands);
and I add text to the pane with a formatting function that takes all previous requests from an ArrayList, deletes all found HTML tags, formats it and then adds new HTML and BODY tags, and then use .setText(String arg0) to set the text to a JTextPane.
public String formatMemoryString(){
StringBuilder sb = new StringBuilder();
sb.append("<html>");
sb.append("<body>");
for(int i=0;i<logMemoryHolder.size(); i++){
sb.append(logMemoryHolder.get(i));
if(!(i==logMemoryHolder.size())){
sb.append("<br>");
}
}
sb.append("</body>");
sb.append("</html>");
return sb.toString();
Now here's the problem - the response is always fit into the box over multiple lines, not on a single line. While this is actually good, I need to add functionality to span it over a single line. ! http://i.stack.imgur.com/zy4yC.jpg - this is what it looks like currently. I would like to add a checkbox that formats the value as a single line, or allows the textPane to do so. Any idea how I go about doing that? The HTML that I put into the pane is as follows : http://www.upload.ee/files/3501071/testHtml.html.html
Thanks in advance!

Inserting bar charts using Aspose Words for Java

Is there a way of creating and inserting a bar chart in a Ms Word document using Aspose Words for Java? I can't find a way to do this. Thanks.
Aspose.Words for Java currently doesn't allow you to create the bar chart in Word documents. However, if you just want to add a static bar chart, you may try Aspose.Cells for Java to create bar chart and render it to image. After that, you can add this bar chart image in Word document using Aspose.Words for Java. Do you think this might help in your scenario? If it does then you can use the following code snippet to create and render the bar chart to image:
//Create a new Workbook.
Workbook workbook = new Workbook();
//Get the first worksheet.
Worksheet sheet = workbook.getWorksheets().get(0);
//Set the name of worksheet
sheet.setName("Data");
//Get the cells collection in the sheet.
Cells cells = workbook.getWorksheets().get(0).getCells();
//Put some values into a cells of the Data sheet.
cells.get("A1").setValue("Region");
cells.get("A2").setValue("France");
cells.get("A3").setValue("Germany");
cells.get("A4").setValue("England");
cells.get("A5").setValue("Sweden");
cells.get("A6").setValue("Italy");
cells.get("A7").setValue("Spain");
cells.get("A8").setValue("Portugal");
cells.get("B1").setValue("Sale");
cells.get("B2").setValue(70000);
cells.get("B3").setValue(55000);
cells.get("B4").setValue(30000);
cells.get("B5").setValue(40000);
cells.get("B6").setValue(35000);
cells.get("B7").setValue(32000);
cells.get("B8").setValue(10000);
//Create chart
int chartIndex = sheet.getCharts().add(ChartType.COLUMN, 12, 1, 33,
12);
Chart chart = sheet.getCharts().get(chartIndex);
//Set properties of chart title
chart.getTitle().setText("Sales By Region");
chart.getTitle().getTextFont().setBold(true);
chart.getTitle().getTextFont().setSize(12);
//Set properties of nseries
chart.getNSeries().add("Data!B2:B8", true);
chart.getNSeries().setCategoryData("Data!A2:A8");
//Set the fill colors for the series's data points (France -
Portugal(7 points))
ChartPointCollection chartPoints =
chart.getNSeries().get(0).getPoints();
ChartPoint point = chartPoints.get(0);
point.getArea().setForegroundColor(Color.getCyan());
point = chartPoints.get(1);
point.getArea().setForegroundColor(Color.getBlue());
point = chartPoints.get(2);
point.getArea().setForegroundColor(Color.getYellow());
point = chartPoints.get(3);
point.getArea().setForegroundColor(Color.getRed());
point = chartPoints.get(4);
point.getArea().setForegroundColor(Color.getBlack());
point = chartPoints.get(5);
point.getArea().setForegroundColor(Color.getGreen());
point = chartPoints.get(6);
point.getArea().setForegroundColor(Color.getMaroon());
//Set the legend invisible
chart.setShowLegend(false);
//Get the Chart mage
ImageOrPrintOptions imgOpts = new ImageOrPrintOptions();
imgOpts.setImageFormat(ImageFormat.getPng());
//Save the chart image file.
chart.toImage(new FileOutputStream("D:\Files\MyChartImage.png"),
imgOpts);
Disclosure: I work as developer evangelist at Aspose.

Categories

Resources