2016-04-29 63 views
-1

我正在評估PdfSharp來創建PDF文檔。與MigraDoc相比,我認爲我必須將每個位置(x,y)或大小乘以1.25以獲得預期結果。例如,如果我將頁邊距設置爲2釐米而沒有更正,我會得到大約1.6釐米的邊距。PdfSharp:爲什麼我需要將位置和大小乘以1.25才能獲得正確的結果?

page.TrimMargins = new TrimMargins 
{ 
    All = XUnit.FromCentimeter(2) 
}; 

當我乘以2與1.25我得到預期2釐米邊距:

page.TrimMargins = new TrimMargins 
{ 
    All = XUnit.FromCentimeter(2 * 1.25) 
}; 

同樣是字體大小。我必須將尺寸乘以1.25才能獲得與MigraDoc相同的尺寸,或者甚至可以打印Word。

順便說一句,我的系統沒有自定義縮放或100%以外的文本大小(我的猜測是這可能是原因)。

有人可以解釋這裏發生了什麼嗎?

編輯: 在TomasH的幫助下,我發現當打印時沒有自動縮放時,尺寸是完美的。 PdfSharp顯然會創建太大的PDF文檔。當用MigraDoc做同樣的工作時,PDF也有點太大了,但是隻有更小的規模。現在仍然存在的問題是文檔太大的原因以及MigraDoc用來糾正PDF大小的原因。

這裏是我完整的測試代碼,只給出了正確的定位,並與修正係數大小:

using (PdfDocument document = new PdfDocument()) 
{ 
    // Create an empty page size A4 with defined margins 
    PdfPage page = CreatePage(document); 
    using (XGraphics graphics = XGraphics.FromPdfPage(page)) 
    { 
     const double sizeCorrectionFactor = 1.25; 
     // Define the page margins. They must be multiplied by 1.25 to be correct!? 
     page.TrimMargins = new TrimMargins 
     { 
      All = XUnit.FromCentimeter(2 * sizeCorrectionFactor) 
     }; 

     // Draw a string. The font size needs to be multiplied by 1.25 to be correct!? 
     double x = 0; 
     double y = 0; 
     graphics.DrawRectangle(XPens.Black, XBrushes.White, 0, 0, page.Width, page.Height); 
     graphics.DrawString("PdfSharp Measure Demo", new XFont("Verdana", 20 * sizeCorrectionFactor), XBrushes.Navy, x, y, XStringFormats.TopLeft); 

     // Draw a rectangle. Position and size must be multiplied by 1.25 to be correct!? 
     x = XUnit.FromCentimeter(2 * sizeCorrectionFactor); 
     y = XUnit.FromCentimeter(2 * sizeCorrectionFactor); 
     double width = XUnit.FromCentimeter(5 * sizeCorrectionFactor); 
     double height = XUnit.FromCentimeter(5 * sizeCorrectionFactor); 
     graphics.DrawRectangle(XPens.Red, XBrushes.Silver, x, y, width, height); 
    } 

    string pdfFilePath = Path.GetTempFileName() + ".pdf"; 
    document.Save(pdfFilePath); 

    Process.Start(pdfFilePath); 
} 
+0

PDF中是錯誤的(例如用Adobe Reader測量)還是打印後錯誤?在不進行自動縮放等情況下以100%打印。確保PDF和打印機使用相同的頁面大小。 –

+0

我以100%印刷,尺寸是完美的。不知怎的,PdfSharp創建的PDF文檔看起來太大了。當用MigraDoc做同樣的工作時,PDF也有點太大了,但是隻有更小的規模。需要弄清楚爲什麼是這種情況,以及MigraDoc如何修正PDF大小。 –

回答

1

我找到了答案:我的意思(無據可查)TrimMargins財產是錯誤的。設置修剪邊距顯然會將邊距的大小添加到頁面的寬度或高度。這意味着如果設置了修剪邊距,頁面尺寸太大,並且在顯示或打印時通常會縮小比例。我爲裁切邊緣設置了2釐米,使頁面明顯變大1.25倍。解決方案是讓所有的邊距都爲0,並代替打印代碼中的所有頁邊距。

相關問題