Hi all,

recently I make something that need to convert DataSet/DataTable to PDF/RTF file, I searched, and asked, thanks for the authors to answer me, now I would like to contribute some codes.

using FontPath/FontSize can define your own font, thus it can support NON-ASCII characters such double-byte characters(like Simplified Chinese etc).

These solutions are somehow quick & dirty, so please feel free to improve them;)

public bool SaveAsPDF(string PDFFile, string FontPath, float FontSize)
{
        Document document = new Document();
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(PDFFile, FileMode.Create));
        document.Open();

        BaseFont baseFont =
                BaseFont.CreateFont(
                FontPath,
                BaseFont.IDENTITY_H,
                BaseFont.NOT_EMBEDDED);
        Font font = new Font(baseFont, FontSize);

        for (int table = 0; table < data.Tables.Count; table++)
        {
                PdfPTable pdfTable = new 
PdfPTable(data.Tables[table].Columns.Count);
                for (int row = 0; row < data.Tables[table].Rows.Count; row++)
                {
for (int column = 0; column < data.Tables[table].Columns.Count; column++)
                        {
pdfTable.AddCell(new Phrase(data.Tables[table].Rows[row][column].ToString(), font));
                        }
                }

                document.Add(pdfTable);                         
        }

        document.Close();
        //writer.Close();
        return true;
}

public bool SaveAsRTF(string RTFFile, string FontPath, float FontSize)
{
        Document document = new Document();
RtfWriter2 writer = RtfWriter2.GetInstance(document, new FileStream(RTFFile, FileMode.Create));
        document.Open();

        BaseFont baseFont =
                BaseFont.CreateFont(
                FontPath,
                BaseFont.IDENTITY_H,
                BaseFont.NOT_EMBEDDED);
        Font font = new Font(baseFont, FontSize);

        for (int table = 0; table < data.Tables.Count; table++)
        {
                Table rtfTable = new Table(data.Tables[table].Columns.Count);
                for (int row = 0; row < data.Tables[table].Rows.Count; row++)
                {
for (int column = 0; column < data.Tables[table].Columns.Count; column++)
                        {
rtfTable.AddCell(new Phrase(data.Tables[table].Rows[row][column].ToString(), font));
                        }
                }

                document.Add(rtfTable);                         
        }

        document.Close();
        //writer.Close();
        return true;
}

_________________________________________________________________
与联机的朋友进行交流,请使用 MSN Messenger: http://messenger.msn.com/cn


-------------------------------------------------------
SF.Net email is Sponsored by the Better Software Conference & EXPO
September 19-22, 2005 * San Francisco, CA * Development Lifecycle Practices
Agile & Plan-Driven Development * Managing Projects & Teams * Testing & QA
Security * Process Improvement & Measurement * http://www.sqe.com/bsce5sf
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions

Reply via email to