Skip to content

Commit

Permalink
Added SampleCsWritePdf.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
dalefugier committed Sep 13, 2018
1 parent 5ba69e8 commit 4374a08
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
5 changes: 5 additions & 0 deletions rhinocommon/cs/SampleCsCommands/SampleCsCommands.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@
<Reference Include="System.Drawing" />
<Reference Include="System.Windows.Forms" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="Xfinium.Pdf.Win">
<HintPath>C:\Program Files\Rhino 6\Plug-ins\Xfinium.Pdf.Win.dll</HintPath>
<Private>False</Private>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Properties\Resources.Designer.cs">
Expand Down Expand Up @@ -200,6 +204,7 @@
<Compile Include="SampleCsViewSize.cs" />
<Compile Include="SampleCsWorldToPageTransform.cs" />
<Compile Include="SampleCsWrite3dmFile.cs" />
<Compile Include="SampleCsWritePdf.cs" />
<Compile Include="SampleCsZAnalysis.cs" />
<Compile Include="SampleCsZebraAnalysis.cs" />
<Compile Include="SampleCsZoom.cs" />
Expand Down
83 changes: 83 additions & 0 deletions rhinocommon/cs/SampleCsCommands/SampleCsWritePdf.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
using System;
using Rhino;
using Rhino.Commands;
using Rhino.FileIO;

namespace SampleCsCommands
{
public class SampleCsWritePdf : Command
{
public override string EnglishName => "SampleCsWritePdf";

protected override Result RunCommand(RhinoDoc doc, RunMode mode)
{
var pdf = FilePdf.Create();

var pdf_doc = pdf.PdfDocumentImplementation() as Xfinium.Pdf.PdfFixedDocument;
if (null == pdf_doc)
return Result.Failure;

var cover_page = pdf_doc.Pages.Add();

using (var font = new System.Drawing.Font("Arial", 30))
{
var pdf_font = new Xfinium.Pdf.Graphics.PdfUnicodeTrueTypeFont(font, false) { Underline = true };
var color = new Xfinium.Pdf.Graphics.PdfRgbColor(0, 0, 0);
var appearance = new Xfinium.Pdf.Graphics.PdfStringAppearanceOptions(pdf_font, null, new Xfinium.Pdf.Graphics.PdfBrush(color));
var layout = new Xfinium.Pdf.Graphics.PdfStringLayoutOptions
{
HorizontalAlign = Xfinium.Pdf.Graphics.PdfStringHorizontalAlign.Center,
X = cover_page.Width / 2,
Y = cover_page.Height / 2
};
cover_page.Graphics.DrawString(EnglishName, appearance, layout);
}

const int w = (int)(8.5 * 300);
const int h = 11 * 300;

var views = doc.Views.GetViewList(true, false);
for (var i = 0; i < views.Length; i++)
{
var view = views[i];
var viewcapture = new Rhino.Display.ViewCaptureSettings(view, new System.Drawing.Size(w, h), 300);
var page_index = pdf.AddPage(viewcapture) - 1;
using (var font = new System.Drawing.Font("Arial", 80))
{
var pdf_font = new Xfinium.Pdf.Graphics.PdfUnicodeTrueTypeFont(font, false);
var color = new Xfinium.Pdf.Graphics.PdfRgbColor(40, 40, 40);
var pen = new Xfinium.Pdf.Graphics.PdfPen(color, 3);
var appearance = new Xfinium.Pdf.Graphics.PdfStringAppearanceOptions(pdf_font, pen, null);
var layout = new Xfinium.Pdf.Graphics.PdfStringLayoutOptions
{
HorizontalAlign = Xfinium.Pdf.Graphics.PdfStringHorizontalAlign.Center,
Rotation = 45,
X = cover_page.Width / 2,
Y = cover_page.Height / 2
};
pdf_doc.Pages[page_index].Graphics.DrawString("Top Secret", appearance, layout);
}

using (var font = new System.Drawing.Font("Arial", 20))
{
var pdf_font = new Xfinium.Pdf.Graphics.PdfUnicodeTrueTypeFont(font, false);
var color = new Xfinium.Pdf.Graphics.PdfRgbColor(0, 0, 0);
var appearance = new Xfinium.Pdf.Graphics.PdfStringAppearanceOptions(pdf_font, null, new Xfinium.Pdf.Graphics.PdfBrush(color));
var layout = new Xfinium.Pdf.Graphics.PdfStringLayoutOptions
{
HorizontalAlign = Xfinium.Pdf.Graphics.PdfStringHorizontalAlign.Right,
X = cover_page.Width * 0.9,
Y = cover_page.Height * 0.9
};
pdf_doc.Pages[page_index].Graphics.DrawString($"Page {i + 1} of {views.Length}", appearance, layout);
}
}

var path = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
path = System.IO.Path.Combine(path, EnglishName + ".pdf");
pdf.Write(path);

return Result.Success;
}
}
}

0 comments on commit 4374a08

Please sign in to comment.