Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Thu Mar 16, 2023 6:09 am

I want to express html tags as a grid using HTMLString. The method using HTMLConverter cannot use grid.draw. I want to connect multiple data to grid naturally.

Code: Select all
grid = new PdfGrid();
               
               rs = noteService.getNoteSection(noteSn);
               doc = new PdfDocument();
               page = doc.getPages().add(PdfPageSize.A4,new PdfMargins(40));
               
               String htmlString = EgovWebUtil.resetXSSMinimum((String)rs.get("cn"));
                
               String outputFile = "C:\\upload\\note\\HtmlToPdf2.pdf";
               String pluginPath = "C:\\upload\\note\\plugins";
               HtmlConverter.setPluginPath(pluginPath);
               HtmlConverter.convert(htmlString, outputFile, true, 100000, new Size(592, 837), new PdfMargins(40), LoadHtmlType.Source_Code);


This code is how to save HTMLString straight to PDF. I want to save all my sources in grid and then save them as PDF. Here is my code.

Code: Select all
list = noteService.noteMethodList(noteSn);
                  grid = new PdfGrid();
                  doc = new PdfDocument();
                  page = doc.getPages().add(PdfPageSize.A4,new PdfMargins(40));
                    //Set font for header and the rest cells
                  grid.getStyle().setFont(cjkFont);
                  
                  data = new String[list.size()+1];
                  items1 = String.valueOf(list.get(0).get("item1Nm")) == "null"? "":list.get(0).get("item1Nm").toString();
                  items2 = String.valueOf(list.get(0).get("item2Nm")) == "null"? "":list.get(0).get("item2Nm").toString();
                  data[0] = "번호; 작성; 설명;"+items1+"; "+items2;
                  for(int j=0; j<list.size();j++) {
                     String expln = String.valueOf(list.get(j).get("expln")) == "null" ? "": list.get(j).get("expln").toString();
                     data[j+1] = j+1+"; "+list.get(j).get("wrt")+"; "+expln+"; "+list.get(j).get("item1")+"; "+list.get(j).get("item2");
                  }
                  
                  
                  dataSource = new String[data.length][];
                    for (int j = 0; j < data.length; j++) {
                        dataSource[j] = data[j].split("[;]", -1);
                    }
                   
                    grid.setDataSource(dataSource);
                    for(int j=0; j<dataSource.length; j++) {
                       
                       grid.getRows().get(0).getStyle().setBackgroundBrush(PdfBrushes.getGray());
                    }
                  //list = noteService.noteFnceList(noteSn);
                    grid.draw(page, new Point2D.Float(0, 380));
                    doc.saveToFile("C:\\upload\\note\\HtmlToPdf7.pdf")


This is a repeat statement. I want to save it to the grid like this and then save the pdf file at the end. But currently this code is 'grid.draw(page, new Point2D.Float(0, 380));' You need to arbitrarily specify the page and height in the part. My data is in flux. Therefore, height and page cannot be specified. Do you have code that splices height and pages rather than fix values?

If not, you need to save all pdf files and then merge them. However, when merging, the grid with small data has too many white backgrounds. Is it possible to connect pdf data naturally rather than merging pages?

KIMDOOPAL
 
Posts: 10
Joined: Thu Mar 16, 2023 4:41 am

Fri Mar 17, 2023 9:29 am

Hi,

Thanks for your inquiry.
Are you trying to implement automatic page breaks based on the amount of data? Could you please provide us with your sample data and the effect you want? This can help us better understand your needs and work out a solution for you. Thanks for your corporation.

Best regards,
Triste
E-iceblue support team
User avatar

Triste.Dai
 
Posts: 1000
Joined: Tue Nov 15, 2022 3:59 am

Mon Mar 20, 2023 12:17 am

KIMDOOPAL wrote:Hi,

my code is long.

In this way, when extracting a PDF, each page is attached separately. I want the data to be continuous without empty spaces. However, the height of the data cannot be given arbitrarily. Because the order also changes.

Also, like case 2, even if you immediately extract it as a file that is not displayed on the grid, you want it to be joined without empty spaces rather than adding pages.

Code: Select all
for(int i=0; arrExport.length > i; i++) {
         
         switch(arrExport[i]) {
            case "1":
               System.out.println("::::: CASE 1 :::::");
               //Create a PdfDocument object
               doc = new PdfDocument();
               page = doc.getPages().add(PdfPageSize.A4,new PdfMargins(40));
                
               grid = new PdfGrid();
               
                 PdfBrush brush = PdfBrushes.getBlack();
                
                 float y = 30;
                
                 //Set cell padding
                 grid.getStyle().setCellPadding(new PdfPaddings(1, 1, 1, 1));
         
                 //Set font
                 grid.getStyle().setFont(cjkFont);
         
                 //Add rows and columns
                 PdfGridRow rows1 = grid.getRows().add();
                 PdfGridRow rows2 = grid.getRows().add();
                 PdfGridRow rows3 = grid.getRows().add();
                 PdfGridRow rows4 = grid.getRows().add();
                 PdfGridRow rows5 = grid.getRows().add();
                 PdfGridRow rows6 = grid.getRows().add();
                 PdfGridRow rows7 = grid.getRows().add();
                 PdfGridRow rows8 = grid.getRows().add();
                 grid.getColumns().add(4);
                
                 //Set column width
                 for (int j = 0; j < grid.getColumns().getCount(); j++) {
                     grid.getColumns().get(j).setWidth(120);
                 }
         
                 rs = noteService.getNote(Integer.parseInt(request.getParameter("noteSn").toString()));
                 String prsl = "";
                 if(rs.get("prslYn").equals("Y")) {prsl = "열람 가능";}
                 else{prsl = "열람 불가";}
                
                 //Write data into specific cells
                 rows1.getCells().get(0).setValue("정보");
                 rows2.getCells().get(0).setValue("제목");
                 rows2.getCells().get(1).setValue(rs.get("ttl").toString());
                 rows3.getCells().get(0).setValue("실험목적");
                 rows3.getCells().get(1).setValue(rs.get("prps").toString());
                 rows4.getCells().get(0).setValue("프로젝트");
                 rows4.getCells().get(1).setValue(rs.get("prjNm").toString());
                 rows5.getCells().get(0).setValue("공동열람");
                 rows5.getCells().get(1).setValue(prsl);
                 rows6.getCells().get(0).setValue("프로젝트 템플릿");
                 rows6.getCells().get(1).setValue(rs.get("prjTmpltNm"));
                 rows7.getCells().get(0).setValue("템플릿");
                 rows7.getCells().get(1).setValue(rs.get("tmpltNm"));
                 rows8.getCells().get(0).setValue("문서번호");
                 rows8.getCells().get(1).setValue(rs.get("docNo"));
         
                 //Span cell across columns
                 rows1.getCells().get(0).setColumnSpan(4);
                 rows2.getCells().get(1).setColumnSpan(3);
                 rows3.getCells().get(1).setColumnSpan(3);
                 rows4.getCells().get(1).setColumnSpan(3);
                 rows5.getCells().get(1).setColumnSpan(3);
                 rows6.getCells().get(1).setColumnSpan(3);
                 rows7.getCells().get(1).setColumnSpan(3);
                 rows8.getCells().get(1).setColumnSpan(3);
         
                 //Span cell across rows
                 //rows3.getCells().get(2).setRowSpan(2);
         
                 //Set text alignment of specific cells
                 rows1.getCells().get(0).setStringFormat(new PdfStringFormat(PdfTextAlignment.Center));
                 rows3.getCells().get(2).setStringFormat(new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle));
         
                 //Set background color of specific cells
                 rows1.getCells().get(0).getStyle().setBackgroundBrush(PdfBrushes.getGray());
                 rows2.getCells().get(0).getStyle().setBackgroundBrush(PdfBrushes.getGray());
                 rows3.getCells().get(0).getStyle().setBackgroundBrush(PdfBrushes.getGray());
                 rows4.getCells().get(0).getStyle().setBackgroundBrush(PdfBrushes.getGray());
                 rows5.getCells().get(0).getStyle().setBackgroundBrush(PdfBrushes.getGray());
                 rows6.getCells().get(0).getStyle().setBackgroundBrush(PdfBrushes.getGray());
                 rows7.getCells().get(0).getStyle().setBackgroundBrush(PdfBrushes.getGray());
                 rows8.getCells().get(0).getStyle().setBackgroundBrush(PdfBrushes.getGray());
         
                 //Format cell border
                 PdfBorders borders = new PdfBorders();
                 borders.setAll(new PdfPen(new PdfRGBColor(Color.BLACK), 0.8f));
                 for (int k = 0; k < grid.getRows().getCapacity(); k++) {
         
                     PdfGridRow gridRow = grid.getRows().get(k);
                     gridRow.setHeight(20f);
                     for (int j = 0; j < gridRow.getCells().getCount(); j++) {
                         gridRow.getCells().get(j).getStyle().setBorders(borders);
                     }
         
                 }
         
                 //Draw table on the page
                 grid.draw(page, new Point2D.Float(0, 30));
         
                 //Save the document to a PDF file
                 doc.saveToFile("C:\\upload\\note\\HtmlToPdf1.pdf");
                 //Define data
                  
               break;
            case "2":
               System.out.println("::::: CASE 2 :::::");
               
               grid = new PdfGrid();
               
               rs = noteService.getNoteSection(noteSn);
               doc = new PdfDocument();
               page = doc.getPages().add(PdfPageSize.A4,new PdfMargins(40));
               
               String htmlString = EgovWebUtil.resetXSSMinimum((String)rs.get("cn"));
                
               String outputFile = "C:\\upload\\note\\HtmlToPdf2.pdf";
               String pluginPath = "C:\\upload\\note\\plugins";
               HtmlConverter.setPluginPath(pluginPath);
               HtmlConverter.convert(htmlString, outputFile, true, 100000, new Size(592, 837), new PdfMargins(40), LoadHtmlType.Source_Code);
               
               grid.draw(page ,new Point2D.Float(0, 30));
               doc.saveToFile("C:\\upload\\note\\htmlTest.pdf");
               
               
               break;
            case "3":
               System.out.println("::::: CASE 3 :::::");
               doc = new PdfDocument();
               page = doc.getPages().add(PdfPageSize.A4,new PdfMargins(40));
         
                 //Create a PdfTable object
               grid = new PdfGrid();
                
                 //Set font for header and the rest cells
               grid.getStyle().setFont(cjkFont);
               
               list = noteService.noteAtchList(noteSn);
               
               data = new String[list.size()+1];
               data[0] = "번호; 파일; 설명; 등록일자";
               for(int j=0; j<list.size();j++) {
                  String expln = String.valueOf(list.get(j).get("expln")) == "null" ? "": list.get(j).get("expln").toString();
                  data[j+1] = j+1+"; "+list.get(j).get("fileNm")+"; "+expln+"; "+list.get(j).get("regDt");
               }
               
               
               dataSource = new String[data.length][];
                 for (int j = 0; j < data.length; j++) {
                     dataSource[j] = data[j].split("[;]", -1);
                 }
                
                 //Set data as the table data
                 grid.setDataSource(dataSource);
                 for(int j=0; j<dataSource.length; j++) {
                    
                    grid.getRows().get(0).getStyle().setBackgroundBrush(PdfBrushes.getGray());
                 }
         
                 //Draw table on the page
                 grid.draw(page, new Point2D.Float(0, 100));
         
                 //Save the document to a PDF file
                 doc.saveToFile("C:\\upload\\note\\HtmlToPdf3.pdf");
               
               break;
            case "4":
               System.out.println("::::: CASE 4 :::::");
               grid = new PdfGrid();
               doc = new PdfDocument();
               page = doc.getPages().add(PdfPageSize.A4,new PdfMargins(40));
                
                 //Set font for header and the rest cells
               grid.getStyle().setFont(cjkFont);
               
               //list = noteService.noteFnceList(noteSn);
               list = noteService.noteFnceList(noteSn);
               
               data = new String[list.size()+1];
               data[0] = "품목; 단가; 단가(부가세포함); 수량; 설명; 합계; 합계(부가세포함)";
               for(int j=0; j<list.size();j++) {
                  String expln = String.valueOf(list.get(j).get("expln")) == "null" ? "": list.get(j).get("expln").toString();
                  data[j+1] = j+1+"; "+list.get(j).get("untprc")+"; "+(Integer.parseInt(list.get(j).get("untprc").toString())*1.1)+"; "+list.get(j).get("cnt")+"; "+expln+"; "+((Integer.parseInt(list.get(j).get("untprc").toString()))*(Integer.parseInt(list.get(j).get("cnt").toString())))+"; "+((Integer.parseInt(list.get(j).get("untprc").toString()))*(Integer.parseInt(list.get(j).get("cnt").toString()))*1.1);
               }
               
               
               dataSource = new String[data.length][];
                 for (int j = 0; j < data.length; j++) {
                     dataSource[j] = data[j].split("[;]", -1);
                 }
                
                 grid.setDataSource(dataSource);
                 for(int j=0; j<dataSource.length; j++) {
                    
                    grid.getRows().get(0).getStyle().setBackgroundBrush(PdfBrushes.getGray());
                 }
               
                 grid.draw(page, new Point2D.Float(0, 170));
                 doc.saveToFile("C:\\upload\\note\\HtmlToPdf4.pdf");
               break;
            case "5":
                  System.out.println("::::: CASE 5 :::::");
                  //list = noteService.noteMatterList(noteSn);
                  list = noteService.noteMatterList(noteSn);
                  grid = new PdfGrid();
                  doc = new PdfDocument();
                  page = doc.getPages().add(PdfPageSize.A4,new PdfMargins(40));
                   
                    //Set font for header and the rest cells
                  grid.getStyle().setFont(cjkFont);
                  
                  data = new String[list.size()+1];
                  items1 = String.valueOf(list.get(0).get("item1Nm")) == "null"? "":list.get(0).get("item1Nm").toString();
                  items2 = String.valueOf(list.get(0).get("item2Nm")) == "null"? "":list.get(0).get("item2Nm").toString();
                  data[0] = "번호; 물질명; 물질코드; 설명;"+items1+"; "+items2;
                  for(int j=0; j<list.size();j++) {
                     String expln = String.valueOf(list.get(j).get("expln")) == "null" ? "": list.get(j).get("expln").toString();
                     data[j+1] = j+1+"; "+list.get(j).get("thingNm")+"; "+list.get(j).get("thingCd")+"; "+expln+"; "+list.get(j).get("item1")+"; "+list.get(j).get("item2");
                  }
                  
                  dataSource = new String[data.length][];
                    for (int j = 0; j < data.length; j++) {
                        dataSource[j] = data[j].split("[;]", -1);
                    }
                    grid.setDataSource(dataSource);
                    for(int j=0; j<dataSource.length; j++) {
                       
                       grid.getRows().get(0).getStyle().setBackgroundBrush(PdfBrushes.getGray());
                    }
                  //list = noteService.noteFnceList(noteSn);
                    grid.draw(page, new Point2D.Float(0, 240));
                    doc.saveToFile("C:\\upload\\note\\HtmlToPdf5.pdf");
                  
               break;
            case "6":
                  System.out.println("::::: CASE 6 :::::");
                  list = noteService.noteEquipList(noteSn);
                  grid = new PdfGrid();
                  doc = new PdfDocument();
                  page = doc.getPages().add(PdfPageSize.A4,new PdfMargins(40));
                    //Set font for header and the rest cells
                  grid.getStyle().setFont(cjkFont);
                  
                  data = new String[list.size()+1];
                  items1 = String.valueOf(list.get(0).get("item1Nm")) == "null"? "":list.get(0).get("item1Nm").toString();
                  items2 = String.valueOf(list.get(0).get("item2Nm")) == "null"? "":list.get(0).get("item2Nm").toString();
                  data[0] = "번호; 이름; 규격; 설명;"+items1+"; "+items2;
                  for(int j=0; j<list.size();j++) {
                     String expln = String.valueOf(list.get(j).get("expln")) == "null" ? "": list.get(j).get("expln").toString();
                     data[j+1] = j+1+"; "+list.get(j).get("eqpmentNm")+"; "+list.get(j).get("spcfct")+"; "+expln+"; "+list.get(j).get("item1")+"; "+list.get(j).get("item2");
                  }
                  
                  
                  dataSource = new String[data.length][];
                    for (int j = 0; j < data.length; j++) {
                        dataSource[j] = data[j].split("[;]", -1);
                    }
                   
                    grid.setDataSource(dataSource);
                    for(int j=0; j<dataSource.length; j++) {
                       
                       grid.getRows().get(0).getStyle().setBackgroundBrush(PdfBrushes.getGray());
                    }
                  //list = noteService.noteFnceList(noteSn);
                    grid.draw(page, new Point2D.Float(0, 310));
                    doc.saveToFile("C:\\upload\\note\\HtmlToPdf6.pdf");
               break;
            case "7":
                  System.out.println("::::: CASE 7 :::::");
                  list = noteService.noteMethodList(noteSn);
                  grid = new PdfGrid();
                  doc = new PdfDocument();
                  page = doc.getPages().add(PdfPageSize.A4,new PdfMargins(40));
                    //Set font for header and the rest cells
                  grid.getStyle().setFont(cjkFont);
                  
                  data = new String[list.size()+1];
                  items1 = String.valueOf(list.get(0).get("item1Nm")) == "null"? "":list.get(0).get("item1Nm").toString();
                  items2 = String.valueOf(list.get(0).get("item2Nm")) == "null"? "":list.get(0).get("item2Nm").toString();
                  data[0] = "번호; 작성; 설명;"+items1+"; "+items2;
                  for(int j=0; j<list.size();j++) {
                     String expln = String.valueOf(list.get(j).get("expln")) == "null" ? "": list.get(j).get("expln").toString();
                     data[j+1] = j+1+"; "+list.get(j).get("wrt")+"; "+expln+"; "+list.get(j).get("item1")+"; "+list.get(j).get("item2");
                  }
                  
                  
                  dataSource = new String[data.length][];
                    for (int j = 0; j < data.length; j++) {
                        dataSource[j] = data[j].split("[;]", -1);
                    }
                   
                    grid.setDataSource(dataSource);
                    for(int j=0; j<dataSource.length; j++) {
                       
                       grid.getRows().get(0).getStyle().setBackgroundBrush(PdfBrushes.getGray());
                    }
                  //list = noteService.noteFnceList(noteSn);
                    grid.draw(page, new Point2D.Float(0, 380));
                    doc.saveToFile("C:\\upload\\note\\HtmlToPdf7.pdf");
               break;
            case "8":
                  System.out.println("::::: CASE 8 :::::");
               break;
         }
         
      }


I would like to attach an image if necessary, but I do not know how to upload a local image file.


KIMDOOPAL
 
Posts: 10
Joined: Thu Mar 16, 2023 4:41 am

Mon Mar 20, 2023 11:37 am

Hi,

Thanks for your feedback.
By calling grid. draw (PdfPageBase, PointF), a PdfLayoutResult object will be returned. This object has many attributes, such as the location of the drawing area, the page on which it is located, and so on. You can try to save this object using variables. When drawing tables in the future, you can determine the drawing location based on the location attribute of this object. I have attached a simple project for your reference and hope it can meet your needs.
Code: Select all
       //create a pdf document
        PdfDocument doc = new PdfDocument();
        PdfPageBase page = doc.getPages().add();

        //create a PdfGrid object
        PdfGrid grid = new PdfGrid();

        //set the cell padding, font, text brush, background brush of the grid
        grid.getStyle().setCellPadding(new PdfPaddings(3,3,3,3));
        grid.getStyle().setFont(new PdfTrueTypeFont(new Font("Arial Unicode MS", Font.PLAIN,10), true));
        grid.getStyle().setTextBrush(PdfBrushes.getBlack());
        grid.getStyle().setBackgroundBrush(PdfBrushes.getLightGray());

        //create a PdfBorders object
        PdfBorders borders= new PdfBorders();
        borders.setAll(new PdfPen(PdfBrushes.getWhite(),1f));

        //define sample data
        String[] data = {"Continent;Country;Population;Ratio to World Pop;Flag",
                "Asia;China;1,391,190,000;18.2%; ",
                "Asia;Japan;126,490,000;1.66%; ",
                "Europe;United Kingdom;65,648,054;0.86%; ",
                "Europe;Germany;82,665,600;1.08%; ",
                "North America; Canada; 37,119,000; 0.49%; ",
                "North America; United States; 327,216,000; 4.29%; ",
                "Continent;Country;Population;Ratio to World Pop;Flag",
                "Asia;China;1,391,190,000;18.2%; ",
                "Asia;Japan;126,490,000;1.66%; ",
                "Europe;United Kingdom;65,648,054;0.86%; ",
                "Europe;Germany;82,665,600;1.08%; ",
                "North America; Canada; 37,119,000; 0.49%; ",
                "North America; United States; 327,216,000; 4.29%; ",
                "Continent;Country;Population;Ratio to World Pop;Flag",
                "Asia;China;1,391,190,000;18.2%; ",
                "Asia;Japan;126,490,000;1.66%; ",
                "Europe;United Kingdom;65,648,054;0.86%; ",
                "Europe;Germany;82,665,600;1.08%; ",
                "North America; Canada; 37,119,000; 0.49%; ",
                "North America; United States; 327,216,000; 4.29%; ",
                "Continent;Country;Population;Ratio to World Pop;Flag",
                "Asia;China;1,391,190,000;18.2%; ",
                "Asia;Japan;126,490,000;1.66%; ",
                "Europe;United Kingdom;65,648,054;0.86%; ",
                "Europe;Germany;82,665,600;1.08%; ",
                "North America; Canada; 37,119,000; 0.49%; ",
                "North America; United States; 327,216,000; 4.29%; "
        };
        String[][] dataSource = new String[data.length][];
        for (int i = 0; i < data.length; i++) {
            dataSource[i] = data[i].split("[;]", -1);
        }

        //fill the grid with data
        grid.setDataSource(dataSource);




        //set the width of the last column
        grid.getColumns().get(grid.getColumns().getCount()-1).setWidth(60f);

        //vertically span cells
        grid.getRows().get(1).getCells().get(0).setRowSpan(2);
        grid.getRows().get(3).getCells().get(0).setRowSpan(2);
        grid.getRows().get(5).getCells().get(0).setRowSpan(2);

        for (int i = 0; i < data.length ; i++) {

            //set the height of each row
            grid.getRows().get(i).setHeight(30f);
            //set the background color of the first column
            grid.getRows().get(i).getCells().get(0).getStyle().setBackgroundBrush(PdfBrushes.getDarkGray());
            //set the font of the first column
            grid.getRows().get(i).getCells().get(0).getStyle().setFont(new PdfTrueTypeFont(new Font("Arial",Font.PLAIN,12),true));

            for (int j = 0; j < grid.getColumns().getCount(); j++) {

                //apply border style to all cells
                grid.getRows().get(i).getCells().get(j).getStyle().setBorders(borders);
                //apply text alignment to all cells
                grid.getRows().get(i).getCells().get(j).setStringFormat(new PdfStringFormat(PdfTextAlignment.Center,PdfVerticalAlignment.Middle));
                //set the font of the first row
                grid.getRows().get(0).getCells().get(j).getStyle().setFont(new PdfTrueTypeFont(new Font("Arial",Font.PLAIN,12),true));
                //set the background color of the first row
                grid.getRows().get(0).getCells().get(j).getStyle().setBackgroundBrush(PdfBrushes.getDarkGray());

            }
        }

        //draw grid on the pdf page
        PdfLayoutResult pdfLayoutResult = grid.draw(page, 0, 30);
        page = pdfLayoutResult.getPage();
        double y = pdfLayoutResult.getBounds().getHeight();

        grid = new PdfGrid();

        //set the cell padding, font, text brush, background brush of the grid
        grid.getStyle().setCellPadding(new PdfPaddings(3,3,3,3));
        grid.getStyle().setFont(new PdfTrueTypeFont(new Font("Arial Unicode MS", Font.PLAIN,10), true));
        grid.getStyle().setTextBrush(PdfBrushes.getBlack());
        grid.getStyle().setBackgroundBrush(PdfBrushes.getLightGray());

        //create a PdfBorders object
        borders= new PdfBorders();
        borders.setAll(new PdfPen(PdfBrushes.getWhite(),1f));

        //define sample data
        String[] data1 = {"Continent;Country;Population;Ratio to World Pop;Flag",
                "Asia;China;1,391,190,000;18.2%; ",
                "Asia;Japan;126,490,000;1.66%; ",
                "Europe;United Kingdom;65,648,054;0.86%; ",
                "Europe;Germany;82,665,600;1.08%; ",
                "North America; Canada; 37,119,000; 0.49%; ",
                "North America; United States; 327,216,000; 4.29%; ",
                "Continent;Country;Population;Ratio to World Pop;Flag",
                "Asia;China;1,391,190,000;18.2%; ",
                "Asia;Japan;126,490,000;1.66%; ",
                "Europe;United Kingdom;65,648,054;0.86%; ",
                "Europe;Germany;82,665,600;1.08%; ",
                "North America; Canada; 37,119,000; 0.49%; ",
                "North America; United States; 327,216,000; 4.29%; ",
                "Continent;Country;Population;Ratio to World Pop;Flag",
                "Asia;China;1,391,190,000;18.2%; ",
                "Asia;Japan;126,490,000;1.66%; ",
                "Europe;United Kingdom;65,648,054;0.86%; ",
                "Europe;Germany;82,665,600;1.08%; ",
                "North America; Canada; 37,119,000; 0.49%; ",
                "North America; United States; 327,216,000; 4.29%; ",
                "Continent;Country;Population;Ratio to World Pop;Flag",
                "Asia;China;1,391,190,000;18.2%; ",
                "Asia;Japan;126,490,000;1.66%; ",
                "Europe;United Kingdom;65,648,054;0.86%; ",
                "Europe;Germany;82,665,600;1.08%; ",
                "North America; Canada; 37,119,000; 0.49%; ",
                "North America; United States; 327,216,000; 4.29%; "
        };
        dataSource = new String[data1.length][];
        for (int i = 0; i < data1.length; i++) {
            dataSource[i] = data1[i].split("[;]", -1);
        }

        //fill the grid with data
        grid.setDataSource(dataSource);




        //set the width of the last column
        grid.getColumns().get(grid.getColumns().getCount()-1).setWidth(60f);

        //vertically span cells
        grid.getRows().get(1).getCells().get(0).setRowSpan(2);
        grid.getRows().get(3).getCells().get(0).setRowSpan(2);
        grid.getRows().get(5).getCells().get(0).setRowSpan(2);

        for (int i = 0; i < data1.length ; i++) {

            //set the height of each row
            grid.getRows().get(i).setHeight(30f);
            //set the background color of the first column
            grid.getRows().get(i).getCells().get(0).getStyle().setBackgroundBrush(PdfBrushes.getDarkGray());
            //set the font of the first column
            grid.getRows().get(i).getCells().get(0).getStyle().setFont(new PdfTrueTypeFont(new Font("Arial",Font.PLAIN,12),true));

            for (int j = 0; j < grid.getColumns().getCount(); j++) {

                //apply border style to all cells
                grid.getRows().get(i).getCells().get(j).getStyle().setBorders(borders);
                //apply text alignment to all cells
                grid.getRows().get(i).getCells().get(j).setStringFormat(new PdfStringFormat(PdfTextAlignment.Center,PdfVerticalAlignment.Middle));
                //set the font of the first row
                grid.getRows().get(0).getCells().get(j).getStyle().setFont(new PdfTrueTypeFont(new Font("Arial",Font.PLAIN,12),true));
                //set the background color of the first row
                grid.getRows().get(0).getCells().get(j).getStyle().setBackgroundBrush(PdfBrushes.getDarkGray());

            }
        }

        grid.draw(page, 0, y);



        //save to file
        doc.saveToFile("Grid.pdf");


Sincerely,
Triste
E-iceblue support team
User avatar

Triste.Dai
 
Posts: 1000
Joined: Tue Nov 15, 2022 3:59 am

Thu Mar 23, 2023 5:33 am

Thank You.


Is it possible to export HTML strings to PDF by putting them in a grid instead of using HTMLConverter? Also, I wonder if the page you wrote is automatically added instead of only on page0.
Because I have a lot of data.
If you look at my code above, I am loading a lot of data with Case statements. The order may also change. Therefore, I want all data to be managed in the grid, and the height and page also want to be managed flexibly.

KIMDOOPAL
 
Posts: 10
Joined: Thu Mar 16, 2023 4:41 am

Fri Mar 24, 2023 7:54 am

Hi,

Thanks for your inquiry.
In the code I provided, the table data exceeds one page, and it is automatically drawn to the second page. If you want to continue drawing a table, you can save the PdfLayoutResult object when the first table is drawn. This object has attributes such as the page and height of the previous table. You can continue to dynamically draw based on this attribute without specifying a fixed page or height.
Code: Select all
        PdfLayoutResult layoutResult = grid.draw(page, 0, y);
        for (int i = 0; i < arrExport[i].length(); i++) {
            switch (arrExport[i]){
                case "1":
                    // something to do
                    layoutResult = grid.draw(layoutResult.getPage(), 0, layoutResult.getBounds().getHeight());
                    break;
                case "2":
                    // something to do
                    layoutResult = grid.draw(layoutResult.getPage(), 0, layoutResult.getBounds().getHeight());

                case "3":
                    // something to do
                    layoutResult = grid.draw(layoutResult.getPage(), 0, layoutResult.getBounds().getHeight());
            }
        }


For your question 1, I am not quite understanding your requirement, could you attach some screenshots for our reference? You can compress them and send to us via email (support@e-iceblue.com) or attach them here. Thanks for your assistance.

Best regards,
Triste
E-iceblue support team
User avatar

Triste.Dai
 
Posts: 1000
Joined: Tue Nov 15, 2022 3:59 am

Mon Mar 27, 2023 1:52 am

Hi.


Let me explain the code.
Code: Select all
rs = noteService.getNoteSection(noteSn);
               
               page = doc.getPages().add(PdfPageSize.A4,new PdfMargins(40));
               
               String htmlString = EgovWebUtil.resetXSSMinimum((String)rs.get("cn"));
                
               String outputFile = "C:\\upload\\note\\HtmlToPdf2.pdf";
               String pluginPath = "C:\\upload\\note\\plugins";
               HtmlConverter.setPluginPath(pluginPath);
               HtmlConverter.convert(htmlString, outputFile, true, 100000, new Size(592, 837), new PdfMargins(40), LoadHtmlType.Source_Code);

this is my code
I need to connect with other data.
Code: Select all
grid.draw(page, new Point2D.Float(0, 30));

I want to use this code.

However, the first code is HTMLString code.

That is, you need to save the first code to grid and export it as PDF along with other data.

KIMDOOPAL
 
Posts: 10
Joined: Thu Mar 16, 2023 4:41 am

Mon Mar 27, 2023 6:12 am

Hi,

Thank you for your feedback.
Our Spire.PDF has no direct way to perform this conversion. Since your HTML data is dynamic, I would like to recommend using Spire.Doc to import your HTML data into a Word table. Then, you can add other data to the table and export it to PDF format.
I believe that Spire.Doc is a better option for your needs. If you agree with our suggestion, please provide us with an example of your HTML content, any additional data, and a sample PDF of the desired layout. This will enable us to create a demo for you. You can send them to us via email (support@e-iceblue.com) or compress and attach them here. Thanks for your assistance.

Best regards,
Triste
E-iceblue support team
User avatar

Triste.Dai
 
Posts: 1000
Joined: Tue Nov 15, 2022 3:59 am

Mon Mar 27, 2023 8:14 am

My code is very long. Do I have to start over when I convert from Spire.Pdf to Spire.Doc? Or are the built-in functions the same?

KIMDOOPAL
 
Posts: 10
Joined: Thu Mar 16, 2023 4:41 am

Mon Mar 27, 2023 12:14 pm

Hi,

Thank you for your feedback. Our PDF grid functionality is similar to the table feature in Microsoft Word. Unfortunately, there is no direct code available for converting Html to PDF grid.
To achieve this conversion, we recommend creating a table in a Word document and manually drawing the content of the PDF grid into it.
Alternatively, we appreciate that if you can provide the raw html data to help us test whether it is possible to directly convert HTML to Word.

Best regards,
Triste
E-iceblue support team
User avatar

Triste.Dai
 
Posts: 1000
Joined: Tue Nov 15, 2022 3:59 am

Tue Apr 11, 2023 5:28 am

Hi,

There is a problem with the source code.
This is a problem with HTML To Pdf.

QApplication was not created in the main() thread.

An error message appears.
It seems that the problem comes from not running xvfb properly.
I installed xvfb according to the document and executed the xvfb-run java -jar HTMLToPDF.jar command, but the jar file was not found.
where is this jar file?

KIMDOOPAL
 
Posts: 10
Joined: Thu Mar 16, 2023 4:41 am

Tue Apr 11, 2023 6:08 am

Hi.

Thanks for your inquiry.
Please note that the HTMLToPDF.jar file mentioned in the command is expected to be provided by yourself. Package your Java project into JAR file and replace the HTMLToPDF.jar with the location of your newly built JAR file.
If you have any other questions, just feel free to contact us.

Sincerely,
Triste
E-iceblue support team
User avatar

Triste.Dai
 
Posts: 1000
Joined: Tue Nov 15, 2022 3:59 am

Tue Apr 11, 2023 6:31 am

I can't understand what you are talking about.
If you look at the xvfb part of the URL below, there is an example to run the HTMLToPDF.jar file, but there is no place to download it?

https://www.e-iceblue.com/Tutorials/Java/Spire.PDF-for-Java/Program-Guide/Conversion/Java-convert-HTML-to-PDF.html

KIMDOOPAL
 
Posts: 10
Joined: Thu Mar 16, 2023 4:41 am

Tue Apr 11, 2023 7:34 am

KIMDOOPAL wrote:

The java project is being deployed as a war rather than a jar. Is there any way?

KIMDOOPAL
 
Posts: 10
Joined: Thu Mar 16, 2023 4:41 am

Wed Apr 12, 2023 6:09 am

Hi,

Thanks for your inquiry.
Please deploy the war package to a servlet container such as Tomcat and then using the following command to start the container with xvfb:
Code: Select all
xvfb-run ./xxxx/tomcat/bin/startup.sh

Please ensure that the path is modified according to your specific system configuration.
We hope this solution will help resolve your issue. If you have any further questions, please don't hesitate to ask.

Best regards,
Triste
E-iceblue support team
User avatar

Triste.Dai
 
Posts: 1000
Joined: Tue Nov 15, 2022 3:59 am

Return to Spire.PDF