[英]Need to create a PDF file from C# with another PDF file as the background watermark
我正在尋找一個解決方案,允許我從C#創建一個PDF文件,它也可以合並為一個單獨的靜態PDF文件作為背景水印。
我正在開發一個允許用戶創建其發票的PDF版本的系統。 我沒有嘗試重新創建C#中的所有發票功能,而是認為最簡單的解決方案是使用空白發票的PDF版本(從Adobe Illustrator創建)作為背景水印,並簡單地將動態發票詳細信息覆蓋在頂部。
我正在查看Data Dynamics的Active Reports,但看起來他們沒有能力將報表疊加或合並到現有的PDF文件中。
是否有任何其他.NET PDF報告產品具有這種能力?
謝謝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();
}
}
}
我遇到了這個問題,由於免費版本的許可證,無法使用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");
}
}
}
}
用這個。 http://itextpdf.com/它適用於Java和.NET
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.