私はこれまで同様のことをする必要がありました。未知数の画像+コンテンツを含むPDFを作成する必要がありました。私の場合、「Entry」はイメージとフィールドのセットによって定義されました。
私は医者がいました。それは「エントリー」テンプレートとして役立った。私はその後、一時的なものを生成しました。 pdfファイルを作成し、生成したファイル名をListに保存しました。 すべての「エントリ」が処理された後、私はすべての一時的なpdfドキュメントを1つの最終的なドキュメントにマージしました。
これは、私が以前のプロジェクトから特定の部分を取り出したので、あなたに良いアイデアを与えるためのコードです(コンパイルできず、単にrefとして機能します)。ここで
List<string> files = new List<string>(); // list of files to merge
foreach (string pageId in pages)
{
// create an intermediate page
string intermediatePdf = Path.Combine(_tempPath, System.Guid.NewGuid() + ".pdf");
files.Add(intermediatePdf);
string pdfTemplate = Path.Combine(_templatePath, _template);
CreatePage(pdfTemplate, intermediatePdf, pc, pageValues, imageMap, tmd);
}
// merge into resulting pdf file
string outputFolder = "~/Output/";
if (preview)
{
outputFolder = "~/temp/";
}
string pdfResult = Path.Combine(HttpContext.Current.Server.MapPath(outputFolder), Guid.NewGuid().ToString() + ".pdf");
PdfMerge.MergeFiles(pdfResult, files);
//////////////////////////////////////////////////////////////////////////
// delete temporary files...
foreach (string fd in files)
{
File.Delete(fd);
}
return pdfResult;
は、テンプレートをマージするためのコードです:
public class PdfMerge
{
public static void MergeFiles(string destinationFile, List<string> sourceFiles)
{
int f = 0;
// we create a reader for a certain document
PdfReader reader = new PdfReader(sourceFiles[f]);
// we retrieve the total number of pages
int n = reader.NumberOfPages;
// step 1: creation of a document-object
Document document = new Document(reader.GetPageSizeWithRotation(1));
// step 2: we create a writer that listens to the document
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(destinationFile, FileMode.Create));
// step 3: we open the document
document.Open();
PdfContentByte cb = writer.DirectContent;
PdfImportedPage page;
int rotation;
// step 4: we add content
while (f < sourceFiles.Count)
{
int i = 0;
while (i < n)
{
i++;
document.SetPageSize(reader.GetPageSizeWithRotation(i));
document.NewPage();
page = writer.GetImportedPage(reader, i);
rotation = reader.GetPageRotation(i);
if (rotation == 90 || rotation == 270)
{
cb.AddTemplate(page, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation(i).Height);
}
else
{
cb.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
}
}
f++;
if (f < sourceFiles.Count)
{
reader = new PdfReader(sourceFiles[f]);
// we retrieve the total number of pages
n = reader.NumberOfPages;
}
}
// step 5: we close the document
document.Close();
}
}
はそれが役に立てば幸い!
部分的なPDFテンプレートの量を作成せずに正常に行うことはできません。ループさせる必要のない情報を持つセクションが必要な場合は、それぞれのドキュメントを別々にして、それらをまとめてマージする必要があります。 –
@Wayne、可能でなければなりません。私はiTextSharpの専門家ではありませんが、私はあなたがドキュメントを照会できると確信しています。私は知っているあなたはコンテンツを挿入することができます。以下は、既存のpdfとの間でコンテンツを読み書きする方法を示すリンクです。 http://www.mikesdotnetting.com/Article/82/iTextSharp-Adding-Text-with-Chunks-Phrases-and-Paragraphs http://stackoverflow.com/questions/3992617/itextsharp-insert-text-to -an-existing-pdf – santiagoIT