'ITextSharp insert text to an existing pdf (its not visible) (no text color? :S)
I found this script here on this page (ITextSharp insert text to an existing pdf)
I try a lot, I can find the text "Hello" with CTRL+F, but it is not visible... anyone have an idea ?
You can see that I have tried very often to bring color black in somehow, unfortunately without success :\
Thanks for your help =)
Code:
using (var reader = new PdfReader(@"E:\input.pdf"))
{
using (var fileStream = new FileStream(@"E:\Output.pdf", FileMode.Create, FileAccess.Write))
{
var document = new Document(reader.GetPageSizeWithRotation(1));
var writer = PdfWriter.GetInstance(document, fileStream);
document.Open();
for (var i = 1; i <= reader.NumberOfPages; i++)
{
document.NewPage();
var baseFont = BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
var importedPage = writer.GetImportedPage(reader, i);
var contentByte = writer.DirectContent;
contentByte.SetColorFill(BaseColor.BLACK);
contentByte.BeginText();
contentByte.SetFontAndSize(baseFont, 12);
contentByte.SetColorFill(BaseColor.BLACK);
contentByte.SetColorStroke(BaseColor.BLACK);
var multiLineString = "Hello,\r\nWorld!".Split('\n');
foreach (var line in multiLineString) {
if (i == 1)
{
Console.WriteLine(i);
contentByte.SetColorFill(BaseColor.BLACK);
contentByte.SetColorStroke(BaseColor.BLACK);
contentByte.ShowTextAligned(PdfContentByte.ALIGN_LEFT, line, 200, 200, 0);
}
}
contentByte.SetColorFill(BaseColor.BLACK);
contentByte.EndText();
contentByte.AddTemplate(importedPage, 0, 0);
}
document.Close();
writer.Close();
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
