protected override void RenderImageInformation(PDFContextBase context, PDFWriter writer) { base.RenderImageInformation(context, writer); if (this.AlphaData != null) { PDFObjectRef alpha = this.RenderAlpaImageData(context, writer); writer.WriteDictionaryObjectRefEntry("SMask", alpha); } }
public void SetAlphaFilteredData(IStreamFilter[] filters, byte[] data, PDFContextBase context) { if (null == data) { throw new ArgumentNullException("data"); } this._filteredAlpha = data; }
/// <summary> /// Implementation method for the IPDFImageComponent to return the image data in a PDFImageXObject instance. /// </summary> /// <returns></returns> public virtual PDFImageXObject GetImageObject(PDFContextBase context, Style fullstyle) { if (null == this.XObject) { bool log = context.ShouldLogDebug; if (log) { context.TraceLog.Begin("Image", "Initializing and loading image for component " + this.UniqueID); } try { this.XObject = this.InitImageXObject(context, fullstyle); } catch (PDFMissingImageException ex) { if (this.AllowMissingImages && this.Document.RenderOptions.AllowMissingImages) { context.TraceLog.Add(TraceLevel.Error, "Image", "Missing Image replaced: " + ex.Message); this.Visible = false; } else { throw; } } catch (PDFException) { throw; } catch (Exception ex) { if (context.Conformance == ParserConformanceMode.Lax) { context.TraceLog.Add(TraceLevel.Error, "Load of image data failed. " + ex.Message, ex); } else { throw new PDFException(string.Format(Errors.CouldNotInitializeTheImageForComponent, this.UniqueID, ex.Message), ex); } } if (log) { if (null == this.XObject) { context.TraceLog.End("Image", "No image loaded for component " + this.UniqueID); } else { context.TraceLog.End("Image", "Initialized and loaded image '" + this.XObject.ToString() + "' for component " + this.UniqueID); } } } return(this.XObject); }
private void AssertInnerStyles(PDFContextBase context) { if (!string.IsNullOrEmpty(this.Contents)) { if (null == this._innerItems) { this._innerItems = CreateInnerStyles(context); } } }
protected virtual string DoLoadRemoteReference(string path, PDFContextBase context) { //TODO: Use the document for any client web requests. //context.Document.LoadRemoteResource(path, context, new RemoteResourceRequest(DoLoadReferenceResult)); this.LoadedSource = path; var request = System.Net.HttpWebRequest.Create(path); using (var result = request.GetResponse()) return(this.DoLoadReferenceResult(result, path, context)); }
protected virtual bool ShouldApplyFilters(PDFContextBase context) { if (this.HasFilter) { if (context.Compression == OutputCompressionType.FlateDecode || this.IsPrecompressedData) { return(true); } } return(false); }
protected override IEnumerable <IPDFComponent> DoParseContents(PDFContextBase context) { if (!string.IsNullOrEmpty(this.HRef)) { return(GetContentFromHRef(this.HRef, context)); } else { return(base.DoParseContents(context)); } }
protected virtual void UpdateShapeBounds(PDFContextBase context) { var bounds = this.GetBounds(); if (bounds != PDFRect.Empty) { this.X = bounds.X; this.Y = bounds.Y; this.Width = bounds.Width; this.Height = bounds.Height; } }
protected virtual PDFObjectRef RenderShadingDictionary(PDFContextBase context, PDFWriter writer) { PDFPoint offset = new PDFPoint(this.Start.X, this.Start.Y);// this.Start; PDFSize size = this.Size; PDFSize graphicsSize = new PDFSize(size.Width + offset.X, size.Height + offset.Y); var func = this._descriptor.GetGradientFunction(offset, size); var coords = GetCoords(offset, size, _descriptor.Size, _descriptor.XCentre, _descriptor.YCentre); writer.BeginDictionaryEntry("Shading"); writer.BeginDictionary(); writer.WriteDictionaryNumberEntry("ShadingType", (int)ShadingType.Radial); writer.WriteDictionaryNameEntry("ColorSpace", "DeviceRGB"); writer.WriteDictionaryBooleanEntry("AntiAlias", true); writer.BeginDictionaryEntry("BBox"); writer.WriteArrayRealEntries(true, offset.X.PointsValue, offset.Y.PointsValue, offset.X.PointsValue + size.Width.PointsValue, offset.Y.PointsValue + size.Height.PointsValue); writer.EndDictionaryEntry(); writer.BeginDictionaryEntry("Coords"); writer.WriteArrayRealEntries(true, coords); writer.EndDictionaryEntry(); writer.BeginDictionaryEntry("Extend"); writer.BeginArray(); writer.BeginArrayEntry(); writer.WriteBooleanS(true); writer.EndArrayEntry(); writer.BeginArrayEntry(); writer.WriteBooleanS(true); writer.EndArrayEntry(); writer.EndArray(); writer.EndDictionaryEntry(); if (null != func) { writer.BeginDictionaryEntry("Function"); func.WriteFunctionDictionary(context, writer); writer.EndDictionaryEntry(); } writer.EndDictionary();//shading return(null); }
protected StyleCollection CreateInnerStyles(string content, PDFContextBase context) { var collection = new StyleCollection(); if (context.TraceLog.ShouldLog(TraceLevel.Verbose)) { context.TraceLog.Add(TraceLevel.Verbose, "HTML", "Parsing the css selectors from string for link " + this.UniqueID); } this.ParseCssStyles(collection, content, context); return(collection); }
public override VisualComponent DoBuildItemField(PDFContextBase context) { Link link = new Link(); link.Action = LinkAction.Uri; link.File = this.LinkUrl; Label label = new Label(); label.Text = string.IsNullOrEmpty(this.LinkText) ? this.LinkUrl : this.LinkText; link.Contents.Add(label); return(link); }
private void InitAndLoadRoot(Component built, PDFContextBase context) { if (context.TraceLog.ShouldLog(TraceLevel.Debug)) { context.TraceLog.Add(TraceLevel.Debug, "DataGrid", "Initializing and loading root component before binding"); } PDFTraceLog log = context.TraceLog; PDFInitContext init = new PDFInitContext(context.Items, log, context.PerformanceMonitor, this.Document); PDFLoadContext load = new PDFLoadContext(context.Items, log, context.PerformanceMonitor, this.Document); built.Init(init); built.Load(load); }
public CSSStyleParser(string content, PDFContextBase context) { this.Content = content; this._err = new List <CSSParsingError>(); this.Context = context; if (null == context) { this.Log = new Logging.DoNothingTraceLog(TraceRecordLevel.Off); } else { this.Log = context.TraceLog; } }
public override void AddContent(Component component, PDFContextBase context) { var doc = component.Document; if (null == _parsedGroup) { _parsedGroup = new StyleGroup(); foreach (var style in this._parsed) { _parsedGroup.Styles.Add(style); } doc.Styles.Add(_parsedGroup); } }
protected virtual PDFAcrobatFormFieldWidget GetFieldEntry(PDFContextBase context) { if (string.IsNullOrEmpty(this.Name)) { if (string.IsNullOrEmpty(this.ID)) { this.ID = this.GetIncrementID(this.Type); } this.Name = this.UniqueID; } PDFAcrobatFormFieldWidget entry = new PDFAcrobatFormFieldWidget(this.Name, this.Value, this.DefaultValue, this.FieldType, this.Options); return(entry); }
/// <summary> /// renders this instance to the output writer if the resource has been registered. /// Resources are only rendered once, so if this resource has already been rendered it simply returns the previous reference /// </summary> /// <param name="context">The context associated with the rendering</param> /// <param name="writer">The writer to output any instructions to</param> /// <returns>A PDFObject reference for this resource</returns> /// <remarks>In order to ensure that this resource is output, then call the RegisterUse with a resource collection.</remarks> public PDFObjectRef EnsureRendered(PDFContextBase context, PDFWriter writer) { if (this.Registered) { if (null == this.RenderReference) { this.RenderReference = this.DoRenderToPDF(context, writer); } return(this.RenderReference); } else { return(null); } }
protected virtual string DoLoadRemoteReference(string path, PDFContextBase context) { //TODO: Use the document for any client web requests. //context.Document.LoadRemoteResource(path, context, new RemoteResourceRequest(DoLoadReferenceResult)); string content; HttpClient client = null; bool dispose = false; try { this.LoadedSource = path; client = this.GetServiceClient(); if (null == client) { client = new HttpClient(); dispose = true; } lock (context.Document) { var task = client.GetStreamAsync(path); var awaiter = task.GetAwaiter(); using (var response = task.Result) content = this.DoLoadReferenceResult(response, path, context); } } catch (Exception ex) { content = string.Empty; if (context.Conformance == ParserConformanceMode.Lax) { context.TraceLog.Add(TraceLevel.Error, "HTML", "Could not load link href the response from '" + path + "'", ex); } else { throw; } } finally { if (null != client && dispose) { client.Dispose(); } } return(content); }
protected override PDFObjectRef DoRenderToPDF(PDFContextBase context, PDFWriter writer) { PDFObjectRef oref = writer.BeginObject(); writer.BeginDictionaryS(); writer.WriteDictionaryNameEntry("Type", "ExtGState"); foreach (PDFName name in this.States.Keys) { writer.BeginDictionaryEntry(name); this.States[name].WriteData(writer); writer.EndDictionaryEntry(); } writer.EndDictionary(); writer.EndObject(); return(oref); }
public override void WriteFunctionDictionary(PDFContextBase context, PDFWriter writer) { writer.BeginDictionary(); writer.WriteDictionaryNumberEntry("FunctionType", 3); writer.BeginDictionaryEntry("Domain"); writer.WriteArrayRealEntries(this.DomainStart, this.DomainEnd); writer.EndDictionaryEntry(); //The bounds is the function extents of the functions writer.BeginDictionaryEntry("Bounds"); writer.BeginArray(); foreach (var boundary in this.Boundaries) { writer.BeginArrayEntry(); writer.WriteRealS(boundary.Bounds); writer.EndArrayEntry(); } writer.EndArray(); writer.EndDictionaryEntry(); //Write the array of function 2 (Axial aka Linear between 2 colours) List <double> encodes = new List <double>(); writer.BeginDictionaryEntry("Functions"); writer.BeginArray(); foreach (var func in this.Functions) { writer.BeginArrayEntry(); func.WriteFunctionDictionary(context, writer); writer.EndArrayEntry(); //May need to change these values encodes.Add(0); encodes.Add(1); } writer.EndArray(); writer.EndDictionaryEntry(); //Write the encodes for each of the functions 0 1 in a single array writer.BeginDictionaryEntry("Encode"); writer.WriteArrayRealEntries(encodes.ToArray()); writer.EndDictionaryEntry(); writer.EndDictionary(); }
protected virtual void BuildContents(PDFContextBase context) { if (this.LayoutType != FieldLayoutType.ValueOnly) { Label label = new Label(); label.Text = this.FieldLabel; if (!string.IsNullOrEmpty(this.LabelPostFix) && !string.IsNullOrEmpty(this.FieldLabel)) { label.Text += this.LabelPostFix; } label.StyleClass = this.LabelClass; this.Contents.Add(label); this._labelItem = label; if (this.LayoutType == FieldLayoutType.Above) { label.PositionMode = Drawing.PositionMode.Block; } else if (this.LayoutType == FieldLayoutType.NextTo) { this.PositionMode = Drawing.PositionMode.Block; label.PositionMode = Drawing.PositionMode.Block; this.ColumnCount = 2; this.Contents.Add(new ColumnBreak()); } else { label.PositionMode = Drawing.PositionMode.Inline; } } VisualComponent field = this.DoBuildItemField(context); field.StyleClass = this.ValueClass; this.Contents.Add(field); if (this.LayoutType == FieldLayoutType.Inline) { field.PositionMode = Drawing.PositionMode.Inline; } else { field.PositionMode = Drawing.PositionMode.Block; } }
private PDFObjectRef RenderAlpaImageData(PDFContextBase context, PDFWriter writer) { context.TraceLog.Add(TraceLevel.Debug, "IMAGE", "Rendering image alpha mask"); PDFObjectRef mask = writer.BeginObject(); writer.BeginDictionary(); writer.WriteDictionaryNameEntry("Type", "XObject"); writer.WriteDictionaryNameEntry("Subtype", "Image"); writer.WriteDictionaryNumberEntry("Width", this.PixelWidth); writer.WriteDictionaryNumberEntry("Height", this.PixelHeight); //writer.WriteDictionaryNumberEntry("Length", this.AlphaData.LongLength); writer.WriteDictionaryNameEntry("ColorSpace", "DeviceGray"); writer.WriteDictionaryNumberEntry("BitsPerComponent", 8); this.WriteFilterNames(context, writer); //writer.EndDictionary(); writer.BeginStream(mask); byte[] data; if (this.HasFilter && this.ShouldApplyFilters(context)) { data = this.GetAlphaFilteredData(this.Filters, context); if (null == data) { data = this.ApplyFiltersToData(this.AlphaData, context); this.SetAlphaFilteredData(this.Filters, data, context); } } else { data = this.AlphaData; } writer.WriteRaw(data, 0, data.Length); writer.EndStream(); //inserted 15/01/15 - Write the filtered length, not the actua image data length to the dictionary. writer.WriteDictionaryNumberEntry("Length", data.Length); writer.EndDictionary(); //end of insert writer.EndObject(); return(mask); }
protected byte[] ApplyFiltersToData(byte[] data, PDFContextBase context) { if (this.ShouldApplyFilters(context)) { int origlength = data.Length; if (context.ShouldLogDebug) { context.TraceLog.Add(TraceLevel.Debug, "ImageData", "Applying filters to image data for '" + this.SourcePath + "'. Original length = " + origlength); } if (this.Filters.Length == 1) { data = this.Filters[0].FilterStream(data); } else { for (int i = 0; i < this.Filters.Length; i++) { if (context.ShouldLogDebug) { context.TraceLog.Add(TraceLevel.Debug, "ImageData", "Applying filter '" + this.Filters[i].FilterName + "' to image data."); } data = this.Filters[i].FilterStream(data); } } int finallength = data.Length; if (context.ShouldLogDebug) { context.TraceLog.Add(TraceLevel.Debug, "ImageData", "Applied all filters to image data for '" + this.SourcePath + "'. Final length = " + finallength); } else if (context.ShouldLogMessage) { context.TraceLog.Add(TraceLevel.Message, "ImageData", "Filters applied to image data for '" + this.SourcePath + "', and size has gone from " + origlength + " to " + finallength); } } else if (context.ShouldLogDebug) { context.TraceLog.Add(TraceLevel.Debug, "ImageData", "Should not apply filters to image data for '" + this.SourcePath + "', returning original data"); } return(data); }
private bool TryGetFont(IPDFDocument doc, PDFContextBase context, out PDFFontDefinition definition) { System.Drawing.FontStyle style = System.Drawing.FontStyle.Regular; if (this.FontBold) { style |= System.Drawing.FontStyle.Bold; } if (this.FontItalic) { style |= System.Drawing.FontStyle.Italic; } string name = this.FontFamily.FamilyName; PDFFontFactory.TryEnsureFont(doc, context, this.Source, name, style, out definition); return(null != definition); }
internal PDFObjectRef Render(PDFName name, PDFContextBase context, PDFWriter writer) { if (context.ShouldLogDebug) { context.TraceLog.Begin(TraceLevel.Message, "Image Data", "Rendering image data for '" + name.ToString() + "'"); } PDFObjectRef renderref = writer.BeginObject(name.Value); writer.BeginDictionaryS(); writer.WriteDictionaryNameEntry("Name", name.Value); writer.WriteDictionaryNameEntry("Type", "XObject"); writer.WriteDictionaryNameEntry("Subtype", "Image"); RenderImageInformation(context, writer); //writer.EndDictionary(); //- commented for data length fix writer.BeginStream(renderref); int length = this.RenderImageStreamData(context, writer); writer.EndStream(); //Added for Data Length fix HRB 15/01/2015 writer.WriteDictionaryNumberEntry("Length", length); writer.EndDictionary(); //End of add writer.EndObject(); if (context.ShouldLogDebug) { context.TraceLog.End(TraceLevel.Message, "Image Data", "Completed render of the image data for '" + name + "' with source " + this.SourcePath); } else { context.TraceLog.Add(TraceLevel.Message, "Image Data", "Rendered the image data for '" + name.ToString() + "' with source " + this.SourcePath); } return(renderref); }
private bool ShouldAddContent(OutputFormat format, PDFContextBase context, out HTMLLinkType type) { type = HTMLLinkType.Other; //If we have a media value and it's not for this format, then we don't add them if (null != Media && this.Media.IsMatchedTo(format) == false) { return(false); } if (this.Visible == false) { return(false); } if (string.IsNullOrEmpty(this.Relationship)) { if (context.Conformance == ParserConformanceMode.Lax) { context.TraceLog.Add(TraceLevel.Error, "HTML", "The 'rel'ationship attribute is required on a html 'link' tag."); return(false); } else { throw new PDFParserException("The 'rel'ationship attribute is required on a html 'link' tag."); } } if (this.Relationship.Equals("stylesheet", StringComparison.OrdinalIgnoreCase) || string.IsNullOrEmpty(this.Relationship)) { type = HTMLLinkType.CSS; return(true); } else if (this.Relationship.Equals("import")) { type = HTMLLinkType.Html; return(true); } else { return(false); } }
protected override Resources.PDFImageXObject InitImageXObject(PDFContextBase context, Style style) { Document doc = this.Document; if (null == doc) { throw new NullReferenceException(Errors.ParentDocumentCannotBeNull); } if (null != this.Data) { _xobj = null; if (!string.IsNullOrEmpty(this.ImageKey)) { _xobj = this.Document.GetImageResource(this.ImageKey, this, false); } if (null == _xobj) { string name; if (string.IsNullOrEmpty(this.ImageKey)) { name = "DataImage_" + this.Document.GetIncrementID(PDFObjectTypes.ImageXObject); } else { name = this.ImageKey; } System.ComponentModel.TypeConverter BitmapConverter = TypeDescriptor.GetConverter(typeof(Bitmap)); Bitmap img = (Bitmap)BitmapConverter.ConvertFrom(this.Data.Raw); PDFImageData data = PDFImageData.LoadImageFromBitmap(name, img, this.Compress); _xobj = PDFImageXObject.Load(data, name); this.Document.SharedResources.Add(_xobj); } } return(_xobj); }
protected override PDFObjectRef DoRenderToPDF(PDFContextBase context, PDFWriter writer) { PDFObjectRef oref = writer.BeginObject(this.Name.Value); writer.BeginDictionary(); writer.WriteDictionaryNameEntry("Type", "Pattern"); writer.WriteDictionaryNumberEntry("PatternType", (int)this.PatternType); //Actual shading dictionary var shading = this.RenderShadingDictionary(context, writer); if (null != shading) { writer.WriteDictionaryObjectRefEntry("Shading", shading); } writer.EndDictionary(); writer.EndObject(); return(oref); }
public override void WriteFunctionDictionary(PDFContextBase context, PDFWriter writer) { writer.BeginDictionary(); writer.WriteDictionaryNumberEntry("FunctionType", 2); writer.BeginDictionaryEntry("Domain"); writer.WriteArrayRealEntries(this.DomainStart, this.DomainEnd); writer.EndDictionaryEntry(); writer.BeginDictionaryEntry("C0"); writer.WriteArrayRealEntries(this.ColorZero.Red.Value, this.ColorZero.Green.Value, this.ColorZero.Blue.Value); writer.EndDictionaryEntry(); writer.BeginDictionaryEntry("C1"); writer.WriteArrayRealEntries(this.ColorOne.Red.Value, this.ColorOne.Green.Value, this.ColorOne.Blue.Value); writer.EndDictionaryEntry(); writer.WriteDictionaryRealEntry("N", this.Exponent); writer.EndDictionary(); //function }
public override void RenderWidthsArrayToPDF(PDFContextBase context, PDFWriter writer) { if (context.ShouldLogDebug) { context.TraceLog.Add(TraceLevel.Debug, "PDFFontWidths", "Width information will be rendered as a CID lookup set"); } List <char> keys = new List <char>(this._char2offset.Keys); keys.Sort(); writer.BeginArray(); SortedDictionary <int, int> glyph2width = new SortedDictionary <int, int>(); foreach (WidthMetric met in this._char2offset.Values) { glyph2width[met.Glyph] = met.Width; } if (context.ShouldLogDebug) { context.TraceLog.Add(TraceLevel.Debug, "PDFFontWidths", "CID Lookup built with glyph widths"); } //Writes the used characters as [c[w] c[w] c[w]] //TODO: Improve rendering foreach (int g in glyph2width.Keys) { int w = glyph2width[g]; writer.WriteNumber(g); writer.BeginArray(); writer.WriteNumber(w); writer.EndArray(); } writer.EndArray(); if (context.ShouldLogDebug) { context.TraceLog.Add(TraceLevel.Debug, "PDFFontWidths", glyph2width.Count.ToString() + " widths written as CID lookup set"); } }
protected void SetCachedFilteredData(IStreamFilter[] filters, byte[] data, PDFContextBase context) { if (null == filters) { throw new ArgumentNullException("filters"); } else if (filters.Length == 0) { throw new ArgumentOutOfRangeException("filters"); } if (null == data) { throw new ArgumentNullException("data"); } string key = GetFullFilterCacheKey(filters); this._filtercachekey = key; this._filterdata = data; }