logo
down
shadow

iText 7 relative column width no longer working starting iText 7.0.2


iText 7 relative column width no longer working starting iText 7.0.2

Content Index :

iText 7 relative column width no longer working starting iText 7.0.2
Tag : development , By : Aires
Date : November 24 2020, 03:01 PM

fixed the issue. Will look into that further The table layout algoritm changed in version 7.0.2 as is stated in the API docs (http://itextsupport.com/apidocs/itext7/latest/com/itextpdf/layout/element/Table.html#Table-float:A-boolean-). The simplest solution is to add a second parameter with the value true to the constructor of Table:
Table htable = new Table(new float[] {3, 8, 5, 10, 5, 10, 30}, true);

Comments
No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

pdf created using itext jar giving error(not supported or damaged) at opening ,I am using itext version 5.3.0


Tag : java , By : Mossy Breen
Date : March 29 2020, 07:55 AM
I wish did fix the issue. The PdfWriter coupled with a Document stores the page content when the page is finished. A page is finished as soon as either the next page is started or the `Document is closed.
In case of as little content as in the OP's sample on only a single page and without a close call, therefore, it is not surprising that the OP observed that the size of file is 0 byte.
document.close();

iText column width wrong


Tag : java , By : rhyhann
Date : March 29 2020, 07:55 AM
With these it helps Summary
If you are only using it to generate PDF tables instead of needing RTF and HTML output, then it might be better to use the PdfPTable class [Table is now unsupported] which is better supported and less prone to quirks (as explained here: PdfpTable vs. Table (vs. SimpleTable?)).

Issue with Column Width in IText 7 Tables


Tag : development , By : pttr
Date : March 29 2020, 07:55 AM
hop of those help? Please upgrade to the latest version of iText - 7.1.x line - and use the code below to create a table with columns of even width:
Table headerTable = new Table(UnitValue.createPercentArray(new float[]{5,5,5}));
headerTable.setWidth(UnitValue.createPercentValue(100));

Get absolute width from PdfPTable column (iText)


Tag : java , By : user121405
Date : March 29 2020, 07:55 AM
To fix the issue you can do Issue and explanation
The function table.getAbsoluteWidths() relies on the requirement that totalWidth of the table must be set, hence table.getTotalWidth() > 0.
this.absoluteWidths[k] = this.totalWidth * this.relativeWidths[k] / total;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;

public class TableWidthSample {

    public static void main(String[] args) {
        Document document = new Document();
        try {
            PdfWriter.getInstance(document, new FileOutputStream("TableColumnWidth.pdf"));
            document.open();

            PdfPTable table = createTableWithRelativeColumnWidths();

            // needed for calculation of getAbsoluteWidths: (A) totalWidth is set for table
            table.setTotalWidth(calculateTableWidthBasedOnPageA4(document, table));
            // now it prints width, because (A)
            printTableWidth(table, "after totalWidth set manually"); // absoluteWidths: [134.4857, 14.942857, 268.9714]

            // long text, that needs to be split among cells (fullAlamatPP)
            String text = "this is a very long text. The text contains a full address. Because the text is too long to be shown in the last cell, it is split.";
            // your CODE to fill text into cells based on restrictions
            addTextAcrossTableCells(table, text);

            document.add(table);

/*
            // need to add filled table to doc (B)
            addToDocumentCellsAndTable(document, table);
            // now it prints width, because (B)
            printTableWidth(table, "after added to doc"); // absoluteWidths: [134.4857, 14.942857, 268.9714]
*/
            document.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static PdfPTable createTableWithRelativeColumnWidths() {
        float[] relativeWidths = {4.5F, 0.5F, 9F};
        PdfPTable table = new PdfPTable(relativeWidths);
        table.setWidthPercentage(100F);
        printTableWidth(table, "after table created"); // absoluteWidths: [0.0, 0.0, 0.0]
        return table;
    }

    private static float calculateTableWidthBasedOnPageA4(Document document, PdfPTable table) {
        return (PageSize.A4.getWidth() - document.leftMargin() - document.rightMargin())
                * table.getWidthPercentage() / 100;
    }

    private static void addToDocumentCellsAndTable(Document document, PdfPTable table) throws DocumentException {
        // needed for calculation of getAbsoluteWidths: (B.1) add cells to table
        table.addCell("Hello");
        table.addCell("World");
        table.addCell("!");
        printTableWidth(table, "after cells added to table"); // absoluteWidths: [0.0, 0.0, 0.0]

        // needed for calculation of getAbsoluteWidths: (B.2) add table to doc
        document.add(table);
    }

    private static void addTextAcrossTableCells(PdfPTable table, String text) throws IOException, DocumentException {
        // restrictions; this is width of a column from table
        float maxColumnWidth = table.getAbsoluteWidths()[2]; // 330.12103F;
        System.out.println("use width of third column as max: " + maxColumnWidth);
        // sample font used for calculation of text-width
        Font font = new Font(BaseFont.createFont("Courier", BaseFont.CP1250, true), 12);

        // alamatSesuaiKtpPP
        String splitText[] = getTextPartsUsingFontWithMaxWidth(text, font, maxColumnWidth);
        String dottedLine = "..";
        table.addCell("Alamat / Address:");

        // p_alamat1_ct
        Phrase phrase1 = new Phrase(splitText[0], font);
        phrase1.add(dottedLine);
        PdfPCell cell1 = new PdfPCell(phrase1);
        cell1.setBackgroundColor(BaseColor.LIGHT_GRAY);
        cell1.setBorder(PdfPCell.NO_BORDER);
        table.addCell(cell1);

        // p_alamat2_ct
        Phrase phrase2 = new Phrase(splitText[1], font);
        phrase2.add(dottedLine);
        PdfPCell cell2 = new PdfPCell(phrase2);
        cell2.setBorder(PdfPCell.NO_BORDER);
        table.addCell(cell2);
    }

    private static String[] getTextPartsUsingFontWithMaxWidth(String text, Font font, float maxWidth) {
        String results[] = new String[] {"",""};
        String firstPartOfText = " ";
        float widthOfText = 0;
        for (int i = 0; i < text.length();i++) {
            firstPartOfText += text.charAt(i);
            widthOfText = font.getCalculatedBaseFont(true).getWidthPoint(firstPartOfText, font.getCalculatedSize());
            System.out.printf("%d: widthOfText: %f\n", i, widthOfText);
            if (widthOfText > maxWidth) {
                results[0] = firstPartOfText.substring(0, firstPartOfText.length() - 1);
                results[1] = text.substring(i); // second argument "text.length()" is not needed
                break;
            }
        }

        if (results[0] == "") {
            results[0] = text;
        }
        return results;
    }

    private static void printTableWidth(PdfPTable table, String labelText) {
        float[] absoluteWidths = table.getAbsoluteWidths();
        System.out.println(labelText + "> getAbsoluteWidths: " + Arrays.toString(absoluteWidths));
    }
}

Can you determine column-width in iText based on input (e.g. a string)?


Tag : java , By : changke
Date : March 29 2020, 07:55 AM
I wish this help you I am working on a project using iText and Java. In the project, I will be making a pdf. In this pdf, there will be multiple tables and they will have changing column counts and content, which means the column-widths have to be determined in a flexible way too. , What was needed was to split the string into an int[], like so:
String inputFromDatabase = "1,5";
String[] p = inputFromDatabase.trim().split(",");
int[] ans = new int[p.length];
for (int a = 0; a < p.length; a++) {
    ans[a] = Integer.parseInt(p[a]);
}
table.setWidths(ans);
Related Posts Related QUESTIONS :
  • Is it just me, or are characters being rendered incorrectly more lately?
  • How do I create an HTML anchor in a FogBugz wiki page?
  • Sleep from within an Informix SPL procedure
  • License Models
  • Rational Purify failing to jump to memory leaks
  • Telligent's Community Server
  • Do people use the Hungarian Naming Conventions in the real world?
  • Memcached chunk limit
  • How do you pull the URL for an ASP.NET web reference from a configuration file in Visual Studio 2008?
  • How to add CVS directories recursively
  • Web App Beta
  • Virtual machine supporting multiple displays
  • LINQ to SQL strings to enums
  • How do I configure eclipse (zend studio 6) to hint and code complete several languages?
  • Recommended Fonts for Programming?
  • Automating VMWare or VirtualPC
  • How do I use more than one OpenID?
  • Domain Specific Language resources
  • Vista or XP for Dev Machine
  • Where can I get the Windows Workflow "wca.exe" application?
  • How to learn ADO.NET
  • What is a good barebones CMS or framework?
  • What's the Developer Express equivalent of System.Windows.Forms.LinkButton?
  • Is it acceptable for invalid XHTML?
  • Anyone soloing using fogbugz?
  • Using MBUnit in TeamCity
  • How to setup a crontab to execute at specific time
  • Linking two Office documents
  • How can I detect if a browser is blocking a popup?
  • Apply filter to t-foreach
  • Lotus Notes convert formula to agent
  • Problem with configuration of odoo for working with pycharm
  • Unexpected number of arguments when calling EM_JS function
  • What does the function state_fidelity() do in Qiskit?
  • Azure function with IoT Hub trigger get send device
  • Cluster similar words using word2vec
  • Add elements into an array then find distinct,
  • Angular8 - error TS2304: Cannot find name
  • Manually polling streams in future implementation
  • How do you perform receipt validation for Android on Xamarin Forms
  • What is the rule behind instruction count in Intel PIN?
  • How to draw some text when click a button in wxWidgets?
  • Restrict entity types in Spacy NER
  • How to animate CN1 Slider progress on load
  • Issue with bwa mem process not running on all output files from previous process
  • Delete videos from playlist using YouTube API
  • How to restart the Hyperledger Composer Playground locally
  • How to configure email alerts in Zabbix Server?
  • Simulator is not working for smart home action with all configured session
  • How parse data using join on Objection.js
  • Assign an array to a property in a Chapel Class
  • Netlogo: How can I obtain the accumulate value in Netlogo?
  • How to populate datasource for listview using api response in react native?
  • Why does gmail API when using history.list method send message ids without the field what action has been preformed on t
  • How to run an append query in ms access vba as part of a transaction
  • Wrong dates shown in Jekyll-based blog
  • How to concatenate two document lists in a webMethods flow service?
  • How to properly set up a site map addition in a customization project
  • IBM Content Collector error calling external Web Service
  • Is (0*1*)* equal to (0 | 1)*?
  • shadow
    Privacy Policy - Terms - Contact Us © scrbit.com