C# 在memorystream中将绝对位置文本添加到pdf
我需要在pdf的右下角添加文本。我可以通过注释成功地做到这一点,但我无法将其展平,因此我尝试通过段落来添加文本。现在我的文本都没有显示。谁能指出我下面的代码哪里出了问题?(或者告诉我如何展平注释……注释掉的部分很好,我只需要展平)C# 在memorystream中将绝对位置文本添加到pdf,c#,pdf,itextsharp,C#,Pdf,Itextsharp,我需要在pdf的右下角添加文本。我可以通过注释成功地做到这一点,但我无法将其展平,因此我尝试通过段落来添加文本。现在我的文本都没有显示。谁能指出我下面的代码哪里出了问题?(或者告诉我如何展平注释……注释掉的部分很好,我只需要展平) private MemoryStream addPageNumber(MemoryStream流) { MemoryStream ms=新的MemoryStream(); PdfStamper压模=null; PdfReader Reader=新PdfReader(流
private MemoryStream addPageNumber(MemoryStream流)
{
MemoryStream ms=新的MemoryStream();
PdfStamper压模=null;
PdfReader Reader=新PdfReader(流);
尝试
{
PdfCopyFields复印机=新的PdfCopyFields(毫秒);
复印机。添加文件(读卡器);
复印机关闭();
PdfReader docReader=新的PdfReader(ms.ToArray());
ms=新内存流();
压模=新PdfStamper(文件阅读器,ms);
对于(int i=1;我在本页上看到了我的编辑,这是一个非常简单的版本:@Chris You's lifesaver,谢谢!我最终调整了您答案中的第三段代码以满足我的需要。我知道我的contentbyte哪里出了问题。
private MemoryStream AddPageNumbers(MemoryStream stream)
{
MemoryStream ms = new MemoryStream();
PdfStamper Stamper = null;
PdfReader Reader = new PdfReader(stream);
try
{
PdfCopyFields Copier = new PdfCopyFields(ms);
Copier.AddDocument(Reader);
Copier.Close();
PdfReader docReader = new PdfReader(ms.ToArray());
ms = new MemoryStream();
Stamper = new PdfStamper(docReader, ms);
for (int i = 1; i <= Reader.NumberOfPages; i++)
{
//iTextSharp.text.Rectangle newRectangle = new iTextSharp.text.Rectangle(315, 19, 560, 33);
//var pcb = new iTextSharp.text.pdf.PdfContentByte(Stamper.Writer);
//string PageNumber = "Confirmation of Completion Report " + i.ToString() + " of " + Reader.NumberOfPages.ToString();
//FontFactory.RegisterDirectories();
//Font fontNormalArial = new Font(FontFactory.GetFont("Arial", 8f, Font.NORMAL));
//Paragraph para = new Paragraph(PageNumber, fontNormalArial);
//var annot = iTextSharp.text.pdf.PdfAnnotation.CreateFreeText(Stamper.Writer, newRectangle, para.Content, pcb);
//annot.Flags = iTextSharp.text.pdf.PdfAnnotation.FLAGS_PRINT;
//annot.BorderStyle = new iTextSharp.text.pdf.PdfBorderDictionary(0, 0);
//Stamper.AddAnnotation(annot, i);
//Stamper.FreeTextFlattening = true;
//Stamper.FormFlattening = true;
PdfContentByte cb = new PdfContentByte(Stamper.Writer);
string PageNumber = "Confirmation of Completion Report " + i.ToString() + " of " + Reader.NumberOfPages.ToString();
FontFactory.RegisterDirectories();
Font fontNormalArial = new Font(FontFactory.GetFont("Arial", 10, Font.NORMAL));
Paragraph para = new Paragraph(PageNumber, fontNormalArial);
para.Alignment = Element.ALIGN_RIGHT;
ColumnText ct = new ColumnText(cb);
ct.AddText(para);
ct.SetSimpleColumn(315, 19, 560 , 38);
ct.SetIndent(316, false);
ct.Go();
}
}
catch (Exception ex)
{
string querystring = "?error=" + ex.Message.ToString();
Response.Redirect("~/ErrorPage.aspx" + querystring);
}
finally
{
if (Stamper != null)
{
Stamper.Close();
}
}
return ms;
}