2012-11-07 60 views
7

您好我正在使用PDF sharp將用戶輸入打印到模板文檔中的位置。C#PDF文檔中的清晰位置

數據(字段)從用戶(網頁)收集,並使用拉繩方法在文檔的適當位置寫入。

目前我正在通過試驗和錯誤找到每個頁面中每個模板字段的像素位置。如果有方法確定pdf頁面中每個字段的像素位置,將會容易得多。

任何建議將是最有幫助的。

感謝

回答

4

當我使用PDF夏普,我的做法是利用的xUnit結構和引用文檔的左上角點作爲我的出發點爲X/Y位置。

很顯然,爲PdfPage上的每個元素引用文檔(0,0)的左上角點會變得雜亂無章。爲了解決這個問題,我使用了XRect類來創建元素座標的矩形。一旦XRect被繪製到頁面上,您就可以通過XRect的屬性來引用矩形的X/Y位置。然後通過使用這些座標和XRect的寬度/高度的一些基本數學運算,您應該能夠計算要添加到PdfPage的下一個元素位置的座標。

請按照下面的代碼示例,我已經提供了最終結果的粗略草圖。該代碼未經測試,但現在基於生產中的代碼非常繁重。

// Create a new PDF document 
PdfDocument document = new PdfDocument(); 

// Create an empty page with the default size/orientation 
PdfPage page = document.AddPage(); 
page.Orientation = PageOrientation.Landscape; 
page.Width = XUnit.FromMillimeter(300); 
page.Height = XUnit.FromMillimeter(200); 

// Get an XGraphics object for drawing 
XGraphics gfx = XGraphics.FromPdfPage(page); 

// Add the first rectangle 
XUnit horizontalOffset = XUnit.FromMillimeter(5); 
XUnit verticalOffset = XUnit.FromMillimeter(5); 
XUnit columnWidth = XUnit.FromMillimeter(100); 
XUnit columnHeight = page.Height - (2 * verticalOffset); 
XRect columnRect = new XRect(horizontalOffset, verticalOffset, columnWidth, columnHeight); 
gfx.DrawRectangle(XBrushes.Teal, columnRect); 

// Insert an image inside the rectangle, referencing the Left and Top properties of the rectangle for image placement 
XImage topLogo = XImage.FromFile(GetFilePath(@"content\img\pdfs\standard\logo-no-strapline.jpg")); // GetFilePath is a private method, not shown for brevity 
gfx.DrawImage(topLogo, 
    columnRect.Left + XUnit.FromMillimeter(5), 
    columnRect.Top + XUnit.FromMillimeter(5), 
    columnRect.Width - XUnit.FromMillimeter(10), 
    XUnit.FromMillimeter(38)); 

和輸出: Mock up of the rendered output

最後,我敢肯定,你是知道的,但有PdfSharp樣品here的一個很好的資源。

0

PDF文件沒有像素,也沒有像素位置。
您可以打印PDF頁面(使用「實際尺寸」尺寸選項)並使用標尺測量位置(這對我們的打印機來說效果很好)。
或者您可以使用Adobe Acrobat來測量PDF頁面上的項目位置。

有些試驗和錯誤仍然存​​在,因爲您可能需要半毫米或半毫米。
根據您的標尺,您可以使用XUnit.FromMillimeter或XUnit.FromInch獲取PDFsharp的點位置(但點不是像素)。

目前PDFsharp樣品在這裏:
http://www.pdfsharp.net/wiki/PDFsharpSamples.ashx

9

我有同樣的問題,因爲你,josephj1989,我發現了我認爲是我們的答案。

按照PDFSharp文檔中this page

當前實現PDFsharp的只有一個圖形上下文的佈局。原點(0,0)在左上角,座標向右和向下增長。度量單位始終爲點(1/72英寸)。

所以,說我想畫一個從左邊3英寸和7英寸的圖像。從8.5×11頁的前5英寸,那麼我會做這樣的事情:

PdfDocument document = GetBlankPdfDocument(); 
PdfPage myPage = document.AddPage(); 
myPage.Orientation = PdfSharp.PageOrientation.Portrait; 
myPage.Width = XUnit.FromInch(8.5); 
myPage.Height = XUnit.FromInch(11); 
XImage myImage = GetSampleImage(); 

double myX = 3 * 72; 
double myY = 7.5 * 72; 
double someWidth = 126; 
double someHeight = 36; 

XGraphics gfx = XGraphics.FromPdfPage(myPage); 
gfx.DrawImage(myBarcode, myX, myY, someWidth, someHeight); 

我有一個條碼圖像測試了這一點我自己,我發現,當你用自己的公式,用於測量定位,您可以在1/72英寸的範圍內獲得精確度。這並不壞。

因此,在這一點上,爲這種測量創建某種封裝會很好,所以我們主要關注手頭的任務而不是轉換的細節。

public static double GetCoordinateFromInch(double inches) 
{ 
    return inches * 72; 
} 

我們可以繼續做其他的助手這樣...

public static double GetCoordinateFromCentimeter(double centimeters) 
{ 
    return centimeters * 0.39370 * 72; 
} 

有了這樣的輔助方法,我們可以用前面的示例代碼做到這一點:

double myX = GetCoordinateFromInch(3); 
double myY = GetCoordinateFromInch(7.5); 
double someWidth = 126; 
double someHeight = 36; 

XGraphics gfx = XGraphics.FromPdfPage(myPage); 
gfx.DrawImage(myBarcode, myX, myY, someWidth, someHeight); 

我希望這是有幫助的。我相信你會寫出比我的例子更乾淨的代碼。另外,可能有更多更智能的方法來簡化這個過程,但我只是想在這裏放置一些能立即使用我們在文檔中看到的東西。

快樂的PDF渲染!