/// <summary> /// Creates a new /// <see cref="ImgTagWorker"/> /// instance. /// </summary> /// <param name="element">the element</param> /// <param name="context">the context</param> public ObjectTagWorker(IElementNode element, ProcessorContext context) { this.processUtil = new SvgProcessingUtil(); //Retrieve object type String type = element.GetAttribute(AttributeConstants.TYPE); if (IsSvgImage(type)) { String dataValue = element.GetAttribute(AttributeConstants.DATA); try { using (Stream svgStream = context.GetResourceResolver().RetrieveResourceAsInputStream(dataValue)) { if (svgStream != null) { SvgConverterProperties props = ContextMappingHelper.MapToSvgConverterProperties(context); if (!context.GetResourceResolver().IsDataSrc(dataValue)) { Uri fullURL = context.GetResourceResolver().ResolveAgainstBaseUri(dataValue); String dir = FileUtil.ParentDirectory(fullURL); props.SetBaseUri(dir); } res = SvgConverter.ParseAndProcess(svgStream, props); } } } catch (SvgProcessingException spe) { LOGGER.Error(spe.Message); } catch (Exception ie) { LOGGER.Error(MessageFormatUtil.Format(iText.Html2pdf.LogMessageConstant.UNABLE_TO_RETRIEVE_STREAM_WITH_GIVEN_BASE_URI , context.GetBaseUri(), element.GetAttribute(AttributeConstants.DATA), ie)); } } }
public virtual void GetCharsetNameRegressionTest() { String expected = System.Text.Encoding.UTF8.Name(); String actual = new SvgConverterProperties().GetCharset(); NUnit.Framework.Assert.AreEqual(expected, actual); }
public virtual void SvgCssResolverStylesheetTest() { iText.StyledXmlParser.Jsoup.Nodes.Element jsoupLink = new iText.StyledXmlParser.Jsoup.Nodes.Element(iText.StyledXmlParser.Jsoup.Parser.Tag .ValueOf(SvgConstants.Tags.LINK), ""); Attributes linkAttributes = jsoupLink.Attributes(); linkAttributes.Put(new iText.StyledXmlParser.Jsoup.Nodes.Attribute(SvgConstants.Attributes.XMLNS, "http://www.w3.org/1999/xhtml" )); linkAttributes.Put(new iText.StyledXmlParser.Jsoup.Nodes.Attribute(SvgConstants.Attributes.REL, SvgConstants.Attributes .STYLESHEET)); linkAttributes.Put(new iText.StyledXmlParser.Jsoup.Nodes.Attribute(SvgConstants.Attributes.HREF, "styleSheetWithLinkStyle.css" )); linkAttributes.Put(new iText.StyledXmlParser.Jsoup.Nodes.Attribute("type", "text/css")); JsoupElementNode node = new JsoupElementNode(jsoupLink); SvgConverterProperties scp = new SvgConverterProperties(); scp.SetBaseUri(baseUri); SvgProcessorContext processorContext = new SvgProcessorContext(scp); SvgStyleResolver sr = new SvgStyleResolver(node, processorContext); IDictionary <String, String> attr = sr.ResolveStyles(node, new SvgCssContext()); IDictionary <String, String> expectedAttr = new Dictionary <String, String>(); expectedAttr.Put(SvgConstants.Attributes.XMLNS, "http://www.w3.org/1999/xhtml"); expectedAttr.Put(SvgConstants.Attributes.REL, SvgConstants.Attributes.STYLESHEET); expectedAttr.Put(SvgConstants.Attributes.HREF, "styleSheetWithLinkStyle.css"); expectedAttr.Put("type", "text/css"); // Attribute from external stylesheet expectedAttr.Put(SvgConstants.Attributes.FILL, "black"); NUnit.Framework.Assert.AreEqual(expectedAttr, attr); }
/// <summary> /// Creates a new /// <see cref="ImgTagWorker"/> /// instance. /// </summary> /// <param name="element">the element</param> /// <param name="context">the context</param> public ObjectTagWorker(IElementNode element, ProcessorContext context) { image = null; res = null; processUtil = new SvgProcessingUtil(); //Retrieve object type String type = element.GetAttribute(AttributeConstants.TYPE); if (IsSvgImage(type)) { //Use resource resolver to retrieve the URL try { using (Stream svgStream = context.GetResourceResolver().RetrieveResourceAsInputStream(element.GetAttribute (AttributeConstants.DATA))) { if (svgStream != null) { try { SvgConverterProperties svgConverterProperties = new SvgConverterProperties(); svgConverterProperties.SetBaseUri(context.GetBaseUri()).SetFontProvider(context.GetFontProvider()).SetMediaDeviceDescription (context.GetDeviceDescription()); res = SvgConverter.ParseAndProcess(svgStream, svgConverterProperties); } catch (SvgProcessingException spe) { LOGGER.Error(spe.Message); } } } } catch (System.IO.IOException) { LOGGER.Error(MessageFormatUtil.Format(iText.Html2pdf.LogMessageConstant.UNABLE_TO_RETRIEVE_STREAM_WITH_GIVEN_BASE_URI , context.GetBaseUri(), element.GetAttribute(AttributeConstants.DATA))); } } }
public static SvgConverterProperties MapToSvgConverterProperties(ProcessorContext context) { SvgConverterProperties svgConverterProperties = new SvgConverterProperties(); svgConverterProperties.SetFontProvider(context.GetFontProvider()).SetBaseUri(context.GetBaseUri()).SetMediaDeviceDescription (context.GetDeviceDescription()); return(svgConverterProperties); }
public virtual void Convert(String svg, String output, PageSize size) { using (PdfDocument doc = new PdfDocument(new PdfWriter(output, new WriterProperties().SetCompressionLevel( 0)))) { doc.AddNewPage(size); ISvgConverterProperties properties = new SvgConverterProperties().SetBaseUri(svg); SvgConverter.DrawOnDocument(new FileStream(svg, FileMode.Open, FileAccess.Read), doc, 1, properties); } }
public static PdfDocument ConvertWithResult(String svg, String output) { PdfDocument doc = new PdfDocument(new PdfWriter(output, new WriterProperties().SetCompressionLevel(0))); doc.AddNewPage(); ISvgConverterProperties properties = new SvgConverterProperties().SetBaseUri(svg); SvgConverter.DrawOnDocument(new FileStream(svg, FileMode.Open, FileAccess.Read), doc, 1, properties); return(doc); }
public virtual void ResolveFontsWithoutWriterProperties() { String fileName = "fontSelectorTest"; ISvgConverterProperties properties = new SvgConverterProperties().SetFontProvider(new BasicFontProvider()) .SetMediaDeviceDescription(new MediaDeviceDescription(MediaType.ALL)); ConvertToSinglePage(new FileInfo(sourceFolder + fileName + ".svg"), new FileInfo(destinationFolder + fileName + ".pdf"), properties); Compare(fileName, sourceFolder, destinationFolder); }
public virtual void ResolveFontsDefaultUri() { SvgConverterProperties properties = new SvgConverterProperties(); properties.SetBaseUri(sourceFolder); String fileName = "fontSelectorTest02"; ConvertToSinglePage(new FileInfo(sourceFolder + fileName + ".svg"), new FileInfo(destinationFolder + fileName + ".pdf"), properties); Compare(fileName, sourceFolder, destinationFolder); }
public virtual void DrawOnPageStringPageConverterProps() { String name = "eclipse"; String destName = "DOP_" + name + "_StringPdfPageConverterProps"; PdfDocument doc = new PdfDocument(new PdfWriter(destinationFolder + destName + ".pdf")); PdfPage page = doc.AddNewPage(); ISvgConverterProperties props = new SvgConverterProperties(); SvgConverter.DrawOnPage(ECLIPSESVGSTRING, page, props); doc.Close(); NUnit.Framework.Assert.IsNull(new CompareTool().CompareVisually(destinationFolder + destName + ".pdf", sourceFolder + "cmp_" + destName + ".pdf", destinationFolder, "diff_")); }
/// <summary> /// Creates a new /// <see cref="SvgTagWorker"/> /// instance. /// </summary> /// <param name="element">the element</param> /// <param name="context">the context</param> public SvgTagWorker(IElementNode element, ProcessorContext context) { svgImage = null; try { SvgConverterProperties props = new SvgConverterProperties().SetBaseUri(context.GetBaseUri()); processingResult = new DefaultSvgProcessor().Process((INode)element, props); context.StartProcessingInlineSvg(); } catch (SvgProcessingException pe) { LogManager.GetLogger(typeof(iText.Html2pdf.Attach.Impl.Tags.SvgTagWorker)).Error(iText.Html2pdf.LogMessageConstant .UNABLE_TO_PROCESS_IMAGE_AS_SVG, pe); } }
public virtual void DrawOnCanvasStringPdfCanvasConverterProps() { String name = "eclipse"; String destName = "DOC_" + name + "_StringCanvasProps"; PdfDocument doc = new PdfDocument(new PdfWriter(destinationFolder + destName + ".pdf")); PdfCanvas canvas = new PdfCanvas(doc.AddNewPage()); ISvgConverterProperties props = new SvgConverterProperties(); SvgConverter.DrawOnCanvas(ECLIPSESVGSTRING, canvas, props); doc.Close(); NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(destinationFolder + destName + ".pdf", sourceFolder + "cmp_" + destName + ".pdf", destinationFolder, "diff_")); }
public virtual void ResolveFontsWithConverterPropsAndWriterProps() { String fileName = "resolveFonts_WithConverterPropsAndWriterProps"; String svgFile = "fontSelectorTest"; WriterProperties writerprops = new WriterProperties().SetCompressionLevel(0); String baseUri = FileUtil.GetParentDirectory(new FileInfo(sourceFolder + svgFile + ".svg")); ISvgConverterProperties properties = new SvgConverterProperties().SetBaseUri(baseUri).SetFontProvider(new BasicFontProvider()).SetMediaDeviceDescription(new MediaDeviceDescription(MediaType.ALL)); ConvertToSinglePage(new FileStream(sourceFolder + svgFile + ".svg", FileMode.Open, FileAccess.Read), new FileStream (destinationFolder + fileName + ".pdf", FileMode.Create), properties, writerprops); Compare(fileName, sourceFolder, destinationFolder); }
public virtual void DrawOnCanvasStreamPdfCanvasConverterProps() { String name = "eclipse"; String destName = "DOC_" + name + "_StreamCanvasProps"; PdfDocument doc = new PdfDocument(new PdfWriter(destinationFolder + destName + ".pdf")); FileStream fis = new FileStream(sourceFolder + name + ".svg", FileMode.Open, FileAccess.Read); PdfCanvas canvas = new PdfCanvas(doc.AddNewPage()); ISvgConverterProperties props = new SvgConverterProperties(); SvgConverter.DrawOnCanvas(fis, canvas, props); doc.Close(); NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(destinationFolder + destName + ".pdf", sourceFolder + "cmp_" + destName + ".pdf", destinationFolder, "diff_")); }
public virtual void DrawOnDocumentStringPdfDocumentIntConverterProperties() { String name = "eclipse"; String destName = "DOD_" + name + "_StringPdfDocumentIntProps"; FileStream fis = new FileStream(sourceFolder + name + ".svg", FileMode.Open, FileAccess.Read); PdfDocument doc = new PdfDocument(new PdfWriter(destinationFolder + destName + ".pdf")); doc.AddNewPage(); ISvgConverterProperties props = new SvgConverterProperties(); SvgConverter.DrawOnDocument(fis, doc, 1, props); doc.Close(); NUnit.Framework.Assert.IsNull(new CompareTool().CompareVisually(destinationFolder + destName + ".pdf", sourceFolder + "cmp_" + destName + ".pdf", destinationFolder, "diff_")); }
public virtual void ConvertToXObjectStringPdfDocumentConverterProps() { String name = "eclipse"; String destName = "CTXO_" + name + "_StringDocProps"; PdfDocument doc = new PdfDocument(new PdfWriter(destinationFolder + destName + ".pdf")); PdfPage page = doc.AddNewPage(); ISvgConverterProperties props = new SvgConverterProperties(); PdfXObject xObj = SvgConverter.ConvertToXObject(ECLIPSESVGSTRING, doc, props); PdfCanvas canv = new PdfCanvas(page); canv.AddXObject(xObj, 0, 0); doc.Close(); NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(destinationFolder + destName + ".pdf", sourceFolder + "cmp_" + destName + ".pdf", destinationFolder, "diff_")); }
public virtual void ConvertToXObjectStreamPdfDocumentConverterProps() { String name = "eclipse"; String destName = "CTXO_" + name + "_StreamDocProps"; FileStream fis = new FileStream(sourceFolder + name + ".svg", FileMode.Open, FileAccess.Read); PdfDocument doc = new PdfDocument(new PdfWriter(destinationFolder + destName + ".pdf")); PdfPage page = doc.AddNewPage(); ISvgConverterProperties props = new SvgConverterProperties(); PdfXObject xObj = SvgConverter.ConvertToXObject(fis, doc, props); PdfCanvas canv = new PdfCanvas(page); canv.AddXObject(xObj, 0, 0); doc.Close(); NUnit.Framework.Assert.IsNull(new CompareTool().CompareVisually(destinationFolder + destName + ".pdf", sourceFolder + "cmp_" + destName + ".pdf", destinationFolder, "diff_")); }
public virtual void SvgCssResolveHashXlinkTest() { iText.StyledXmlParser.Jsoup.Nodes.Element jsoupImage = new iText.StyledXmlParser.Jsoup.Nodes.Element(iText.StyledXmlParser.Jsoup.Parser.Tag .ValueOf("image"), ""); iText.StyledXmlParser.Jsoup.Nodes.Attributes imageAttributes = jsoupImage.Attributes(); imageAttributes.Put(new iText.StyledXmlParser.Jsoup.Nodes.Attribute("xlink:href", "#testid")); JsoupElementNode node = new JsoupElementNode(jsoupImage); SvgConverterProperties scp = new SvgConverterProperties(); scp.SetBaseUri(baseUri); SvgProcessorContext processorContext = new SvgProcessorContext(scp); SvgStyleResolver sr = new SvgStyleResolver(node, processorContext); IDictionary <String, String> attr = sr.ResolveStyles(node, new SvgCssContext()); NUnit.Framework.Assert.AreEqual("#testid", attr.Get("xlink:href")); }
public virtual void ConvertToImageStreamDocumentConverterProperties() { String name = "eclipse"; FileStream fis = new FileStream(sourceFolder + name + ".svg", FileMode.Open, FileAccess.Read); String destName = "CTI_" + name + "_StreamDocumentProps"; FileStream fos = new FileStream(destinationFolder + destName + ".pdf", FileMode.Create); PdfDocument pdfDocument = new PdfDocument(new PdfWriter(fos, new WriterProperties().SetCompressionLevel(0) )); ISvgConverterProperties props = new SvgConverterProperties(); Image image = SvgConverter.ConvertToImage(fis, pdfDocument, props); Document doc = new Document(pdfDocument); doc.Add(image); doc.Close(); NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(destinationFolder + destName + ".pdf", sourceFolder + "cmp_" + name + ".pdf", destinationFolder, "diff_")); }
/// <exception cref="System.IO.IOException"/> private PdfFormXObject ProcessAsSvg(Stream stream, ProcessorContext context) { SvgProcessingUtil processingUtil = new SvgProcessingUtil(); SvgConverterProperties svgConverterProperties = new SvgConverterProperties(); svgConverterProperties.SetBaseUri(context.GetBaseUri()).SetFontProvider(context.GetFontProvider()).SetMediaDeviceDescription (context.GetDeviceDescription()); ISvgProcessorResult res = SvgConverter.ParseAndProcess(stream, svgConverterProperties); if (context.GetPdfDocument() != null) { return(processingUtil.CreateXObjectFromProcessingResult(res, context.GetPdfDocument())); } else { return(null); } }
public virtual void SvgCssResolverXlinkTest() { iText.StyledXmlParser.Jsoup.Nodes.Element jsoupImage = new iText.StyledXmlParser.Jsoup.Nodes.Element(iText.StyledXmlParser.Jsoup.Parser.Tag .ValueOf("image"), ""); iText.StyledXmlParser.Jsoup.Nodes.Attributes imageAttributes = jsoupImage.Attributes(); imageAttributes.Put(new iText.StyledXmlParser.Jsoup.Nodes.Attribute("xlink:href", "itis.jpg")); JsoupElementNode node = new JsoupElementNode(jsoupImage); SvgConverterProperties scp = new SvgConverterProperties(); scp.SetBaseUri(baseUri); SvgProcessorContext processorContext = new SvgProcessorContext(scp); SvgStyleResolver sr = new SvgStyleResolver(node, processorContext); IDictionary <String, String> attr = sr.ResolveStyles(node, new SvgCssContext()); String fileName = baseUri + "itis.jpg"; String expectedURL = UrlUtil.ToNormalizedURI(fileName).ToString(); NUnit.Framework.Assert.AreEqual(expectedURL, attr.Get("xlink:href")); }
/// <exception cref="System.IO.IOException"/> private PdfFormXObject ProcessAsSvg(Stream stream, ProcessorContext context, String parentDir) { SvgProcessingUtil processingUtil = new SvgProcessingUtil(); SvgConverterProperties svgConverterProperties = ContextMappingHelper.MapToSvgConverterProperties(context); if (parentDir != null) { svgConverterProperties.SetBaseUri(parentDir); } ISvgProcessorResult res = SvgConverter.ParseAndProcess(stream, svgConverterProperties); if (context.GetPdfDocument() != null) { return(processingUtil.CreateXObjectFromProcessingResult(res, context.GetPdfDocument())); } else { return(null); } }
public virtual void ClipPathCss() { //TODO: update after DEVSIX-2827 properties = new SvgConverterProperties().SetBaseUri(sourceFolder); ConvertAndCompareSinglePage(sourceFolder, destinationFolder, "clipPathCss", properties); }
public virtual void ClipPathCssProperty() { properties = new SvgConverterProperties().SetBaseUri(sourceFolder); ConvertAndCompareSinglePage(sourceFolder, destinationFolder, "clipPathCssProperty", properties); }