PDF2SVG:Apache Batik textAsShape 选项导致字体转换 [英] PDF2SVG: Apache Batik textAsShape Option Causes Fonts Get Converted

查看:46
本文介绍了PDF2SVG:Apache Batik textAsShape 选项导致字体转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码完美地将PDF文件转换为SVG格式,但无论我做什么,它都会将字体转换成形状……而且文件越来越大……

The code below converts PDF files into SVG format perfectly, but whatever I do, it converts Fonts into shapes... and file size are getting bigger and bigger...

有:

SVGGraphics2D g2d = new CustomSVGGraphics2D(ctx, false);

哪个触发

super(generatorCtx, textAsShapes);

但更近的假"工作,也不是真"......

but neighter "false" works, nor "true"...

如何做到这一点?

代码如下:

package pdf2svg;

import java.awt.Desktop;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.Rectangle;
import java.awt.geom.AffineTransform;
import java.awt.image.ColorModel;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import org.apache.batik.dom.GenericDOMImplementation;
import org.apache.batik.svggen.ExtensionHandler;
import org.apache.batik.svggen.ImageHandler;
import org.apache.batik.svggen.SVGGeneratorContext;
import org.apache.batik.svggen.SVGGraphics2D;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.rendering.PDFRenderer;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;

/**
 *
 * @author HP
 */
public class PDF2SVG {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws Exception {

        InputStream is = new FileInputStream("../pdf/test.pdf") ;
        PDDocument document = PDDocument.load(is);
        PDFRenderer pdfRenderer = new PDFRenderer(document);

        SVGGeneratorContext ctx = createContext();

        int pageCounter = 0;
        for (@SuppressWarnings("unused") PDPage page : document.getPages()) {
            SVGGraphics2D g = createGraphics(ctx);
            pdfRenderer.renderPageToGraphics(pageCounter, g);

            // suffix in filename will be used as the file format
            System.out.println("Saving page " + (pageCounter+1));                       

            Writer out = new OutputStreamWriter(new FileOutputStream("../pdf/test-"+pageCounter+".svg"));
            g.stream(out, true);

            pageCounter++;
        }

    }   

    private static SVGGraphics2D createGraphics(SVGGeneratorContext ctx) {
        SVGGraphics2D g2d = new CustomSVGGraphics2D(ctx, false);            
        return g2d;
    }

    private static SVGGeneratorContext createContext() {
        DOMImplementation impl = GenericDOMImplementation.getDOMImplementation();
        String svgNS = "http://www.w3.org/2000/svg";
        Document myFactory = impl.createDocument(svgNS, "svg", null);

        SVGGeneratorContext ctx = SVGGeneratorContext.createDefault(myFactory);
        ctx.setComment("Generated by FooApplication with Batik SVG Generator");
        return ctx;
    }

    public static class CustomSVGGraphics2D extends SVGGraphics2D {

        public CustomSVGGraphics2D(SVGGeneratorContext generatorCtx, boolean textAsShapes) {
            super(generatorCtx, textAsShapes);
        }

        @Override
        public GraphicsConfiguration getDeviceConfiguration() {
            return new CustomGraphicsConfiguration();
        }
    }

    private static final class CustomGraphicsConfiguration extends GraphicsConfiguration {

        @Override
        public AffineTransform getNormalizingTransform() {
            return null;
        }

        @Override
        public GraphicsDevice getDevice() {
            return new CustomGraphicsDevice();
        }

        @Override
        public AffineTransform getDefaultTransform() {
            return null;
        }

        @Override
        public ColorModel getColorModel(int transparency) {
            return null;
        }

        @Override
        public ColorModel getColorModel() {
            return null;
        }

        @Override
        public Rectangle getBounds() {
            return null;
        }
    }

    private static final class CustomGraphicsDevice extends GraphicsDevice {
        @Override
        public int getType() {
            return 0;
        }

        @Override
        public String getIDstring() {
            return null;
        }

        @Override
        public GraphicsConfiguration[] getConfigurations() {
            return null;
        }

        @Override
        public GraphicsConfiguration getDefaultConfiguration() {
            return null;
        }
    }

}

推荐答案

也许你应该创建图形设置第二个参数为false":

Maybe you should create the graphics setting second parameter to "false":

public SVGGraphics2D(org.apache.batik.svggen.SVGGeneratorContext generatorCtx, boolean textAsShapes);

这篇关于PDF2SVG:Apache Batik textAsShape 选项导致字体转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆