Java Question B4J - SLC (making PDFjet lib) problems

Harris

Expert
Licensed User
I am trying to create (wrap) a new PDFjet lib after discovering this partial wrap by: @Enrique Gonzalez R

It uses JavaObject in new classes to access methods of the PDFjet.jar, since the XML for the IDE was not created.
This amounts to re-writing every class and method to use the jar! For example, here is the jetTable class:
B4X:
Sub Class_Globals
    Private fx As JFX
    Dim tableObj As JavaObject
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
    tableObj.InitializeNewInstance("com.pdfjet.Table",Null)
End Sub

public Sub setData(listOfData As List)
    tableObj.RunMethod("setData",Array(listOfData))
End Sub

public Sub wrapAroundCellText
    tableObj.RunMethod("wrapAroundCellText",Null)
End Sub

public Sub setNoCellBorders
    tableObj.RunMethod("setNoCellBorders", Null)
End Sub

public Sub setColumnWidth(index As Int, width As Float)
    tableObj.RunMethod("setColumnWidth", Array(index, width))
End Sub

public Sub autoAdjustColumnWidths
    tableObj.RunMethod("autoAdjustColumnWidths", Null)
End Sub

public Sub setPosition(x As Double,y As Double)
    tableObj.RunMethod("setPosition",Array(x,y))
End Sub

public Sub getNumberOfPages(page As JetPage)
    tableObj.RunMethod("getNumberOfPages",Array(page.Obj))
End Sub

public Sub hasMoreData As Boolean
    Return tableObj.RunMethod("hasMoreData",Null)
End Sub

public Sub drawOn(page As JetPage)
    tableObj.RunMethod("drawOn",Array(page.Obj))
End Sub


I would like to build the XML and have included in each source java file (@ShortName), since SLC needs this reference in order to build the XML.
B4X:
package com.pdfjet;

import java.util.*;
import anywheresoftware.b4a.BA.ShortName;
 @ShortName("Table")
/**
 *  Used to create table objects and draw them on a page.
 *
 *  Please see Example_08.
 */
 public class Table ....

After SLC compiles the jar and generates the XML, I get "Error Parsing Libraries - com.pdfjet.Point.x already exists" in the Toastmessage when selecting PDFjet from the libraries tab.
This same issue was reported by @DonManfred in this post:

But, was never really resolved as to how to correct "duplicate <name>Text<\name>" (in XML) issue.... - other than to exclude java source not needed in the XML (which ones?).
I went down a rabbit hole (for hours), trying to rename duplicate names in the XML, but the list of duplicates is endless. For example <name>x</name> is used 1400 times for various parameters of different classes and methods.

Can SLC create a wrapped B4X usable jar for the IDE, or MUST I resort to the JavaObject to the method and essentially re-write all classes and methods?

Thanks

Harris
 

DonManfred

Expert
Licensed User
After SLC compiles the jar and generates the XML, I get "Error Parsing Libraries - com.pdfjet.Point.x already exists" in the Toastmessage when selecting PDFjet from the libraries tab.
Add

com.pdfjet

to the b4aignore line in SLC

Your classes (the ones using the annotations) should not have com.pdfjet as parentclassname
 
Last edited:

Harris

Expert
Licensed User
Well, let me try that - after I feed my starving belly and report back!
Thanks
 

Harris

Expert
Licensed User
The result when adding com.pdfjet to the b4aignore....
The xml is essentially empty.

I don't get error message when loading - obviously..., but I cannot create a "Dim pdf As PDFjet" - even thou it is selected in the libs tab...


B4X:
<?xml version="1.0" encoding="UTF-8"?>
<root>
    <doclet-version-NOT-library-version>1.07</doclet-version-NOT-library-version>
</root>
 

Harris

Expert
Licensed User
Results of SLC compile....
B4X:
Starting step: Compiling Java code.
Completed successfully.
Starting step: Creating jar file.
Completed successfully.
Starting step: Creating XML file.
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\A3.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\A4.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\A5.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\Align.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\Annotation.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\B5.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\BitBuffer.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\BMPImage.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\Bookmark.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\Border.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\Box.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\CalendarMonth.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\Cap.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\Cell.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\CheckBox.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\Chunk.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\ClockWise.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\CodePage.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\Color.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\Compliance.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\CompositeTextLine.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\Compressor.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\CoreFont.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\Courier.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\Courier_Bold.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\Courier_BoldOblique.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\Courier_Oblique.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\Decompressor.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\Destination.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\Dimension.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\Drawable.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\Effect.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\Embed.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\EmbeddedFile.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\ErrorCorrectLevel.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\Executive.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\FastFont.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\FastFont2.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\Field.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\FileAttachment.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\Font.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\FontTable.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\Form.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\Glyph.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\GraphicsState.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\Helvetica.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\Helvetica_Bold.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\Helvetica_BoldOblique.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\Helvetica_Oblique.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\ICCBlackScaled.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\Image.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\ImageType.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\Join.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\JPGImage.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\Legal.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\Letter.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\Line.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\LookupTable.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\LZWEncode.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\Mark.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\MaskPattern.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\Operation.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\OptionalContentGroup.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\Page.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\PageLayout.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\PageMode.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\Paragraph.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\Path.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\PDF.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\PDFobj.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\PlainText.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\PNGImage.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\Point.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\Polynomial.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\QRCode.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\QRMath.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\QRUtil.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\RadioButton.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\Round.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\RSBlock.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\Salsa20.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\Segment.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\Single.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\Standard.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\StandardFont.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\State.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\StructElem.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\Symbol.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\Table.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\Tabloid.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\Text.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\TextBlock.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\TextBox.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\TextFrame.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\TextLine.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\Times_Bold.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\Times_BoldItalic.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\Times_Italic.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\Times_Roman.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\Title.java...
Loading source file C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler\com\src\b4x\PDFjet\ZapfDingbats.java...
Constructing Javadoc information...
[-doclet, BADoclet]
[-docletpath, C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler]
[-doclet, BADoclet]
[-docletpath, C:\Users\mhw\Documents\A_msgbox\SimpleLibraryCompiler]
[-classpath, C:\Anywhere Software\B4J\B4J.exe\../libraries\jFX.jar;C:\Anywhere Software\B4J\B4J.exe\../libraries\jCore.jar;C:\Program Files\Java\jdk1.8.0_77\bin\..\jre\lib\jfxrt.jar;]
[-sourcepath, src]
[-b4atarget, C:\Users\mhw\Documents\AdditionalLibs\B4J\PDFjet.xml]
[-b4aignore, com.pdfjet]
Ignoring: [com.pdfjet]
starting....
finish: C:\Users\mhw\Documents\AdditionalLibs\B4J\PDFjet.xml

Completed successfully.
*** Don't forget to refresh the libraries list in the IDE (right click and choose Refresh) ***
 

DonManfred

Expert
Licensed User
My wrappers are always using de.donmanfred as a parent class.

As an example one of my personal pdfjet wrap classes

B4X:
package de.donmanfred;

import anywheresoftware.b4a.AbsObjectWrapper;
import anywheresoftware.b4a.BA.ShortName;
import com.pdfjet.Box;
import com.pdfjet.Page;

@ShortName("PDFjetBox")
public class BoxWrapper extends AbsObjectWrapper<Box> {
    public BoxWrapper(Box pBox) {
        setObject(pBox);
    }

    public void DrawOn(Page PDFjetPage) throws Exception {
        ((Box) getObject()).drawOn(PDFjetPage);
    }

    // public void Initialize() {
    // setObject(new Box());
    // }

    public void Initialize() {
        setObject(new Box());
    }

    public void Initialize2(float X, float Y, float Width, float Height) {
        setObject(new Box(X, Y, Width, Height));
    }

    public void PlaceIn(Box PDFjetBox, float XOffset, float YOffset) throws Exception {
        ((Box) getObject()).placeIn(PDFjetBox, XOffset, YOffset);
    }

    public void ScaleBy(float Factor) throws Exception {
        ((Box) getObject()).scaleBy(Factor);
    }

    public void SetColor(int Color1) {
        ((Box) getObject()).setColor(Color1);
    }

    public void SetFillShape(boolean Enable) {
        ((Box) getObject()).setFillShape(Enable);
    }

    public void SetGoToAction(String Key) {
        ((Box) getObject()).setGoToAction(Key);
    }

    public void SetLineWidth(float Width) {
        ((Box) getObject()).setLineWidth(Width);
    }

    public void SetPattern(String Pattern) {
        ((Box) getObject()).setPattern(Pattern);
    }

    public void SetPosition(float X, float Y) {
        ((Box) getObject()).setPosition(X, Y);
    }

    public void SetSize(float Width, float Height) {
        ((Box) getObject()).setSize(Width, Height);
    }

    public void SetURIAction(String Uri) {
        ((Box) getObject()).setURIAction(Uri);
    }
}
 

Harris

Expert
Licensed User
1587402570066.png


There is my path structure...

All the .java files were downloaded from the OpenSource tab from:

Here is the Table.java file contents...
Could "package com.pdfjet;" in each file be an issue (as seen in this source file) ????

B4X:
/**
 *  Table.java
 *
Copyright (c) 2018, Innovatics Inc.
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice,
      this list of conditions and the following disclaimer.

    * Redistributions in binary form must reproduce the above copyright notice,
      this list of conditions and the following disclaimer in the documentation
      and / or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package com.pdfjet;

import java.util.*;
import anywheresoftware.b4a.BA.ShortName;
 @ShortName("Table")
/**
 *  Used to create table objects and draw them on a page.
 *
 *  Please see Example_08.
 */
 public class Table {

    public static final int DATA_HAS_0_HEADER_ROWS = 0;
    public static final int DATA_HAS_1_HEADER_ROWS = 1;
    public static final int DATA_HAS_2_HEADER_ROWS = 2;
    public static final int DATA_HAS_3_HEADER_ROWS = 3;
    public static final int DATA_HAS_4_HEADER_ROWS = 4;
    public static final int DATA_HAS_5_HEADER_ROWS = 5;
    public static final int DATA_HAS_6_HEADER_ROWS = 6;
    public static final int DATA_HAS_7_HEADER_ROWS = 7;
    public static final int DATA_HAS_8_HEADER_ROWS = 8;
    public static final int DATA_HAS_9_HEADER_ROWS = 9;

    private int rendered = 0;
    private int numOfPages;

    private List<List<Cell>> tableData = null;
    private int numOfHeaderRows = 0;

    private float x1;
    private float y1;

    private float bottom_margin = 30f;


    /**
     *  Create a table object.
     *
     */
    public Table() {
        tableData = new ArrayList<List<Cell>>();
    }


    /**
     *  Sets the position (x, y) of the top left corner of this table on the page.
     *
     *  @param x the x coordinate of the top left point of the table.
     *  @param y the y coordinate of the top left point of the table.
     */
    public void setPosition(double x, double y) {
        this.x1 = (float) x;
        this.y1 = (float) y;
    }


    /**
     *  Sets the position (x, y) of the top left corner of this table on the page.
     *
     *  @param x the x coordinate of the top left point of the table.
     *  @param y the y coordinate of the top left point of the table.
     */
    public void setPosition(float x, float y) {
        setLocation(x, y);
    }


    /**
     *  Sets the location (x, y) of the top left corner of this table on the page.
     *
     *  @param x the x coordinate of the top left point of the table.
     *  @param y the y coordinate of the top left point of the table.
     */
    public void setLocation(float x, float y) {
        this.x1 = x;
        this.y1 = y;
    }


    /**
     *  Sets the bottom margin for this table.
     *
     *  @param bottom_margin the margin.
     */
    public void setBottomMargin(double bottom_margin) {
        this.bottom_margin = (float) bottom_margin;
    }


    /**
     *  Sets the bottom margin for this table.
     *
     *  @param bottom_margin the margin.
     */
    public void setBottomMargin(float bottom_margin) {
        this.bottom_margin = bottom_margin;
    }


    /**
     *  Sets the table data.
     *
     *  The table data is a perfect grid of cells.
     *  All cell should be an unique object and you can not reuse blank cell objects.
     *  Even if one or more cells have colspan bigger than zero the number of cells in the row will not change.
     *
     *  @param tableData the table data.
     */
    public void setData(
            List<List<Cell>> tableData) throws Exception {
        this.tableData = tableData;
        this.numOfHeaderRows = 0;
        this.rendered = numOfHeaderRows;
    }


    /**
     *  Sets the table data and specifies the number of header rows in this data.
     *
     *  @param tableData the table data.
     *  @param numOfHeaderRows the number of header rows in this data.
     */
    public void setData(
            List<List<Cell>> tableData, int numOfHeaderRows) throws Exception {
        this.tableData = tableData;
        this.numOfHeaderRows = numOfHeaderRows;
        this.rendered = numOfHeaderRows;
    }


    /**
     *  Auto adjusts the widths of all columns so that they are just wide enough to hold the text without truncation.
     */
    public void autoAdjustColumnWidths() {
        // Find the maximum text width for each column
        float[] max_col_widths = new float[tableData.get(0).size()];
        for (int i = 0; i < tableData.size(); i++) {
            List<Cell> row = tableData.get(i);
            for (int j = 0; j < row.size(); j++) {
                Cell cell = row.get(j);
                if (cell.getColSpan() == 1) {
                    float cellWidth = 0f;
                    if (cell.image != null) {
                        cellWidth = cell.image.getWidth();
                    }
                    if (cell.text != null) {
                        if (cell.font.stringWidth(cell.fallbackFont, cell.text) > cellWidth) {
                            cellWidth = cell.font.stringWidth(cell.fallbackFont, cell.text);
                        }
                    }
                    cell.setWidth(cellWidth + cell.left_padding + cell.right_padding);
                    if (max_col_widths[j] == 0f ||
                            cell.getWidth() > max_col_widths[j]) {
                        max_col_widths[j] = cell.getWidth();
                    }
                }
            }
        }

        for (int i = 0; i < tableData.size(); i++) {
            List<Cell> row = tableData.get(i);
            for (int j = 0; j < row.size(); j++) {
                Cell cell = row.get(j);
                cell.setWidth(max_col_widths[j]);
            }
        }
    }


    /**
     *  Sets the alignment of the numbers to the right.
     */
    public void rightAlignNumbers() {
        for (int i = numOfHeaderRows; i < tableData.size(); i++) {
            List<Cell> row = tableData.get(i);
            for (int j = 0; j < row.size(); j++) {
                Cell cell = row.get(j);
                if (cell.text != null) {
                    String str = cell.text;
                    int len = str.length();
                    boolean isNumber = true;
                    int k = 0;
                    while (k < len) {
                        char ch = str.charAt(k++);
                        if (!Character.isDigit(ch)
                                && ch != '('
                                && ch != ')'
                                && ch != '-'
                                && ch != '.'
                                && ch != ','
                                && ch != '\'') {
                            isNumber = false;
                        }
                    }
                    if (isNumber) {
                        cell.setTextAlignment(Align.RIGHT);
                    }
                }
            }
        }
    }


    /**
     *  Removes the horizontal lines between the rows from index1 to index2.
     */
    public void removeLineBetweenRows(
            int index1, int index2) throws Exception {
        for (int j = index1; j < index2; j++) {
            List<Cell> row = tableData.get(j);
            for (int i = 0; i < row.size(); i++) {
                Cell cell = row.get(i);
                cell.setBorder(Border.BOTTOM, false);
            }
            row = tableData.get(j + 1);
            for (int i = 0; i < row.size(); i++) {
                Cell cell = row.get(i);
                cell.setBorder(Border.TOP, false);
            }
        }
    }


    /**
     *  Sets the text alignment in the specified column.
     *
     *  @param index the index of the specified column.
     *  @param alignment the specified alignment. Supported values: Align.LEFT, Align.RIGHT, Align.CENTER and Align.JUSTIFY.
     */
    public void setTextAlignInColumn(
            int index, int alignment) throws Exception {
        for (int i = 0; i < tableData.size(); i++) {
            List<Cell> row = tableData.get(i);
            if (index < row.size()) {
                row.get(index).setTextAlignment(alignment);
            }
        }
    }


    /**
     *  Sets the color of the text in the specified column.
     *
     *  @param index the index of the specified column.
     *  @param color the color specified as an integer.
     */
    public void setTextColorInColumn(
            int index, int color) throws Exception {
        for (int i = 0; i < tableData.size(); i++) {
            List<Cell> row = tableData.get(i);
            if (index < row.size()) {
                row.get(index).setBrushColor(color);
            }
        }
    }


    /**
     *  Sets the font for the specified column.
     *
     *  @param index the column index.
     *  @param font the font.
     */
    public void setFontInColumn(int index, Font font) throws Exception {
        for (int i = 0; i < tableData.size(); i++) {
            List<Cell> row = tableData.get(i);
            if (index < row.size()) {
                row.get(index).font = font;
            }
        }
    }


    /**
     *  Sets the color of the text in the specified row.
     *
     *  @param index the index of the specified row.
     *  @param color the color specified as an integer.
     */
    public void setTextColorInRow(
            int index, int color) throws Exception {
        List<Cell> row = tableData.get(index);
        for (int i = 0; i < row.size(); i++) {
            row.get(i).setBrushColor(color);
        }
    }


    /**
     *  Sets the font for the specified row.
     *
     *  @param index the row index.
     *  @param font the font.
     */
    public void setFontInRow(int index, Font font) throws Exception {
        List<Cell> row = tableData.get(index);
        for (int i = 0; i < row.size(); i++) {
            row.get(i).font = font;
        }
    }


    /**
     *  Sets the width of the column with the specified index.
     *
     *  @param index the index of specified column.
     *  @param width the specified width.
     */
    public void setColumnWidth(
            int index, float width) throws Exception {
        for (int i = 0; i < tableData.size(); i++) {
            List<Cell> row = tableData.get(i);
            if (index < row.size()) {
                row.get(index).setWidth(width);
            }
        }
    }


    /**
     *  Returns the column width of the column at the specified index.
     *
     *  @param index the index of the column.
     *  @return the width of the column.
     */
    public float getColumnWidth(int index) throws Exception {
        return getCellAtRowColumn(0, index).getWidth();
    }


    /**
     *  Returns the cell at the specified row and column.
     *
     *  @param row the specified row.
     *  @param col the specified column.
     *
     *  @return the cell at the specified row and column.
     */
    public Cell getCellAt(
            int row, int col) throws Exception {
        if (row >= 0) {
            return tableData.get(row).get(col);
        }
        return tableData.get(tableData.size() + row).get(col);
    }


    /**
     *  Returns the cell at the specified row and column.
     *
     *  @param row the specified row.
     *  @param col the specified column.
     *
     *  @return the cell at the specified row and column.
     */
    public Cell getCellAtRowColumn(int row, int col) throws Exception {
        return getCellAt(row, col);
    }


    /**
     *  Returns a list of cell for the specified row.
     *
     *  @param index the index of the specified row.
     *
     *  @return the list of cells.
     */
    public List<Cell> getRow(int index) throws Exception {
        return tableData.get(index);
    }


    public List<Cell> getRowAtIndex(int index) throws Exception {
        return getRow(index);
    }


    /**
     *  Returns a list of cell for the specified column.
     *
     *  @param index the index of the specified column.
     *
     *  @return the list of cells.
     */
    public List<Cell> getColumn(int index) throws Exception {
        List<Cell> column = new ArrayList<Cell>();
        for (int i = 0; i < tableData.size(); i++) {
            List<Cell> row = tableData.get(i);
            if (index < row.size()) {
                column.add(row.get(index));
            }
        }
        return column;
    }


    public List<Cell> getColumnAtIndex(int index) throws Exception {
        return getColumn(index);
    }


    /**
     *  Returns the total number of pages that are required to draw this table on.
     *
     *  @param page the type of pages we are drawing this table on.
     *
     *  @return the number of pages.
     */
    public int getNumberOfPages(Page page) throws Exception {
        numOfPages = 1;
        while (hasMoreData()) {
            drawOn(page, false);
        }
        resetRenderedPagesCount();
        return numOfPages;
    }


    /**
     *  Draws this table on the specified page.
     *
     *  @param page the page to draw this table on.
     *
     *  @return Point the point on the page where to draw the next component.
     */
    public Point drawOn(Page page) throws Exception {
        return drawOn(page, true);
    }


    /**
     *  Draws this table on the specified page.
     *
     *  @param page the page to draw this table on.
     *  @param draw if false - do not draw the table. Use to only find out where the table ends.
     *
     *  @return Point the point on the page where to draw the next component.
     */
    private Point drawOn(Page page, boolean draw) throws Exception {
        return drawTableRows(page, draw, drawHeaderRows(page, draw));
    }


    private float[] drawHeaderRows(Page page, boolean draw) throws Exception {
        float x = x1;
        float y = y1;
        float cell_w = 0f;
        float cell_h = 0f;

        for (int i = 0; i < numOfHeaderRows; i++) {
            List<Cell> dataRow = tableData.get(i);
            cell_h = getMaxCellHeight(dataRow);

            for (int j = 0; j < dataRow.size(); j++) {
                Cell cell = dataRow.get(j);
                cell_w = cell.getWidth();
                int colspan = cell.getColSpan();
                for (int k = 1; k < colspan; k++) {
                    cell_w += dataRow.get(++j).width;
                }

                if (draw) {
                    page.setBrushColor(cell.getBrushColor());
                    cell.paint(page, x, y, cell_w, cell_h);
                }

                x += cell_w;
            }
            x = x1;
            y += cell_h;
        }

        return new float[] {x, y, cell_w, cell_h};
    }


    private Point drawTableRows(Page page, boolean draw, float[] parameter) throws Exception {
        float x = parameter[0];
        float y = parameter[1];
        float cell_w = parameter[2];
        float cell_h = parameter[3];

        for (int i = rendered; i < tableData.size(); i++) {
            List<Cell> dataRow = tableData.get(i);
            cell_h = getMaxCellHeight(dataRow);

            for (int j = 0; j < dataRow.size(); j++) {
                Cell cell = dataRow.get(j);
                cell_w = cell.getWidth();
                int colspan = cell.getColSpan();
                for (int k = 1; k < colspan; k++) {
                    cell_w += dataRow.get(++j).getWidth();
                }

                if (draw) {
                    page.setBrushColor(cell.getBrushColor());
                    cell.paint(page, x, y, cell_w, cell_h);
                }

                x += cell_w;
            }
            x = x1;
            y += cell_h;

            // Consider the height of the next row when checking if we must go to a new page
            if (i < (tableData.size() - 1)) {
                List<Cell> nextRow = tableData.get(i + 1);
                for (int j = 0; j < nextRow.size(); j++) {
                    Cell cell = nextRow.get(j);
                    float cellHeight = cell.getHeight();
                    if (cellHeight > cell_h) {
                        cell_h = cellHeight;
                    }
                }
            }

            if ((y + cell_h) > (page.height - bottom_margin)) {
                if (i == tableData.size() - 1) {
                    rendered = -1;
                }
                else {
                    rendered = i + 1;
                    numOfPages++;
                }
                return new Point(x, y);
            }
        }
        rendered = -1;

        return new Point(x, y);
    }


    private float getMaxCellHeight(List<Cell> row) {
        float max_cell_height = 0f;
        for (int j = 0; j < row.size(); j++) {
            Cell cell = row.get(j);
            if (cell.getHeight() > max_cell_height) {
                max_cell_height = cell.getHeight();
            }
        }
        return max_cell_height;
    }


    /**
     *  Returns true if the table contains more data that needs to be drawn on a page.
     */
    public boolean hasMoreData() {
        return rendered != -1;
    }


    /**
     *  Returns the width of this table when drawn on a page.
     *
     *  @return the widht of this table.
     */
    public float getWidth() {
        float table_width = 0f;
        List<Cell> row = tableData.get(0);
        for (int i = 0; i < row.size(); i++) {
            table_width += row.get(i).getWidth();
        }
        return table_width;
    }


    /**
     *  Returns the number of data rows that have been rendered so far.
     *
     *  @return the number of data rows that have been rendered so far.
     */
    public int getRowsRendered() {
        return rendered == -1 ? rendered : rendered - numOfHeaderRows;
    }


    /**
     *  Just calls the wrapAroundCellText method.
     */
    @Deprecated
    public void wrapAroundCellText2() {
        wrapAroundCellText();
    }


    /**
     *  Wraps around the text in all cells so it fits the column width.
     *  This method should be called after all calls to setColumnWidth and autoAdjustColumnWidths.
     *
     */
    public void wrapAroundCellText() {
        List<List<Cell>> tableData2 = new ArrayList<List<Cell>>();

        for (int i = 0; i < tableData.size(); i++) {
            List<Cell> row = tableData.get(i);
            int maxNumVerCells = 1;
            for (int j = 0; j < row.size(); j++) {
                Cell cell = row.get(j);
                int colspan = cell.getColSpan();
                for (int n = 1; n < colspan; n++) {
                    Cell next = row.get(j + n);
                    cell.setWidth(cell.getWidth() + next.getWidth());
                    next.setWidth(0f);
                }
                int numVerCells = cell.getNumVerCells();
                if (numVerCells > maxNumVerCells) {
                    maxNumVerCells = numVerCells;
                }
            }

            for (int j = 0; j < maxNumVerCells; j++) {
                List<Cell> row2 = new ArrayList<Cell>();
                for (int k = 0; k < row.size(); k++) {
                    Cell cell = row.get(k);

                    Cell cell2 = new Cell(cell.getFont(), "");
                    cell2.setFallbackFont(cell.getFallbackFont());
                    cell2.setPoint(cell.getPoint());
                    cell2.setWidth(cell.getWidth());
                    if (j == 0) {
                        cell2.setTopPadding(cell.top_padding);
                    }
                    if (j == (maxNumVerCells - 1)) {
                        cell2.setBottomPadding(cell.bottom_padding);
                    }
                    cell2.setLeftPadding(cell.left_padding);
                    cell2.setRightPadding(cell.right_padding);
                    cell2.setLineWidth(cell.lineWidth);
                    cell2.setBgColor(cell.getBgColor());
                    cell2.setPenColor(cell.getPenColor());
                    cell2.setBrushColor(cell.getBrushColor());
                    cell2.setProperties(cell.getProperties());
                    cell2.setVerTextAlignment(cell.getVerTextAlignment());
                    cell2.setIgnoreImageHeight(cell.getIgnoreImageHeight());

                    if (j == 0) {
                        cell2.setImage(cell.getImage());
                        if (cell.getCompositeTextLine() != null) {
                            cell2.setCompositeTextLine(cell.getCompositeTextLine());
                        }
                        else {
                            cell2.setText(cell.getText());
                        }
                        if (maxNumVerCells > 1) {
                            cell2.setBorder(Border.BOTTOM, false);
                        }
                    }
                    else  {
                        cell2.setBorder(Border.TOP, false);
                        if (j < (maxNumVerCells - 1)) {
                            cell2.setBorder(Border.BOTTOM, false);
                        }
                    }
                    row2.add(cell2);
                }
                tableData2.add(row2);
            }
        }

        for (int i = 0; i < tableData2.size(); i++) {
            List<Cell> row = tableData2.get(i);
            for (int j = 0; j < row.size(); j++) {
                Cell cell = row.get(j);
                if (cell.text != null) {
                    int n = 0;
                    String[] textLines = cell.getText().split("\\r?\\n");
                    for (String textLine : textLines) {
                        StringBuilder sb = new StringBuilder();
                        String[] tokens = textLine.split("\\s+");
                        if (tokens.length == 1) {
                            sb.append(tokens[0]);
                        }
                        else {
                            for (int k = 0; k < tokens.length; k++) {
                                String token = tokens[k];
                                if (cell.font.stringWidth(cell.fallbackFont, sb.toString() + " " + token) >
                                        (cell.getWidth() - (cell.left_padding + cell.right_padding))) {
                                    tableData2.get(i + n).get(j).setText(sb.toString());
                                    sb = new StringBuilder(token);
                                    n++;
                                }
                                else {
                                    if (k > 0) {
                                        sb.append(" ");
                                    }
                                    sb.append(token);
                                }
                            }
                        }
                        tableData2.get(i + n).get(j).setText(sb.toString());
                        n++;
                    }
                }
                else {
                    tableData2.get(i).get(j).setCompositeTextLine(cell.getCompositeTextLine());
                }
            }
        }

        tableData = tableData2;
    }


    /**
     *  Sets all table cells borders to <strong>false</strong>.
     *
     */
    public void setNoCellBorders() {
        for (int i = 0; i < tableData.size(); i++) {
            List<Cell> row = tableData.get(i);
            for (int j = 0; j < row.size(); j++) {
                tableData.get(i).get(j).setNoBorders();
            }
        }
    }


    /**
     *  Sets the color of the cell border lines.
     *
     *  @param color the color of the cell border lines.
     */
    public void setCellBordersColor(int color) {
        for (int i = 0; i < tableData.size(); i++) {
            List<Cell> row = tableData.get(i);
            for (int j = 0; j < row.size(); j++) {
                tableData.get(i).get(j).setPenColor(color);
            }
        }
    }


    /**
     *  Sets the width of the cell border lines.
     *
     *  @param width the width of the border lines.
     */
    public void setCellBordersWidth(float width) {
        for (int i = 0; i < tableData.size(); i++) {
            List<Cell> row = tableData.get(i);
            for (int j = 0; j < row.size(); j++) {
                tableData.get(i).get(j).setLineWidth(width);
            }
        }
    }


    /**
     * Resets the rendered pages count.
     * Call this method if you have to draw this table more than one time.
     */
    public void resetRenderedPagesCount() {
        this.rendered = numOfHeaderRows;
    }


    /**
     * This method removes borders that have the same color and overlap 100%.
     * The result is improved onscreen rendering of thin border lines by some PDF viewers.
     */
    public void mergeOverlaidBorders() {
        for (int i = 0; i < tableData.size(); i++) {
            List<Cell> currentRow = tableData.get(i);
            for (int j = 0; j < currentRow.size(); j++) {
                Cell currentCell = currentRow.get(j);
                if (j < currentRow.size() - 1) {
                    Cell cellAtRight = currentRow.get(j + 1);
                    if (cellAtRight.getBorder(Border.LEFT) &&
                            currentCell.getPenColor() == cellAtRight.getPenColor() &&
                            currentCell.getLineWidth() == cellAtRight.getLineWidth() &&
                            (currentCell.getColSpan() + j) < (currentRow.size() - 1)) {
                        currentCell.setBorder(Border.RIGHT, false);
                    }
                }
                if (i < tableData.size() - 1) {
                    List<Cell> nextRow = tableData.get(i + 1);
                    Cell cellBelow = nextRow.get(j);
                    if (cellBelow.getBorder(Border.TOP) &&
                            currentCell.getPenColor() == cellBelow.getPenColor() &&
                            currentCell.getLineWidth() == cellBelow.getLineWidth()) {
                        currentCell.setBorder(Border.BOTTOM, false);
                    }
                }
            }
        }
    }

}   // End of Table.java
 

DonManfred

Expert
Licensed User
you can not use the pdfjet source classes inside your wrapper.

As an example you can use all the pdfjet source classes in a tree com\pdfjet\...
and then you create a wrapper class for any pdfjet class you want to wrap.

com.example.table for example (to reference the file you uploaded). Here you have to create the wrappercode
com\example\table.java should be the file you write. And it is not done with copying the code from compdfjet.table.java to your code....

Honestly it doesn´t look like you know what you are doing there
 
Last edited:

Harris

Expert
Licensed User
Honestly it doesn´t look like you know what you are doing there
Truer words have never been spoken....

I have NO freakin idea as to what I am doing.... It is all based on "hope" and "assumption".
I was just hoping that I could take the java source (PDFjet) and make a B4J lib from it using SLC (after some discovery like adding @ShortName for example).

I was also hoping someone who knew what they were doing had already wrapped PDFjet for B4J.
@warwound did once... but it is no longer supported or available - the links are broken in his project...

Experimenting with the JavaObject method works - but man it's allota work re-creating that wheel.... However, if I must, I shall. I need the PDFjet for my webapp projects...
I just hope it all works when I load it onto my Linux VPS servers. I will likely need a Non-UI version to rid the jFX from it...

Right now, I create RTF docs from my DB tables - allow the user to download it - and PRINT to a PDF printer to make a PDF (for their distribution).
I was wanting to shorten the process for them by generating a PDF as an option.

Thanks for you help....
 
Top