public virtual void DrawOnDocumentStreamPdfDocumentIntConverterProperties() { String name = "eclipse"; String destName = "DOD_" + name + "_StreamPdfDocumentIntProps"; PdfDocument doc = new PdfDocument(new PdfWriter(destinationFolder + destName + ".pdf")); doc.AddNewPage(); ISvgConverterProperties props = new SvgConverterProperties(); SvgConverter.DrawOnDocument(ECLIPSESVGSTRING, doc, 1, props); doc.Close(); NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(destinationFolder + destName + ".pdf", sourceFolder + "cmp_" + destName + ".pdf", destinationFolder, "diff_")); }
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 UnusedXObjectIntegrationTest() { // This method tests that making an XObject does not, in itself, influence the document it's for. PdfDocument doc1 = new PdfDocument(new PdfWriter(destinationFolder + "unusedXObjectIntegrationTest1.pdf")); PdfDocument doc2 = new PdfDocument(new PdfWriter(destinationFolder + "unusedXObjectIntegrationTest2.pdf")); doc1.AddNewPage(); doc2.AddNewPage(); SvgConverter.ConvertToXObject("<svg width='100pt' height='100pt' />", doc1); doc1.Close(); doc2.Close(); NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(destinationFolder + "unusedXObjectIntegrationTest1.pdf" , destinationFolder + "unusedXObjectIntegrationTest2.pdf", destinationFolder, "diff_")); }
public virtual void CreatePdf(String dest) { using (FileStream svg = File.Open(SVG, FileMode.Open)) { // This approach (commented below) directly converts from SVG to PDF. //SvgConverter.CreatePdf(new FileInfo(SVG), new FileInfo(DEST)); // This approach (below) creates a PDF document, then adds SVG to that document. PdfDocument pdf = new PdfDocument(new PdfWriter(DEST)); Document doc = new Document(pdf); Image converted = SvgConverter.ConvertToImage(svg, pdf); doc.Add(converted); doc.Close(); } }
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_")); }
private async void ConvertOnClick(object sender, RoutedEventArgs e) { Error.Text = string.Empty; this.ProgressBar.Visibility = Visibility.Visible; try { await SvgConverter.Convert(ViewModel, fileName => { }); } catch (Exception ex) { Error.Text = ex.Message; } this.ProgressBar.Visibility = Visibility.Hidden; }
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 ConvertToImageStreamDocument() { String name = "eclipse"; FileStream fis = new FileStream(sourceFolder + name + ".svg", FileMode.Open, FileAccess.Read); String destName = "CTI_" + name + "_StreamDocument"; FileStream fos = new FileStream(destinationFolder + destName + ".pdf", FileMode.Create); PdfDocument pdfDocument = new PdfDocument(new PdfWriter(fos, new WriterProperties().SetCompressionLevel(0) )); Image image = SvgConverter.ConvertToImage(fis, pdfDocument); 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_")); }
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 AddToExistingDoc() { PdfReader reader = new PdfReader(sourceFolder + "cmp_eclipse.pdf"); PdfWriter writer = new PdfWriter(destinationFolder + "addToExistingDoc.pdf"); PdfDocument pdfDoc = new PdfDocument(reader, writer); pdfDoc.AddNewPage(); String output = destinationFolder + "addToExistingDoc.pdf"; String cmp_file = sourceFolder + "cmp_addToExistingDoc.pdf"; int pagenr = 1; FileStream fis = new FileStream(sourceFolder + "minimal.svg", FileMode.Open, FileAccess.Read); SvgConverter.DrawOnDocument(fis, pdfDoc, pagenr); pdfDoc.Close(); NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(output, cmp_file, destinationFolder, "diff_" )); }
public virtual void NoBoundingBoxOnXObjectTest() { NUnit.Framework.Assert.That(() => { PdfDocument document = new PdfDocument(new PdfWriter(new MemoryStream(), new WriterProperties().SetCompressionLevel (0))); document.AddNewPage(); ISvgNodeRenderer processed = SvgConverter.Process(SvgConverter.Parse("<svg />")).GetRootRenderer(); PdfRootSvgNodeRenderer root = new PdfRootSvgNodeRenderer(processed); PdfFormXObject pdfForm = new PdfFormXObject(new PdfStream()); PdfCanvas canvas = new PdfCanvas(pdfForm, document); SvgDrawContext context = new SvgDrawContext(null, null); context.PushCanvas(canvas); root.Draw(context); } , NUnit.Framework.Throws.InstanceOf <SvgProcessingException>().With.Message.EqualTo(SvgLogMessageConstant.ROOT_SVG_NO_BBOX)) ; }
/// <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); } }
/// <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 PdfFromSvgString() { PdfWriter writer = new PdfWriter(destinationFolder + "pdfFromSvgString.pdf"); PdfDocument pdfDoc = new PdfDocument(writer); pdfDoc.AddNewPage(); String svg = "<?xml version=\"1.0\" standalone=\"no\"?>\n" + "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n" + " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n" + "<svg width=\"500\" height=\"400\" xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\">\n" + " <rect width=\"500\" height=\"400\" fill=\"none\" stroke=\"black\"/>\n" + " <line x1=\"0\" y1=\"0\" x2=\"20\" y2=\"135\" stroke=\"black\"/>\n" + " <circle cx=\"20\" cy=\"135\" r=\"5\" fill=\"none\" stroke=\"black\"/>\n" + " <text x=\"20\" y=\"135\" font-family=\"Verdana\" font-size=\"35\">\n" + " Hello world\n" + " </text>\n" + "</svg>"; int pagenr = 1; SvgConverter.DrawOnDocument(svg, pdfDoc, pagenr); String output = destinationFolder + "pdfFromSvgString.pdf"; String cmp_file = sourceFolder + "cmp_pdfFromSvgString.pdf"; pdfDoc.Close(); NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(output, cmp_file, destinationFolder, "diff_" )); }
/// <summary> /// Create an /// <see cref="iText.Kernel.Pdf.Xobject.PdfFormXObject"/> /// tied to the passed /// <c>PdfDocument</c> /// using the SVG processing result. /// </summary> /// <param name="result">Processing result containing the SVG information</param> /// <param name="pdfDocument">pdf that shall contain the image</param> /// <returns>PdfFormXObject instance</returns> public virtual PdfFormXObject CreateXObjectFromProcessingResult(ISvgProcessorResult result, PdfDocument pdfDocument ) { ISvgNodeRenderer topSvgRenderer = result.GetRootRenderer(); float width; float height; float[] wh = SvgConverter.ExtractWidthAndHeight(topSvgRenderer); width = wh[0]; height = wh[1]; PdfFormXObject pdfForm = new PdfFormXObject(new Rectangle(0, 0, width, height)); PdfCanvas canvas = new PdfCanvas(pdfForm, pdfDocument); SvgDrawContext context = new SvgDrawContext(null, result.GetFontProvider()); context.AddNamedObjects(result.GetNamedObjects()); context.PushCanvas(canvas); ISvgNodeRenderer root = new PdfRootSvgNodeRenderer(topSvgRenderer); root.Draw(context); return(pdfForm); }
private async void ConvertOnClick(object sender, RoutedEventArgs e) { Error.Text = string.Empty; ProgressBar.Visibility = Visibility.Visible; try { await SvgConverter.Convert(ViewModel, fileName => { if (ViewModel.AddItemsToProject) { ViewModel.SelectedProject?.ProjectItems.AddFromFile(fileName); } }); } catch (Exception ex) { Error.Text = ex.Message; } ProgressBar.Visibility = Visibility.Hidden; }
public virtual void AttemptToProcessBySvgProcessingUtilSvgWithImageTest() { // TODO review this test in the scope of DEVSIX-4107 String fileName = "svgWithImage.svg"; ProcessorContext context = new ProcessorContext(new ConverterProperties()); HtmlResourceResolver resourceResolver = new HtmlResourceResolver(sourceFolder, context); ISvgConverterProperties svgConverterProperties = ContextMappingHelper.MapToSvgConverterProperties(context); ISvgProcessorResult res = SvgConverter.ParseAndProcess(resourceResolver.RetrieveResourceAsInputStream(fileName ), svgConverterProperties); ISvgNodeRenderer imageRenderer = ((SvgTagSvgNodeRenderer)res.GetRootRenderer()).GetChildren()[0]; // Remove the previous result of the resource resolving in order to demonstrate that the resource will not be // resolved due to not setting of baseUri in the SvgProcessingUtil#createXObjectFromProcessingResult method. imageRenderer.SetAttribute(SvgConstants.Attributes.XLINK_HREF, "doggo.jpg"); SvgProcessingUtil processingUtil = new SvgProcessingUtil(resourceResolver); PdfDocument pdfDocument = new PdfDocument(new PdfWriter(new ByteArrayOutputStream())); PdfFormXObject pdfFormXObject = processingUtil.CreateXObjectFromProcessingResult(res, pdfDocument); PdfDictionary resources = (PdfDictionary)pdfFormXObject.GetResources().GetPdfObject().Get(PdfName.XObject); PdfDictionary fm1Dict = (PdfDictionary)resources.Get(new PdfName("Fm1")); NUnit.Framework.Assert.IsFalse(((PdfDictionary)fm1Dict.Get(PdfName.Resources)).ContainsKey(PdfName.XObject )); }
public virtual void ParseAndProcessSuccessTest() { String name = "minimal"; FileStream fis = new FileStream(sourceFolder + name + ".svg", FileMode.Open, FileAccess.Read); IDictionary <String, ISvgNodeRenderer> map = new Dictionary <String, ISvgNodeRenderer>(); RectangleSvgNodeRenderer rect = new RectangleSvgNodeRenderer(); rect.SetAttribute("fill", "none"); rect.SetAttribute("stroke", "black"); rect.SetAttribute("width", "500"); rect.SetAttribute("height", "400"); ISvgNodeRenderer root = new SvgTagSvgNodeRenderer(); root.SetAttribute("xmlns", "http://www.w3.org/2000/svg"); root.SetAttribute("version", "1.1"); root.SetAttribute("width", "500"); root.SetAttribute("height", "400"); ISvgProcessorResult expected = new SvgProcessorResult(map, root, new FontProvider(), new FontSet()); ISvgProcessorResult actual = SvgConverter.ParseAndProcess(fis); NUnit.Framework.Assert.AreEqual(expected.GetRootRenderer().GetAttributeMapCopy(), actual.GetRootRenderer() .GetAttributeMapCopy()); }
/// <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(context.GetResourceResolver()); //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 (System.IO.IOException ie) { LOGGER.Error(MessageFormatUtil.Format(iText.Html2pdf.LogMessageConstant.UNABLE_TO_RETRIEVE_STREAM_WITH_GIVEN_BASE_URI , context.GetBaseUri(), element.GetAttribute(AttributeConstants.DATA), ie)); } catch (UriFormatException 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 ParseStreamPropsNullTest() { SvgConverter.Parse(@is, null); }
public virtual void ConvertToXObjectInputStreamPropsNullTest() { SvgConverter.ConvertToXObject(@is, doc, null); }
public virtual void ConvertToXObjectStringPropsNullTest() { SvgConverter.ConvertToXObject(content, doc, null); }
public virtual void DrawOnCanvasInputStreamPropsNullTest() { PdfCanvas canvas = new PdfCanvas(doc.GetLastPage()); SvgConverter.DrawOnCanvas(@is, canvas, null); }
public virtual void DrawOnCanvasStringPropsNullTest() { PdfCanvas canvas = new PdfCanvas(doc.GetLastPage()); SvgConverter.DrawOnCanvas(content, canvas, null); }
public virtual void DrawOnPageInputStreamPropsNullTest() { PdfPage page = doc.GetFirstPage(); SvgConverter.DrawOnPage(@is, page, null); }
public virtual void DrawOnPageStringPropsNullTest() { PdfPage page = doc.GetFirstPage(); SvgConverter.DrawOnPage(content, page, null); }
public virtual void DrawOnDocumentInputStreamPropsNullTest() { SvgConverter.DrawOnDocument(@is, doc, 1, null); }
public virtual void DrawOnDocumentStringPropsNullTest() { SvgConverter.DrawOnDocument(content, doc, 1, null); }
private SvgRectF GetElementBounds(SvgTransformableElement element, float margin) { SvgRenderingHint hint = element.RenderingHint; if (hint == SvgRenderingHint.Shape || hint == SvgRenderingHint.Text) { GraphicsPath gp = GdiRendering.CreatePath(element); ISvgMatrix svgMatrix = element.GetScreenCTM(); Matrix matrix = new Matrix((float)svgMatrix.A, (float)svgMatrix.B, (float)svgMatrix.C, (float)svgMatrix.D, (float)svgMatrix.E, (float)svgMatrix.F); SvgRectF bounds = SvgConverter.ToRect(gp.GetBounds(matrix)); bounds = SvgRectF.Inflate(bounds, margin, margin); return(bounds); } SvgUseElement useElement = element as SvgUseElement; if (useElement != null) { SvgTransformableElement refEl = useElement.ReferencedElement as SvgTransformableElement; if (refEl == null) { return(SvgRectF.Empty); } XmlElement refElParent = (XmlElement)refEl.ParentNode; element.OwnerDocument.Static = true; useElement.CopyToReferencedElement(refEl); element.AppendChild(refEl); SvgRectF bbox = this.GetElementBounds(refEl, margin); element.RemoveChild(refEl); useElement.RestoreReferencedElement(refEl); refElParent.AppendChild(refEl); element.OwnerDocument.Static = false; return(bbox); } SvgRectF union = SvgRectF.Empty; SvgTransformableElement transformChild; foreach (XmlNode childNode in element.ChildNodes) { if (childNode is SvgDefsElement) { continue; } if (childNode is ISvgTransformable) { transformChild = (SvgTransformableElement)childNode; SvgRectF bbox = this.GetElementBounds(transformChild, margin); if (bbox != SvgRectF.Empty) { if (union == SvgRectF.Empty) { union = bbox; } else { union = SvgRectF.Union(union, bbox); } } } } return(union); }
public virtual void ConvertToSinglePage(FileInfo svg, FileInfo pdf, ISvgConverterProperties properties) { SvgConverter.CreatePdf(svg, pdf, properties); }