I have been working with SVG formats recently. Mainly, trying to find a decent swing component for creating and modifying SVGs. As a result of such escapade, I decided to write a PDF to SVG converter. Now, I haven't fully tested it, but with the PDFs I have tried, it converted successfully.
The libraries I used were Batik 1.7 and PDF-Renderer v1.0.5
Here is My Main
public class ConversionMain
{
public ConversionMain()
{
File pdf = new File("ss12.pdf");
PDFUtility.PDF_TO_SVG(pdf);
}
public static void main(String[] args)
{
new ConversionMain();
}
}
Here is my PDFUtility function for taking PDF, reading it and then converting to SVG
public class PDFUtility
{
public static void PDF_TO_SVG(File pdfFile)
{
try
{
RandomAccessFile raf = new RandomAccessFile(pdfFile, "r");
FileChannel channel = raf.getChannel();
ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
PDFFile pdf = new PDFFile(buf);
DOMImplementation domImp = GenericDOMImplementation.getDOMImplementation();
for (int i = 1; i <= pdf.getNumPages(); i++)
{
PDFPage page = pdf.getPage(i);
// image dimensions
int width = (int) page.getWidth(); //Can be changed to whatever you like
int height = (int) page.getHeight();
Rectangle rect = new Rectangle(0, 0, (int) page.getBBox().getWidth(), (int) page.getBBox().getHeight());
Image image = page.getImage(width, height, rect, null, true, true);
String svgNS = "http://www.w3.org/2000/svg";
Document document = domImp.createDocument(svgNS, "svg", null);
SVGGraphics2D svgGenerator = new SVGGraphics2D(document);
Graphics graphics = svgGenerator.create();
graphics.drawImage(image, 0, 0, null);
boolean userCSS = true;
FileOutputStream fos = new FileOutputStream(new File("Page " + i + ".svg"));
Writer out = new OutputStreamWriter(fos);
svgGenerator.stream(out, userCSS);
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
Try it out and let me know what you think
Thanks!