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 Nov 12, 2020 10:33 am

I want do crate a pdfgrid with 3 rows and 3 cols. In each cell place than an image with different alignment:
Row:0 Col:0 --> top;left Row:0 Col:1 --> top;center Row:0 Col:2 --> top;right
Row:1 Col:0 --> middle;left Row:1 Col:1 --> middle;center Row:1 Col:2 --> middle;right
Row:2 Col:0 --> bottom;left Row:2 Col:1 --> bottom;center; Row:2 Col:2 --> bottom;right

The image has do be inserted into the cell with this:
Code: Select all
   private void setCellContent(PdfGridRow gridRow, int cellIndex, String imagePath, String alignment) {
      PdfImage pdfImage = PdfImage.fromFile(imagePath);   
      PdfStringFormat pdfStringFormat = getAlignments(alignment);   
      PdfGridCellContentList lst = new PdfGridCellContentList();
      PdfGridCellContent textAndStyle = new PdfGridCellContent();
      textAndStyle.setImage(pdfImage);
      textAndStyle.setImageSize(new Dimension((int)20, (int)20));
      textAndStyle.setStringFormat(pdfStringFormat);
      lst.getList().add(textAndStyle);
      PdfGridCell cell = gridRow.getCells().get(cellIndex);      
      cell.setValue(lst);
   }

What I now expect, is that the image in each cell is alignment, as descript above:
2020-11-12_11-09-06.png


But what I get, is the images has do be aligned only horizontal. vertical the image is always aligned top:
2020-11-12_11-04-33.png


Now my question:
Is the vertical alignment of an image into a pdfgrid-cell not possible with PdfStringFormat? But if not, why the horizontal alignment is working as expected?

I create the expected table as follows: in this case, I set a separate imageLocation.
Code: Select all
   private void setCellContentV2(PdfGridRow gridRow, int cellIndex, String imagePath, String alignment) {
      PdfImage pdfImage = PdfImage.fromFile(imagePath);   
      PdfStringFormat pdfStringFormat = getAlignments(alignment);
      float height = 20;
      float width = 20;   
      float gridRowHeight = gridRow.getHeight();
      float imagePositionY = 0;
      if(alignment.equals("bottom") || alignment.endsWith(";bottom")) {
         imagePositionY = gridRowHeight - height;
      }
      else if(alignment.endsWith(";center")) {
         imagePositionY = (gridRowHeight - height) / 2;
      }
      Point2D imageLocation = new Point2D.Double(0,imagePositionY);   
      PdfGridCellContentList lst = new PdfGridCellContentList();
      PdfGridCellContent textAndStyle = new PdfGridCellContent();
      textAndStyle.setImage(pdfImage);
      textAndStyle.setImageSize(new Dimension((int)width, (int)height));
      textAndStyle.setImageLocation(imageLocation);   
      textAndStyle.setStringFormat(pdfStringFormat);
      lst.getList().add(textAndStyle);      
      PdfGridCell cell = gridRow.getCells().get(cellIndex);      
      cell.setValue(lst);
   }


The complete class to reporduce this problem ist here:
Code: Select all
import java.awt.Dimension;
import java.awt.geom.Dimension2D;
import java.awt.geom.Point2D;
import java.net.URL;

import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.PdfPageOrientation;
import com.spire.pdf.PdfPageSettings;
import com.spire.pdf.PdfPageSize;
import com.spire.pdf.PdfVersion;
import com.spire.pdf.graphics.PdfImage;
import com.spire.pdf.graphics.PdfMargins;
import com.spire.pdf.graphics.PdfStringFormat;
import com.spire.pdf.graphics.PdfSubSuperScript;
import com.spire.pdf.graphics.PdfTextAlignment;
import com.spire.pdf.graphics.PdfVerticalAlignment;
import com.spire.pdf.grid.PdfGrid;
import com.spire.pdf.grid.PdfGridCell;
import com.spire.pdf.grid.PdfGridCellContent;
import com.spire.pdf.grid.PdfGridCellContentList;
import com.spire.pdf.grid.PdfGridRow;


public class MyPdfTest {

   public void createPdfGrid() {
      PdfDocument pdfDocument = new PdfDocument();
      pdfDocument.getFileInfo().setVersion(PdfVersion.Version_1_7);   
      Dimension2D pageSize = new Dimension((int)PdfPageSize.A4.getHeight(), (int)PdfPageSize.A4.getWidth());
      PdfPageSettings pageSettings = new PdfPageSettings(pageSize, PdfPageOrientation.Landscape);
      pdfDocument.setPageSettings(pageSettings);
      PdfMargins pageMargins = new PdfMargins();
      pageMargins.setLeft(20);
      pageMargins.setTop(40);
      pageMargins.setRight(20);
      pageMargins.setBottom(0);   
      PdfPageBase page = pdfDocument.getPages().add(pageSize, pageMargins);   
      PdfGrid pdfGrid = new PdfGrid();
      int cols = 3;
      int rows = 3;   
      // create the grid with rows and cols
      pdfGrid.getColumns().add(cols);
      
      for (int i = 0; i < rows; i++) {
         PdfGridRow pdfGridRow = pdfGrid.getRows().add();
         pdfGridRow.setHeight(40);
      }
      
      for (int i = 0; i < cols; i++) {
         pdfGrid.getColumns().get(i).setWidth(40);
      }
      
      URL resource = getClass().getClassLoader().getResource("face-cool.png");
      String imagePath = resource.getPath();
      String alignmentString = "";
      alignmentString = "left;top";
      setCellContentV2(pdfGrid.getRows().get(0), 0, imagePath, alignmentString);
      
      alignmentString = "center;top";
      setCellContentV2(pdfGrid.getRows().get(0), 1, imagePath, alignmentString);
      
      alignmentString = "right;top";
      setCellContentV2(pdfGrid.getRows().get(0), 2, imagePath, alignmentString);

      alignmentString = "left;center";
      setCellContentV2(pdfGrid.getRows().get(1), 0, imagePath, alignmentString);
      
      alignmentString = "center;center";
      setCellContentV2(pdfGrid.getRows().get(1), 1, imagePath, alignmentString);
      
      alignmentString = "right;center";
      setCellContentV2(pdfGrid.getRows().get(1), 2, imagePath, alignmentString);
      
      alignmentString = "left;bottom";
      setCellContentV2(pdfGrid.getRows().get(2), 0, imagePath, alignmentString);
      
      alignmentString = "center;bottom";
      setCellContentV2(pdfGrid.getRows().get(2), 1, imagePath, alignmentString);
      
      alignmentString = "right;bottom";
      setCellContentV2(pdfGrid.getRows().get(2), 2, imagePath, alignmentString);
      
      pdfGrid.draw(page, 0, 0);
      pdfDocument.saveToFile("d:\\test.pdf");
      
   }
   
   private void setCellContent(PdfGridRow gridRow, int cellIndex, String imagePath, String alignment) {
      PdfImage pdfImage = PdfImage.fromFile(imagePath);
      PdfStringFormat pdfStringFormat = getAlignments(alignment);   
      PdfGridCellContentList lst = new PdfGridCellContentList();
      PdfGridCellContent textAndStyle = new PdfGridCellContent();
      textAndStyle.setImage(pdfImage);
      textAndStyle.setImageSize(new Dimension((int)20, (int)20));
      textAndStyle.setStringFormat(pdfStringFormat);
      lst.getList().add(textAndStyle);
      PdfGridCell cell = gridRow.getCells().get(cellIndex);      
      cell.setValue(lst);
   }
   
   private void setCellContentV2(PdfGridRow gridRow, int cellIndex, String imagePath, String alignment) {
      PdfImage pdfImage = PdfImage.fromFile(imagePath);
      PdfStringFormat pdfStringFormat = getAlignments(alignment);
      float height = 20;
      float width = 20;
      float gridRowHeight = gridRow.getHeight();
      float imagePositionY = 0;      
      if(alignment.equals("bottom") || alignment.endsWith(";bottom")) {
         imagePositionY = gridRowHeight - height;
      }
      else if(alignment.endsWith(";center")) {
         imagePositionY = (gridRowHeight - height) / 2;
      }
      Point2D imageLocation = new Point2D.Double(0,imagePositionY);   
      PdfGridCellContentList lst = new PdfGridCellContentList();
      PdfGridCellContent textAndStyle = new PdfGridCellContent();
      textAndStyle.setImage(pdfImage);
      textAndStyle.setImageSize(new Dimension((int)width, (int)height));
      textAndStyle.setImageLocation(imageLocation);   
      textAndStyle.setStringFormat(pdfStringFormat);
      lst.getList().add(textAndStyle);      
      PdfGridCell cell = gridRow.getCells().get(cellIndex);      
      cell.setValue(lst);
   }
   
   public static PdfStringFormat getAlignments(String alignment) {
      PdfTextAlignment pdfHorizontalAlignment = null;
      PdfVerticalAlignment pdfVerticalAlignment = null;
      PdfStringFormat pdfStringFormat = null;
      if(alignment == null || alignment.isEmpty()) {
         pdfHorizontalAlignment = getHorizonztalTextAlignment("");
         pdfVerticalAlignment = getVerticalTextAlignment("");
         pdfStringFormat = new PdfStringFormat(pdfHorizontalAlignment, pdfVerticalAlignment);   
         return pdfStringFormat;
      }
      
      String[] splitts;
      if(!alignment.contains(";"))
         splitts = new String[] {alignment};
      else
         splitts = alignment.split(";");
      
      if(splitts.length == 1) {
         switch (splitts[0]) {
         case "left":
         case "center":   
         case "right":
            pdfHorizontalAlignment = getHorizonztalTextAlignment(splitts[0]);
            break;
         case "top":
         case "bottom":
            pdfVerticalAlignment = getVerticalTextAlignment(splitts[0]);
            break;
         }
      }
      else if(splitts.length == 2) {
         pdfHorizontalAlignment = getHorizonztalTextAlignment(splitts[0]);
         pdfVerticalAlignment = getVerticalTextAlignment(splitts[1]);
      }   
      if(pdfHorizontalAlignment == null)
         pdfHorizontalAlignment = getHorizonztalTextAlignment("");
      if(pdfVerticalAlignment == null)
         pdfVerticalAlignment = getVerticalTextAlignment("");
      
      pdfStringFormat = new PdfStringFormat(pdfHorizontalAlignment, pdfVerticalAlignment);                  
      return pdfStringFormat;
   }
   
   private static PdfVerticalAlignment getVerticalTextAlignment(String alignment) {
      if("top".equals(alignment))
         return PdfVerticalAlignment.Top;
      if("bottom".equals(alignment))
         return PdfVerticalAlignment.Bottom;
      if("center".equals(alignment))
         return PdfVerticalAlignment.Middle;
      return PdfVerticalAlignment.Top;
   }
   
   private static PdfTextAlignment getHorizonztalTextAlignment(String alignment) {
      if("right".equals(alignment))
         return PdfTextAlignment.Right;
      if("left".equals(alignment))
         return PdfTextAlignment.Left;
      if("center".equals(alignment))
         return PdfTextAlignment.Center;
      return PdfTextAlignment.Left;
   }   
}

ep_pfa_12
 
Posts: 6
Joined: Thu Nov 12, 2020 9:49 am

Fri Nov 13, 2020 8:37 am

Hello,

Thanks for your inquiry.
I did an initial test and did notice that the vertical alignment is not working properly. I have posted this issue to our Dev team with the ticket SPIREPDF-3772. If there is any update, we will let you know.
Besides, there is a workaround for you, you can use nested grid to insert pictures like below.
Code: Select all
    private static void setCellContentV2(PdfGridRow gridRow, int cellIndex, String imagePath, String alignment) {
        PdfImage pdfImage = PdfImage.fromFile(imagePath);
        PdfStringFormat pdfStringFormat = getAlignments(alignment);
        PdfGridCellContentList lst = new PdfGridCellContentList();
        PdfGridCellContent textAndStyle = new PdfGridCellContent();
        textAndStyle.setImage(pdfImage);
        textAndStyle.setImageSize(new Dimension((int) 20, (int) 20));

        lst.getList().add(textAndStyle);

        PdfGrid embedGrid = new PdfGrid();
        embedGrid.getRows().add();
        embedGrid.getColumns().add(1);

        embedGrid.getColumns().get(0).setWidth(20);
        embedGrid.getRows().get(0).setHeight(20);
        PdfGridCell embedGridCell = embedGrid.getRows().get(0).getCells().get(0);
        embedGridCell.setValue(lst);
        embedGridCell.getStyle().getBorders().setAll(new PdfPen(new PdfRGBColor()));

        PdfGridCell cell = gridRow.getCells().get(cellIndex);
        cell.setStringFormat(pdfStringFormat);
        cell.setValue(embedGrid);
    }


Sincerely,
Brian
E-iceblue support team
User avatar

Brian.Li
 
Posts: 1271
Joined: Mon Oct 19, 2020 3:04 am

Tue Nov 17, 2020 9:03 am

Thank you for your information. I wait till the alignment is working properly.

ep_pfa_12
 
Posts: 6
Joined: Thu Nov 12, 2020 9:49 am

Tue Nov 17, 2020 10:12 am

Ok, we will keep you informed if there is any progress

Sincerely,
Brian
E-iceblue support team
User avatar

Brian.Li
 
Posts: 1271
Joined: Mon Oct 19, 2020 3:04 am

Fri Nov 27, 2020 9:22 am

Hello,

Glad to inform you that we just released Spire.PDF for Java Version:3.11.6 which fixes your issue, please download it from the following links to test on your side. Look forward to your testing result.
Website link: https://www.e-iceblue.com/Download/pdf-for-java.html
Maven link: http://repo.e-iceblue.com/nexus/content ... df/3.11.6/

Sincerely,
Brian
E-iceblue support team
User avatar

Brian.Li
 
Posts: 1271
Joined: Mon Oct 19, 2020 3:04 am

Fri Dec 11, 2020 7:48 am

Thank you for your replay.
I have download the lastet jar from maven (spre.pdf 3.11.6.jar) and tryed them again.
The image in center cell looks as well not crorrect in center.
2020-12-11_08-32-35.png

See in center cell, the left and rigth distance in this cell is different. As well the top and bottom distance.
The icon is a 24 x 24-pixel- image, so its is square. that shouldn,t be so difficult to align this in the center, when i want (But this is my mind).

Can you please check this case agin?

Best regards
Ep

ep_pfa_12
 
Posts: 6
Joined: Thu Nov 12, 2020 9:49 am

Fri Dec 11, 2020 10:13 am

Hello,

Thanks for your feedback.
I did an initial test and did notice the issue you mentioned. I have posted it to our Dev team with the ticket SPIREPDF-3876. If there is any update, we will let you know.
Besides, to ensure that our Spire.PDF will work perfectly for you after this issue is fixed, please provide us with your image file to help us test and verify. Thanks in advance for your assistance.

Sincerely,
Brian
E-iceblue support team
User avatar

Brian.Li
 
Posts: 1271
Joined: Mon Oct 19, 2020 3:04 am

Fri Jan 15, 2021 10:31 am

Hello,

Hope you are doing well.
Glad to inform you that we just released Spire.PDF for Java Version:4.1.2 which fixes your issue, please download it from the following links to test on your side. Looking forward to your testing result.
Website link: https://www.e-iceblue.com/Download/pdf-for-java.html
Maven link: http://repo.e-iceblue.com/nexus/content ... pdf/4.1.2/

Sincerely,
Brian
E-iceblue support team
User avatar

Brian.Li
 
Posts: 1271
Joined: Mon Oct 19, 2020 3:04 am

Return to Spire.PDF

cron