我正在尝试使用iTextSharp创建具有多个页面的pdf
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();
}
这对我来说工作得很好。当我试图在同一文档中添加一个新页面时,它正在用新页面替换现有的书面文本,并且没有添加任何新页面。下面是不起作用的代码。
_pcb.EndText();
writer.Flush();
document.NewPage();
_pcb = writer.DirectContent;
_pcb.BeginText();
_pcb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, text, x, y, 0);
_pcb.EndText();
writer.Flush();
发布于 2012-07-04 02:01:52
下面是我清理和统一你的代码的尝试。通常避免try-catch,直到你不得不这样做,你经常会遗漏一些非常重要的错误。(例如,您实际上并没有设置所需的字体和大小,但可能只是省略了该代码。)此外,除非你正在写一个非常大的PDF,否则没有理由刷新缓冲区,必要时留给操作系统来做。
当我运行下面的代码时,我得到了两个页面,两个页面上都有文本,这对您有效吗?(目标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();
}
}
}
发布于 2012-07-03 17:23:34
为什么要使用DirectContent
?如果你只是想从头开始创建一个PDF,只要在Document
中添加内容即可。
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();
}
发布于 2018-04-12 17:37:29
下面的代码正常工作
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正文填充字符串
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类:
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);
}
}
https://stackoverflow.com/questions/11307749
复制相似问题