C# PDF 在文档中的尖锐位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13263252/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 07:59:14  来源:igfitidea点击:

C# PDF Sharp position in document

c#pdfpdfsharp

提问by josephj1989

Hi I am using PDF sharp to print user input onto positions in a template document.

嗨,我正在使用 PDF 锐利将用户输入打印到模板文档中的位置。

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

谢谢

回答by Steve Kennaird

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. To combat this, I used the XRect class to create rectangles for elements to sit within. 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. 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.

显然,为 PdfPage 上的每个元素引用文档的左上角 (0,0) 会变得混乱。为了解决这个问题,我使用 XRect 类来为元素创建矩形。一旦 XRect 绘制到页面上,您就可以通过 XRect 的属性引用矩形的 X/Y 位置。然后使用这些坐标和 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: Mock up of the rendered output

和输出: 渲染输出的模拟

Lastly, I'm sure you're aware, but there's a good resource of PdfSharp samples here.

最后,我敢肯定,你是知道的,但有PdfSharp样的一个很好的资源在这里

回答by I liked the old Stack Overflow

PDF files have no pixels and no pixel positions.
You can print PDF pages (use the "Actual size" size option) and measure positions with a ruler (this works pretty good with our printers).
Or you can use Adobe Acrobat to measure the positions of items on the PDF pages.

PDF 文件没有像素,也没有像素位置。
您可以打印 PDF 页面(使用“实际尺寸”尺寸选项)并使用标尺测量位置(这对我们的打印机非常有效)。
或者您可以使用 Adob​​e 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:
http://www.pdfsharp.net/wiki/PDFsharpSamples.ashx

当前的 PDFsharp 示例在这里:http://www.pdfsharp.net/wiki/PDFsharpSamples.ashx

回答by Kevin Lee Garner

I had the same issue as you, josephj1989, and I found what I believe to be our answer.

我和你有同样的问题,josephj1989,我找到了我认为是我们的答案。

According to this pagein the PDFSharp documentation,

根据PDFSharp 文档中的此页面

The current implementation of PDFsharp has only one layout of the graphics context. The origin (0, 0) is top left and coordinates grow right and down. The unit of measure is always point (1/72 inch).

PDFsharp 的当前实现只有一种图形上下文布局。原点 (0, 0) 位于左上角,坐标向右和向下增长。测量单位始终为磅(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:

因此,假设我想在 8.5 x 11 页面的左侧和顶部分别绘制 3 英寸和 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. That's not bad.

我自己用条形码图像对此进行了测试,我发现当你使用他们的公式来测量定位时,你可以获得 1/72 英寸的精度。这还不错。

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 渲染!