Reputation: 1
I am new to iTextSharp, currently working on conversion of HTML to PDF using Html2pdf (iTextSharp extension). I am able to generate pdf but not able to add logo to pdf on every page.
Image coming but I can't change width of images.
CSS what I am using for pdf logo is below:
@page {
@top-left {
content:"test ";
background:url(../images/template/test_logo_pdf.jpg) no-repeat 0px 0px;
border:1px solid red;
background-color: #cccccc;
margin-top:10px;
}
@top-right {
content: flow(header);
}
@bottom-right {
content: "Page " counter(page) " of " counter(pages);
font: 8pt Arial, sans-serif;
}
@bottom-left {
content: string(repname);
font: 8pt Arial, sans-serif;
}
}
Upvotes: 0
Views: 689
Reputation: 588
It's indeed not entirely easy to control dimensions of images added to the page margin boxes. One possible approach that I can suggest is to add image as a content (rather than as background-image) and make use of custom tag worker, that would specify height and width as desired for page margin box children images:
HTML:
@top-left {
content: url(../images/template/test_logo_pdf.jpg);
border:1px solid red;
background-color: #cccccc;
margin-top:10px;
}
This is Java code, however .NET version has exact same API, only differing in code style (captial letters in the beginning of method names, etc.):
private static class PageMarginBoxImagesTagWorkerFactory extends DefaultTagWorkerFactory {
@Override
public ITagWorker getCustomTagWorker(IElementNode tag, ProcessorContext context) {
if (tag.name().equals(PageMarginBoxContextNode.PAGE_MARGIN_BOX_TAG)) {
return new PageMarginBoxImagesWorker(tag, context);
}
return super.getCustomTagWorker(tag, context);
}
}
private static class PageMarginBoxImagesWorker extends PageMarginBoxWorker {
public PageMarginBoxImagesWorker(IElementNode element, ProcessorContext context) {
super(element, context);
}
@Override
public boolean processTagChild(ITagWorker childTagWorker, ProcessorContext context) {
if (childTagWorker.getElementResult() instanceof Image) {
// Or set fixed dimensions via setWidth/setHeight
((Image) childTagWorker.getElementResult()).setAutoScale(true);
}
return super.processTagChild(childTagWorker, context);
}
}
And make use of the PageMarginBoxImagesTagWorkerFactory by specifying it in ConverterProperties
:
HtmlConverter.convertToPdf(htmlSrc, pdfDocument,
new ConverterProperties()
.setTagWorkerFactory(new PageMarginBoxImagesTagWorkerFactory()));
Upvotes: 1