Resize / Crop a PDF Document created with PDFsharp

3.7k Views Asked by At

I'm creating a sale ticket with PDFsharp, whose height depends on the bought items and other sale parameters. My question is, once I have the final height of the ticket, how can I resize / crop the final PDF document?

I've tried to set a big page height and then changing once known, but this produces an empty document:

PdfDocument document = new PdfDocument();
PdfPage page = document.AddPage();
page.Width = XUnit.FromMillimeter(80).Point;
page.Height = XUnit.FromMillimeter(800).Point;
XGraphics gfx = XGraphics.FromPdfPage(page);

double height = printHeader(gfx, saleData);
height = printItems(gfx, saleItems, height);
height = printFooter(gfx, saleData, height);

page.Height = XUnit.FromMillimeter(height + 10).Point;

document.Save(path);

As an MCVE :

class Program
{
    static void Main(string[] args)
    {
        PdfDocument document = new PdfDocument();
        XFont fontTicket = new XFont("Courier New", 9, XFontStyle.Regular);

        PdfPage page = document.AddPage();
        page.Width = XUnit.FromMillimeter(80).Point;
        page.Height = XUnit.FromMillimeter(800).Point;
        XGraphics gfx = XGraphics.FromPdfPage(page);

        int baseX = 5;
        int baseY = 10;
        gfx.DrawString("************************************", fontTicket, XBrushes.Black, XUnit.FromMillimeter(baseX), XUnit.FromMillimeter(baseY += 5));
        gfx.DrawString("***            Ticket            ***", fontTicket, XBrushes.Black, XUnit.FromMillimeter(baseX), XUnit.FromMillimeter(baseY += 5));
        gfx.DrawString("************************************", fontTicket, XBrushes.Black, XUnit.FromMillimeter(baseX), XUnit.FromMillimeter(baseY += 5));

        page.Height = XUnit.FromMillimeter(baseY + 10).Point;

        document.Save("ticket.pdf");
    }

}

If I remove the line 'page.Height = XUnit.FromMillimeter(baseY + 10).Point;' the document is generated correctly, but with 800mm of height. When I add this line, it generates a document with the correct dimensions but empty.

1

There are 1 best solutions below

0
On BEST ANSWER

You have to set the page size before creating the XGraphics object.

Instead of setting the page height, you can set the CropBox as shown here:

//page.Height = XUnit.FromMillimeter(baseY + 10).Point;
double height = XUnit.FromMillimeter(baseY + 10).Point;
page.CropBox = new PdfRectangle(new XPoint(0, page.Height - height),
                                new XSize(page.Width, height));

With respect to your MCVE, I took one line out and added two new lines instead. Tested with PDFsharp 1.50 beta 3b. Older versions may behave differently.

Or make two passes: one pass to determine the height, one pass to draw items after setting the page height.

You could also draw on an XForm, then create the page with the correct size and draw the XForm onto the page.
The XForm sample can be found here:
http://pdfsharp.net/wiki/XForms-sample.ashx
Requires more code and is not as straightforward as setting the CropBox.