Thank you for the sample code. Is there a way to iterate through stencils and print each stencil and its Name in a grid layout, starting from the upper left of the diagram, and then output that diagram to PDF? I do not want to save the stencils as bitmaps first if I can avoid it.
Also, from your bitmap output code above where you showed how to resize and then output the bitmap, it doesn’t resize the shape for the example you showed. All it does is add whitespace padding to the output PNG. I attached output examples so you can see what I mean from sample code below:
// resize shape Output-3x.png
shape.SetWidth(3 * shape.XForm.Width.Value);
shape.SetHeight(3 * shape.XForm.Height.Value);
// resize shape Output-2x.png
shape.SetWidth(2 * shape.XForm.Width.Value);
shape.SetHeight(2 * shape.XForm.Height.Value);
// resize shape Output-1x.png
shape.SetWidth(shape.XForm.Width.Value);
shape.SetHeight(shape.XForm.Height.Value);
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.5px Consolas; color: #008f00}
p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.5px Consolas}
Hi Per,
int pageindex = 0;
// get page by index
Page page = diagram.Pages[pageindex];
// retrieve page’s statistics
double pagewidth = page.PageSheet.PageProps.PageWidth.Value;
double pageheight = page.PageSheet.PageProps.PageHeight.Value;
double pageleftmargin = page.PageSheet.PrintProps.PageLeftMargin.Value;
double pagerightmargin = page.PageSheet.PrintProps.PageRightMargin.Value;
double pagetopmargin = page.PageSheet.PrintProps.PageTopMargin.Value;
double pagebottommargin = page.PageSheet.PrintProps.PageBottomMargin.Value;
int mastercount = 0;
// lets place master shapes with same width and height as 2
// here 2 is the width of master shape
// e.g. if cols is 4, then we can place 4 shapes in a row
int cols = ((int)Math.Floor(pagewidth - pageleftmargin - pagerightmargin)) / 2;
// here 2 is the height of master shape
int rows = ((int)Math.Floor(pageheight - pagetopmargin - pagebottommargin)) / 2;
// here 1 is the half of master shape’s width
double pinx = pageleftmargin + 1;
// here 1 is the half of master shape’s height
double piny = pageheight - 1 - pagetopmargin;
foreach (Master master in diagram.Masters)
{
long shapeid = 0;
if (mastercount < cols)
shapeid = diagram.AddShape(pinx + mastercount*2, piny, 2, 2, master.Name, pageindex);
else
{
mastercount = 0;
piny = piny - 2;
rows = rows - 1;
if (rows == 0)
break;
shapeid = diagram.AddShape(pinx + mastercount * 2, piny, 2, 2, master.Name, pageindex);
}
Aspose.Diagram.Shape shape = diagram.Pages[pageindex].Shapes.GetShape(shapeid);
shape.Text.Value.Add(new Txt(master.Name));
mastercount = mastercount+1;
}
diagram.Save(@“C:\Diagram\testdiagram2001\Output.pdf”, SaveFileFormat.PDF);
Thank you for giving this sample code. I actually didn’t expect you to do algorithm work yourself, so thanks for working with me here.
Hi Per,
spurgeon:Also, from your bitmap output code above where you showed how to resize and then output the bitmap, it doesn’t resize the shape for the example you showed. All it does is add whitespace padding to the output PNG. I attached output examples so you can see what I mean from sample code below:
Hi Per,
<span class=“kwrd” style=“color: rgb(0, 0, 255); font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>long<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”> shapeID = diagram.AddShape(0, 0, “100BaseT hub”, 0);<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
Aspose.Diagram.Shape shape = srcPage.Shapes.GetShape(shapeID);<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
<span style=“color: rgb(0, 128, 0); font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>// setting shape’s text as master name
shape.TextXForm.TxtPinX.Ufe.F = “Width+TxtWidth/2”;<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
shape.TextXForm.TxtPinY.Ufe.F = “Height+TxtHeight/2”;
Thanks, but I don’t see the text labels on the output PNGs. Is this because you trim the page size to the shapeWidth and shapeHeight and it clips the text? Can you please provide the complete code sample and verify the output?
Hi Per,
spurgeon:Also, only 20 icons are shown, whereas I believe there are 300 in the 3015.vss file.
Hi Per,
<span class=“rem” style=“color: rgb(0, 128, 0); font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>// add master from an existing stencil <span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
diagram.AddMaster(@“C:\Diagram\3015.vss”, “100BaseT hub”);<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
<span class=“rem” style=“color: rgb(0, 128, 0); font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>// get Visio page <span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
Aspose.Diagram.Page srcPage = diagram.Pages[0];<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
<span class=“rem” style=“color: rgb(0, 128, 0); font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>// remove background page <span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
srcPage.BackPage = <span class=“kwrd” style=“color: rgb(0, 0, 255); font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>null<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>;<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
<br style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”><span class=“rem” style=“color: rgb(0, 128, 0); font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>// place a shape in the drawing <span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
<span class=“kwrd” style=“color: rgb(0, 0, 255); font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>long<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”> shapeID = diagram.AddShape(0, 0, “100BaseT hub”, 0);<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
Aspose.Diagram.Shape shape = srcPage.Shapes.GetShape(shapeID);<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
<br style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”><span class=“rem” style=“color: rgb(0, 128, 0); font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>// resize shape <span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
shape.SetWidth(2 * shape.XForm.Width.Value);<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
shape.SetHeight(2 * shape.XForm.Height.Value);<br style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”><br style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”><span class=“rem” style=“color: rgb(0, 128, 0); font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>// move shape to the origin corner <span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
<span class=“kwrd” style=“color: rgb(0, 0, 255); font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>double<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”> shapeWidth = shape.XForm.Width.Value;<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
<span class=“kwrd” style=“color: rgb(0, 0, 255); font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>double<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”> shapeHeight = shape.XForm.Height.Value;<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
shape.MoveTo(shapeWidth * 0.5, shapeHeight * 0.5);<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>shape.Foreign.ImgWidth.Value = shapeWidth;<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>
shape.Foreign.ImgHeight.Value = shapeHeight;<br style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”><span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
<span class=“rem” style=“color: rgb(0, 128, 0); font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>// trim page size <span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
srcPage.PageSheet.PageProps.PageWidth.Value = shapeWidth;<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
srcPage.PageSheet.PageProps.PageHeight.Value = shapeHeight;<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
<br style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”><span class=“rem” style=“color: rgb(0, 128, 0); font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>// specify saving options <span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
ImageSaveOptions opts = <span class=“kwrd” style=“color: rgb(0, 0, 255); font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>new<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”> ImageSaveOptions(Aspose.Diagram.SaveFileFormat.PNG);
opts.PageCount = 1;<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
<span class=“rem” style=“color: rgb(0, 128, 0); font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>// set starting index of the page <span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
opts.PageIndex = 0;<br style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”><br style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”><span class=“rem” style=“color: rgb(0, 128, 0); font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>// save it <span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
diagram.Save(@“C:\Diagram\Output.png”, opts);
Hi Per,
Thanks, the code for scaling the shapes and then saving to bitmaps seems to work (DIAGRAMNET-51172), and therefore DIAGRAMNET-51171 is a lower priority to me at this point than the other open tickets (51182, 51183, 51184, 51185, 51186). I show how I’m creating 96x96px thumbnails while maintaining the aspect ratio in the code below.
Some .vss and .vssx files render pretty well to thumbnails using the code, but some do not. Do you know why some thumbs don’t render well? Attached are a couple of .vssx files from the Visio 2016 stencils library, as well as some other .vss files where I’ve seen rendering issues.
public string GetSafeFilename(string filename)
{
return string.Join(" ", filename.Split(Path.GetInvalidFileNameChars()));
}
public int GenerateStencilsImages(String inputPath, String outputDir)
{
try
{
diagram = new Diagram(inputPath);
if (diagram == null)
{
return -1;
}
}
catch (Exception e)
{
return -1;
}
int i = 0;
foreach (Master master in diagram.Masters)
{
try
{
i++;
string tempfile = outputDir + “\” + GetSafeFilename(master.Name) + “.png”;
// create a new drawing
Diagram stencil = new Diagram();
// add master from an existing stencil
stencil.AddMaster(diagram, master.Name);
// get Visio page
Aspose.Diagram.Page srcPage = stencil.Pages[0];
// remove background page
srcPage.BackPage = null;
// place a shape in the drawing
long shapeID = stencil.AddShape(0, 0, master.Name, 0);
Aspose.Diagram.Shape shape = srcPage.Shapes.GetShape(shapeID);
double w = 0;
double h = 0;
FitToBox(shape.XForm.Width.Value, shape.XForm.Height.Value, 1, 1, ref w, ref h); // .5 seems to be 48px when exported, 1=96px
// resize shape
shape.SetWidth(w);
shape.SetHeight(h);
// move shape to the origin corner
double shapeWidth = shape.XForm.Width.Value;
double shapeHeight = shape.XForm.Height.Value;
shape.MoveTo(shapeWidth * 0.5, shapeHeight * 0.5);
shape.Foreign.ImgWidth.Value = shapeWidth;
shape.Foreign.ImgHeight.Value = shapeHeight;
// trim page size
srcPage.PageSheet.PageProps.PageWidth.Value = shapeWidth;
srcPage.PageSheet.PageProps.PageHeight.Value = shapeHeight;
// specify saving options
Aspose.Diagram.Saving.ImageSaveOptions opts = new Aspose.Diagram.Saving.ImageSaveOptions(SaveFileFormat.PNG);
// set page count to save
opts.PageCount = 1;
// set starting index of the page
opts.PageIndex = 0;
// save it
stencil.Save(tempfile, opts);
}
catch (Exception e)
{
[//sMessageBox.Show](https://smessagebox.show/)("Exception: " + e.Message);
continue;
}
}
return 1;
}
private void FitToBox(double width, double height, double maxWidth, double maxHeight, ref double outWidth, ref double outHeight)
{
double widthRatio, heightRatio, multiplier;
if(maxWidth == -1) {
widthRatio = 1;
} else {
widthRatio = (1.0 * maxWidth) / (1.0 * width);
}
if(maxHeight == -1) {
heightRatio = 1;
} else {
heightRatio = (1.0 * maxHeight) / (1.0 * height);
}
if(widthRatio < heightRatio) {
multiplier = widthRatio;
} else {
multiplier = heightRatio;
}
double newWidth = width;
double newHeight = height;
if(multiplier < 1) {
newWidth = multiplier * width;
newHeight = multiplier * height;
}
outWidth = newWidth;
outHeight = newHeight;
}
Hi Per,
Hi Per,
Thanks for fixing that issue. Of the remaining issues, below is my prioritized list in order of priority. All except 51160 are important to me. The top group is something I need for a customer as soon as possible. I know there’s a lot here, but do you have an idea of which of these, if any, I might expect fixed in 17.4.0?
Hi Per,
Hi Per,
The issues you have found earlier (filed as DIAGRAMNET-51209;DIAGRAMNET-51210) have been fixed in Aspose.Diagram for .NET 17.4.0.
This message was posted using Notification2Forum from Downloads module by Aspose Notifier.