PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Thu Mar 28, 2024 10:50 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Tue Jan 21, 2020 9:01 am 
Offline

Joined: Sat Jan 18, 2020 1:34 pm
Posts: 5
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?


Top
 Profile  
Reply with quote  
PostPosted: Tue Jan 21, 2020 7:43 pm 
Offline

Joined: Tue Sep 30, 2014 12:29 pm
Posts: 36
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 ! ;-)


Top
 Profile  
Reply with quote  
PostPosted: Wed Jan 22, 2020 9:47 am 
Offline

Joined: Sat Jan 18, 2020 1:34 pm
Posts: 5
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


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC


Who is online

Users browsing this forum: Baidu [Spider] and 154 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group