現在、データベースの複数の行のデータに基づいてPDFを作成する作業版があります。データベースの各行に対して、PDFに新しいページを作成します。これはすごく効果的です。今度は、各行のいくつかのフィールドを解析して、HTMLが適切に表示されるようにする必要があります。私はsee an example hereですが、全体の文字列を取り、ドキュメントを解析していますが、ドキュメント全体の解析を示しています。異なるページのループ内でiTextを使用してHTMLのチャンクを解析する
私が必要とするのは、HTMLの特定のフィールドだけを解析して個々の書式設定されたページを作成することです。これは可能ですか?以下は
新しいページを作成し、私はいくつかのサンプルコードです:
PdfFont fTimes = PdfFontFactory.CreateFont(FontConstants.TIMES_ROMAN);
PdfFont fTimesBold = PdfFontFactory.CreateFont(FontConstants.TIMES_BOLD);
// create the first page here
doc.Add(new Paragraph("Abstract Submissions for " + eventName).SetFont(fTimes).SetFontSize(18).SetFontColor(Color.BLACK));
doc.Add(new Paragraph("Section Name: " + GetSectionName(ddlSections.SelectedValue)).SetFont(fTimes).SetFontSize(14).SetFontColor(Color.BLACK));
doc.Add(new Paragraph("Created: " + DateTime.Now.ToString("dddd, MMMM d, yyyy h:mm tt")).SetFont(fTimes).SetFontSize(11).SetFontColor(Color.BLACK));
// iterate through each of the items
foreach (DataRow row in dsItems.Tables[0].Rows)
{
// create a new page for each abstract submission
doc.Add(new AreaBreak(iText.Layout.Properties.AreaBreakType.NEXT_PAGE));
doc.Add(new Paragraph(ValidationHelper.GetString(row["PresentationType"], "")).SetFont(fTimes).SetFontSize(12).SetFontColor(Color.BLACK));
doc.Add(new Paragraph(ValidationHelper.GetString(row["PresentationTitle"], "")).SetFont(fTimes).SetFontSize(16).SetFontColor(Color.BLACK));
// html field
doc.Add(new Paragraph(ValidationHelper.GetString(row["Authors"], "")).SetFont(fTimes).SetFontSize(12).SetFontColor(Color.BLACK));
// html field
doc.Add(new Paragraph(ValidationHelper.GetString(row["Abstract"], "")).SetFont(fTimes).SetFontSize(12).SetFontColor(Color.BLACK));
}
doc.Close();
私はMemoryStream
対FileStream
を使用してそのクライアントはすぐにダウンロードすることができ、ファイルシステムに保存する必要はありませんよ注意してください。
** EDIT - あなたはiTextの翻訳者への独自のXML/HTMLを作成することができ、このようなパターンでサンプルデータを追加する**
<table>
<tr>
<td>Poster</td>
<td>Abstract 1</td>
<td><strong><em>Doctor Name 1</em></strong> <strong>Doctor Name 2</strong></td>
<td><p>Some really long text <strong>which can have</strong> some different basic HTML <u>formatting in it</u></p></td>
</tr>
<tr>
<td>Presentation</td>
<td>Abstract 2</td>
<td><strong>Doctor Name 15 </strong><em>Doctor 3</em></td>
<td><p>Some really long text which can have some different basic HTML <em>formatting in it</em></p></td>
</tr>
</table>
パーズ/レンダリングするコンテンツのサンプルを共有できますか?このコンテンツは、リッチテキストエディタのような一貫した書式設定のhtmlの小さなサブセットですか、それとも野生のhtml/cssのものですか? – COeDev
サンプルデータ@COeDevを追加しました。フォーマットが貧弱なため申し訳ありません。基本的にタグのすべてがデータベース列です。エディタを使わずにマークアップの効果を最大限に引き出すことができた唯一の方法です。 –
"strong"、 "p"、 "em"とhtmlが有効なxml以外のものがあまりない場合、この内容を簡単に解析してitext要素を作成できます。 – COeDev