You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@pdfbox.apache.org by Michael Karnerfors <mi...@digitalcontext.se> on 2012/09/20 14:43:41 UTC

Getting PDFImageWriter to honor disabled Optional Content Groups

 

Hello all,

 

I want to use PDFBox to load a PDF file, disable an Optional Content Group,
and then use PDFImageWriter  to output the resulting document to an image
file. 

 

The problem is that the image writer does not seem to honor the fact I
disabled the OCG and still outputs the layer. 

 

When I write the document as a new PDF file, Adobe Acrobat honors the fact I
turned the OCG off. 

 

Does anyone know how you can make PDFImageWriter honor the disabled OCG?

 

With best regards

/Michael

 

Code Sample: 

 

String password = "";

String pdfFile = "input_file.pdf";

String outputPrefix = "output_file";

String imageFormat = "png";

int startPage = 1;

int endPage = Integer.MAX_VALUE;

String color = "rgba";

int resolution = 300;

 

PDDocument document = null;

 

try {

document = PDDocument.load(pdfFile);

 

PDDocumentCatalog catalog = document.getDocumentCatalog();

PDOptionalContentProperties ocgs = catalog.getOCProperties();

                

           for(String groupName : ocgs.getGroupNames()) {

           if(groupName.startsWith("SmartMarks")) {

                                 ocgs.setGroupEnabled(groupName, false);

}

}

 

           PDFImageWriter imageWriter = new PDFImageWriter();

imageWriter.writeImage(document, imageFormat, password, startPage, endPage,
outputPrefix, imageType, resolution);

 

} catch (Exception e) {

System.err.println(e);

} finally {

if (document != null) { document.close(); }

}


RE: Getting PDFImageWriter to honor disabled Optional Content Groups

Posted by Michael Karnerfors <mi...@digitalcontext.se>.
Oh yes, that is exactly where I am: a newbie in the wonderful and confusing
world of PDFs. :)

1-2 days is alright though. Compared to the commercial alternatives this
translates to less money compared to the effort spent.

Any and all clues here will be greatly appreciated. 

With best regards
/Michael


-----Original Message-----
From: Jeremias Maerki [mailto:dev@jeremias-maerki.ch] 
Sent: den 20 september 2012 16:50
To: users@pdfbox.apache.org
Subject: Re: Getting PDFImageWriter to honor disabled Optional Content
Groups

I don't think it will be a lot. For someone new to PDFBox's source code the
biggest part is probably to understand the execution flow when painting PDF.
Basically, one needs to intercept the BMC/MDC/EMC operators and find a clean
way to suppress the drawing of content inside the OCGs that are disabled.
The decision whether to suppress is easy since the API for that is
available. I haven't looked closely at how this would be be done but I can't
imagine this will take more than 1-2 days for someone relatively new to
PDFBox. No guarantees.

Maybe someone else already has an idea how to do the filtering?


Jeremias Maerki


On 20.09.2012 16:42:54 Michael Karnerfors wrote:
> 
> Hi Jeremias,
> 
> Ok, understood. Do you have any sense of how much work that might be? 
> 
> With best regards,
> /Michael
> 
> 
> 
> -----Original Message-----
> From: Jeremias Maerki [mailto:dev@jeremias-maerki.ch]
> Sent: den 20 september 2012 16:15
> To: users@pdfbox.apache.org
> Subject: Re: Getting PDFImageWriter to honor disabled Optional Content 
> Groups
> 
> Hi Michael,
> I'm afraid support for optional content groups has not reached the 
> stage that they are checked when painting pages. You'll have to dive 
> in and add filtering for disabled OCGs.
> 
> As a starting point, see org.apache.pdfbox.pdfviewer.PageDrawer's
> constructor which points you to the properties file that defines the 
> Operator implementations for PageDrawer. There, implementations for 
> BMC/BDC/EMC are missing. These will have to be implemented in a way 
> that painting operators are suppressed for a disabled OCG.
> 
> Jeremias Maerki
> 
> 
> On 20.09.2012 14:43:41 Michael Karnerfors wrote:
> >  
> > 
> > Hello all,
> > 
> >  
> > 
> > I want to use PDFBox to load a PDF file, disable an Optional Content 
> > Group, and then use PDFImageWriter  to output the resulting document 
> > to an image file.
> > 
> >  
> > 
> > The problem is that the image writer does not seem to honor the fact 
> > I disabled the OCG and still outputs the layer.
> > 
> >  
> > 
> > When I write the document as a new PDF file, Adobe Acrobat honors 
> > the fact I turned the OCG off.
> > 
> >  
> > 
> > Does anyone know how you can make PDFImageWriter honor the disabled OCG?
> > 
> >  
> > 
> > With best regards
> > 
> > /Michael
> > 
> >  
> > 
> > Code Sample: 
> > 
> >  
> > 
> > String password = "";
> > 
> > String pdfFile = "input_file.pdf";
> > 
> > String outputPrefix = "output_file";
> > 
> > String imageFormat = "png";
> > 
> > int startPage = 1;
> > 
> > int endPage = Integer.MAX_VALUE;
> > 
> > String color = "rgba";
> > 
> > int resolution = 300;
> > 
> >  
> > 
> > PDDocument document = null;
> > 
> >  
> > 
> > try {
> > 
> > document = PDDocument.load(pdfFile);
> > 
> >  
> > 
> > PDDocumentCatalog catalog = document.getDocumentCatalog();
> > 
> > PDOptionalContentProperties ocgs = catalog.getOCProperties();
> > 
> >                 
> > 
> >            for(String groupName : ocgs.getGroupNames()) {
> > 
> >            if(groupName.startsWith("SmartMarks")) {
> > 
> >                                  ocgs.setGroupEnabled(groupName, 
> > false);
> > 
> > }
> > 
> > }
> > 
> >  
> > 
> >            PDFImageWriter imageWriter = new PDFImageWriter();
> > 
> > imageWriter.writeImage(document, imageFormat, password, startPage, 
> > endPage, outputPrefix, imageType, resolution);
> > 
> >  
> > 
> > } catch (Exception e) {
> > 
> > System.err.println(e);
> > 
> > } finally {
> > 
> > if (document != null) { document.close(); }
> > 
> > }
> > 


Re: Getting PDFImageWriter to honor disabled Optional Content Groups

Posted by Jeremias Maerki <de...@jeremias-maerki.ch>.
I don't think it will be a lot. For someone new to PDFBox's source code
the biggest part is probably to understand the execution flow when
painting PDF. Basically, one needs to intercept the BMC/MDC/EMC
operators and find a clean way to suppress the drawing of content inside
the OCGs that are disabled. The decision whether to suppress is easy
since the API for that is available. I haven't looked closely at how
this would be be done but I can't imagine this will take more than 1-2
days for someone relatively new to PDFBox. No guarantees.

Maybe someone else already has an idea how to do the filtering?


Jeremias Maerki


On 20.09.2012 16:42:54 Michael Karnerfors wrote:
> 
> Hi Jeremias,
> 
> Ok, understood. Do you have any sense of how much work that might be? 
> 
> With best regards,
> /Michael
> 
> 
> 
> -----Original Message-----
> From: Jeremias Maerki [mailto:dev@jeremias-maerki.ch] 
> Sent: den 20 september 2012 16:15
> To: users@pdfbox.apache.org
> Subject: Re: Getting PDFImageWriter to honor disabled Optional Content
> Groups
> 
> Hi Michael,
> I'm afraid support for optional content groups has not reached the stage
> that they are checked when painting pages. You'll have to dive in and add
> filtering for disabled OCGs.
> 
> As a starting point, see org.apache.pdfbox.pdfviewer.PageDrawer's
> constructor which points you to the properties file that defines the
> Operator implementations for PageDrawer. There, implementations for
> BMC/BDC/EMC are missing. These will have to be implemented in a way that
> painting operators are suppressed for a disabled OCG.
> 
> Jeremias Maerki
> 
> 
> On 20.09.2012 14:43:41 Michael Karnerfors wrote:
> >  
> > 
> > Hello all,
> > 
> >  
> > 
> > I want to use PDFBox to load a PDF file, disable an Optional Content 
> > Group, and then use PDFImageWriter  to output the resulting document 
> > to an image file.
> > 
> >  
> > 
> > The problem is that the image writer does not seem to honor the fact I 
> > disabled the OCG and still outputs the layer.
> > 
> >  
> > 
> > When I write the document as a new PDF file, Adobe Acrobat honors the 
> > fact I turned the OCG off.
> > 
> >  
> > 
> > Does anyone know how you can make PDFImageWriter honor the disabled OCG?
> > 
> >  
> > 
> > With best regards
> > 
> > /Michael
> > 
> >  
> > 
> > Code Sample: 
> > 
> >  
> > 
> > String password = "";
> > 
> > String pdfFile = "input_file.pdf";
> > 
> > String outputPrefix = "output_file";
> > 
> > String imageFormat = "png";
> > 
> > int startPage = 1;
> > 
> > int endPage = Integer.MAX_VALUE;
> > 
> > String color = "rgba";
> > 
> > int resolution = 300;
> > 
> >  
> > 
> > PDDocument document = null;
> > 
> >  
> > 
> > try {
> > 
> > document = PDDocument.load(pdfFile);
> > 
> >  
> > 
> > PDDocumentCatalog catalog = document.getDocumentCatalog();
> > 
> > PDOptionalContentProperties ocgs = catalog.getOCProperties();
> > 
> >                 
> > 
> >            for(String groupName : ocgs.getGroupNames()) {
> > 
> >            if(groupName.startsWith("SmartMarks")) {
> > 
> >                                  ocgs.setGroupEnabled(groupName, 
> > false);
> > 
> > }
> > 
> > }
> > 
> >  
> > 
> >            PDFImageWriter imageWriter = new PDFImageWriter();
> > 
> > imageWriter.writeImage(document, imageFormat, password, startPage, 
> > endPage, outputPrefix, imageType, resolution);
> > 
> >  
> > 
> > } catch (Exception e) {
> > 
> > System.err.println(e);
> > 
> > } finally {
> > 
> > if (document != null) { document.close(); }
> > 
> > }
> > 


RE: Getting PDFImageWriter to honor disabled Optional Content Groups

Posted by Michael Karnerfors <mi...@digitalcontext.se>.
Hi Jeremias,

Ok, understood. Do you have any sense of how much work that might be? 

With best regards,
/Michael



-----Original Message-----
From: Jeremias Maerki [mailto:dev@jeremias-maerki.ch] 
Sent: den 20 september 2012 16:15
To: users@pdfbox.apache.org
Subject: Re: Getting PDFImageWriter to honor disabled Optional Content
Groups

Hi Michael,
I'm afraid support for optional content groups has not reached the stage
that they are checked when painting pages. You'll have to dive in and add
filtering for disabled OCGs.

As a starting point, see org.apache.pdfbox.pdfviewer.PageDrawer's
constructor which points you to the properties file that defines the
Operator implementations for PageDrawer. There, implementations for
BMC/BDC/EMC are missing. These will have to be implemented in a way that
painting operators are suppressed for a disabled OCG.

Jeremias Maerki


On 20.09.2012 14:43:41 Michael Karnerfors wrote:
>  
> 
> Hello all,
> 
>  
> 
> I want to use PDFBox to load a PDF file, disable an Optional Content 
> Group, and then use PDFImageWriter  to output the resulting document 
> to an image file.
> 
>  
> 
> The problem is that the image writer does not seem to honor the fact I 
> disabled the OCG and still outputs the layer.
> 
>  
> 
> When I write the document as a new PDF file, Adobe Acrobat honors the 
> fact I turned the OCG off.
> 
>  
> 
> Does anyone know how you can make PDFImageWriter honor the disabled OCG?
> 
>  
> 
> With best regards
> 
> /Michael
> 
>  
> 
> Code Sample: 
> 
>  
> 
> String password = "";
> 
> String pdfFile = "input_file.pdf";
> 
> String outputPrefix = "output_file";
> 
> String imageFormat = "png";
> 
> int startPage = 1;
> 
> int endPage = Integer.MAX_VALUE;
> 
> String color = "rgba";
> 
> int resolution = 300;
> 
>  
> 
> PDDocument document = null;
> 
>  
> 
> try {
> 
> document = PDDocument.load(pdfFile);
> 
>  
> 
> PDDocumentCatalog catalog = document.getDocumentCatalog();
> 
> PDOptionalContentProperties ocgs = catalog.getOCProperties();
> 
>                 
> 
>            for(String groupName : ocgs.getGroupNames()) {
> 
>            if(groupName.startsWith("SmartMarks")) {
> 
>                                  ocgs.setGroupEnabled(groupName, 
> false);
> 
> }
> 
> }
> 
>  
> 
>            PDFImageWriter imageWriter = new PDFImageWriter();
> 
> imageWriter.writeImage(document, imageFormat, password, startPage, 
> endPage, outputPrefix, imageType, resolution);
> 
>  
> 
> } catch (Exception e) {
> 
> System.err.println(e);
> 
> } finally {
> 
> if (document != null) { document.close(); }
> 
> }
> 


Re: Getting PDFImageWriter to honor disabled Optional Content Groups

Posted by Jeremias Maerki <de...@jeremias-maerki.ch>.
Hi Michael,
I'm afraid support for optional content groups has not reached the stage
that they are checked when painting pages. You'll have to dive in and
add filtering for disabled OCGs.

As a starting point, see org.apache.pdfbox.pdfviewer.PageDrawer's
constructor which points you to the properties file that defines the
Operator implementations for PageDrawer. There, implementations for
BMC/BDC/EMC are missing. These will have to be implemented in a way that
painting operators are suppressed for a disabled OCG.

Jeremias Maerki


On 20.09.2012 14:43:41 Michael Karnerfors wrote:
>  
> 
> Hello all,
> 
>  
> 
> I want to use PDFBox to load a PDF file, disable an Optional Content Group,
> and then use PDFImageWriter  to output the resulting document to an image
> file. 
> 
>  
> 
> The problem is that the image writer does not seem to honor the fact I
> disabled the OCG and still outputs the layer. 
> 
>  
> 
> When I write the document as a new PDF file, Adobe Acrobat honors the fact I
> turned the OCG off. 
> 
>  
> 
> Does anyone know how you can make PDFImageWriter honor the disabled OCG?
> 
>  
> 
> With best regards
> 
> /Michael
> 
>  
> 
> Code Sample: 
> 
>  
> 
> String password = "";
> 
> String pdfFile = "input_file.pdf";
> 
> String outputPrefix = "output_file";
> 
> String imageFormat = "png";
> 
> int startPage = 1;
> 
> int endPage = Integer.MAX_VALUE;
> 
> String color = "rgba";
> 
> int resolution = 300;
> 
>  
> 
> PDDocument document = null;
> 
>  
> 
> try {
> 
> document = PDDocument.load(pdfFile);
> 
>  
> 
> PDDocumentCatalog catalog = document.getDocumentCatalog();
> 
> PDOptionalContentProperties ocgs = catalog.getOCProperties();
> 
>                 
> 
>            for(String groupName : ocgs.getGroupNames()) {
> 
>            if(groupName.startsWith("SmartMarks")) {
> 
>                                  ocgs.setGroupEnabled(groupName, false);
> 
> }
> 
> }
> 
>  
> 
>            PDFImageWriter imageWriter = new PDFImageWriter();
> 
> imageWriter.writeImage(document, imageFormat, password, startPage, endPage,
> outputPrefix, imageType, resolution);
> 
>  
> 
> } catch (Exception e) {
> 
> System.err.println(e);
> 
> } finally {
> 
> if (document != null) { document.close(); }
> 
> }
>