我需要使用Batik覆盖2个SVG文件.一个文件用作背景图像,为308px×308px,而第二个文件(260px×260px)是必须居中的前景图像(即背景图像的中心).我希望将操作的结果保存在第三个SVG文件中.如果您熟悉Batik,我会很感激您的建议.
谢谢,
奥利维尔.
我正在研究一个为衣服创建模板的Java程序.用户输入他们想要在服装项目上看到的单词,系统创建PDF模板.要创建模板,我以编程方式创建SVG文档,然后使用Batik将SVG转码为PDF格式.
我的客户现在希望能够使用自定义字体来创建模板.我想知道是否有可能像使用Batik转码器的TTF一样使用字体?如果是这样,您如何设置SVG?
我正在编写代码将SVG转换为PNG:
package com.example;
import java.io.*;
import java.nio.file.Paths;
import org.apache.batik.transcoder.image.PNGTranscoder;
import org.apache.batik.transcoder.SVGAbstractTranscoder;
import org.apache.batik.transcoder.TranscoderInput;
import org.apache.batik.transcoder.TranscoderOutput;
public class Main {
public static void main(String [] args) throws Exception {
// read the input SVG document into TranscoderInput
String svgURI = Paths.get(args[0]).toUri().toURL().toString();
TranscoderInput input = new TranscoderInput(svgURI);
// define OutputStream to PNG Image and attach to TranscoderOutput
OutputStream ostream = new FileOutputStream("out.png");
TranscoderOutput output = new TranscoderOutput(ostream);
// create a JPEG transcoder
PNGTranscoder t = new PNGTranscoder();
// set the transcoding hints …Run Code Online (Sandbox Code Playgroud) 我想以编程方式将SVG文件转换为PDF文件.但是,SVG文件包含必须可在生成的PDF文件中搜索的文本.此外,它必须适用于x86_64架构的Red Hat Enterprise Linux 5.3或CentOS 5.3.如果它是开源的或者至少不是非常昂贵的话会很好.
这是我尝试过的.所有这些,除了Batik,都可以在Debian Lenny上正常工作.
Inkscape
我可以使用来自 http://inkscape.modevia.com/ap的自动包装来安装它,但是当我从命令行使用它时,文本无法搜索.
Batik rasterizer [原文如此]
当它将SVG文件转换为PDF文件时,文本不再可搜索.
svg2pdf
可以下载此源及其几个依赖项的源代码.我一直试图让它在CentOS上编译,但还没有成功.我找到了Debian x86_64的预编译版本,但它在CentOS上不起作用.
rsvg-convert
生成的PDF在CentOS 5.3上无法搜索.也许安装更新版本的cairo会有所帮助.感谢DaveParillo提到rsvg-convert(超级用户).
解决方案(但也许上面的部分内容对读者仍然有用)
princeXML
从源代码安装时,它在CentOS上运行良好.出于某种原因,从.rpm安装时它不起作用.谢谢ErikDahlström!
我最近发现了如何使用本地GraphicsEnvironment st注册TTF字体,对于我的用例(SVG-to-PNG转码),Apache Batik可能会识别字体:
import java.awt.Font;
import java.awt.FontFormatException;
import java.awt.GraphicsEnvironment;
// [...]
GraphicsEnvironment lge = GraphicsEnvironment.getLocalGraphicsEnvironment();
try {
Font font = Font.createFont(Font.TRUETYPE_FONT, fontFile);
lge.registerFont(font);
} catch (FontFormatException e) {
logger.warn(e.getMessage(), e);
} catch (IOException e) {
logger.warn(e.getMessage(), e);
}
Run Code Online (Sandbox Code Playgroud)
但是,我想知道是否可以取消注册任何预先存在的字体,以保证只有我注册的字体才会用于转码.
没有GraphicsEnvironment #unregisterFont(...),我怎么能实现这个呢?
PS:我不想继承GraphicsEnvironment的子类,因为我不能假设存在任何特定的子类,如sun.awt.Win32GraphicsEnvironment.
编辑: 更多信息:
我在配置 Batik PDFTranscoder 以进行 Svg 到 Pdf 转换时遇到问题。我想将自定义 truetype 字体嵌入到 PDF 输出中,因此我使用 Batik 转码器。我在 fop 配置文件中提供字体配置,如下所述: https: //xmlgraphics.apache.org/fop/2.2/fonts.html
我尝试使用 org.apache.xmlgraphics.Fop 版本 2.1 和 2.2 进行转换。没有任何成功。
我的 pdf 输出带有“Times New Roman”字体,而不是带有嵌入字体“Arial”,因为它应该基于以下示例:
我的配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<fop version="1.0">
<renderers>
<renderer mime="application/pdf">
<fonts>
<directory>C:/path/to/fontsfolder</directory>
<auto-detect/>
</fonts>
</renderer>
</renderers>
</fop>
Run Code Online (Sandbox Code Playgroud)
这是我的转码器代码:
import org.apache.batik.transcoder.*;
import org.apache.fop.svg.PDFTranscoder;
import org.w3c.dom.Document;
PDFTranscoder transcoder = new PDFTranscoder();
try {
DefaultConfigurationBuilder cfgBuilder = new DefaultConfigurationBuilder();
Configuration cfg = cfgBuilder.buildFromFile(new File("path-to-xml-config-file"));
ContainerUtil.configure(transcoder, cfg);
} catch (Exception e) {
throw new TranscoderException(e); …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用Batik库将BufferedImage绘制到SVG文件中.我有非常相似的代码适用于EPS/PS文件,但由于某种原因,以下代码:
// Get a DOMImplementation and create an XML document
DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation();
Document document = domImpl.createDocument(null, "svg", null);
// Create an instance of the SVG Generator
SVGGraphics2D svgGenerator = new SVGGraphics2D(document);
// draw from image into the svg:
svgGenerator.drawImage(image,new RescaleOp((float)1.0,(float)0.0,null),0,0);
// Write svg file
OutputStream outputStream = new FileOutputStream(svgFile);
Writer out = new OutputStreamWriter(outputStream, "UTF-8");
//svgGenerator.stream(out, true /* use css */);
// write and close file:
outputStream.flush();
outputStream.close();
Run Code Online (Sandbox Code Playgroud)
导致svgGenerator.drawImage(...)行上的NullPointerException.由于基本相同的命令适用于AbstractPSDocumentGraphics2D(在另一种方法中),我不知道这里有什么问题.
编辑:图像在别处声明(类变量,此代码在该类的方法内).
这是一个堆栈跟踪:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at org.apache.batik.svggen.ImageHandlerBase64Encoder.encodeImage(ImageHandlerBase64Encoder.java:157) …Run Code Online (Sandbox Code Playgroud) 我有一个使用Apache Batik转换PNG的问题是不同的,当有一个不同的字体系列的文本,如Arial.问题出现在Cent OS 6运行Tomcat 7和Java 6的环境中.
用于将SVG转换为PNG的Java代码是:
// Convert the SVG image to png and send back
PNGTranscoder transcoder = new PNGTranscoder();
//
TranscoderInput input = new TranscoderInput(new ByteArrayInputStream(svgImage));
outStream = new ByteArrayOutputStream();
TranscoderOutput output = new TranscoderOutput(outStream);
// Transcode the given SVG
transcoder.transcode(input, output);
outStream.flush();
pngImage = outStream.toByteArray();
Run Code Online (Sandbox Code Playgroud)
我要转换为PNG的SVG文件是:
<svg version="1.1" x="0" y="0" id="hjtqebzv1" width="610" height="240" xmlns="http://www.w3.org/2000/svg" xmlns:xml="http://www.w3.org/XML/1998/namespace" xmlns:xlink="http://www.w3.org/1999/xlink">
<defs>
<linearGradient id="LFFFFFF0" x1="0%" y1="0%" x2="100%" y2="0%">
<stop offset="0%" style="stop-color:#FFFFFF;stop-opacity:0.8"/>
<stop offset="100%" style="stop-color:#FAFAFA;stop-opacity:1"/>
</linearGradient>
</defs>
<g id="hjtqebzv-o1" transform="translate(5,5)">
<rect x="1" …Run Code Online (Sandbox Code Playgroud) 我在工作中正在努力完成这部分任务。我故意不详细说明工作任务的背景,以尽量将注意力集中在问题上。我必须将矩形合并为单个多边形,如附图所示,但我需要点列表,以便我可以将它们写入 Swing 画布的多边形形状(DOM 对象),然后导出 SVG。
我知道每个矩形的原点,即左上角的x和y坐标(float x,float y)以及每个矩形的宽度(float)和高度(float),因此我可以计算出所有四个矩形的坐标每个矩形的角,即顶部、右侧、底部、左侧,即顶部 = 原点 = x, y,右侧 = x + 宽度,底部 = x + 宽度,y + 高度,左侧 = x, y + 高度。
我有一个List<Rectangle> rectangles并且想要一个算法,它将将此列表转换为单个多边形(List<Points>其中一个点代表每个点的坐标(x,y),如标记为红色“x”的图中所示。
然后,我将使用这个点列表在 DOM 中写出一个元素,以便最终在 SVG 中打印网页。所以,我的最终结果必须是一个点列表(即用于在 SVG 中构造多边形形状的 x,y 坐标)。
我确实看到了这个答案,它做了类似的事情,但我不确定是否可以将其应用到我的情况 - 而且它是用Python而不是Java编写的:Merging multiple相邻矩形到一个多边形
我不使用maven、pom 或任何东西。我只有简单的测试类进行测试,我只是尝试将 XSL-FO 转换为 PDF,但出现此错误。代码很简单:
package testing;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.Result;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamSource;
import org.apache.fop.apps.Fop;
import org.apache.fop.apps.FopFactory;
import org.apache.xmlgraphics.util.MimeConstants;
public class test {
public static void main(String[] args) {
String pth="C:/Temp/";
FopFactory fopFactory = FopFactory.newInstance(new File(".").toURI());
TransformerFactory factory = TransformerFactory.newInstance();
OutputStream out;
try
{
out = new BufferedOutputStream(new FileOutputStream(new File(pth+"myfile.pdf")));
InputStream in = new FileInputStream(pth+"everything.fo");
Source source = null;
Transformer transformer = …Run Code Online (Sandbox Code Playgroud)