ImageGear v26.5 - Updated
Developer Guide / How to Work with ... / PDF / How to... / Convert... / PDF to SVG
In This Topic
    PDF to SVG
    In This Topic

    ImageGear .NET provides the ability to convert and save any page from a PDF document to a Scalable Vector Graphics (SVG) file. This can be done by using one of the following API calls:

    Since SVG format is not a multi-page format, the SaveDocument API called with default options will concatenate all converted pages into one SVG file.

    Text Conversion

    ImageGear .NET PDF to SVG converter is optimized to provide the best text fidelity results. For that purpose, all PDF text elements are converted to SVG outlining paths, which are not searchable for text.

    Color Profiles

    ImageGear .NET PDF to SVG color converter applies the default color space defined in the PDF document. If no default color space is defined, all PDF colors defined in CMYK color space are converted to RGB using the Windows ICM and the color profiles enabled in the ImageGear .NET color profile manager. The color profile transformation uses the ICC profile specified in ImGearColorProfileManager.CmykProfile. If this is not set, then the default color profile distributed with ImageGear .NET (ig_cmyk_profile.icm) is used.

    Password-Protected PDF Documents

    ImageGear .NET PDF to SVG converter requires “Content Copying” permission to the source PDF document to be granted, which means that if a PDF document is secured, it should be opened with the password that allows access to its internals.

    If this permission is not granted, the ImageGear .NET PDF to SVG converter will turn on the rasterizaton mode for this document, which will rasterize any PDF page from this document prior to converting to SVG. The resolution of the raster image embedded to SVG will be set using the PDF control parameters.

    Anti-Aliasing

    Most SVG viewers, including Internet Explorer, Chrome, and Firefox browsers, enable edge anti-aliasing, but do not allow turning it off. Although in most cases anti-aliasing makes vector graphics look much better and smoother, there are cases when anti-aliasing leads to unintended SVG rendering effects like the following:

    • Although using a set of thin bars of different shades of a color to emulate a gradient background may look good in PDF, it may not produce the desired effect in SVG. Thin white lines may appear between bars, depending on the viewer’s zoom. It is recommended that the PDF producers use the native PDF gradients instead of emulating them.
    • Small sized text in the converted SVG may look blurry compared to the original PDF, because of the differences between the algorithms specific to PDF text anti-aliasing and anti-aliasing of the graphic paths representing text in SVG.

    No Thin Line Enhancement in SVG

    While the ImageGear .NET PDF renderer provides enhanced rendering of thin lines when a PDF page is zoomed out, ensuring that 1 pixel lines are still visible even when the page is zoomed far out, SVG viewers normally don’t support that. So, thin lines converted from PDF will have the correct widths in SVG, but will be scaled proportionally and may disappear if a page is zoomed out too much.

    ConvertPDFtoSVG Sample

    The following ConvertPdftoSVG sample method illustrates how you can convert a PDF file to SVG file.

    C#

    private void ConvertPdftoSVG(string inputFile, string outputFile)
    {
        ImGearPage imGearPDFPage = null;
    
        // Open file and load PDF page.
        using (var stream = new FileStream(inputFile, FileMode.Open, FileAccess.Read, FileShare.Read))
        {
            try
            {
                // Load PDF page.
                imGearPDFPage = ImGearFileFormats.LoadPage(stream, 0);
            }
            catch (ImGearException ex)
            {
                MessageBox.Show(string.Format("Could not load file {0}", ex.Message));
            }
        }
    
        // Open output file and save to SVG.
        using (var fileStream = new FileStream(outputFile, FileMode.Create, FileAccess.ReadWrite))
        {
            ImGearFileFormats.SavePage(imGearPDFPage, fileStream, 0, ImGearSavingModes.OVERWRITE, ImGearSavingFormats.SVG);
        }
    }
    

    Also you can specify a background color for the converted SVG file using PDF control parameters.

    C#

    // Get SVGBackgroundColor global control parameter.
    IImGearFormat pdfFormat = ImGearFileFormats.Filters.Get(ImGearFormats.PDF);
    ImGearControlParameter xParam = pdfFormat.Parameters.GetByName("SVGBackgroundColor");
    ImGearRGBQuad rgb = (ImGearRGBQuad)xParam.Value;
    
    // Select new color.
    var colorDialog = new ColorDialog
    {
        Color = System.Drawing.Color.FromArgb(rgb.Red, rgb.Green, rgb.Blue)
    };
    
    // Set selected color as new backround value.
    if (colorDialog.ShowDialog() == DialogResult.OK)
        xParam.Value = new ImGearRGBQuad(colorDialog.Color.R, colorDialog.Color.G, colorDialog.Color.B);