Код: Выделить всё
string src = "36x24_Rot_90.pdf";
string dest = "output.pdf";
string text = "This is a text annotation";
PdfDocument pdfDoc = new PdfDocument(new PdfReader(src), new PdfWriter(dest));
PdfPage page = pdfDoc.GetFirstPage();
Rectangle rect = new Rectangle(300, 300, 200, 50);
PdfTextAnnotation annotation = new PdfTextAnnotation(rect);
annotation.SetContents(text);
PdfFormXObject xObject = new PdfFormXObject(new Rectangle(rect.GetWidth(), rect.GetHeight()));
PdfCanvas canvas = new PdfCanvas(xObject, pdfDoc);
PdfFont font = PdfFontFactory.CreateFont(iText.IO.Font.Constants.StandardFonts.HELVETICA);
float fontSize = 12;
float textWidth = font.GetWidth(text, fontSize);
float textHeight = fontSize;
// Keep the text upright despite page rotation
canvas.ConcatMatrix(0, 1, -1, 0, rect.GetWidth(), 0);
// Add text to the appearance
canvas.BeginText();
canvas.SetFontAndSize(font, fontSize);
canvas.SetFillColor(ColorConstants.BLACK);
canvas.MoveText(rect.GetWidth() / 2 - textHeight / 2, rect.GetHeight() / 2 - textWidth / 2);
canvas.ShowText(text);
canvas.EndText();
PdfDictionary appearanceDict = new PdfDictionary();
appearanceDict.Put(PdfName.N, xObject.GetPdfObject());
annotation.SetAppearance(PdfName.N, xObject.GetPdfObject());
page.AddAnnotation(annotation);
pdfDoc.Close();
Мой вопросы:
Как сделать текст видимым в аннотации?
Что-то не так с тем, как я создаю и применяю поток внешнего вида?
Любые рекомендации или предложения будут с благодарностью приняты. Спасибо!
Подробнее здесь: https://stackoverflow.com/questions/787 ... text7-in-c