使用iTextSharp将Fusionchart导出为PDF

时间:2016-07-01 14:21:04

标签: c# asp.net-mvc-5 itext

我想使用iTextSharp将FusionChart导出为PDF。我可以通过设置图表exportEnabled ='1'导出为PDF,但我希望能够从我的控制器导出fusionchart。

我在str变量中有我的整个fusionchart并尝试了以下内容:

public FileStreamResult ExportPDF(int ProjectID)
    {
        string exportData = String.Format("<html><body>{0}</body></html>", getGraphXMLData(ProjectID));//This Gets the chart and stores into string
        var bytes = System.Text.Encoding.UTF8.GetBytes(exportData);
        using (var input = new MemoryStream(bytes))
        {
            var output = new MemoryStream();
            var document = new iTextSharp.text.Document(PageSize.A4, 50, 50, 50, 50);
            var writer = PdfWriter.GetInstance(document, output);

            Font headerFont = FontFactory.GetFont("Verdana", 10);
            Font rowfont = FontFactory.GetFont("Verdana", 10);

            writer.CloseStream = false;
            document.Open();

            var xmlWorker = iTextSharp.tool.xml.XMLWorkerHelper.GetInstance();
            xmlWorker.ParseXHtml(writer, document, input, System.Text.Encoding.UTF8);
            document.Close();
            output.Position = 0;

            return File(output, "application/pdf", "ProfileChart_Report.pdf");

            //return new FileStreamResult(output, "application/pdf");
        }
    }

但是我在这一行收到错误:document.Close();

我得到的错误是:

An exception of type 'System.IO.IOException' occurred in iTextSharp.dll but was not handled in user code

Additional information: The document has no pages.

以下是exportData字段中的数据:

<html>
<body>
<chart caption='Project Test' yaxisname='Elevation' xaxisname='Accumulated Length' xaxismaxvalue='1000' xaxisminvalue='100' yaxismaxvalue='200' xnumberprefix='' ynumberprefix='' showcanvasborder='1' canvasborderthickness='0.5' canvasborderalpha= '50' showXAxisLine= '0' showformbtn= '1' formAction='#' submitdataasxml='1' baseFontColor='#333333' baseFont='Helvetica Neue, Arial' captionFontSize='14' subcaptionFontSize='14' subcaptionFontBold='0' showBorder='0' bgColor='#ffffff' showValues='0' showShadow='0' canvasBgColor='#ffffff' divlineAlpha='100' divlineColor='#999999' divlineThickness='1' divLineIsDashed='1' divLineDashLen='1' divLineGapLen='1' usePlotGradientColor='0' showplotborder='0' showAlternateHGridColor='0' showAlternateVGridColor='0' legendBgAlpha='0' legendBorderAlpha='0' legendShadow='0' legendItemFontSize='10' legendItemFontColor='#666666' toolTipColor='#ffffff' toolTipBorderThickness='0' toolTipBgColor='#000000' toolTipBgAlpha='80' toolTipBorderRadius='2' toolTipPadding = '5' labeldisplay= 'rotate' slantlabels= '1' exportEnabled='1' > <categories verticallinecolor='666666' verticallinethickness='1' alpha='40' anchorsides='0' anchorradius='0'><category label='0' x='0' showverticalline='0'/>
<category label='46.17' x='46.17' showverticalline='0'/>
<category label='142.98' x='142.98' showverticalline='0'/>
<category label='543.88' x='543.88' showverticalline='0'/>
<category label='826.58' x='826.58' showverticalline='0'/>
<category label='1037.93' x='1037.93' showverticalline='0'/>
<category label='1106.1' x='1106.1' showverticalline='0'/>
<category label='1338.1' x='1338.1' showverticalline='0'/>
<category label='1838.5' x='1838.5' showverticalline='0'/>
<category label='1889.37' x='1889.37' showverticalline='0'/>
<category label='2538' x='2538' showverticalline='0'/>
<category label='2789.04' x='2789.04' showverticalline='0'/>
<category label='3154.34' x='3154.34' showverticalline='0'/>
<category label='3345.17' x='3345.17' showverticalline='0'/>
<category label='4220.75' x='4220.75' showverticalline='0'/>
<category label='4671.9' x='4671.9' showverticalline='0'/>
<category label='5057.74' x='5057.74' showverticalline='0'/>
<category label='5318.51' x='5318.51' showverticalline='0'/>
<category label='5576.18' x='5576.18' showverticalline='0'/>
<category label='5958.13' x='5958.13' showverticalline='0'/>
<category label='6537.92' x='6537.92' showverticalline='0'/>
<category label='7282.09' x='7282.09' showverticalline='0'/>
<category label='8178.84' x='8178.84' showverticalline='0'/>
<category label='8511.22' x='8511.22' showverticalline='0'/>
<category label='8989.59' x='8989.59' showverticalline='0'/>
<category label='9442.78' x='9442.78' showverticalline='0'/>
<category label='9862.78' x='9862.78' showverticalline='0'/>
<category label='9942.78' x='9942.78' showverticalline='0'/>
<category label='10402.78' x='10402.78' showverticalline='0'/>
<category label='10607.78' x='10607.78' showverticalline='0'/>
<category label='11052.78' x='11052.78' showverticalline='0'/>
<category label='11267.78' x='11267.78' showverticalline='0'/>
<category label='11797.78' x='11797.78' showverticalline='0'/>
<category label='12307.78' x='12307.78' showverticalline='0'/>
<category label='12572.78' x='12572.78' showverticalline='0'/>
<category label='13229.78' x='13229.78' showverticalline='0'/>
<category label='13369.78' x='13369.78' showverticalline='0'/>
</categories>
<dataset  drawline= '1' seriesname= 'Elevation' color= '#0045ff' anchorsides= '0' anchorradius= '0' anchorbgcolor= '#0045ff' anchorbordercolor= '#0045ff'>
<set y='321' x='0'/>
<set y='322' x='46.17'/>
<set y='322.54' x='142.98'/>
<set y='309.12' x='543.88'/>
<set y='292.63' x='826.58'/>
<set y='299.12' x='1037.93'/>
<set y='298.68' x='1106.1'/>
<set y='303.49' x='1338.1'/>
<set y='294.23' x='1838.5'/>
<set y='295.77' x='1889.37'/>
<set y='269.98' x='2538'/>
<set y='274.17' x='2789.04'/>
<set y='261.71' x='3154.34'/>
<set y='267.01' x='3345.17'/>
<set y='243.04' x='4220.75'/>
<set y='247.05' x='4671.9'/>
<set y='250.81' x='5057.74'/>
<set y='246.4' x='5318.51'/>
<set y='249.62' x='5576.18'/>
<set y='238.03' x='5958.13'/>
<set y='253.08' x='6537.92'/>
<set y='235.53' x='7282.09'/>
<set y='173.53' x='8178.84'/>
<set y='189.91' x='8511.22'/>
<set y='224.23' x='8989.59'/>
<set y='250' x='9442.78'/>
<set y='250' x='9862.78'/>
<set y='253.45' x='9942.78'/>
<set y='240' x='10402.78'/>
<set y='235' x='10607.78'/>
<set y='210' x='11052.78'/>
<set y='210' x='11267.78'/>
<set y='195' x='11797.78'/>
<set y='170' x='12307.78'/>
<set y='160' x='12572.78'/>
<set y='150' x='13229.78'/>
<set y='141.36' x='13369.78'/>
</dataset>
<dataset  drawline= '1' seriesname= 'HGL' color= '#198500' anchorsides= '0' anchorradius= '0' anchorbgcolor= '#198500' anchorbordercolor= '#198500'>
<set y='321' x='0'/>
<set y='320.87' x='46.17'/>
<set y='320.59' x='142.98'/>
<set y='319.46' x='543.88'/>
<set y='318.66' x='826.58'/>
<set y='318.06' x='1037.93'/>
<set y='317.87' x='1106.1'/>
<set y='317.22' x='1338.1'/>
<set y='315.81' x='1838.5'/>
<set y='315.67' x='1889.37'/>
<set y='313.84' x='2538'/>
<set y='313.1' x='2789.04'/>
<set y='312.02' x='3154.34'/>
<set y='311.38' x='3345.17'/>
<set y='308.46' x='4220.75'/>
<set y='306.96' x='4671.9'/>
<set y='305.68' x='5057.74'/>
<set y='304.81' x='5318.51'/>
<set y='303.95' x='5576.18'/>
<set y='302.68' x='5958.13'/>
<set y='300.75' x='6537.92'/>
<set y='298.27' x='7282.09'/>
<set y='294.88' x='8178.84'/>
<set y='293.39' x='8511.22'/>
<set y='291.25' x='8989.59'/>
<set y='285.78' x='9442.78'/>
<set y='281.31' x='9862.78'/>
<set y='280.46' x='9942.78'/>
<set y='275.55' x='10402.78'/>
<set y='273.37' x='10607.78'/>
<set y='256.15' x='11052.78'/>
<set y='247.84' x='11267.78'/>
<set y='227.36' x='11797.78'/>
<set y='204.03' x='12307.78'/>
<set y='191.92' x='12572.78'/>
<set y='156.49' x='13229.78'/>
<set y='148.94' x='13369.78'/>
</dataset>
<dataset  drawline= '0' seriesname= 'Peak' color= '#ff0000' anchorsides= '3' anchorradius= '5' anchorbgcolor= '#ff0000' anchorbordercolor= '#ff0000'>
<set y='321' x='0'/>
<set y='322.54' x='142.98'/>
<set y='299.12' x='1037.93'/>
<set y='303.49' x='1338.1'/>
<set y='295.77' x='1889.37'/>
<set y='274.17' x='2789.04'/>
<set y='267.01' x='3345.17'/>
<set y='250.81' x='5057.74'/>
<set y='249.62' x='5576.18'/>
<set y='253.08' x='6537.92'/>
<set y='253.45' x='9942.78'/>
<set y='141.36' x='13369.78'/>
</dataset>
<dataset  drawline= '0' seriesname= 'Valve - Change' color= '#198500' anchorsides= '4' anchorradius= '5' anchorbgcolor= '#198500' anchorbordercolor= '#198500'>
<set y='309.12' x='543.88'/>
<set y='235.53' x='7282.09'/>
<set y='235' x='10607.78'/>
<set y='195' x='11797.78'/>
<set y='150' x='13229.78'/>
</dataset>
<dataset  drawline= '0' seriesname= 'Valve suggest - Long Node' color= '#fdff00' anchorsides= '4' anchorradius= '3' anchorbgcolor= '#fdff00' anchorbordercolor= '#fdff00'>
<set y='321' x='0'/>
<set y='309.12' x='543.88'/>
<set y='294.23' x='1838.5'/>
<set y='269.98' x='2538'/>
<set y='261.71' x='3154.34'/>
<set y='243.04' x='4220.75'/>
<set y='247.05' x='4671.9'/>
<set y='250.81' x='5057.74'/>
<set y='238.03' x='5958.13'/>
<set y='253.08' x='6537.92'/>
<set y='235.53' x='7282.09'/>
<set y='173.53' x='8178.84'/>
<set y='189.91' x='8511.22'/>
<set y='224.23' x='8989.59'/>
<set y='250' x='9442.78'/>
<set y='250' x='9862.78'/>
<set y='240' x='10402.78'/>
<set y='210' x='11052.78'/>
<set y='195' x='11797.78'/>
<set y='170' x='12307.78'/>
<set y='150' x='13229.78'/>
</dataset>
</chart>
</body>
</html>

我特别想在iTextSharp中这样做,因为我有其他的导出,我也在使用iTextSharp并最终想要将所有内容组合成一个PDF。

我会感激任何人的帮助。感谢。

0 个答案:

没有答案