C#PDF文档中的尖锐位置

jos*_*989 10 c# pdf pdfsharp

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

数据(字段)从用户(网页)收集并使用抽绳方法写在文档上的适当位置.

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

任何建议都会有所帮助.

谢谢

Kev*_*ner 13

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

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

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

所以,假设我要画一张距离左边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);
Run Code Online (Sandbox Code Playgroud)

我用条形码图像自己测试了这个,我发现当你使用它们的公式来测量定位时,你可以获得一个精确度,即1/72英寸.那不错.

因此,在这一点上,为这样的测量创建某种封装是好的,这样我们主要关注手头的任务,而不是转换的细节.

public static double GetCoordinateFromInch(double inches)
{
  return inches * 72;
}
Run Code Online (Sandbox Code Playgroud)

我们可以用这种方式继续做其他帮助......

public static double GetCoordinateFromCentimeter(double centimeters)
{
  return centimeters * 0.39370 * 72;
}
Run Code Online (Sandbox Code Playgroud)

使用这样的辅助方法,我们可以使用前面的示例代码执行此操作:

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);
Run Code Online (Sandbox Code Playgroud)

我希望这是有帮助的.我相信你会编写比我的例子更清晰的代码.此外,可能有更聪明的方法来简化这个过程,但我只是想在这里放一些可以立即使用我们在文档中看到的东西.

快乐的PDF渲染!


Ste*_*ird 5

当我使用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));
Run Code Online (Sandbox Code Playgroud)

并输出: 模拟渲染的输出

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