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);
}
追加したいコメントや提案があれば、お気軽に! 感謝