如何添加段然后一行? [英] How to add paragraph and then a line?

查看:97
本文介绍了如何添加段然后一行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,我所看到的所有使用ITextSharp的示例都是从头开始创建一个新文档,向其中添加内容并关闭它。
如果我需要对PDF执行多项操作,例如我想添加一个段落然后添加一行,该怎么办?
例如,如果我运行这个简单的控制台应用程序,我只需创建一个PDF并添加一个段落,然后关闭它,一切运行正常。

All of the examples I have seen so far using ITextSharp start from scratch and create a new document, add something to it and close it. What if I need to do multiple things to a PDF for example I want to add a paragraph and then add a line. For example if I run this simple console app in which I just create a PDF and add a paragraph and then close it everything runs fine.

class Program
{
    static void Main(string[] args)
    {
        Document pdfDoc = new Document();
        PdfWriter.GetInstance(pdfDoc, new FileStream("TestPDF.pdf", FileMode.Create));
        pdfDoc.Open();

        pdfDoc.Add(new Paragraph("Some Text added"));

        pdfDoc.Close();

        Console.WriteLine("The file was created.");
        Console.ReadLine();
    }
}

但是如果我需要做其他事情,比如画一个这样的行

However if I need to do something else like draw a line like this

class Program
{
    static void Main(string[] args)
    {
        Document pdfDoc = new Document();
        PdfWriter.GetInstance(pdfDoc, new FileStream("TestPDF.pdf", FileMode.Create));
        pdfDoc.Open();

        pdfDoc.Add(new Paragraph("Some Text added"));

        PdfWriter writer = PdfWriter.GetInstance(pdfDoc, new FileStream("TestPDF.pdf", FileMode.OpenOrCreate));
        PdfContentByte cb = writer.DirectContent;
        cb.MoveTo(pdfDoc.PageSize.Width / 2, pdfDoc.PageSize.Height / 2);
        cb.LineTo(pdfDoc.PageSize.Width / 2, pdfDoc.PageSize.Height);
        cb.Stroke();
        writer.Close();

        pdfDoc.Close();

        Console.WriteLine("The file was created.");
        Console.ReadLine();
    }
}

我在尝试打开文件时收到错误,因为它已经被pdfDoc打开了。
如果我把突出显示的代码放在pdfDoc.Close()之后,我收到一条错误,说文档没有打开
如何从添加文本切换到添加一行?
我是否需要关闭文档,然后使用PDFReader重新打开它并在那里修改它或者我可以一次完成所有操作吗?

I get an error when trying to open the file because it is already opened by pdfDoc. If I put the highlighted code after the pdfDoc.Close() I get an error saying "The Document is not opened" How do I switch from adding text to adding a line? Do I need to close the doc and then re-open it again with the PDFReader and modify it there or can I do it all at once?

推荐答案

您收到错误是因为您已经拥有一个PDFWriter的第二个实例。第二个PdfWriter.GetInstance(pdfDoc,新的FileStream(TestPDF.pdf,FileMode.OpenOrCreate));不需要。我对你的代码做了一个小编辑,现在看起来似乎有效了

You're getting an error because you're trying to request a second instance of a PDFWriter when you already have one. The second PdfWriter.GetInstance(pdfDoc, new FileStream("TestPDF.pdf", FileMode.OpenOrCreate)); is not needed. I made a small edit to your code and this now seems to work

Document pdfDoc = new Document();
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, new FileStream("TestPDF.pdf", FileMode.OpenOrCreate));

pdfDoc.Open();
pdfDoc.Add(new Paragraph("Some Text added"));             
PdfContentByte cb = writer.DirectContent;
cb.MoveTo(pdfDoc.PageSize.Width / 2, pdfDoc.PageSize.Height / 2);
cb.LineTo(pdfDoc.PageSize.Width / 2, pdfDoc.PageSize.Height);
cb.Stroke();

pdfDoc.Close();

Console.WriteLine("The file was created.");
Console.ReadLine();

这篇关于如何添加段然后一行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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