2012-08-27 56 views
1

嗨即時使用PDFSharp爲某些圖創建PDF文檔。在PDF中轉換我的圖表後,我應該在一頁上打印它們以獲得非常小的圖表,但是如果我有大圖表,則在一頁上打印它們會產生較差的打印質量,圖表將顯示得很小,圖表內容不可讀。如果我給出一個較高的比例,圖表將會更大,但有些節點會消失。使用PDFSharp創建多個頁面

所以我怎麼能創建更多的頁面,取決於我的規模和圖表大小?

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

這是什麼圖?它是作爲參數「BitmapSource Img」傳遞的圖像嗎? –

+0

是..... var bmp = activeDiagram.Panel.MakeBitmap(new System.Windows.Size(w,h),300,new System.Windows.Point(b.X,b.Y),s,convertBitmap); – User1979

回答

1

創建具有PDFsharp多個頁面很簡單 - 但PDFsharp不準備發佈你的位圖翻過幾頁,所以這個任務留給你。

根據位圖的大小,您的代碼應該決定將圖像剪切成兩個或四個部分,然後在兩個或四個頁面上繪製它們。這樣您就不必依賴打印機驅動程序的功能。

PDFsharp可以創建更大的頁面 - 但您必須依靠打印機驅動程序的功能才能將多個PDF頁面打印到多個物理頁面上。這可能會或可能不會工作。

如果您自己分割圖像,則完全控制出來的PDF文件。我建議將兩個或四個部分用一個共同的(重疊的)條打印。

+0

我希望看到更多關於如何創建多個頁面的解釋 – hitme

+0

@Hitme:你調用'document.AddPage();'添加一個新頁面,你可以調用'XGraphics.FromPdfPage(pdfPage);'爲您在頁面上繪製的新頁面獲取gfx對象。你有具體問題嗎? –

相關問題