We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Converted PDF tables to have editable input fields

Hi team,

For my requirement, I need to convert an excel to a pdf
Let’s assume the excel to be like -
| Col A | Col B | Col C |
| data | data | data |

so 2 rows and 3 columns in the excel.

Now after conversion, I want a pdf that has a table like this -
| Col A | Col B | Col C | User Input Col X | User Input Col Z |
| data | data | data | | |

The are TextBoxField elements from your API.
Please help how to achieve this.

Thank you!

Regards
Shaurya

@shaurya.chawla,

Thanks for the details.

Aspose.Cells follows MS Excel standards and specifications when rendering Excel spreadsheet to PDF. The output PDF (from Excel file) would be same as you do the conversion in MS Excel manually.

How could you perform the task in MS Excel manually? Unless you add textboxes controls to the cells to fill data, you cannot achieve this. You can add text box fields to the worksheet cells first accordingly for your scenario/case, see the document on how to add TextBox shapes to the worksheet for your reference. Then, you may convert it to PDF via Aspose.Cells APIs. This way, you will get editable fields (textboxes) in the output PDF.

In case, you still could not implement it or have some doubts, kindly do share your input Excel file and desired output PDF file for reference. We will check it on how to do it via Aspose.Cells APIs.

Thanks for your reply!
I get what you mean. For further clarification though - please let me know if the following is possible -
Let’s take a scenario where the input is an excel file and the output has to be a pdf. ( I am not able to control the input)

  1. Is it not possible to add these textbox fields to the excel after I receive the input?
  2. Another way could be to add more columns to the generated table once the conversion is completed to pdf.

Please let me know.

Thanks!

@shaurya.chawla,

Please see the suggested document (with example code) on how to add text boxes to the Excel worksheet for your reference.

Yes, this can be done also. You need to use Aspose.PDF to manipulate the output PDF generated by Aspose.Cells from source Excel spreadsheet. Please post your queries in Aspose.PDF forum on how to add more columns/fields to the PDF file and one of our fellow colleagues will assist you soon there.

I meant to say - is it possible to add fillable fields to the input excel through aspose and then convert it to pdf so that pdf is also fillable?

@shaurya.chawla,

Please see previous reply:

You can add text box fields to the worksheet cells first accordingly for your scenario/case, see the document on how to add TextBox shapes to the worksheet for your reference. Then, you may convert it to PDF via Aspose.Cells APIs.

There is no API ref in the provided document, can you please check that?

@shaurya.chawla,

Did you not see the example code on the document? Please note the example code is just for your reference and you have to write your own code accordingly to add text boxes to the worksheet.

Honestly, all I see is images explaining how to add textbox in excel. I don’t see any code examples in the provided link.
image.png (41.1 KB)

@shaurya.chawla,

Thanks for the screenshot.

The code is coming from Github repos., so you might have some restrictions to access it in your environment. You may ask relevant authorities to fix it. Anyways, I will paste the Raw sample code here.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET

// The path to the documents directory.
string dataDir = "";
if (!System.IO.Directory.Exists(dataDir))
{
    System.IO.Directory.CreateDirectory(dataDir);
}

// Open an Excel file.
//Workbook workbook = new Workbook(dataDir+ "Book_SourceData.xlsx");//If you want to insert a text box in an existing file, use this code.

// Create an object of the Workbook class
Workbook workbook = new Workbook();

// Access first worksheet from the collection
Worksheet sheet = workbook.Worksheets[0];

// Add the TextBox to the worksheet
sheet.TextBoxes.Add(6, 10, 100, 200);

//Save.You can check your text box in this way.
workbook.Save("result.xlsx", SaveFormat.Xlsx);

Hi team,

Thanks for your patience and replies.
arena.zip (103.6 KB)
are the files I used.

I tried to add textbox in the last column of the excel sheet. I wanted to make each textbox the size of the left one for each row. I used the following code -

public static void convertToPdf(ByteArrayInputStream excelInputStream) throws Exception {
        ByteArrayOutputStream outputStreamPdf = new ByteArrayOutputStream();
        try {
            log.info("Initiating Excel to pdf conversion");
            Workbook workbook = new Workbook(excelInputStream);

            // Expanding columns before merging
            WorksheetCollection worksheets = workbook.getWorksheets();
            int workSheetCount = worksheets.getCount();
            for (int sheetNo = 0; sheetNo < workSheetCount; sheetNo++) {
                Worksheet sheet = worksheets.get(sheetNo);

                var lastRow = sheet.getCells().getMaxDataRow();
                var lastCol = sheet.getCells().getMaxDataColumn();

                Cells cells = sheet.getCells();

                for(int i=7;i<=lastRow;i++) {
                    Cell cell = cells.get(i, lastCol);
                    Cell leftCell = cells.get(i, lastCol-1);
                    System.out.println("adding textbox at (" + i + ", " + lastCol + ")");
                    System.out.println("height: " + leftCell.getHeightOfValue() + " width: " + leftCell.getWidthOfValue());
                    int textBoxIndex = sheet.getTextBoxes().add(i, lastCol, leftCell.getHeightOfValue(), leftCell.getWidthOfValue());
                    TextBox textbox = sheet.getTextBoxes().get(textBoxIndex);
                    textbox.setText("Row: " + i + " Col: " + lastCol);
                    textbox.getTextBody().getTextAlignment().setAutoSize(true);
                    textbox.setPlacement(PlacementType.FREE_FLOATING);
                    textbox.getFont().setColor(Color.getBlue());
                    FillFormat fillFormat = textbox.getFill();
                    fillFormat.getSolidFill().setColor(Color.getAquamarine());
                }
                
                // cosmetics
                PageSetup pageSetup = sheet.getPageSetup();
                pageSetup.setPrintArea("");
                pageSetup.setPrintGridlines(true);
                sheet.autoFitColumns();
            }

            PdfSaveOptions saveOptions = new PdfSaveOptions();
            saveOptions.setOnePagePerSheet(true);
            saveOptions.setAllColumnsInOnePagePerSheet(true);
            workbook.save("C:/Users/shauryac/arena/output.pdf", saveOptions);

            log.info("Conversion from Excel to pdf completed");


        } catch (Exception ex) {
            log.info("Exception occurred while converting Excel to pdf : ", ex);
            throw ex;
        } finally {
            outputStreamPdf.close();
        }
    }

There are some problems that I now see -

  1. There are new columns added to the right.
  2. The textboxes added are at least 2-3 columns right of where I want them to be.
  3. In the final converted pdf file, I’m unable to fill the added textboxes.

The requirement is to have a fillable pdf in the end.

Please help

Thanks,
Shaurya

@shaurya.chawla,

You mean you cannot add/update text to the textboxes you added via Aspose.Cells in Adobe Acrobat? If true, I am afraid, in that case, you have to use Aspose.PDF to add those text fields in the PDF (generated by Aspose.Cells) and save the final PDF document. And, you do not need to add text boxes to Excel spreadsheet via Aspose.Cells API.

You got that exactly right.
In that case, please help me out with the PDF API…
I will need to identify the table row and column and insert a textboxfield in there.
How to do that?

@shaurya.chawla,

I checked by adding some textbox controls to an Excel file and then save it as “PDF” via MS Excel manually. I found in the output PDF the text fields are not fillable either. So, Aspose.Cells works the same as MS Excel regarding PDF generation. I cannot help you much regarding Aspose.PDF API. Since you have already added a thread in Aspose.PDF forum, so kindly follow up that thread to get updates and more help from Aspose.PDF team to sort out your issue there.