protected void HandleTextBlock(TextBlock item, PdfPageWriter writer, Context context) { var physicalBox = ToPhysicalBox(item, context); var x = physicalBox[0]; var y = physicalBox[1]; var width = physicalBox[2] - physicalBox[0]; var height = physicalBox[3] - physicalBox[1]; var to = (PdfTextOptions)GetReferenceObject(item.TextOptionsRef ?? context.TextOptionsRef) ?? new PdfTextOptions(); var leftRotationDegrees = GetAngleValue(item.Rotation); var alignment = Int32.Parse(item.Alignment.CaseTranslate(StringComparison.OrdinalIgnoreCase, "", "-1", "left", "-1", "center", "0", "middle", "0", "right", "1", item.Alignment) ?? "-1"); // Correct y value to reflect top of textblock: y = y - to.FontSize; var content = item.Content; if (!String.IsNullOrWhiteSpace(item.ContentKey)) { if (this.ContentSource == null) { throw new NullReferenceException("Failed to render TextBlock content using ContentKey as ContentSource of PdfModelWriter is not set."); } content = this.ContentSource[item.ContentKey] ?? item.Content ?? String.Empty; } writer.DrawTextblock(x, y, content ?? String.Empty, width, to, leftRotationDegrees, alignment); }
protected void HandleLine(Line item, PdfPageWriter writer, Context context) { var physicalBox = ToPhysicalBox(item, context); var go = (PdfGraphicsOptions)GetReferenceObject(item.GraphicsOptionsRef ?? context.GraphicsOptionsRef) ?? new PdfGraphicsOptions(); writer.DrawLine(physicalBox[0], physicalBox[1], physicalBox[2], physicalBox[3], go); }
protected void Dispatch(IPageItem pageItem, PdfPageWriter writer, Context context) { if (!pageItem.Hidden) { this.GetType().GetMethod("Handle" + pageItem.GetType().Name, BindingFlags.Instance | BindingFlags.NonPublic).Invoke(this, new object[] { pageItem, writer, context }); } }
/// <summary> /// Writes a page item to the given PDF page writer. /// </summary> public void Write(IPageItem pageItem, PdfPageWriter writer) { var context = new Context(); context.Coordinates = GetCoordinates(writer); this.Dispatch(pageItem, writer, context); }
protected void HandleScript(Script item, PdfPageWriter writer, Context context) { if (!String.IsNullOrWhiteSpace(item.ScriptRef)) { writer.WriteObjectRef((PdfObjectRef)GetReferenceObject(item.ScriptRef)); } else { var so = new PdfScriptObject(); so.Write(item.Content); writer.WriteObject(so); } }
protected void HandleImage(Image item, PdfPageWriter writer, Context context) { var physicalBox = ToPhysicalBox(item, context); //var x = physicalBox[0]; //var y = physicalBox[1]; var x = Math.Min(physicalBox[0], physicalBox[2]); var y = Math.Min(physicalBox[1], physicalBox[3]); var width = Math.Abs(physicalBox[2] - physicalBox[0]); var height = Math.Abs(physicalBox[3] - physicalBox[1]); var placement = (PdfImagePlacement)Enum.Parse(typeof(PdfImagePlacement), item.Placement ?? "Stretch"); var rotation = GetAngleValue(item.Rotation); var imageRotation = (PdfImageRotation)Enum.Parse(typeof(PdfImageRotation), item.ImageRotation ?? "None"); if (String.IsNullOrWhiteSpace(item.ImageRef)) { item.ImageRef = item.FileName ?? item.Url ?? Guid.NewGuid().ToString(); if (!this.referables.ContainsKey(item.ImageRef)) { var img = LoadImage(item.FileName, item.Url); var pdfRef = writer.DocumentWriter.AddImage(img); SetReferenceObject(item.ImageRef, pdfRef); } } if (rotation != 0.0) { if (!String.IsNullOrWhiteSpace(item.ImageRef)) { var imgRef = (PdfObjectRef)GetReferenceObject(item.ImageRef); writer.DrawImageRef(x, y, imgRef, width, rotation); } else { var img = new System.Drawing.Bitmap(item.FileName ?? item.Url); writer.DrawImage(x, y, img, width, rotation); } } else { if (!String.IsNullOrWhiteSpace(item.ImageRef)) { var imgRef = (PdfObjectRef)GetReferenceObject(item.ImageRef); writer.DrawImageRef(x, y, imgRef, width, height, placement, imageRotation); } else { var img = new System.Drawing.Bitmap(item.FileName ?? item.Url); writer.DrawImage(x, y, img, width, height, placement, imageRotation); } } }
private static double WriteSampleLines(int[] sizes, PdfPageWriter page, double cursor, PdfFont font, string fontName) { page.DrawText(40, cursor, String.Format("{0} in sizes {1}:", fontName, String.Join(", ", sizes.Select(s => s.ToString()))), CaptionText); cursor -= CaptionText.FontSize; cursor -= 32; foreach (var size in sizes) { page.DrawText(40, cursor, "01234-9 THE Quick BROWN FOX walks in tbe woods - " + font.Name + " " + size, new PdfTextOptions(font, size, PdfColor.Black)); cursor -= size; } cursor -= 32; return(cursor); }
protected void HandleRectangle(Rectangle item, PdfPageWriter writer, Context context) { var physicalBox = ToPhysicalBox(item, context); var rotation = GetAngleValue(item.Rotation); var radius = Double.Parse(item.Radius ?? "0.0", CultureInfo.InvariantCulture); var go = (PdfGraphicsOptions)GetReferenceObject(item.GraphicsOptionsRef ?? context.GraphicsOptionsRef) ?? new PdfGraphicsOptions(); if (radius > 0.0 && rotation == 0.0) { writer.DrawRoundedRectangle2(Math.Min(physicalBox[0], physicalBox[2]), Math.Min(physicalBox[1], physicalBox[3]), Math.Max(physicalBox[2], physicalBox[0]), Math.Max(physicalBox[3], physicalBox[1]), radius, go); } else { writer.DrawRectangle2(physicalBox[0], physicalBox[1], physicalBox[2], physicalBox[3], go, rotation); } }
protected void HandleArea(Area item, PdfPageWriter writer, Context context) { var logicalBox = ToLogicalBox(item, context); var physicalBox = ToPhysicalBox(logicalBox, context); context = new Context(context); context.GraphicsOptionsRef = item.GraphicsOptionsRef ?? context.GraphicsOptionsRef; context.TextOptionsRef = item.TextOptionsRef ?? context.TextOptionsRef; if (item.CoordinateSpace == null) { context.Coordinates = new Coordinates(physicalBox, logicalBox); } else { context.Coordinates = new Coordinates(physicalBox, 0.0, 0.0, item.CoordinateSpace.Width, item.CoordinateSpace.Height); } foreach (var subitem in item.Items) { this.Dispatch(subitem, writer, context); } }
protected void HandleText(Text item, PdfPageWriter writer, Context context) { var physicalBox = ToPhysicalPoint(item, context); var x = physicalBox[0]; var y = physicalBox[1]; var to = (PdfTextOptions)GetReferenceObject(item.TextOptionsRef ?? context.TextOptionsRef) ?? new PdfTextOptions(); var leftRotationDegrees = GetAngleValue(item.Rotation); // Correct y value to reflect top of text: y = y - to.FontSize; var content = item.Content; if (!String.IsNullOrWhiteSpace(item.ContentKey)) { if (this.ContentSource == null) { throw new NullReferenceException("Failed to render Text content using ContentKey as ContentSource of PdfModelWriter is not set."); } content = this.ContentSource[item.ContentKey] ?? item.Content ?? String.Empty; } writer.DrawText(x, y, content ?? String.Empty, to, leftRotationDegrees); }
private Coordinates GetCoordinates(PdfPageWriter pageWriter) { return(new Coordinates(0.0, pageWriter.Height, pageWriter.Width, 0.0)); }