gpt4 book ai didi

c# - iTextSharp PdfWriter 在不应该的时候旋转页面布局

转载 作者:太空宇宙 更新时间:2023-11-03 10:45:59 28 4
gpt4 key购买 nike

我有一个程序使用 Itextsharp 和 PdfWriter 获取 pdf 并将文本打印到第一页。目前,对于我必须在其上输入文本的每个 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);
}

任何您想添加的评论或建议,请随意!谢谢

最佳答案

阅读时http://manning.com/lowagie2/samplechapter6.pdf Bruno 指出的,具体注意6.3.1,其中解释了如何使用PdfStamper 在某个页面的某个位置添加文本。它还显示了两种横向页面之间的区别:旋转页面和宽度 > 高度的页面。

完整的代码示例可以在这里找到:http://www.itextpdf.com/examples/iia.php?id=117

关于c# - iTextSharp PdfWriter 在不应该的时候旋转页面布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23399329/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com