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

Free Support Forum - aspose.com

Table and Text not rendered during Aspose PDF to HTML

I am creating a new PDF Document and inserting an Image followed by some text and a table.

I am then appending the above mentioned document with an existing document.


When converting the appended document to HTML, the text and table of the first document are not being rendered.

I am inserting my code for reference :

public Document insertAdditionalData(Document documentObj, JsonArray additionalDataJSON, File logoFile) throws FileNotFoundException, IOException
{
if (additionalDataJSON.size()==0)
return documentObj;
MarginInfo marginInfo = new MarginInfo();
Table table = new Table();
Gson gson = new Gson();
double textStartY, distanceBetweenImageAndText = 80.0;
double upperMargin = 20.0, leftMargin = 70.0;
Document summaryPdfDoc = new Document();
PageCollection pageCollection = summaryPdfDoc.getPages();
double pageWidth = summaryPdfDoc.getPageInfo().getWidth();
double pageHeight = summaryPdfDoc.getPageInfo().getHeight();
pageCollection.add();
//Company Logo Insertion Logic
if (logoFile!=null)
{
BufferedImage bufferedImage = ImageIO.read(logoFile);
double imageWidth = bufferedImage.getWidth();
double imageHeight = bufferedImage.getHeight();
double aspectRatio = imageWidth/imageHeight;
Page page = pageCollection.get_Item(1);
page.getResources().getImages().add(bufferedImage);
page.getContents().add(new Operator.GSave());
imageWidth = MiscUtil.adjustImageWidth(imageWidth, pageWidth);
imageHeight = MiscUtil.adjustImageHeight(imageWidth, imageHeight, aspectRatio);
textStartY = imageHeight + upperMargin + distanceBetweenImageAndText;
Matrix matrix = new Matrix(new double[] { imageWidth, 0, 0,
imageHeight, leftMargin, pageHeight - (imageHeight+upperMargin) });

// Using ConcatenateMatrix (concatenate matrix) operator: defines how image must be placed
page.getContents().add(new Operator.ConcatenateMatrix(matrix));
XImage ximage = page.getResources().getImages().get_Item(page.getResources().getImages().size());

// Using Do operator: this operator draws image
page.getContents().add(new Operator.Do(ximage.getName()));

// Using GRestore operator: this operator restores graphics state
page.getContents().add(new Operator.GRestore());
}
else
{
textStartY = distanceBetweenImageAndText + upperMargin;
}
//Text Insertion
TextStamp textStamp = new TextStamp(“Candidate Summary”);
//set properties of the stamp
textStamp.setTopMargin(textStartY);
textStamp.setLeftMargin(leftMargin);
textStamp.setHorizontalAlignment(HorizontalAlignment.Left);
textStamp.setVerticalAlignment(VerticalAlignment.Top);
//set text properties
textStamp.getTextState().setFontSize(12);
textStamp.getTextState().setFontStyle(FontStyles.Bold);
pageCollection.get_Item(1).addStamp(textStamp);
//Starting with Table Insertion
// Set default cell border using BorderInfo object
table.setBorder(new BorderInfo(BorderSide.All, .1f));
table.setAlignment(HorizontalAlignment.Center);
table.setDefaultCellBorder(new BorderInfo(BorderSide.All, .1f));
table.setDefaultCellPadding(new MarginInfo(10, 5, 0, 5));
table.setColumnWidths(“200 250”);
marginInfo.setTop(textStartY+20);
table.setMargin(marginInfo);
marginInfo.setBottom(100);
marginInfo.setTop(100);
summaryPdfDoc.getPageInfo().setMargin(marginInfo);
// set the border for table cells
//table.setDefaultCellBorder(new BorderInfo(BorderSide.All, .5f));
Iterator jsonArrayIterator = additionalDataJSON.iterator();
while (jsonArrayIterator.hasNext())
{
Row row = table.getRows().add();
JsonElement je = jsonArrayIterator.next();
NameAndValuePair pair = gson.fromJson(je, NameAndValuePair.class);
row.getCells().add(pair.getName());
row.getCells().add(pair.getValue());
}
pageCollection.get_Item(1).getParagraphs().add(table);
Iterator it = documentObj.getPages().iterator();
while (it.hasNext())
{
summaryPdfDoc.getPages().add(it.next());
}
return summaryPdfDoc;
}

Hello Rahul,


Thanks for contacting support.

I have tried to test the scenario using your code snippet but there were some undefined objects/classes (i.e MiscUtil) in the code and I could not execute it. Somehow I have managed to test the similar scenario by following code snippet and was unable to notice the rendering issue.

public void TableAndText()
{
try {
Document doc = insertAdditionalData(new Document(dataDir + “Sample PDF for Aspose.pdf”), new File(dataDir + “aspose.jpg”));
doc.save(dataDir + “Experiment.pdf”);
doc.save(dataDir + “Experiment.html”, new HtmlSaveOptions());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public Document insertAdditionalData(Document documentObj, File logoFile) throws FileNotFoundException, IOException
{
MarginInfo marginInfo = new MarginInfo();
Table table = new Table();
double textStartY, distanceBetweenImageAndText = 80.0;
double upperMargin = 20.0, leftMargin = 70.0;
Document summaryPdfDoc = new Document();
PageCollection pageCollection = summaryPdfDoc.getPages();
double pageWidth = summaryPdfDoc.getPageInfo().getWidth();
double pageHeight = summaryPdfDoc.getPageInfo().getHeight();
pageCollection.add();
//Company Logo Insertion Logic
if (logoFile!=null)
{
BufferedImage bufferedImage = ImageIO.read(logoFile);
double imageWidth = bufferedImage.getWidth();
double imageHeight = bufferedImage.getHeight();
double aspectRatio = imageWidth/imageHeight;
Page page = pageCollection.get_Item(1);
page.getResources().getImages().add(bufferedImage);
page.getContents().add(new Operator.GSave());
textStartY = imageHeight + upperMargin + distanceBetweenImageAndText;
Matrix matrix = new Matrix(new double[] { imageWidth, 0, 0,
imageHeight, leftMargin, pageHeight - (imageHeight+upperMargin) });

// Using ConcatenateMatrix (concatenate matrix) operator: defines how image must be placed
page.getContents().add(new Operator.ConcatenateMatrix(matrix));
XImage ximage = page.getResources().getImages().get_Item(page.getResources().getImages().size());

// Using Do operator: this operator draws image
page.getContents().add(new Operator.Do(ximage.getName()));

// Using GRestore operator: this operator restores graphics state
page.getContents().add(new Operator.GRestore());
}
else
{
textStartY = distanceBetweenImageAndText + upperMargin;
}
//Text Insertion
TextStamp textStamp = new TextStamp(“Candidate Summary”);
//set properties of the stamp
textStamp.setTopMargin(textStartY);
textStamp.setLeftMargin(leftMargin);
textStamp.setHorizontalAlignment(HorizontalAlignment.Left);
textStamp.setVerticalAlignment(VerticalAlignment.Top);
//set text properties
textStamp.getTextState().setFontSize(12);
textStamp.getTextState().setFontStyle(FontStyles.Bold);
pageCollection.get_Item(1).addStamp(textStamp);
//Starting with Table Insertion
// Set default cell border using BorderInfo object
table.setBorder(new BorderInfo(BorderSide.All, .1f));
table.setAlignment(HorizontalAlignment.Center);
table.setDefaultCellBorder(new BorderInfo(BorderSide.All, .1f));
table.setDefaultCellPadding(new MarginInfo(10, 5, 0, 5));
table.setColumnWidths(“200 250”);
marginInfo.setTop(textStartY+20);
table.setMargin(marginInfo);
marginInfo.setBottom(100);
marginInfo.setTop(100);
summaryPdfDoc.getPageInfo().setMargin(marginInfo);
// set the border for table cells
//table.setDefaultCellBorder(new BorderInfo(BorderSide.All, .5f));
int i = 0;
while (i < 6)
{
Row row = table.getRows().add();
row.getCells().add("Cell " + i);
row.getCells().add("Cell 2 " + i);
i++;
}
pageCollection.get_Item(1).getParagraphs().add(table);
summaryPdfDoc.getPages().add(documentObj.getPages());
return summaryPdfDoc;
}

We will really appreciate if you please provide working code snippet which demonstrates complete routine along with sample input document, so that we can try to replicate the issue in our environment. Also, sharing environment information like OS Version, JDK Version, API Version, etc, would help us to test the scenario accordingly.


PS: I have tested the scenario using Aspose.Pdf for Java 17.4.