PDF2SVG: Apache Batik textAsShape Option Causes Fonts Get Converted

♀尐吖头ヾ 提交于 2019-12-13 03:35:30

问题


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...

There is:

SVGGraphics2D g2d = new CustomSVGGraphics2D(ctx, false);

which triggering

super(generatorCtx, textAsShapes);

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

How to accomplish this?

Here is the code:

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;
        }
    }

}

来源:https://stackoverflow.com/questions/48527687/pdf2svg-apache-batik-textasshape-option-causes-fonts-get-converted

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!