首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用iTextSharp创建多页pdf

使用iTextSharp创建多页pdf
EN

Stack Overflow用户
提问于 2012-07-03 17:08:07
回答 3查看 31.6K关注 0票数 3

我正在尝试使用iTextSharp创建具有多个页面的pdf

代码语言:javascript
运行
复制
Document document = new Document(PageSize.A4, 2, 2, 10, 10);
private PdfContentByte _pcb;

try
{
    PdfWriter writer = PdfWriter.GetInstance(document, output);
    document.Open();             
    document.NewPage();
    _pcb = writer.DirectContent;
    _pcb.BeginText();
    _pcb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, text, x, y, 0);
    _pcb.EndText();
    writer.Flush();
}
catch(e)
{

}
finally
{
    document.Close();
}

这对我来说工作得很好。当我试图在同一文档中添加一个新页面时,它正在用新页面替换现有的书面文本,并且没有添加任何新页面。下面是不起作用的代码。

代码语言:javascript
运行
复制
_pcb.EndText();
writer.Flush();
document.NewPage();
_pcb = writer.DirectContent;
_pcb.BeginText();
_pcb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, text, x, y, 0);
_pcb.EndText();
writer.Flush();
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-07-04 02:01:52

下面是我清理和统一你的代码的尝试。通常避免try-catch,直到你不得不这样做,你经常会遗漏一些非常重要的错误。(例如,您实际上并没有设置所需的字体和大小,但可能只是省略了该代码。)此外,除非你正在写一个非常大的PDF,否则没有理由刷新缓冲区,必要时留给操作系统来做。

当我运行下面的代码时,我得到了两个页面,两个页面上都有文本,这对您有效吗?(目标iTextSharp 5.2.0.0)

代码语言:javascript
运行
复制
        var output = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Output.pdf");
        var bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1250, BaseFont.NOT_EMBEDDED);
        using (FileStream fs = new FileStream(output, FileMode.Create, FileAccess.Write, FileShare.None)) {
            using (Document doc = new Document(PageSize.A4, 2, 2, 10, 10)) {
                PdfContentByte _pcb;
                using (PdfWriter writer = PdfWriter.GetInstance(doc, fs)) {
                    //Open document for writing
                    doc.Open();
                    //Insert page
                    doc.NewPage();
                    //Alias to DirectContent
                    _pcb = writer.DirectContent;
                    //Set the font and size
                    _pcb.SetFontAndSize(bf, 12);
                    //Show some text
                    _pcb.BeginText();
                    _pcb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "Page 1", 40, 600, 0);
                    _pcb.EndText();
                    //Insert a new page
                    doc.NewPage();
                    //Re-set font and size
                    _pcb.SetFontAndSize(bf, 12);
                    //Show more text on page 2
                    _pcb.BeginText();
                    _pcb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "Page 2", 100, 400, 0);
                    _pcb.EndText();
                    doc.Close();
                }
            }
        }
票数 9
EN

Stack Overflow用户

发布于 2012-07-03 17:23:34

为什么要使用DirectContent?如果你只是想从头开始创建一个PDF,只要在Document中添加内容即可。

代码语言:javascript
运行
复制
try
{
    iTextSharp.text.Document doc = new iTextSharp.text.Document();
    PdfWriter.GetInstance(doc, new FileStream("HelloWorld.pdf", FileMode.Create));
    doc.Open();
    doc.Add(new Paragraph("Hello World!"));
    doc.NewPage();
    doc.Add(new Paragraph("Hello World on a new page!"));
}
catch (Exception ex)
{

}
finally 
{
    doc.Close();
}
票数 5
EN

Stack Overflow用户

发布于 2018-04-12 17:37:29

下面的代码正常工作

代码语言:javascript
运行
复制
string str = "Page1Page1Page1Page1Page1Page1Page1Page1Page1Page1";
string str2 = "Page2Page2Page2Page2Page2Page2Page2Page2Page2Page2";
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
using (var htmlWorker = new HTMLWokExtend(pdfDoc))
{
    using (var sr = new StringReader(str))
    {
        htmlWorker.Parse(sr);
    }
}
pdfDoc.NewPage();
using (var htmlWorker = new HTMLWokExtend(pdfDoc))
{
    using (var sr = new StringReader(str2))
    {
      htmlWorker.Parse(sr);
    }
}
pdfDoc.Close();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;" +
                               "filename=Proforma_Invoice.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Write(pdfDoc);
Response.End();

您可以使用下面的函数从HTML正文填充字符串

代码语言:javascript
运行
复制
private string populatebody()
{
    string body="";
    using (StreamReader reader = new StreamReader(Server.MapPath("~/Dir/Page.html")))
    {
        body = reader.ReadToEnd();
    }
    body = body.Replace("{Content in htmlpage}", "Your Content");
    return body
}

然后在上面的代码中将这个正文返回到字符串。您可以根据需要使用此代码进行操作。

下面是HTMLWokExtend类:

代码语言:javascript
运行
复制
public class HTMLWokExtend : HTMLWorker
{
    LineSeparator line = new LineSeparator(1f, 90f, BaseColor.GRAY, Element.ALIGN_CENTER, -12);
    public HTMLWokExtend(IDocListener document) : base(document)
    {

    }
    public override void StartElement(string tag, IDictionary<string, string> str)
    {
        if (tag.Equals("hrline"))
            document.Add(new Chunk(line));
        else
            base.StartElement(tag, str);
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11307749

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档