2011年1月26日水曜日

iTextSharpでテンプレートを使用する方法

iTextSharpでテンプレートを使用する方法
CodeZineや@ITにも方法が載っていたが、バージョンが古いせいか
コピペで動作しなかった。

iTextSharp 5.0.5で動作確認

using iTextSharp.text;
using iTextSharp.text.pdf;

// step 1: creation of a document-object
Document doc = new Document();

// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
String vTempFile = iPDFFileName + "_.pdf";
PdfReader pr = new PdfReader(iPDFFileName);
PdfWriter pw = PdfWriter.GetInstance(doc, new FileStream(vTempFile, FileMode.Create));

// step 3: we open the document
doc.Open();

// template.pdfを現在のPDF文書に対して適用
PdfImportedPage page = pw.GetImportedPage(pr, 1);
PdfContentByte pcb = pw.DirectContent; //旧だとGetDirectContent;
pcb.AddTemplate(page, 0, 0);

// step 4: we add content
//iTextSharp.text.Image bmp = iTextSharp.text.Image.GetInstance(iBarCodeFileName);
Image bmp = Image.GetInstance(iBarCodeFileName);
bmp.SetAbsolutePosition(50, 630);

pcb.AddImage(bmp);

//doc.Add(bmp);

// step 5: we close the document
doc.Close();

0 件のコメント:

コメントを投稿