0

ASP.NET Web フォーム アプリケーションから iTextSharp を使用して、2 列と必要な数の行と画像とテキストを含むテーブルを含む pdf を生成したいと考えています。しかし、現在、1 つのセルに画像またはテキストの両方を追加することはできません。以下は、希望する形式の HTML マークアップの例です。

<table align="center" style="border-spacing:20px;">
    <tr>
        <td>
            <label style="display:block;text-align:center;">Corvette</label>
            <br />
            <img src="QrCodes/Ibrahim.jpg" />
        </td>
        <td>
            <label style="display:block;text-align:center;">Mercedes</label>
            <br />
            <img src="QrCodes/Amazon.jpg" />
        </td>
    </tr>
</table>

コード ビハインドからこの形式を作成するにはどうすればよいですか? HTML 文字列または iTextSharp オブジェクトを使用できます。どちらでも機能します。どんな提案でも大歓迎です。

現在のコード:

PdfWriter.GetInstance(doc, new FileStream(filePath, FileMode.Create));
doc.Open();
PdfPTable table = new PdfPTable(2);
System.Drawing.Image image = System.Drawing.Image.FromFile(Server.MapPath(@item.ItemQrCode));
iTextSharp.text.Image pdfImage = iTextSharp.text.Image.GetInstance(image, ImageFormat.Jpeg);
pdfImage.Alignment = iTextSharp.text.Image.UNDERLYING;

Phrase phrase = new Phrase(item.ItemName);
PdfPCell cellText = new PdfPCell(phrase);
cellText.HorizontalAlignment = Element.ALIGN_CENTER;
cellText.VerticalAlignment = Element.ALIGN_MIDDLE;

PdfPCell cellImage = new PdfPCell(pdfImage);
cellImage.HorizontalAlignment = Element.ALIGN_CENTER;
cellImage.VerticalAlignment = Element.ALIGN_MIDDLE;

table.AddCell(cellImage);

doc.Add(table);
doc.Close();
4

1 に答える 1

0

メインテーブルには2つの列があったので、セルを追加すると横に並べて追加されました。しかし、私の解決策は、セルごとに1列のテーブルを取得し、それらをメインテーブルセルに追加して、テキストとその下の画像を取得することでした。コード:

PdfWriter.GetInstance(doc, new FileStream(filePath, FileMode.Create));
doc.Open();
PdfPTable table = new PdfPTable(2);

System.Drawing.Image image = System.Drawing.Image.FromFile(Server.MapPath(@item.ItemQrCode));
iTextSharp.text.Image pdfImage = iTextSharp.text.Image.GetInstance(image, ImageFormat.Jpeg);

PdfPTable cellTable = new PdfPTable(1); //Table for each cell

Phrase phrase = new Phrase(item.ItemName);
PdfPCell cellText = new PdfPCell(phrase);
cellText.HorizontalAlignment = Element.ALIGN_CENTER;
cellText.VerticalAlignment = Element.ALIGN_MIDDLE;

PdfPCell cellImage = new PdfPCell(pdfImage);
cellImage.HorizontalAlignment = Element.ALIGN_CENTER;
cellImage.VerticalAlignment = Element.ALIGN_MIDDLE;

cellTable.AddCell(cellText);
table.AddCell(cellImage);

table.AddCell(cellTable); //Add each cells' table to main table cell

doc.Add(table);
doc.Close();
于 2013-06-26T05:41:08.190 に答える