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;
}