C# 使用ItextSharp格式化文本

C# 使用ItextSharp格式化文本,c#,itextsharp,C#,Itextsharp,出于某些原因,我从PDFSharp迁移到ItextSharp dll。我想从Itextsharp那里获得上述信息。你能帮我用一下这个功能吗 private void CreatePdf() { Document doc = new Document(PageSize.LETTER, 10, 10, 20, 10); doc.Open(); int totalfonts = FontFactory.RegisterDirectory("C:\\W

出于某些原因,我从PDFSharp迁移到ItextSharp dll。我想从Itextsharp那里获得上述信息。你能帮我用一下这个功能吗

 private void CreatePdf()
 {
        Document doc = new Document(PageSize.LETTER, 10, 10, 20, 10);
        doc.Open();
        int totalfonts = FontFactory.RegisterDirectory("C:\\WINDOWS\\Fonts");

        StringBuilder sb = new StringBuilder();

        foreach (string fontname in FontFactory.RegisteredFonts)
        {
            sb.Append(fontname + "\n");
        }

        doc.Add(new Paragraph("All Fonts:\n" + sb.ToString()));
        Font arial = FontFactory.GetFont("Arial", 12, BaseColor.GRAY);
        Font verdana = FontFactory.GetFont("Verdana", 20, Font.BOLD | Font.UNDERLINE, new BaseColor(255, 0, 0));

        //rdana.SetStyle(4);
        iTextSharp.text.Font baseFontNormal = new iTextSharp.text.Font(verdana);
        using (FileStream msReport = new FileStream(Server.MapPath("~") + "/App_Data/" + DateTime.Now.Ticks + ".pdf", FileMode.Create))
        {
            doc.Open();

            PdfWriter pdfWriter = PdfWriter.GetInstance(doc, msReport);
            PdfPCell cell;
            PdfPTable table = new PdfPTable(1);

            cell = new PdfPCell(new Phrase("User I", new Font(verdana)));

            cell.Colspan = 4;

            cell.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
            cell.VerticalAlignment = 1;
            cell.Border = 0;
            table.AddCell(cell);

            table.AddCell(cell);
            doc.Open();
            doc.Add(table);
            doc.Close();
        }
  }
我没有位置概念。

使用此功能

 private void CreatePdf()
 {
        Document doc = new Document(PageSize.LETTER, 10, 10, 20, 10);
        doc.Open();
        int totalfonts = FontFactory.RegisterDirectory("C:\\WINDOWS\\Fonts");

        StringBuilder sb = new StringBuilder();

        foreach (string fontname in FontFactory.RegisteredFonts)
        {
            sb.Append(fontname + "\n");
        }

        doc.Add(new Paragraph("All Fonts:\n" + sb.ToString()));
        Font arial = FontFactory.GetFont("Arial", 12, BaseColor.GRAY);
        Font verdana = FontFactory.GetFont("Verdana", 20, Font.BOLD | Font.UNDERLINE, new BaseColor(255, 0, 0));

        //rdana.SetStyle(4);
        iTextSharp.text.Font baseFontNormal = new iTextSharp.text.Font(verdana);
        using (FileStream msReport = new FileStream(Server.MapPath("~") + "/App_Data/" + DateTime.Now.Ticks + ".pdf", FileMode.Create))
        {
            doc.Open();

            PdfWriter pdfWriter = PdfWriter.GetInstance(doc, msReport);
            PdfPCell cell;
            PdfPTable table = new PdfPTable(1);

            cell = new PdfPCell(new Phrase("User I", new Font(verdana)));

            cell.Colspan = 4;

            cell.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
            cell.VerticalAlignment = 1;
            cell.Border = 0;
            table.AddCell(cell);

            table.AddCell(cell);
            doc.Open();
            doc.Add(table);
            doc.Close();
        }
  }

我没有位置概念。

什么是x=100和y=100。请在pdf中描述字符串的.x和y位置。请概述整个问题,而不是链接到其他资源。什么是x=100和y=100。请在pdf中描述字符串的.x和y位置。请概述整个问题,而不是链接到其他资源资源。关于定位:不要做
doc.Add(table)
,使用
WriteSelectedRows()
方法。另一个建议:为什么不使用
ColumnText
来定位内容呢?thnx!建议。我在哪里使用ColumnText?您应该使用
ColumnText
而不是
PdfPTable
。阅读文档(iText正在运行)或查看第3章的示例。关于定位:不要执行
doc.Add(table)
,请使用
WriteSelectedRows()
方法。另一个建议:为什么不使用
ColumnText
来定位内容呢?thnx!建议。我在哪里使用ColumnText?您应该使用
ColumnText
而不是
PdfPTable
。阅读文档(iText正在运行)或查看第3章的示例。