Я хочу вставить текст в существующий PDF-файл с помощью iTextSharp.В интернете полно примеров.Но я на самом деле не мог найти пример в соответствии с моими потребностями.Очень важно определить область / прямоугольник, в котором текст может быть записан в документе.Вот почему я не могу просто вставить текст в координатах х и у.Я на самом деле нашел что-то с помощью ColumnText и попытался использовать это с Stamper, но что-то, кажется, не работает.
Я был бы очень благодарен, если бы кто-то мог помочь мне с этим.
Вот что я пробовал:
string currentFile = viewModel.FilePath;
string destination = System.IO.Path.Combine(destinationFolder, System.IO.Path.GetFileName(currentFile)) + "_x.pdf";
if (!Directory.Exists(destinationFolder))
{
Directory.CreateDirectory(destinationFolder);
}
if (File.Exists(destination))
{
destination = getNewFileName(destination);
}
using (PdfReader reader = new PdfReader(currentFile))
{
//create PdfStamper object to write to get the pages from reader
using (PdfStamper stamper = new PdfStamper(reader, new FileStream(destination, FileMode.Create)))
{
//THIS PART IS WORKING PROPERLY --->
//FIRST WRITE ARBEITSCOPY AT TOP OF EACH PAGE
for (int i = 1; i <= reader.NumberOfPages; i++)
{
String textToWrite = "Arbeitskopie";
//gettins the page size in order to substract from the iTextSharp coordinates
var pageSize = reader.GetPageSize(i);
// PdfContentByte from stamper to add content to the pages over the original content
PdfContentByte pbover = stamper.GetOverContent(i);
//add content to the page using ColumnText
iTextSharp.text.Font font = new iTextSharp.text.Font();
font.Size = 16;
//setting up the X and Y coordinates of the document
float y = pageSize.Height - 20;
float x = 20;
ColumnText.ShowTextAligned(pbover, iTextSharp.text.Element.ALIGN_LEFT, new iTextSharp.text.Phrase(textToWrite, font), x, y, 0);
}
//<-- THIS PART IS WORKING PROPERLY
//####################################################################
// NOT WORKING PART
//iterate through all labels
foreach (Canvas canvas in StackPanel_Canvas_Container.Children)
{
foreach (object o in canvas.Children)
{
if (o.GetType().Equals(typeof(Label)))
{
Label label = (Label)o;
Fielddata fd = (Fielddata)label.DataContext;
double top = Canvas.GetTop(label);
double left = Canvas.GetLeft(label);
double percentWidth = label.ActualWidth / canvas.ActualWidth;
double percentHeight = label.ActualHeight / canvas.ActualHeight;
double percentTop = top / canvas.ActualHeight;
double percentLeft = left / canvas.ActualWidth;
int page = fd.Page;
var pagesize = reader.GetPageSize(page);
float llx = (float) percentLeft * pagesize.Width;
float lly = (float)percentTop * pagesize.Height;
float urx = llx + (float)percentWidth * pagesize.Width;
float ury = lly + (float)percentHeight *pagesize.Height;
PdfContentByte cB = stamper.GetOverContent(page);
ColumnText ct = new ColumnText(cB);
iTextSharp.text.Phrase myText = new iTextSharp.text.Phrase(label.Content.ToString());
ct.SetSimpleColumn(myText,llx ,lly ,urx ,ury , 15, iTextSharp.text.Element.ALIGN_CENTER);
}
}
}
// NOT WORKING PART
//#########################################################################
}
}