0

我发现其中一个 Microsoft 报告控件(图表)不足以满足我的需要。由于这些控件不可扩展,因此我扩展了 WPF 控件的功能并将控件保存为图形。不幸的是,ReportViewer 的图像控件也不支持矢量图形。因此,我的目标是从带有某些“占位符”(例如矩形项)的 ReportViewer 生成 PDF,然后使用 iTextSharp 并将矢量图形添加到这些占位符之上。由于报告长度基于输入数据流动,因此在获取这些占位符之前我无法知道它们的坐标。

我想不出一种在 RDLC 中包含 PDF 字段的方法,因为 iText 具有 GetFieldPosition() 方法,这会很好。此外,我知道有 iText 的 PDFWriter.GetVerticalPosition() 方法,但这仅在您将光标移动到 PDFWriter 中的该位置时才真正有用。事实是,我仍在尝试了解 iText 和 PDF。它是广阔的

所以我的两个问题相互引用:

  1. 我应该在 RDLC 设计器中使用什么样的占位符,之后可以系统地识别并查询其位置?
  2. 我将如何获得这个职位?

提前致谢!

-布兰登

4

2 回答 2

2

我不完美但可行的解决方案是使用与背景匹配的文本项占位符来标记 .RDLC 报告中的位置。生成 PDF 后,这些是通过继承 iTextSharp 的 LocationTextExtractionStrategy 来识别的(根据greenhat 的这个条目)。在我当前的测试环境中,以上内容是逐字复制粘贴的,仅在 RenderText() 覆盖中有所不同,除非info.GetText()是适当的占位符字符串,否则不会将 TextChunk 添加到列表中。

占位符提取的片段:

PdfReader reader = new PdfReader(fileName);
LocationTextExtractionStrategyEx strat = new LocationTextExtractionStrategyEx();
for (int i = 1; i <= reader.NumberOfPages; i++)
   PdfTextExtractor.GetTextFromPage(reader, i, strat);

本机矢量图像类型是 WMF,可以插入到占位符文本的绝对位置。要对列表中的第一个占位符执行此操作,则:

using (FileStream fs = new FileStream("output.pdf", FileMode.Create, FileAccess.Write, FileShare.None))
{
   using (PdfStamper stamper = new PdfStamper(reader, fs))
   {
      using (Stream wmfStream = new FileStream(@"C:\Paint.wmf", FileMode.Open, FileAccess.Read, FileShare.Read))
      {
         Image replacementVectorImage = Image.GetInstance(wmfStream);
         replacementVectorImage.SetAbsolutePosition(strat.TextLocationInfo[0].TopLeft[0],     strat.TextLocationInfo[0].BottomRight[1]);
         PdfContentByte cb = stamper.GetOverContent(1);
         cb.AddImage(replacementVectorImage);
      }
   }
}

感谢 mkl 和 VahidN 的宝贵意见!

于 2013-01-11T22:32:25.627 回答
1
于 2013-01-09T06:12:12.733 回答