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

Free Support Forum - aspose.com

Pdf appending a Image In pdf and word document

Hi,


Please provide us quick solution as we are evaluating this tool for our product.

I am trying to append a image in header of pdf and word with out replacing existing header.

My image width would be header width i.e document should be like letter head .

I have tryed multiple options in pdf like bellow code

insertImage(Document documentObj, String Image){
ImageStamp imageStamp = new ImageStamp(Image);
// imageStamp.setBackground(true);
imageStamp.setTopMargin(5);
imageStamp.setHorizontalAlignment(HorizontalAlignment.Center);
imageStamp.setVerticalAlignment(VerticalAlignment.Top);
// imageStamp.setXIndent(100); imageStamp.setQuality(100);
for (int Page_counter = 1; Page_counter <= documentObj.getPages().size(); Page_counter++) {
documentObj.getPages().get_Item(Page_counter).addStamp(imageStamp);
}
documentObj.save(“output.pdf”);
}
}

Also tried same with like
insertImage(Document documentObj, String imagePath,String outputFile){
PdfFileStamp fileStamp = new PdfFileStamp();
fileStamp.bindPdf(documentObj);
fileStamp.addHeader(imagePath, 2, 10, 10);
fileStamp.save(outputFile);
fileStamp.close();
}

Both these are replacing my existing content.

Tried one more option

public void insertImage(Document documentObj, String Image){
Image image = new Image();
image.setInLineParagraph(true);
com.aspose.pdf.MarginInfo m = new com.aspose.pdf.MarginInfo();
m.setTop(2);
m.setBottom(10);
m.setRight(5);
m.setLeft(10);
image.setMargin(m);
image.setFile(Image);
image.setFixHeight(40);
image.setFixWidth(510);

for (int Page_counter = 1; Page_counter <= documentObj.getPages().size(); Page_counter++) {
HeaderFooter h1 = new HeaderFooter();
h1.getParagraphs().add( image );
documentObj.getPages().get_Item(Page_counter).setHeader(h1);
}

}

Can you please provided me some example to do this.


Also adding same for word
public void insertImage(Document documentObj, String ImagePath) {
DocumentBuilder builder = new DocumentBuilder(documentObj);
Section currentSection = builder.getCurrentSection();
PageSetup pageSetup = currentSection.getPageSetup();
pageSetup.setDifferentFirstPageHeaderFooter(true);
pageSetup.setHeaderDistance(50);
builder.moveToHeaderFooter(HeaderFooterType.HEADER_FIRST);
builder.moveToHeaderFooter(HeaderFooterType.HEADER_PRIMARY);
String imageFileName = ImagePath;
Shape obj = builder.insertImage(imageFileName, RelativeHorizontalPosition.PAGE, 80,
RelativeVerticalPosition.PAGE, 15, 610, 60, WrapType.THROUGH);
builder.moveToDocumentStart();
currentSection = builder.getCurrentSection();
pageSetup = currentSection.getPageSetup();
pageSetup.setOrientation(Orientation.LANDSCAPE);
pageSetup.setDifferentFirstPageHeaderFooter(false);
currentSection.getHeadersFooters().linkToPrevious(false);
copyHeadersFootersFromPreviousSection(currentSection);
HeaderFooter primaryFooter = currentSection.getHeadersFooters()
.getByHeaderFooterType(HeaderFooterType.FOOTER_PRIMARY);

}
private static void copyHeadersFootersFromPreviousSection(Section section) {
try {
Section previousSection = (Section) section.getPreviousSibling();

if (previousSection == null)
return;

section.getHeadersFooters().clear();

for (HeaderFooter headerFooter : previousSection.getHeadersFooters()) {
section.getHeadersFooters().add(headerFooter.deepClone(true));
}
} catch (Exception e) {
}
}


Please help

Thanks
Srikanth

Hi Srikanth,


Thanks for contacting support.

Can you please share your input PDF and Word document, so that we can test the scenarios in our environment. We are sorry for your inconvenience.