10

您好我正在使用 PDF sharp 将用户输入打印到模板文档中的位置。

数据(字段)是从用户(网页)收集的,并使用拉绳方法写入文档的适当位置。

目前,我正在通过反复试验找到每个页面中每个模板化字段的像素位置。如果有一种方法可以确定 pdf 页面中每个字段的像素位置,那会容易得多。

任何建议都是最有帮助的。

谢谢

4

3 回答 3

16

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

根据PDFSharp 文档中的这个页面,

PDFsharp 的当前实现只有一种图形上下文布局。原点 (0, 0) 在左上角,坐标向右和向下增长。测量单位始终为磅(1/72 英寸)。

因此,假设我想在 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);

我自己用条形码图像对此进行了测试,我发现当你使用他们的公式来测量定位时,你可以获得 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 渲染!

于 2014-09-03T19:24:22.317 回答
6

当我使用 PDF Sharp 时,我的方法是使用 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));

和输出: 渲染输出的模拟

最后,我相信您知道,但这里有一个很好的 PdfSharp 示例资源

于 2012-11-07T08:57:29.737 回答
0

PDF 文件没有像素,也没有像素位置。
您可以打印 PDF 页面(使用“实际大小”大小选项)并用尺子测量位置(这对我们的打印机非常有效)。
或者您可以使用 Adob​​e Acrobat 来测量 PDF 页面上项目的位置。

仍然存在一些试验和错误,因为您可能不得不给予或采取半毫米。
根据您的标尺,您可以使用 XUnit.FromMillimeter 或 XUnit.FromInch 来获取 PDFsharp 的点位置(但点不是像素)。

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

于 2012-11-07T09:14:38.890 回答