PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

Insert XGraphic to Existing PDF OCG Layer/Group
https://forum.pdfsharp.net/viewtopic.php?f=2&t=4086
Page 1 of 1

Author:  VanguardAnon [ Tue Jan 21, 2020 9:01 am ]
Post subject:  Insert XGraphic to Existing PDF OCG Layer/Group

The issue is the PDF has an Existing OCG layer Structure like this:
/Resources
/Properties
/OC1
/Name : Printing Layer
/Type : OCG

I am now Creating the Text Objects that need to be placed into an OCG Layer so that my Printing device can recognise the text.
I generate the Text like this

Code:
public static void DrawTextIn(IList<org.pdfclown.documents.contents.ITextString> texts, PdfSharp.Pdf.PdfDocument doc)
        {
            var page = doc.Pages[0];
           
            XForm form = new XForm(doc, XUnit.FromMillimeter(70), XUnit.FromMillimeter(55));
            XGraphics formGfx = XGraphics.FromForm(form);
         

            XPdfFontOptions options = new XPdfFontOptions(PdfFontEncoding.Unicode);
            XFont BodyFont = new XFont("Tahoma", 10.5, XFontStyle.Bold, options);
 
            var cont = formGfx.BeginContainer();
            foreach (var text in texts)
            {
                var box = text.Box;
                var content = text.Text;
                // Re-drawing the  Text at their positions
                formGfx.DrawString(content, BodyFont, XBrushes.Black, new XRect(box.Value.X, box.Value.Y, box.Value.Width, box.Value.Height),  XStringFormats.TopCenter);
               
             
            }
            formGfx.EndContainer(cont);
            formGfx.Dispose();

            XGraphics gfx = XGraphics.FromPdfPage(page);
            gfx.DrawImage(form, 0, 0);

        }


Now that i have the XGraphic and XForm added to the PDF they show up as an /XObject /Form in the dictionary, but how can i then add these to the OCG Properties layer?

Author:  (void) [ Tue Jan 21, 2020 7:43 pm ]
Post subject:  Re: Insert XGraphic to Existing PDF OCG Layer/Group

Quote:
I essentially need to either copy the 6 Vector icons which are located in OCG Layers to a new PDF, or delete all the Stroke/Vector Lines from the original file and save that.

Out of curiosity i had a look at your document.
What you're trying to achieve is probably doable, but it gets somewhat involved.

It helps, when you are able to look at your pdf in a text-editor.
For this i used mutool with the "clean" option (https://mupdf.com/docs/manual-mutool-clean.html)
This helps identifying objects and seeing the overall document structure.
Having a clean pdf, we can look at the objects involved:

Page object:
Code:
9 0 obj
<<
  /Contents 10 0 R               <- Defines, what gets drawn onto the page
  ....
  /Resources <<
    /XObject <<
      /Fm0 16 0 R               <- Your icons are defined here, one XObject for each icon, note the Name (/Fm0 to /Fm5)
      /Fm1 17 0 R
      /Fm2 18 0 R
      /Fm3 19 0 R
      /Fm4 20 0 R
      /Fm5 21 0 R
    >>
    .....
  >>
  /Type /Page
>>
endobj


The Page contents:
Code:
10 0 obj
<<
  /Length 983
>>
stream                        <- Page-contents are a stream of drawing operators
....
q                           <- Save graphics state
0 0 0 RG                     <- Set stroke color
425 41 130 82 re               <- Append rectangle to path
S                           <- Stroke path
Q                           <- Restore graphics state
....
q
1 0 0 1 354.23965 397.89322 cm      <- Append transformation matrix
/C2_0 8 Tf                     <- Set drawing font (should not be necessary, but not sure)
/Fm0 Do                        <- Draw the icon (a named XObject, the name corresponds to an entry in the Resources/XObject dictionary above)
Q


I would try the following:
- open the document
- from the Catalog, delete the "OCProperties" to get rid of the layers (just to clean up, maybe you can keep it)
- create a PdfSharp.Pdf.Content.ContentReader for the page
- remove all operators not involved in drawing the icons, the goal is to end up with a content-stream like this:
Code:
q
1 0 0 1 354.23965 397.89322 cm
/C2_0 8 Tf      <- maybe you can delete these as well
/Fm0 Do
Q

q
1 0 0 1 432.90109 397.89322 cm
/C2_0 8 Tf
/Fm1 Do
Q
....

- save the document

The steps may be incorrect and/or incomplete, but you should get the idea...


Quote:
Is there a way for me to Parse all Text Objects, find their Position and Value and then just use the XGraphics options in PDFSharp to just re-draw the text to a layer?

If the text is also wrapped in named XObjects, you can use the same method as described above.
But when the text is drawn directly onto the page, then it gets far more involved; you basically have to write your own pdf-rendering-engine, handling all kinds of font-stuff and drawing operators, just to get the positions right.
Thats outside my expertise, but i wish you best of luck ! ;-)

Author:  VanguardAnon [ Wed Jan 22, 2020 9:47 am ]
Post subject:  Re: Insert XGraphic to Existing PDF OCG Layer/Group

Hi!
That was exactly the sort of explanation i needed to understand it.
I was able to throw together a pretty hacky implementation to get what i needed done by writing directly to the content stream

Thanks again

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/