Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# PDF戳记在没有页眉信息或正文的页面中不起作用_C#_Itextsharp - Fatal编程技术网

C# PDF戳记在没有页眉信息或正文的页面中不起作用

C# PDF戳记在没有页眉信息或正文的页面中不起作用,c#,itextsharp,C#,Itextsharp,使用下面的代码pdf戳记可以正常工作,但如果pdf页面没有标题信息或正文为空,则戳记不起作用,我的意思是pdfData.ShowText(strcsTommessage)不起作用 //create pdfreader object to read sorce pdf PdfReader pdfReader=new PdfReader(Server.MapPath("Input") + "/" + "input.pdf"); //create stream of filestream or mem

使用下面的代码pdf戳记可以正常工作,但如果pdf页面没有标题信息或正文为空,则戳记不起作用,我的意思是pdfData.ShowText(strcsTommessage)不起作用

//create pdfreader object to read sorce pdf
PdfReader pdfReader=new PdfReader(Server.MapPath("Input") + "/" + "input.pdf");
//create stream of filestream or memorystream etc. to create output file
FileStream stream = new FileStream(Server.MapPath("Output") + "/output.pdf",  FileMode.OpenOrCreate);
//create pdfstamper object which is used to add addtional content to source pdf file
PdfStamper pdfStamper = new PdfStamper(pdfReader,stream);
//iterate through all pages in source pdf
for (int pageIndex = 1; pageIndex <= pdfReader.NumberOfPages; pageIndex++)
{
//Rectangle class in iText represent geomatric representation... in this case, rectanle object would contain page geomatry
Rectangle pageRectangle = pdfReader.GetPageSizeWithRotation(pageIndex);
//pdfcontentbyte object contains graphics and text content of page returned by pdfstamper
PdfContentByte pdfData = pdfStamper.GetUnderContent(pageIndex);
//create fontsize for watermark
pdfData.SetFontAndSize(BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1252, BaseFont.NOT_EMBEDDED), 40);
//create new graphics state and assign opacity
PdfGState graphicsState = new PdfGState();
graphicsState.FillOpacity = 0.4F;
//set graphics state to pdfcontentbyte
pdfData.SetGState(graphicsState);
//set color of watermark
pdfData.SetColorFill(BaseColor.BLUE);
//indicates start of writing of text
pdfData.BeginText();
//show text as per position and rotation
pdfData.SetTextMatrix(pageRectangle.Width/2,pageRectangle.Height/2);                                                                                     pdfData.SetFontAndSize(BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED), 9);
string strCustomMessage="PDF Stamping Test"
pdfData.ShowText(strCustomMessage);
//call endText to invalid font set
pdfData.EndText();
}
//close stamper and output filestream
pdfStamper.Close();
stream.Close();
//创建pdfreader对象以读取sorce pdf
PdfReader PdfReader=新的PdfReader(Server.MapPath(“Input”)+“/”+“Input.pdf”);
//创建filestream或memorystream等流以创建输出文件
FileStream-stream=newfilestream(Server.MapPath(“Output”)+“/Output.pdf”,FileMode.OpenOrCreate);
//创建用于向源pdf文件添加附加内容的pdfstamper对象
PdfStamper PdfStamper=新的PdfStamper(pdfReader,流);
//遍历源pdf中的所有页面

对于(int pageIndex=1;pageIndex我认为你的问题是错误的。你说“pdf页面没有页眉信息或正文为空”是什么意思?因为这没有意义

一些观察:我认为您使用的是一个过时版本的iTextSharp。来找出这是一个坏主意的原因

由于此错误,较新版本的iTextSharp将引发异常:

pdfData.BeginText();
//show text as per position and rotation
string strCustomMessage="PDF Stamping Test"
pdfData.ShowText(strCustomMessage);
//call endText to invalid font set
pdfData.EndText();
在这些行中,您违反了PDF引用:您在BT/ET序列中显示文本,而没有定义字体和大小。如果您使用了较新版本的iTextSharp,则会出现异常。您需要在
BeginText()
/
EndText()中使用
SetFontAndSize()
方法移动该行
sequence。在文本对象之外使用它是非法的

另外:为什么要使用
BeginText()
/
EndText()
添加文本?这是PDF专家的代码。阅读文档了解如何使用
ColumnText.ShowTextAligned()
ShowTextAligned()
method是为不熟练使用PDF语法的开发人员编写的一种方便的方法

添加了
ShowText()
的文本是否可见取决于:

  • 页面的不透明度:假设您的页面由扫描的图像组成,您将看不到任何文本,因为您正在图像下添加文本。请使用
    GetOverContent()
    方法而不是
    GetUnderContent()
    方法来避免这种情况
  • 页面上的位置:我看到您使用
    GetPageSizeWithRotation()
    方法获得页面大小,但我没有看到您对该信息做任何处理。我看不到您在哪个坐标显示文本。这也是错误的。您需要确保将文本添加到页面的可见区域内
此答案列出了代码中的几个问题。如果您使用
ShowTextAligned()
和正确的X、Y坐标简化代码会更好。

不起作用-以哪种方式不起作用?