【问题标题】:Create more than one Page with PDFSharp使用 PDFSharp 创建多个页面
【发布时间】:2012-08-27 13:57:54
【问题描述】:

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

那么如何根据我的 Scale 和 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");
        }
    }

【问题讨论】:

  • 图表是什么?是作为参数“BitmapSource Img”传递的图像吗?
  • 是..... var bmp = activeDiagram.Panel.MakeBitmap(new System.Windows.Size(w, h), 300, new System.Windows.Point(b.X, b.Y), s , 转换位图);

标签: c# pdf printing pdfsharp


【解决方案1】:

使用 PDFsharp 创建多个页面很简单 - 但 PDFsharp 不准备将您的位图分布在多个页面上,因此这项任务留给您。

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

PDFsharp 可以创建更大的页面 - 但是您必须依赖打印机驱动程序的功能才能将单个 PDF 页面打印到多个物理页面上。这可能有效,也可能无效。

如果您自己拆分图像,您可以完全控制输出的 PDF 文件。我建议使用公共(重叠)条打印两个或四个段。

【讨论】:

  • 我希望看到更多关于如何创建多个页面的说明
  • @Hitme:你调用document.AddPage(); 来添加一个新页面,你调用XGraphics.FromPdfPage(pdfPage); 来获取一个用于在该页面上绘制的新页面的gfx 对象。您有具体问题吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-12-14
  • 2014-02-04
  • 1970-01-01
  • 2023-04-02
  • 2014-07-20
  • 2012-09-27
  • 2015-07-21
相关资源
最近更新 更多