简体   繁体   中英

How to export n number columns for PDF in Java?

I have the requirement to generate PDF file with 120 columns(all columns should be on a single page).I am using iText. When I generate pdf columns gets overlapped.

My Questions are:

1)Is there any way to put horizontal scroll bar to navigate between the columns ?

2)How do we prevent overlapping of columns?

I have seen the similar question in this site ( How to export n number columns as headings for PDF in Java? )

but the link mentioned in the answer there ( http://itext-general.2136553.n4.nabble.com/What-is-the-maximum-page-size-for-PDF-in-iText-td2150839.html%22 ) is inactive .

Please suggest me on this.

Following code will set the page size. So, If we have large number of columns (for example: 120 as in my case), it will automatically show horizontal bar in pdf to navigate.

Rectangle pageSize=new Rectangle(9400f,9400f);
Document doc=new Document(pageSize);

PdfWriter writer=PdfWriter.getInstance(document,baos);
writer.setUserunit(9900f);

// To prevent overlapping of columns, set the column widths as follows

PdfPTable table=new PdfPTable(120); // 120 is number of columns in table
table.setWidths(600f);

I had the similar problem but thanks to this question answer series i got the solution as stated by Mr.Harjinder, but here's a tip for those who face the same problem create the document after you create the PDF elements and keep the track of the width you want and the finally when you have the track of horizontal scrolling create your document below is the example

a PDF with lists and with horizontal scroll bar

public class Sample {
public static void main(String[] args) {
    try {
        float indentationLeft = 20f;
        float indentationLeftTemp = 20f;
        List prtLst = new List(List.UNORDERED);
        prtLst.setListSymbol("\u2022");
        prtLst.add("P1");
        prtLst.setListSymbol("+");
        prtLst.add("P2");
        prtLst.setListSymbol("\u2022");
        List prevList= prtLst;
        for(int i=0;i<500;i++){
            List chldLst2 = new List(List.UNORDERED);
            chldLst2.setIndentationLeft(indentationLeft);
            indentationLeftTemp+=20f;
            chldLst2.setListSymbol("\u2022");
            chldLst2.add("YY"+i);
            prevList.add(chldLst2);
            prevList=chldLst2;
        }
        prtLst.add("P3");


        Document d = null;
        if(indentationLeftTemp>400f){
            Rectangle pageSize=new Rectangle(indentationLeftTemp+200f,indentationLeftTemp+200f); 
            d = new Document(pageSize);
        }else{
            d = new Document();
        }
        PdfWriter.getInstance(d, new FileOutputStream("D:/test.pdf"));
        d.open();            
        d.add(prtLst);d.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM