Discussion:
[iText-questions] Does XmlWorker support absolute positioning?
Georg Nozicka
2014-12-20 10:06:08 UTC
Permalink
Hello,

I can not really figure out if XmlWorker does support absolute positioning.

Here is a sample which demonstrates absolute positioning: http://sourceforge.net/p/xmlworker/code-0/379/tree//trunk/src/test/resources/com/itextpdf/tool/xml/examples/css/position/text/position_text01/position_text01.html#l6 <http://sourceforge.net/p/xmlworker/code-0/379/tree//trunk/src/test/resources/com/itextpdf/tool/xml/examples/css/position/text/position_text01/position_text01.html#l6>

When I try to run a similar sample this does not work. The code looks as follows:

public class TestAbsolutePositioning extends Setup {

@Test
public void setupDefaultProcessingYourself() throws IOException, DocumentException {
Document doc = new Document(PageSize.A4);
PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream(new File("/tmp/xmlworker.pdf")));
doc.open();
HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);
htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());
CSSResolver cssResolver = XMLWorkerHelper.getInstance().getDefaultCssResolver(true);
Pipeline<?> pipeline = new CssResolverPipeline(cssResolver, new HtmlPipeline(htmlContext, new PdfWriterPipeline(doc, writer)));
XMLWorker worker = new XMLWorker(pipeline, true);
XMLParser p = new XMLParser(worker);
InputStream is = getClass().getClassLoader().getResourceAsStream("test1.html");
p.parse(is);
doc.close();
}
}


public class Setup {

static {
LoggerFactory.getInstance().setLogger(new SysoLogger());
FontFactory.registerDirectories();
}
}

test1.html looks as follows:

<html>
<head>
<style>
h2 {
text-decoration: underline;
position: absolute;
left: 50mm;
top: 50mm;
}
</style>
</head>

<body>
<h2>This is a heading with an absolute position</h2>
<p>This is the text after the header.</p>
</body>

</html>

The output looks as follows (see attachment):





I use the following libraries:

<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.4</version>
<scope>compile</scope>
</dependency>

<dependency>
<groupId>com.itextpdf.tool</groupId>
<artifactId>xmlworker</artifactId>
<version>5.5.4</version>
<scope>compile</scope>
</dependency>


Regards,
Georg

Loading...