Hi Aspose Pdf Team, Little urgent can you please do needful.
Would you kindly share the sample image that you are adding in the PDF along with respective input PDF document? It looks like you have attached the generated output only.
Hi @asad.ali I have enclosed the input file, output file and logo image. Below is the aspose pdf code sample, which reads input pdf file and add Logo, report name and execution date. As explained earlier, my concerns are,
- reduce gap b/w image, report name and execution date. should be side by side
- “Execution Of Date” should be in single line, shouldn’t wrap into next line.
setAsposePdfLicense();
//setPageMargins(pdfFilePath);
Document pdfDocument = new Document(pdfFilePath);
Page page = pdfDocument.getPages().get_Item(1);
Table table = new Table();
//table.setColumnAdjustment(ColumnAdjustment.AutoFitToWindow);
// Add an image to the header
Image image = new Image();
image.setImageStream(Thread.currentThread().getContextClassLoader()
.getResourceAsStream("images/".concat(PdfOutputConstants.SPLASH_ICON_IMAGE_FILE_NAME)));
image.setFixWidth(20); // Set the image width
image.setFixHeight(20); // Set the image height
Row imageRow = table.getRows().add();
Row hrRow = table.getRows().add();
Row executedOn = table.getRows().add();
Cell imageCell = imageRow.getCells().add();
imageCell.setRowSpan(3);
imageCell.setAlignment(HorizontalAlignment.Left);
imageCell.getParagraphs().add(image);
imageCell.setWidth(50);
Cell titleCell = imageRow.getCells().add();
titleCell.getParagraphs().add(new TextFragment(replaceCharactersNotSupportedByFontFile(title, " ", font)));
titleCell.setAlignment(HorizontalAlignment.Left);
titleCell.setWidth(300);
TextState tState = titleCell.getDefaultCellTextState();
tState.setForegroundColor(Color.getBlue());
tState.setFont(font);
tState.setFontSize(6);
Cell hrCell = hrRow.getCells().add();
hrCell.setAlignment(HorizontalAlignment.Left);
hrCell.setWidth(300);
hrCell.getParagraphs().add(new HtmlFragment("<HR>"));
Cell executedOncell = executedOn.getCells().add();
executedOncell.setAlignment(HorizontalAlignment.Left);
executedOncell.getParagraphs().add(new TextFragment("Report Executed On : " + requestedDate));
executedOncell.setWidth(300);
tState = executedOncell.getDefaultCellTextState();
tState.setForegroundColor(Color.getGray());
tState.setFont(font);
tState.setFontSize(6);
page.getParagraphs().add(table);
savePdfDocument(pdfDocument, pdfFilePath);
input file.pdf (24.6 KB)
output file.pdf (107.3 KB)
splash-icon-30X30.jpg (1.9 KB)
Please check below code snippet that we used to test the case. We have attached the generated output as well for your kind reference:
Document pdfDocument = new Document(dataDir + "input file.pdf");
Page page = pdfDocument.getPages().get_Item(1);
Table table = new Table();
table.setColumnWidths("20 90");
// Add an image to the header
Image image = new Image();
image.setFile(dataDir + "splash-icon-30X30.jpg");
image.setFixWidth(20); // Set the image width
image.setFixHeight(20); // Set the image height
Row imageRow = table.getRows().add();
Row hrRow = table.getRows().add();
Row executedOn = table.getRows().add();
Cell imageCell = imageRow.getCells().add();
imageCell.setRowSpan(3);
imageCell.setAlignment(HorizontalAlignment.Right);
imageCell.getParagraphs().add(image);
imageCell.setWidth(50);
Cell titleCell = imageRow.getCells().add();
titleCell.getParagraphs().add(new TextFragment("Aspose Pty Ltd"));
titleCell.setAlignment(HorizontalAlignment.Left);
titleCell.setWidth(300);
TextState tState = titleCell.getDefaultCellTextState();
tState.setForegroundColor(Color.getBlue());
//tState.setFont(font);
tState.setFontSize(6);
Cell hrCell = hrRow.getCells().add();
hrCell.setAlignment(HorizontalAlignment.Left);
hrCell.setWidth(300);
hrCell.getParagraphs().add(new HtmlFragment("<HR>"));
Cell executedOncell = executedOn.getCells().add();
executedOncell.setAlignment(HorizontalAlignment.Left);
executedOncell.getParagraphs().add(new TextFragment("Report Executed On : 06/25/2024"));
executedOncell.setWidth(300);
tState = executedOncell.getDefaultCellTextState();
tState.setForegroundColor(Color.getGray());
//tState.setFont(font);
tState.setFontSize(6);
page.getParagraphs().add(table);
pdfDocument.save(dataDir + "output.pdf");
output.pdf (33.1 KB)
We have used table.setColumnWidths("20 90")
to adjust the column widths as per the image height/width to bring image closer to the title. Also, we increased the width of second column of the table to fit the sub-title text in one line. You can also make such adjustments in these values to obtain expected results.