使用iTextSharp创建多页pdf [英] Creating multiple page pdf using iTextSharp

查看:241
本文介绍了使用iTextSharp创建多页pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to create pdf with multiple pages using iTextSharp

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();
}

这对我来说很好.当我尝试在同一文档上添加新页面时,它用新页面替换了现有的书面文本,并且没有添加新页面.下面是无法正常工作的代码.

This is working fine for me. When I am trying to add a new page on the same document, it is replacing the existing written text with new page and no new page is getting added. Below is the code which is not working.

_pcb.EndText();
writer.Flush();
document.NewPage();
_pcb = writer.DirectContent;
_pcb.BeginText();
_pcb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, text, x, y, 0);
_pcb.EndText();
writer.Flush();

推荐答案

以下是我尝试清理并统一您的代码的尝试.通常在实际需要之前避免尝试捕获,否则您经常会错过一些非常重要的错误. (例如,您实际上没有设置所需的字体和大小,但是也许您只是省略了该代码.)而且,除非您编写的PDF非常大,否则实际上没有理由刷新缓冲区,而是将其留给操作系统必要时为您做.

Below is my attempt to clean-up and unify your code. Generally avoid try-catch until you actually have to, you'll often miss some very important errors. (For instance, you're not actually setting the font and size which is required but maybe you just omitted that code.) Also, unless you are writing a very large PDF there's really no reason to flush the buffers, leave that to the OS to do for you when necessary.

当我运行下面的代码时,我得到两个页面,两个页面上都有文字,对您有用吗? (定位到iTextSharp 5.2.0.0)

When I run the code below I get two pages with text on both pages, does it work for you? (Targeting iTextSharp 5.2.0.0)

        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();
                }
            }
        }

这篇关于使用iTextSharp创建多页pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆