iTextSharp - 将格式化文本适合单页

时间:2013-12-16 20:26:49

标签: pdf pdf-generation itextsharp

我有一个预先格式化了间距和换行符的文本文件。我正在将文本写入一个空白的pdf,该文本已设置为具有最小边距的横向,以使所有文本适合单个页面,但是我仍然在页面上运行。任何人都可以通过减少字体大小和/或行高(引导)来推荐如何使用itextsharp动态“适合页面”。我已经看到有关使用文本字段或矩形的响应,但我似乎无法使这些工作正常。

更新:到目前为止我没有使用任何高级内容,只需要进行边距控制和字体大小调整,以强制我的示例文本到页面。如果我总是有固定的线路长度,这可以正常工作,但不幸的是,情况并非如此。我可以在文件中使用共同的最大行长度,但此时我没有这些数据。

private void CreatePDF()
{
    string line = string.Empty;
    StreamReader sr = new StreamReader(@"C:\dev\text1.txt");
    StringBuilder sb = new StringBuilder();
    string newFile = @"C:\dev\testPDF1.pdf";
    Document pdfDoc = new Document(PageSize.LETTER.Rotate(), 50, 5, 5, 5);
    PdfWriter writer = PdfWriter.GetInstance(pdfDoc, new FileStream(newFile, FileMode.OpenOrCreate));
    pdfDoc.Open();
    while ((line = sr.ReadLine()) != null)
    {
    if (line != "\f")
    {
        sb.AppendLine(line);
    }
    else
    {
        pdfDoc.Add(new Paragraph(sb.ToString(), new Font(Font.NORMAL, 6)));
        pdfDoc.NewPage();
        pdfDoc.SetPageSize(PageSize.LETTER.Rotate());
        pdfDoc.SetMargins(50, 5, 5, 5);
        sb.Clear();
        sb.AppendLine("");
    }
    }
    pdfDoc.Add(new Paragraph(sb.ToString(), new Font(Font.NORMAL, 6)));
    pdfDoc.Close();
    //Console.Write(sb);            
}

0 个答案:

没有答案