我正在使用columnText通过XMLWorkerHelper将html绘制成pdf,并且它运行良好。
唯一的问题是,我正在使用的代码库需要通过stamper.addAnnotation()绘制所有内容。
我不确定如何将columnText转换为可以添加为注释的东西?
一些上下文:这用作将前端内容可编辑div转换为pdf元素的方法,但必须以某种方式通过stamper.addAnnotation完成。
如果不可能,那么将html绘制成PDFAnnotations?
当前使用列文本的代码(JRuby):
elementsList = XMLWorkerHelper::parseToElementList(html, css) @stamper = PdfStamper.new(@reader, output_stream) canvas = @stamper.getOverContent(page_no) ct = ColumnText.new(canvas) ct.setSimpleColumn(rect) elementsList.each{|element| ct.addElement(element)} ct.go()通常如何附加注释的示例代码:
annotation = PdfAnnotation.createFreeText(@stamper.getWiter(), rect, "blah blah", defaultAppearance) @stamper = PdfStamper.new(@reader, output_stream) @stamper.addAnnotation(annotation, page_no)defaultAppearance的类型是PdfContentByte,所以也许我可以将columnText转换成那个? 我还没弄明白该怎么做。
有任何想法吗?
I'm using columnText to draw html into a pdf through XMLWorkerHelper and it's working great.
The only problem is, the code base I'm working with requires everything to be drawn through stamper.addAnnotation()
I'm not sure how to convert a columnText into something which can be added as an annotation?
Some context: this is used as a way to convert content-editable div in the frontend to pdf elements but must be done through stamper.addAnnotation somehow.
If impossible is there anyway to draw html into PDFAnnotations?
Code that currently uses column text(JRuby):
elementsList = XMLWorkerHelper::parseToElementList(html, css) @stamper = PdfStamper.new(@reader, output_stream) canvas = @stamper.getOverContent(page_no) ct = ColumnText.new(canvas) ct.setSimpleColumn(rect) elementsList.each{|element| ct.addElement(element)} ct.go()Example code of how annotations are normally appended:
annotation = PdfAnnotation.createFreeText(@stamper.getWiter(), rect, "blah blah", defaultAppearance) @stamper = PdfStamper.new(@reader, output_stream) @stamper.addAnnotation(annotation, page_no)defaultAppearance is of type PdfContentByte so maybe theres a way I can convert the columnText into that? I haven't figured out how to do that exactly.
Any ideas?
最满意答案
你的代码是JRuby,但是你也标记了你的问题java ,我假设Java样本也会帮助你。
您可以通过ColumnText对象从HTML向外观添加内容,如下所示:
String html ="<html><h1>Header</h1><p>A paragraph</p><p>Another Paragraph</p></html>"; String css = "h1 {color: red;}"; ElementList elementsList = XMLWorkerHelper.parseToElementList(html, css); PdfReader reader = new PdfReader(resource); PdfStamper stamper = new PdfStamper(reader, result); Rectangle cropBox = reader.getCropBox(1); PdfAnnotation annotation = stamper.getWriter().createAnnotation(cropBox, PdfName.FREETEXT); PdfAppearance appearance = PdfAppearance.createAppearance(stamper.getWriter(), cropBox.getWidth(), cropBox.getHeight()); ColumnText ct = new ColumnText(appearance); ct.setSimpleColumn(new Rectangle(cropBox.getWidth(), cropBox.getHeight())); elementsList.forEach(element -> ct.addElement(element)); ct.go(); annotation.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, appearance); stamper.addAnnotation(annotation, 1); stamper.close(); reader.close();( AddAnnotation.java测试方法testAddAnnotationLikeJasonY )
对于此源文档
结果看起来像这样
如你所见,你已经拥有几乎所有必需的建筑砖块......
Your code is JRuby but as you also have tagged your question java, I assume a Java sample will also help you.
You can add content to an appearance from HTML via a ColumnText object like this:
String html ="<html><h1>Header</h1><p>A paragraph</p><p>Another Paragraph</p></html>"; String css = "h1 {color: red;}"; ElementList elementsList = XMLWorkerHelper.parseToElementList(html, css); PdfReader reader = new PdfReader(resource); PdfStamper stamper = new PdfStamper(reader, result); Rectangle cropBox = reader.getCropBox(1); PdfAnnotation annotation = stamper.getWriter().createAnnotation(cropBox, PdfName.FREETEXT); PdfAppearance appearance = PdfAppearance.createAppearance(stamper.getWriter(), cropBox.getWidth(), cropBox.getHeight()); ColumnText ct = new ColumnText(appearance); ct.setSimpleColumn(new Rectangle(cropBox.getWidth(), cropBox.getHeight())); elementsList.forEach(element -> ct.addElement(element)); ct.go(); annotation.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, appearance); stamper.addAnnotation(annotation, 1); stamper.close(); reader.close();(AddAnnotation.java test method testAddAnnotationLikeJasonY)
For this source document
the result looks like this
As you see, you already had nearly all the required buildings bricks...
更多推荐
发布评论