我有一个程序,它使用pdf并使用Itextsharp和PdfWriter将文本打印到第一页 . 目前,这已经按照我必须输入文本的每个pdf的方式工作 . 但是,当源pdf的布局为横向时,在将文本输入到pdf的第一页后,作者将布局旋转为纵向 . 我无法找到有关在pdf上输入文本后默认布局更改为纵向的原因的文档 . 由于原始布局是横向的,因此该旋转导致信息最终在右侧被切断 .
我已经查看了涉及PdfStamper的其他答案,但是在操作现有代码以处理我正在做的事情时遇到了麻烦 . 我是C#,pdf操作和iTextSharp编程的新手 . pdf文本的最终目标是可突出显示的 .
//Adds white invisible text to the pdf document that is highlightable
public static void stamp(string pdfName, string filePath, string textToStamp)
{
//Make a Temporary copy of the original to manipulate
string tempPath = @"C:\TemporaryFilePath\" + pdfName + "";
File.Copy(filePath, tempPath);
//Make a new reader using the copied source file
PdfReader reader = new PdfReader(tempPath);
using (Document document = new Document())
{
using (PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(filePath, FileMode.Create)))
{
document.Open();
int numofpages = reader.NumberOfPages;
for (int p = 1; p <= numofpages; p++)
{
//create the ContentByte to give the text a position on the first page
PdfContentByte cb = writer.DirectContent;
//Get the page to work with
PdfImportedPage page = writer.GetImportedPage(reader, p);
document.NewPage();
cb.AddTemplate(page, 0, 20);
var FontColour = new BaseColor(255, 255, 255);
var MyFont = FontFactory.GetFont("Times New Roman", 12, FontColour);
//Gets the first page and sends the text to the upper left corner
if (p == 1)
{
if (TextToStamp!= null)
{
document.Add(new Paragraph("Hello World", MyFont));
}
}
document.Close();
}
}
reader.Close();
File.Delete(tempPath);
}
任何评论或建议你想添加,随意!谢谢
2 回答
当阅读Bruno指出的http://manning.com/lowagie2/samplechapter6.pdf时,请特别注意6.3.1,其中解释了如何使用
PdfStamper
在某个页面的某个位置添加文本 . 它还显示了两种类型的横向页面之间的差异:旋转页面和宽度>高度的页面 .完整的代码示例可以在这里找到:http://www.itextpdf.com/examples/iia.php?id=117
使用Bruno发布的信息,我想出了这个解决方案 . 这样就可以在页面上标记信息,无论布局是什么,并为其提供少量的自定义 .