Search code examples
c#pdfprintingpdfsharp

Create more than one Page with PDFSharp


hi im using PDFSharp for creating PDF-Document for some diagrams. after converting my diagrams in PDF, i should print them on one Page for very small Diagrams, but if i have big-Diagrams then printing them on one Page produce a bad Printing quality the diagram will be small displayed and the diagram content is not readable. if i give a high Scale, the diagram will be larger displayed but some of the nodes will disapear.

so how can i create more pages that depend on my Scale and Diagram-Size?

private void convertBitmap(BitmapSource Img)
{
  try
  {
     PdfSharp.Pdf.PdfDocument document = new PdfSharp.Pdf.PdfDocument();
     document.Info.Title = activeDiagram.Model.Name;
     PdfSharp.Pdf.PdfPage pdfPage = document.AddPage();
     XGraphics gfx = XGraphics.FromPdfPage(pdfPage);
     XImage xIMage = XImage.FromBitmapSource(Img);
     XImage logo = XImage.FromFile("logo.png");
     pdfPage.Width = xIMage.PointWidth;
     pdfPage.Height = xIMage.PointHeight;
     //draw the logo
     gfx.DrawImage(xIMage, 15, 70, pdfPage.Width, pdfPage.Height);
     gfx.DrawImage(logo, 500, 5);
     // Draw the texts
     string typ = "";
     if (activeDiagram == myDiagram1)
         typ = "EPC";

     XFont font = new XFont("Arial", 12, XFontStyle.Bold);
     XFont font2 = new XFont("Arial", 10, XFontStyle.Bold);
     gfx.DrawString("Modelname: " + activeDiagram.Model.Name, font, XBrushes.Black, 
                     new XRect(50, 5, 400, 20), XStringFormats.TopLeft);
    gfx.DrawString("Modeltyp: " + typ, font, XBrushes.Black, new XRect(50, 25, 400, 
                    20), XStringFormats.TopLeft);
    gfx.DrawLine(new XPen(XColor.FromKnownColor(XKnownColor.CornflowerBlue), 2), 20, 
                    45, 600, 45);

   gfx.DrawLine(new XPen(XColor.FromKnownColor(XKnownColor.CornflowerBlue), 2), 20, 
                    900, 600, 900);
   gfx.DrawString("Date: " + DateTime.Now.ToShortDateString(), font2, XBrushes.Black, 
                    new XRect(50, 905, 100, 25), XStringFormats.TopLeft);
   gfx.DrawString("Page: 1 von 1 ", font2, XBrushes.Black, new XRect(530, 905, 100, 
                25), XStringFormats.TopLeft);

    SaveFileDialog dlg = new SaveFileDialog();
    lg.FileName = activeDiagram.Model.Name;
    dlg.AddExtension = true;
    dlg.DefaultExt = "pdf";
    dlg.Filter = "PDF Document|*.pdf|*.pdf|";
    if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
    {
                // Save the document...
                string filename = dlg.FileName;
                document.Save(filename);
                // ...and start a viewer.
                Process.Start(filename);
            }
        }
        catch (Exception ex)
        {
            System.Windows.Forms.MessageBox.Show(ex.Message, "Error saving graph as a 
             pdf");
        }
    }

Solution

  • Creating multiple pages with PDFsharp is simple - but PDFsharp is not prepared to distribute your bitmap accross several pages, so this task is left to you.

    Depending on the size of the bitmap, your code should decide to cut the image into two or four parts and draw them on two or four pages. This way you don't have to rely onto the capabilities of a printer driver.

    PDFsharp can create larger pages - but then you'd have to rely onto the capabilities of the printer driver to print a single PDF page onto several physical pages. That may or may not work.

    If you split the image yourself, you have full control of the PDF file that comes out. I'd recommend having the two or four segments printed with a common (overlapping) strip.