Rank: Administration
Groups: Administrators
Joined: 1/5/2016(UTC) Posts: 1,138
Thanks: 10 times Was thanked: 133 time(s) in 130 post(s)
|
I have slightly modify the code above to show how you can add text object to existing PDF Code:
static void Main(string[] args)
{
PdfCommon.Initialize();
//Save text to existing document
var doc = PdfDocument.Load(@"d:\1\review.pdf");
var pageDict = doc.Pages[0].Dictionary;
AddTextToPage(doc, doc.Pages[0]);
//or to newly created document
var doc = PdfDocument.CreateNew();
doc.Pages.InsertPageAt(0, 500, 500);
AddTextToPage(doc, doc.Pages[0]);
}
private static void AddTextToPage(PdfDocument doc, PdfPage page)
{
var pageDict = page.Dictionary;
//Create and insert a text object
PdfTextObject txtObj = PdfTextObject.Create();
LOGFONT lf = new LOGFONT() { lfCharSet = LOGFONT.FontCharSet.ANSI_CHARSET, lfPitchAndFamily = LOGFONT.FontPitchAndFamily.DEFAULT_PITCH | LOGFONT.FontPitchAndFamily.FF_ROMAN, lfWeight = LOGFONT.FontWeight.FW_BOLD };
txtObj.Font = PdfFont.CreateWindowsFont(doc, lf, false, true);
txtObj.FillColor = Color.Blue;
txtObj.Transform(50, 0, 0, 50, 0, 0);
txtObj.Location = new PointF(50, 50);
txtObj.TextUnicode = "Hello World";
page.PageObjects.InsertObject(txtObj);
//Get the list of indirect objects and add the text object Font dictionary into it
var list = PdfIndirectList.FromPdfDocument(doc);
int objNum = list.Add(txtObj.Font.Dictionary);
//Create an indirect object for text object Font dictionary
var indirect = PdfTypeIndirect.Create(list, objNum);
//Get "Font" dictionary from page resource
PdfTypeDictionary fontDict = GetFontDictFromResource(pageDict, list);
//Insert that indirect object into Page's resources
fontDict.Add("FontMyQniqName", indirect);
//Create streram for the text object
var stream = PdfTypeStream.Create();
objNum = list.Add(stream);
indirect = PdfTypeIndirect.Create(list, objNum);
//Encode text object and insert it into stream
byte[] data = EncodeTextObject(txtObj);
var streamDict = PdfTypeDictionary.Create();
streamDict.Add("Length", PdfTypeNumber.Create(data.Length));
stream.Init(data, streamDict);
//Get page's content and add text stream into it
PdfTypeArray contents = GetContentsAsArray(list, pageDict);
contents.Add(indirect, list);
//Save document
doc.Save(@"d:\1\review_modified.pdf", SaveFlags.NoIncremental);
}
private static PdfTypeDictionary GetFontDictFromResource(PdfTypeDictionary pageDict, PdfIndirectList list)
{
var res = (pageDict["Resources"] as PdfTypeDictionary);
if (!res.ContainsKey("Font"))
{
var ret = PdfTypeDictionary.Create();
int objNum = list.Add(ret);
var indirect = PdfTypeIndirect.Create(list, objNum);
res.Add("Font", indirect);
return ret;
}
var font = res["Font"];
if (font is PdfTypeDictionary)
return font as PdfTypeDictionary;
else if(font is PdfTypeIndirect)
{
if ((font as PdfTypeIndirect).Direct is PdfTypeDictionary)
return (font as PdfTypeIndirect).Direct as PdfTypeDictionary;
}
throw new Exception("Unexpected type of font resource");
}
public static PdfTypeArray GetContentsAsArray(PdfIndirectList list, PdfTypeDictionary pageDict)
{
if(!pageDict.ContainsKey("Contents"))
{
var array = PdfTypeArray.Create();
//Add array into list of indirect objects
list.Add(array);
//And set it as a contents of the page
pageDict.SetIndirectAt("Contents", list, array);
return array;
}
var contents = pageDict["Contents"];
//check the original content whether it's an array
if (contents is PdfTypeArray)
return contents as PdfTypeArray; //if contents is a array just return it
else if (contents is PdfTypeIndirect)
{
if ((contents as PdfTypeIndirect).Direct is PdfTypeArray)
return (contents as PdfTypeIndirect).Direct as PdfTypeArray; //if contents is a reference to array then return that array
else if ((contents as PdfTypeIndirect).Direct is PdfTypeStream)
{
//if contents is a reference to a stream then create a new array and insert stream as a first element of array
var array = PdfTypeArray.Create();
array.AddIndirect(list, (contents as PdfTypeIndirect).Direct);
//Add array into list of indirect objects
list.Add(array);
//And set it as a contents of the page
pageDict.SetIndirectAt("Contents", list, array);
return array;
}
else
throw new Exception("Unexpected content type");
}
else
throw new Exception("Unexpected content type");
}
private static byte[] EncodeTextObject(PdfTextObject txtObj)
{
string ret = string.Format(
@"BT
/FontMyQniqName {0} Tf
{1} {2} {3} {4} {5} {6} Tm
{7} {8} {9} rg
({10})Tj
ET",
txtObj.FontSize,
txtObj.Matrix.a, txtObj.Matrix.b, txtObj.Matrix.c, txtObj.Matrix.d, txtObj.Matrix.e, txtObj.Matrix.f,
txtObj.FillColor.R, txtObj.FillColor.G, txtObj.FillColor.B,
txtObj.TextAnsi
);
return System.Text.Encoding.GetEncoding(1251).GetBytes(ret);
}
Edited by user Friday, November 3, 2017 5:21:34 AM(UTC)
| Reason: Not specified
|