本文整理汇总了C#中Shape.Remove方法的典型用法代码示例。如果您正苦于以下问题:C# Shape.Remove方法的具体用法?C# Shape.Remove怎么用?C# Shape.Remove使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Shape
的用法示例。
在下文中一共展示了Shape.Remove方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: RenderNode
//ExStart
//ExId:RenderNode
//ExSummary:Shows how to render a node independent of the document by building on the functionality provided by ShapeRenderer class.
/// <summary>
/// Renders any node in a document to the path specified using the image save options.
/// </summary>
/// <param name="node">The node to render.</param>
/// <param name="path">The path to save the rendered image to.</param>
/// <param name="imageOptions">The image options to use during rendering. This can be null.</param>
public static void RenderNode(Node node, string filePath, ImageSaveOptions imageOptions)
{
// Run some argument checks.
if (node == null)
throw new ArgumentException("Node cannot be null");
// If no image options are supplied, create default options.
if (imageOptions == null)
imageOptions = new ImageSaveOptions(FileFormatUtil.ExtensionToSaveFormat(Path.GetExtension(filePath)));
// Store the paper color to be used on the final image and change to transparent.
// This will cause any content around the rendered node to be removed later on.
Color savePaperColor = imageOptions.PaperColor;
imageOptions.PaperColor = Color.Transparent;
// There a bug which affects the cache of a cloned node. To avoid this we instead clone the entire document including all nodes,
// find the matching node in the cloned document and render that instead.
Document doc = (Document)node.Document.Clone(true);
node = doc.GetChild(NodeType.Any, node.Document.GetChildNodes(NodeType.Any, true).IndexOf(node), true);
// Create a temporary shape to store the target node in. This shape will be rendered to retrieve
// the rendered content of the node.
Shape shape = new Shape(doc, ShapeType.TextBox);
Section parentSection = (Section)node.GetAncestor(NodeType.Section);
// Assume that the node cannot be larger than the page in size.
shape.Width = parentSection.PageSetup.PageWidth;
shape.Height = parentSection.PageSetup.PageHeight;
shape.FillColor = Color.Transparent; // We must make the shape and paper color transparent.
// Don't draw a surronding line on the shape.
shape.Stroked = false;
// Move up through the DOM until we find node which is suitable to insert into a Shape (a node with a parent can contain paragraph, tables the same as a shape).
// Each parent node is cloned on the way up so even a descendant node passed to this method can be rendered.
// Since we are working with the actual nodes of the document we need to clone the target node into the temporary shape.
Node currentNode = node;
while (!(currentNode.ParentNode is InlineStory || currentNode.ParentNode is Story || currentNode.ParentNode is ShapeBase))
{
CompositeNode parent = (CompositeNode)currentNode.ParentNode.Clone(false);
currentNode = currentNode.ParentNode;
parent.AppendChild(node.Clone(true));
node = parent; // Store this new node to be inserted into the shape.
}
// We must add the shape to the document tree to have it rendered.
shape.AppendChild(node.Clone(true));
parentSection.Body.FirstParagraph.AppendChild(shape);
// Render the shape to stream so we can take advantage of the effects of the ImageSaveOptions class.
// Retrieve the rendered image and remove the shape from the document.
MemoryStream stream = new MemoryStream();
shape.GetShapeRenderer().Save(stream, imageOptions);
shape.Remove();
// Load the image into a new bitmap.
using (Bitmap renderedImage = new Bitmap(stream))
{
// Extract the actual content of the image by cropping transparent space around
// the rendered shape.
Rectangle cropRectangle = FindBoundingBoxAroundNode(renderedImage);
Bitmap croppedImage = new Bitmap(cropRectangle.Width, cropRectangle.Height);
croppedImage.SetResolution(imageOptions.Resolution, imageOptions.Resolution);
// Create the final image with the proper background color.
using (Graphics g = Graphics.FromImage(croppedImage))
{
g.Clear(savePaperColor);
g.DrawImage(renderedImage, new Rectangle(0, 0, croppedImage.Width, croppedImage.Height), cropRectangle.X, cropRectangle.Y, cropRectangle.Width, cropRectangle.Height, GraphicsUnit.Pixel);
croppedImage.Save(filePath);
}
}
}
示例2: VisitShapeStart
/// <summary>
/// Called when a Shape is encountered in the document.
/// </summary>
public override VisitorAction VisitShapeStart(Shape shape)
{
if (isHidden(shape))
shape.Remove();
return VisitorAction.Continue;
}
示例3: ConvertTextboxToTable
/// <summary>
/// Converts a textbox to a table by copying the same content and formatting.
/// Currently export to HTML will render the textbox as an image which looses any text functionality.
/// This is useful to convert textboxes in order to retain proper text.
/// </summary>
/// <param name="textbox">The textbox shape to convert to a table</param>
private static void ConvertTextboxToTable(Shape textBox)
{
if (textBox.StoryType != StoryType.Textbox)
throw new ArgumentException("Can only convert a shape of type textbox");
Document doc = (Document)textBox.Document;
Section section = (Section)textBox.GetAncestor(NodeType.Section);
// Create a table to replace the textbox and transfer the same content and formatting.
Table table = new Table(doc);
// Ensure that the table contains a row and a cell.
table.EnsureMinimum();
// Use fixed column widths.
table.AutoFit(AutoFitBehavior.FixedColumnWidths);
// A shape is inline level (within a paragraph) where a table can only be block level so insert the table
// after the paragraph which contains the shape.
Node shapeParent = textBox.ParentNode;
shapeParent.ParentNode.InsertAfter(table, shapeParent);
// If the textbox is not inline then try to match the shape's left position using the table's left indent.
if (!textBox.IsInline && textBox.Left < section.PageSetup.PageWidth)
table.LeftIndent = textBox.Left;
// We are only using one cell to replicate a textbox so we can make use of the FirstRow and FirstCell property.
// Carry over borders and shading.
Row firstRow = table.FirstRow;
Cell firstCell = firstRow.FirstCell;
firstCell.CellFormat.Borders.Color = textBox.StrokeColor;
firstCell.CellFormat.Borders.LineWidth = textBox.StrokeWeight;
firstCell.CellFormat.Shading.BackgroundPatternColor = textBox.Fill.Color;
// Transfer the same height and width of the textbox to the table.
firstRow.RowFormat.HeightRule = HeightRule.Exactly;
firstRow.RowFormat.Height = textBox.Height;
firstCell.CellFormat.Width = textBox.Width;
table.AllowAutoFit = false;
// Replicate the textbox's horizontal alignment.
TableAlignment horizontalAlignment;
switch (textBox.HorizontalAlignment)
{
case HorizontalAlignment.Left:
horizontalAlignment = TableAlignment.Left;
break;
case HorizontalAlignment.Center:
horizontalAlignment = TableAlignment.Center;
break;
case HorizontalAlignment.Right:
horizontalAlignment = TableAlignment.Right;
break;
default:
// Most other options are left by default.
horizontalAlignment = TableAlignment.Left;
break;
}
table.Alignment = horizontalAlignment;
firstCell.RemoveAllChildren();
// Append all content from the textbox to the new table
foreach (Node node in textBox.GetChildNodes(NodeType.Any, false).ToArray())
{
table.FirstRow.FirstCell.AppendChild(node);
}
// Remove the empty textbox from the document.
textBox.Remove();
}