tags:

views:

12

answers:

1

I have tried out nipdf from www.nipdf.com to convert a wpf visual to pdf. It works fine except one problem which i am not able to figure out. no matter how big or small my visual is, the pdf created is always the default page size (which is A4 in my case) .

How can i make the page size to be the size of my visual ?

A: 

I don't have a good answer to why the content get stretched in the PDF to fill the entire A4.
Depending on your needs their might be another solution as long as you don't mind a Dialog. I've used this with CutePDF Writer. Pick CutePDF Writer from the available printers, name the file and it'll get saved with the same Size as you Visual.

private void FrameworkElementToPDF(FrameworkElement elementToPrint)
{
    PrintDialog dialog = new PrintDialog();
    if (dialog.ShowDialog() != true)
    {
        return;
    }

    // Save current canvas transorm
    Transform transform = elementToPrint.LayoutTransform;
    // Temporarily reset the layout transform before saving
    elementToPrint.LayoutTransform = null;

    // Get the size of the canvas
    Size size = new Size(elementToPrint.ActualWidth, elementToPrint.ActualHeight);
    // Measure and arrange elements
    elementToPrint.Measure(size);
    elementToPrint.Arrange(new Rect(size));

    dialog.PrintVisual(elementToPrint, "My Visual");

    // Restore previously saved layout
    elementToPrint.LayoutTransform = transform;
}
Meleak