简体   繁体   English

C#PDF文档中的尖锐位置

[英]C# PDF Sharp position in document

Hi I am using PDF sharp to print user input onto positions in a template document. 您好我使用PDF sharp将用户输入打印到模板文档中的位置。

The data (fields) are collected from user (web page) and written at appropriate positions on the document using drawstring method. 数据(字段)从用户(网页)收集并使用抽绳方法写在文档上的适当位置。

Currently I am finding the Pixel position of each templated field in each page by trial and error .It would be much easier if there is a way to determine pixel position of each field in pdf page. 目前我通过反复试验找到每个模板中每个模板化字段的像素位置。如果有一种方法可以确定pdf页面中每个字段的像素位置,那将会容易得多。

Any suggestion would be most helpful. 任何建议都会有所帮助。

thanks 谢谢

I had the same issue as you, josephj1989, and I found what I believe to be our answer. 我和你有同样的问题,josephj1989,我找到了我认为是我们的答案。

According to this page in the PDFSharp documentation, 根据PDFSharp文档中的这个页面

The current implementation of PDFsharp has only one layout of the graphics context. PDFsharp的当前实现只有一个图形上下文布局。 The origin (0, 0) is top left and coordinates grow right and down. 原点(0,0)位于左上角,坐标向右和向下增长。 The unit of measure is always point (1/72 inch). 测量单位始终为点(1/72英寸)。

So, say I want to draw an image 3 inches from the left and 7.5 inches from the top of an 8.5 x 11 page, then I would do something like this: 所以,假设我要画一张距离左边3英寸,距离8.5 x 11页面顶部7.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);

I tested this out myself with a barcode image, and I found that when you use their formula for measuring positioning, you can get a level of precision at, well, 1/72 of an inch. 我用条形码图像自己测试了这个,我发现当你使用它们的公式来测量定位时,你可以获得一个精确度,即1/72英寸。 That's not bad. 那不错。

So, at this point, it'd be good to create some kind of encapsulation for such measurements so that we're focused mostly on the task at hand and not the details of conversion. 因此,在这一点上,为这样的测量创建某种封装是好的,这样我们主要关注手头的任务而不是转换的细节。

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

We could go on to make other helpers in this way... 我们可以用这种方式继续做其他帮手......

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

With such helper methods, we could do this with the previous sample code: 使用这样的辅助方法,我们可以使用前面的示例代码执行此操作:

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

I hope this is helpful. 我希望这是有帮助的。 I'm sure you will write cleaner code than what is in my example. 我相信你会编写比我的例子更清晰的代码。 Additionally, there are probably much smarter ways to streamline this process, but I just wanted to put something here that would make immediate use of what we saw in the documentation. 此外,可能有更聪明的方法来简化这个过程,但我只是想在这里放一些可以立即使用我们在文档中看到的东西。

Happy PDF Rendering! 快乐的PDF渲染!

When I used PDF Sharp, my approach was to make use the XUnit struct and to reference the top left point of the document as my starting point for X/Y positions. 当我使用PDF Sharp时,我的方法是使用XUnit结构并引用文档的左上角作为X / Y位置的起点。

Obviously referencing the top left point of the document (0,0) for every element on a PdfPage will get messy. 显然,对于PdfPage上的每个元素,引用文档的左上角(0,0)都会变得混乱。 To combat this, I used the XRect class to create rectangles for elements to sit within. 为了解决这个问题,我使用了XRect类来创建元素的矩形。 Once the XRect is drawn onto the page, you are then able to reference the X/Y position of the rectange via the XRect's properties. 将XRect绘制到页面上后,您就可以通过XRect的属性引用矩形的X / Y位置。 Then with some basic maths using those coordinates and the width/height of the XRect, you should be able to calculate the coordinates for the position of the next element you want to add to the PdfPage. 然后使用这些坐标和XRect的宽度/高度进行一些基本数学运算,您应该能够计算要添加到PdfPage的下一个元素的位置坐标。

Follow this code sample, I've provided a rough sketch of what the end result would be. 按照此代码示例,我提供了最终结果的粗略草图。 The code is untested but is very heavily based on code in production right now. 代码未经测试,但现在非常基于生产中的代码。

// 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));

And the output: 并输出: 模拟渲染的输出

Lastly, I'm sure you're aware, but there's a good resource of PdfSharp samples here . 最后,我敢肯定,你是知道的,但有PdfSharp样的一个很好的资源在这里

PDF files have no pixels and no pixel positions. PDF文件没有像素,也没有像素位置。
You can print PDF pages (use the "Actual size" size option) and measure positions with a ruler (this works pretty good with our printers). 您可以打印PDF页面(使用“实际尺寸”大小选项)并使用标尺测量位置(这对我们的打印机非常有用)。
Or you can use Adobe Acrobat to measure the positions of items on the PDF pages. 或者,您可以使用Adobe Acrobat测量PDF页面上项目的位置。

A bit of trial and error remains as you may have to give or take half a millimeter. 一些试验和错误仍然存​​在,因为您可能需要给予或采取半毫米。
Depending on your ruler you can use XUnit.FromMillimeter or XUnit.FromInch to get the point positions for PDFsharp (but points are no pixels). 根据您的标尺,您可以使用XUnit.FromMillimeter或XUnit.FromInch来获取PDFsharp的点位置(但点不是像素)。

The current PDFsharp samples are here: 目前的PDFsharp示例如下:
http://www.pdfsharp.net/wiki/PDFsharpSamples.ashx http://www.pdfsharp.net/wiki/PDFsharpSamples.ashx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM