我正在寻找一个解决方案,允许我从C#创建一个PDF文件,它也可以合并为一个单独的静态PDF文件作为背景水印.
我正在开发一个允许用户创建其发票的PDF版本的系统.我没有尝试重新创建C#中的所有发票功能,而是认为最简单的解决方案是使用空白发票的PDF版本(从Adobe Illustrator创建)作为背景水印,并简单地在顶部覆盖动态发票详细信息.
我正在查看来自Data Dynamics的Active Reports,但看起来他们没有能力将报表叠加或合并到现有PDF文件中.
是否有任何其他.NET PDF报告产品具有这种能力?
Ric*_*est 14
谢谢bhavinp.iText似乎完成了诀窍,并且正如我所希望的那样工作.
对于试图合并到PDF文件并覆盖它们的任何其他人,基于使用iTextPDF库的以下示例代码可能会有所帮助.
结果文件是原始文件和背景文件的组合
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using iTextSharp.text;
using iTextSharp.text.pdf;
namespace iTextTest
{
class Program
{
/** The original PDF file. */
const String Original = @"C:\Jobs\InvoiceSource.pdf";
const String Background = @"C:\Jobs\InvoiceTemplate.pdf";
const String Result = @"C:\Jobs\InvoiceOutput.pdf";
static void Main(string[] args)
{
ManipulatePdf(Original, Background, Result);
}
static void ManipulatePdf(String src, String stationery, String dest)
{
// Create readers
PdfReader reader = new PdfReader(src);
PdfReader sReader = new PdfReader(stationery);
// Create the stamper
PdfStamper stamper = new PdfStamper(reader, new FileStream(dest, FileMode.Create));
// Add the stationery to each page
PdfImportedPage page = stamper.GetImportedPage(sReader, 1);
int n = reader.NumberOfPages;
PdfContentByte background;
for (int i = 1; i <= n; i++)
{
background = stamper.GetUnderContent(i);
background.AddTemplate(page, 0, 0);
}
// CLose the stamper
stamper.Close();
}
}
}
Run Code Online (Sandbox Code Playgroud)
我遇到了这个问题并且由于免费版本的许可而无法使用 iTextSharp 库
iText AGPL 许可证适用于希望在 AGPL “copyleft”条款下将其整个应用程序源代码作为免费软件与开源社区共享的开发人员。
但是我发现 PDFSharp 可以使用以下代码工作。
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PdfSharp.Drawing;
using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;
namespace PDFTest
{
class Program
{
static Stream Main(string[] args)
{
using (PdfDocument originalDocument= PdfReader.Open("C:\\MainDocument.pdf", PdfDocumentOpenMode.Import))
using (PdfDocument outputPdf = new PdfDocument())
{
foreach (PdfPage page in originalDocument.Pages)
{
outputPdf.AddPage(page);
}
var background = XImage.FromFile("C:\\Watermark.pdf");
foreach (PdfPage page in outputPdf.Pages)
{
XGraphics graphics = XGraphics.FromPdfPage(page);
graphics.DrawImage(background, 1, 1);
}
MemoryStream stream = new MemoryStream();
outputPdf.Save("C:\\OutputFile.pdf");
}
}
}
}
Run Code Online (Sandbox Code Playgroud)