您好,我使用PDFSharp为某些图表创建PDF文档。在将我的图转换为PDF后,我应该将它们打印在非常小的图的一页上,但是如果我有大图,则将它们打印在一页上会产生不良的打印质量,该图将显示很小,并且图的内容不可读。如果我给出高比例,该图将显示较大,但某些节点将消失。

因此,如何创建更多依赖于“比例”和“图表大小”的页面?

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");
        }
    }

最佳答案

使用PDFsharp创建多个页面很简单-但是PDFsharp尚未准备好将位图分布在多个页面上,因此该任务留给了您。

根据位图的大小,您的代码应决定将图像切成两部分或四部分,然后将其绘制在两页或四页上。这样,您不必依赖打印机驱动程序的功能。

PDFsharp可以创建更大的页面-但是您必须依靠打印机驱动程序的功能才能将单个PDF页面打印到多个物理页面上。那可能行不通。

如果您自己分割图像,则可以完全控制输出的PDF文件。我建议在两个或四个部分上打印一条普通的(重叠的)条。

09-10 23:13
查看更多