我在c#中有一個面板,其中包含像圖片框和datagridview各種組件。 我想創建一個包含整個datagridview和圖片框在一起的pdf。 現在,只有datagridview或圖片框出現在pdf中。合併在一起是不可能的。我正在使用iTextSharp來創建PDF。 我的代碼如下..合併使用itextsharp在c#中創建pdf的各種組件#
string strFileName;
string FontPath = "C:\\WINDOWS\\Fonts\\simsun.ttc,1";
int FontSize = 12;
///
Boolean cc = false;
SaveFileDialog savFile = new SaveFileDialog();
savFile.AddExtension = true;
savFile.DefaultExt = "pdf";
savFile.Filter = "PDF Document|*.pdf|*.pdf|";
savFile.ShowDialog();
if (savFile.FileName != "")
{
strFileName = savFile.FileName;
}
else
{
MessageBox.Show("export stop", "export stop", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
iTextSharp.text.Image jpg= iTextSharp.text.Image.GetInstance(Properties.Resources.templete3, System.Drawing.Imaging.ImageFormat.Png);
jpg.ScaleToFit(750, 850);
jpg.SetAbsolutePosition(0, 0);
// Page site and margin left, right, top, bottom is defined
Document pdfDoc = new Document(PageSize.A4);//, 10f, 10f, 10f, 0f);
//If you want to choose image as background then,
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, new FileStream(strFileName, FileMode.Create));
BaseFont baseFont = BaseFont.CreateFont(FontPath, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
iTextSharp.text.Font font = new iTextSharp.text.Font(baseFont, FontSize);
pdfDoc.Open();
PdfPTable table = new PdfPTable(dataGridView1.Columns.Count);
for (int j = 0; j < dataGridView1.Columns.Count; j++)
{
table.AddCell(new Phrase(datagridview[j, 0].Value.ToString(), font));
}
table.HeaderRows = 1;
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
for (int j = 0; j < dataGridView1.Columns.Count; j++)
{
try
{
table.AddCell(new Phrase(dataGridView1[j, i].Value.ToString(), font));
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
cc = true;
}
}
}
pdfDoc.NewPage();
pdfDoc.Add(jpg);
pdfDoc.Add(table);
pdfDoc.Close();
Process.Start(strFileName);
}
}
面板/數據網格/圖像在asp.net應用程序中使用?如果是這樣,您可能可以將它們呈現爲HTML並將其轉換。與這個問題無關,我覺得你很可愛如何將你的PNG命名爲'jpg'。 – Nenotlep 2013-03-22 07:28:21
@Nenotlep:不幸的是,我們使用的是c#語言。有沒有其他的方式來使用c# – 2013-03-22 10:08:15