I'm new to itext7
Recently, I'm generating custom report pdf by iText7.
There is one layout I want to approach.
Make the vertically text align in the middle of the cell.
To illustrate my purpose, the first screenshot demonstrate my progress.
The second screenshot is what I want.
Here is how I implement my approach.
//Init table
Table table = new Table(UnitValue.createPercentArray(new float[]{20, 20, 20, 20, 20})).setWidth(UnitValue.createPercentValue(100));
//Init cellHeader
Paragraph servicePara = new Paragraph("Date of Service");
servicePara.setBorder(new SolidBorder(new DeviceRgb(5, 10, 50), 1));
servicePara.setBackgroundColor(DeviceCmyk.YELLOW);
servicePara.setRotationAngle(Math.PI / 2);
Cell cell = new Cell();
cell.add(servicePara);
table.addHeaderCell(cell);
Related
I am trying to set border, margin, and padding to a PDF document, is it possible to achieve using itext7
Margin works fine by setting below code
document.setLeftMargin(180);
But the border is not working, below code used to set the border
float width = 1.5f;
Color color = ColorConstants.BLUE;
Border border = new DottedBorder(color,width);
Document document = new Document(pdfDocument);
document.setBorder(border);
Unfortunately it's not possible to specify the document's background and borders just by setting some of the Document's properties. The good news is that iText7 provides us with an opportunity to override DocumentRenderer (renderers are classes responsible to render corresponding model objects, for example, ParagraphRenderer renders Paragraph and so on).
In the custom DocumentRenderer below updateCurrentArea was overridden to tackle the following two issues:
shrinking the area which will be used by DocumentRenderer to layout the Document's children
adding a background Div, whic will be resposible for border rendering (I've also shown how one could set background, if needed)
class CustomDocumentRenderer extends DocumentRenderer {
public CustomDocumentRenderer(Document document) {
super(document);
}
#Override
protected LayoutArea updateCurrentArea(LayoutResult overflowResult) {
LayoutArea area = super.updateCurrentArea(overflowResult); // margins are applied on this level
Rectangle newBBox = area.getBBox().clone();
// apply border
float[] borderWidths = {10, 10, 10, 10};
newBBox.applyMargins(borderWidths[0], borderWidths[1], borderWidths[2], borderWidths[3], false);
// this div will be added as a background
Div div = new Div()
.setWidth(newBBox.getWidth())
.setHeight(newBBox.getHeight())
.setBorder(new SolidBorder(10))
.setBackgroundColor(ColorConstants.GREEN);
addChild(new DivRenderer(div));
// apply padding
float[] paddingWidths = {20, 20, 20, 20};
newBBox.applyMargins(paddingWidths[0], paddingWidths[1], paddingWidths[2], paddingWidths[3], false);
return (currentArea = new RootLayoutArea(area.getPageNumber(), newBBox));
}
}
The last quetions is how to apply it on your document. It could be done as follows:
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(outFileName));
Document doc = new Document(pdfDoc);
doc.setRenderer(new CustomDocumentRenderer(doc));
The resultant PDF:
My problem is when i run the program the data displayed at the JTable but not all records are displayed although i have a scroll Pane and aJtable inside it,when i run it provides a scroll but for the firs few records ,the jtable have a textfield to provide searching in table using rowSorter ,although jtable doesn't show all records when i search for a record that is not viewed i gets it.this is an image describing the problem.please how can i fix that.
this is the link i have no permission to attache an image here
http://www.megafileupload.com/en/file/561971/Untitled-jpg.html
this is piece of code where i initialize these component
table2 = new JTable(tableModel);
table2.setFont(new Font("Tahoma", Font.PLAIN, 16));
table2.getTableHeader().setFont(new Font("Tahoma", Font.BOLD, 14));
scrollPane_1.setColumnHeaderView(table2.getTableHeader());
table2.setBackground(new Color(210, 180, 140));
table2.setBorder(new LineBorder(new Color(105, 105, 105)));
table2.setForeground(new Color(0, 0, 0));
sorter2 = new TableRowSorter<TableModel>(table2.getModel());
table2.setRowSorter(sorter2);
JViewport vp2 = new JViewport();
vp2.setBackground(new Color(210, 105, 30));
vp2.setForeground(new Color(0, 0, 0));
scrollPane_1.setViewportView(vp2);
vp2.add(table2);
this is the method that fill the table
table2Data = new Vector<>();
Iterator<SellBean> buyIt = boughtedList.iterator();
while (buyIt.hasNext()) {
SellBean buyBean = buyIt.next();
Vector rec = new Vector<>();
rec.add(buyBean.getPRODUCT_ID());
rec.add(buyBean.getPRODUCT_NAME());
// System.out.println(buyBean.getPRODUCT_NAME());
rec.add(buyBean.getQUANTITY());
rec.add(buyBean.getBUY_PRICE());
rec.add(buyBean.getSELL_PRICE());
rec.add(buyBean.getPROFIT());
rec.add(buyBean.getBUY_DATE());
rec.add(buyBean.getRETURNED());
table2Data.add(rec);
}
tm2.setDataVector(table2Data, table2ColNames);
for (int i = 0; i < table2.getColumnCount(); i++) {
DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
centerRenderer.setHorizontalAlignment(JLabel.CENTER);
table2.getColumnModel().getColumn(i)
.setCellRenderer(centerRenderer);
}
for (int i = 0; i < table2.getRowCount(); i++) {
table2.setRowHeight(i, 28);
}
table2.getColumnModel().getColumn(0).setPreferredWidth(25);
}
i hope it is clear now
Maybe you have problems with the scrollpane (like: no scrollbars visible).
Are your sure you are using it correct?
this looks strange to me:
You create a ViewPort and put this as View?!
JViewport vp2 = new JViewport();
scrollPane_1.setViewportView(vp2);
vp2.add(table2);
why not just set the Table as ViewportView (as the name implies?)
just:
scrollPane_1.setViewportView(table2);
Edit: Info about the ViewPort:
The "viewport" or "porthole" through which you see the underlying information. When you scroll, what moves is the viewport. It is like peering through a camera's viewfinder. Moving the viewfinder upwards brings new things into view at the top of the picture and loses things that were at the bottom.
http://docs.oracle.com/javase/7/docs/api/javax/swing/JViewport.html
i am using the following code to set water mark in the excel sheet, i also get the water mark but instead of showing the watermark at the back round it shows the water mark at foreground.
how to resolve this, the code :
//to add water mark
HSSFPatriarch dp = sheet.createDrawingPatriarch();
HSSFClientAnchor anchor = new HSSFClientAnchor
(0, 0, 1023, 255, (short) 2, 4, (short) 13, 26);
HSSFTextbox txtbox = dp.createTextbox(anchor);
HSSFRichTextString rtxt = new HSSFRichTextString("POC");
HSSFFont draftFont = hwb.createFont();
draftFont.setColor((short) 27);
draftFont.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
draftFont.setFontHeightInPoints((short) 192);
draftFont.setFontName("Verdana");
rtxt.applyFont(draftFont);
txtbox.setString(rtxt);
txtbox.setLineStyle(HSSFShape.LINESTYLE_NONE);
txtbox.setNoFill(true);
also how to merge the cells to add title at the top of the excel sheet using poi in java?
Please help me to find and resolve the isssue.
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.
I am writing the following code in order to create a PDF file with a table in it.
Document document = new Document(PageSize.A4, 20, 20, 20, 80);
Font myfont = new Font(FontFactory.getFont(FontFactory.COURIER_BOLD, 13, Font.NORMAL));
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(request.getRealPath("/") + "SAMPLE.pdf"));
document.open();
Table table = new Table(2);
Cell c2 = new Cell();
int[] widths = {8, 150}; //Tried different values, but no change
table.setBorder(Rectangle.BOX);
table.setAlignment(Element.ALIGN_LEFT);
table.setSpacing(0);
table.setPadding(0);
table.setTableFitsPage(true); //Tried with 'false', even removed it, but no change
table.setWidths(widths);
c2 = new Cell(new Paragraph("1. ", myfont));
c2.setBorder(Rectangle.NO_BORDER);
table.addCell(c2);
c2 = new Cell(new Paragraph("TEST DATA ", myfont));
c2.setBorder(Rectangle.NO_BORDER);
table.addCell(c2);
c2 = new Cell(new Paragraph("2. ", myfont));
c2.setBorder(Rectangle.NO_BORDER);
table.addCell(c2);
c2 = new Cell(new Paragraph("TEST DATA", myfont));
c2.setBorder(Rectangle.NO_BORDER);
table.addCell(c2);
c2 = new Cell(new Paragraph("3. ", myfont));
c2.setBorder(Rectangle.NO_BORDER);
table.addCell(c2);
c2 = new Cell(new Paragraph("TEST DATA", myfont));
c2.setBorder(Rectangle.NO_BORDER);
table.addCell(c2);
document.add(table);
document.close();
But the file created contains a table that occupies around 80-85% of the page. I want it to utilise the entire page.
I tried making some adjustments to the code like changing the table.setTableFitsPage(true); to table.setTableFitsPage(false); and even tried removing it.
also altered around with the widths assigned. But in vain as in all cases it only gave me the file with a table occupying only 80-85% of page.
Is there something I am missing to add to my code or is there an attribute that is stopping the Table from taking up 100% of page.
It creates problem when the content is large as I end up getting long tables with spaces on the page still unoccupied.
Attached a screen-shot of the actual PDF file generated here!
Screen-shot of the PDF generated
You should rewrite your table code to use PdfPTable instead. You can find some examples of its use online. The entire 4th chapter of iText in Action 2nd ed is about tables, PdfPTables to be precise.
Lots of example code. Enjoy.
Use the PdfPTable class instead, and you can set the column widths on the entire table. Making it span the entire page can be done by constructing a
float delta = document.getPageSize().getWidth() / numberOfColumns;
// add delta numberOfColumn times
PdfPTable table = new PdfPTable(new float[] {delta, delta, delta, delta });
table.setWidthPercentage(100f);
The widths passed in the constructor determine how much space a given column gets relative to the width of the entire table.