Once your ViewCaptureSettingsObject is configured with all of the property’s you want set you can then use the PDF writer by passing the ViewCaptureSettings object to it.
Below is a basic example that prompts the user to draw a rectangle and then prints the contents in raster to a pdf on an A4 size paper. You can skip the A4 paper size creation and just use your rectangle dimensions.
import Rhino
import Rhino.UI
import System.Drawing as sd
import Rhino.Geometry as rg
def capture_rectangle_to_a4_pdf():
# Prompt the user to select a rectangle in world coordinates
rc, corners = Rhino.Input.RhinoGet.GetRectangle()
if rc != Rhino.Commands.Result.Success or corners is None:
return
# Find the active viewport that the rectangle was selected in
view = Rhino.RhinoDoc.ActiveDoc.Views.ActiveView
if not view:
return
# Convert from world to client coordinates
client_points = [view.ActiveViewport.WorldToClient(pt) for pt in corners]
# Create a bounding box from the client points
client_points_3d = [rg.Point3d(p.X, p.Y, 0) for p in client_points]
bb = rg.BoundingBox(client_points_3d)
# Create Point2d objects directly from the bounding box min and max
top_left = rg.Point2d(bb.Min.X, bb.Min.Y)
bottom_right = rg.Point2d(bb.Max.X, bb.Max.Y)
# Determine the rectangle size in pixels
rect_width = int(bb.Max.X - bb.Min.X)
rect_height = int(bb.Max.Y - bb.Min.Y)
# Create an A4 paper size in inches in landscape
a4_width = 11.69
a4_height = 8.27
dpi = 300
# Convert A4 inches to pixels
width_pixels = int(a4_width * dpi)
height_pixels = int(a4_height * dpi)
# Set up ViewCaptureSettings for A4 page size
vcs = Rhino.Display.ViewCaptureSettings(view, sd.Size(width_pixels, height_pixels), dpi)
vcs.RasterMode = True
vcs.ViewArea = Rhino.Display.ViewCaptureSettings.ViewAreaMapping.Window
vcs.SetWindowRect(top_left, bottom_right)
# Prompt for PDF save location
dlg = Rhino.UI.SaveFileDialog()
dlg.Filter = "PDF Files (*.pdf)|*.pdf"
if not dlg.ShowDialog():
return
filename = dlg.FileName
if not filename.endswith(".pdf"):
filename += ".pdf"
# Create and write the PDF
pdf = Rhino.FileIO.FilePdf.Create()
# Using A4 page size in inches
if pdf.AddPage(vcs) >= 0:
pdf.Write(filename)
print("PDF saved to: " + filename)
capture_rectangle_to_a4_pdf()
This test snippet doesn’t seem to return what I’m looking for:
var pdf = Rhino.FileIO.FilePdf.Create();
var page = doc.Views.GetPageViews()[0]; // TODO get the proper page
var origin = page.ClientToScreen(new System.Drawing.Point());
ViewCaptureSettings settings = new ViewCaptureSettings(page, 96);
settings.SetWindowRect(new Rhino.Geometry.Point2d(origin.X, origin.Y), new Rhino.Geometry.Point2d(origin.X + 500, origin.Y + 500));
pdf.AddPage(settings);
pdf.Write("C:\\testPrint.pdf");