Rank: Advanced Member
Groups: Registered
Joined: 4/30/2019(UTC) Posts: 48   Location: Raisio Thanks: 11 times Was thanked: 3 time(s) in 2 post(s)
|
Greetings from Finland! Sorry to bug you again, but it seems that the approach with "annotation as container converted to stream" is not working for some files (in our case, those PDF files are generated by Teigha / OpenDesign Alliance converter from DWG)... :( Here are three files: jv 755002_Limited (2).pdf (5kb) downloaded 65 time(s). - original file jv 755002_Limited (2)-modified links.pdf (9kb) downloaded 68 time(s). - modified so that link annotations have rectangles (correct positions and sizes, but does not work in e.g. Adobe reader) jv 755002_Limited (2)-modified objects.pdf (9kb) downloaded 67 time(s). - modified so that link annotations have no rectangles, but rectangles are added into other annotation converted to stream (incorrect positions and sizes); I specifically use PdfLinkAnnotation here, but the result is same with other annotation-containers. The code (this one will output both variants; colors may vary; just adds links to all text runs): Code:
using (var doc = PdfDocument.Load(@"C:\temp\jv 755002_Limited (2).pdf"))
{
var page = doc.Pages[0];
if (page.Annots == null) page.CreateAnnotations();
var contentsArray = ConvertContentsToArray(page);
using (var tmpAnnot = new PdfLinkAnnotation(page))
{
tmpAnnot.CreateEmptyAppearance(AppearanceStreamModes.Normal);
tmpAnnot.NormalAppearance.Add(PdfTextObject.Create("Sample Text", 150, 150, PdfFont.CreateStock(doc, FontStockNames.Arial), 45.0f));
var pdfTextObjects = (
from pdfPageObject in page.PageObjects
let pdfTextObject = pdfPageObject as PdfTextObject
where pdfTextObject != null
select pdfTextObject).ToList();
foreach (var pdfTextObject in pdfTextObjects)
{
var text = pdfTextObject.TextUnicode;
var boundingBox = pdfTextObject.BoundingBox;
var x = boundingBox.left;
var y = boundingBox.top;
var w = boundingBox.Width;
var h = boundingBox.Height;
Console.WriteLine($"@({x}, {y}) {w} x {h} : {text}");
var pdfPathObject = PdfPathObject.Create(FillModes.Winding, true);
pdfPathObject.FillColor = new FS_COLOR(50, 200, 0, 0);
pdfPathObject.StrokeColor = new FS_COLOR(50, 0, 100, 0);
//Patagames.Pdf.Pdfium.FPDFPageObj_SetLineWidth(pdfPathObject.Handle, 1.15f);
var d = 1;
var bb = new FS_RECTF(x - d, y + d, x + w + d, y - h - d);
pdfPathObject.Path.AppendRect(bb);
pdfPathObject.CalcBoundingBox();
tmpAnnot.NormalAppearance.Add(pdfPathObject);
var pdfPathObjectNormalAppearance = PdfPathObject.Create(FillModes.Winding, false);
pdfPathObjectNormalAppearance.FillColor = new FS_COLOR(50, 0, 200, 0);
pdfPathObjectNormalAppearance.StrokeColor = new FS_COLOR(50, 100, 0, 0);
pdfPathObjectNormalAppearance.Path.AppendRect(bb);
pdfPathObjectNormalAppearance.CalcBoundingBox();
var pdfTypeDictionary = PdfTypeDictionary.Create();
pdfTypeDictionary["S"] = PdfTypeName.Create("URI");
pdfTypeDictionary["URI"] = PdfTypeString.Create("https://google.com?" + text);
var pdfLinkAnnotation = new PdfLinkAnnotation(page)
{
Rectangle = bb
};
pdfLinkAnnotation.Dictionary["A"] = pdfTypeDictionary;
pdfLinkAnnotation.Dictionary["H"] = PdfTypeName.Create("N");
var pdfTypeArray = PdfTypeArray.Create();
pdfTypeArray.AddInteger(0);
pdfTypeArray.AddInteger(0);
pdfTypeArray.AddInteger(0);
pdfLinkAnnotation.Dictionary["Border"] = pdfTypeArray;
pdfLinkAnnotation.CreateEmptyAppearance(AppearanceStreamModes.Normal);
pdfLinkAnnotation.NormalAppearance.Add(pdfPathObjectNormalAppearance);
pdfLinkAnnotation.GenerateAppearance(AppearanceStreamModes.Normal);
page.Annots.Add(pdfLinkAnnotation);
}
var stream = PdfTypeStream.Create();
var list = PdfIndirectList.FromPdfDocument(doc);
list.Add(stream);
var resDict = FindResource(page.Dictionary);
Patagames.Pdf.Pdfium.FPDF_GenerateContentToStream(doc.Handle, tmpAnnot.NormalAppearance.Handle, stream.Handle, resDict.Handle);
//Add stream to the contentsArray
contentsArray.AddIndirect(list, stream);
}
doc.Save(@"C:\temp\jv 755002_Limited-mod.pdf", SaveFlags.Incremental);
}
It seems that conversion from fake annotation to stream shrinks all contained objects. But this happens only to some files, like this one. Any ideas? Edited by user Wednesday, June 26, 2019 2:19:54 AM(UTC)
| Reason: Not specified
|