pdfbox-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Dale King <dalewk...@gmail.com>
Subject Re: Imposition with annotations
Date Mon, 04 Dec 2017 17:42:04 GMT
OK I now have an example to demonstrate the problem I am trying to solve.
First some background:

I am a bass player at my church and rather than use a music stand with
printed sheet music I use a tablet with a pedal to turn pages in a PDF
viewer. I used to try to crop out roughly tablet size chunks from the
original PDF of the music which worked for the most part, but due to where
page breaks landed in the original PDF it was not that great. I had one
song where literally it had a page break before a single measure with a
repeat so I had a page on my tablet with a single measure.

So I have been trying to get around the page break limitation. What I am
doing now is taking the original PDF, duplicating the pages and cropping
out the individual staves of the music. I duplicate and rearrange those
staves to get a linear forward only flow. Right now that is a manual
process, but I hope to automate that a bit more with PDFBox. Then I wrote
some software using PDFBox that takes those individual strips and lays them
out automatically to perfectly my tablet page size using imposition. That
all works.

Sometimes on a staff I need to remove some measures or highlight some
musical direction. To erase stuff I just put a white rectangle annotation
over it to hide it. It would be much more convenient to do this while they
are still strips instead of after being laid out since somtimes the layout
process can be an iterative process. But I have not been able to figure out
how to translate the annotation from the strips to the correct position on
the final page.

So I have attached an input PDF that has only one staff on it. I put a blue
rectangle over a couple of measures as a test. Below is a test program that
imposes that PDF on another page and tries to copy the annotation. Since I
could not come close to figuring out how to get it in the right place (and
I do completely understand matrices and coordinate transformations) I tried
for a simpler goal, just take that annotation and translate it to cover the
entire output page, but even that did not work as can be seen in the output
pdf. Something is getting in the way and adding its own coordinate
translation but I cannot figure out what.

Here is the sample code:

public class PdfTest
    // Pages are 8" x 5" for 1280 x 800 screen
    private static final float PAGE_WIDTH = 8f * 72f;
    private static final float PAGE_HEIGHT = 5f * 72f;

    public static void main(String[] args) throws Exception
        PDDocument document = PDDocument.load(new File(args[0]));

        PDPage sourcePage = document.getPage(0);
        PDRectangle r = sourcePage.getCropBox();

        float maxWidth = r.getWidth();

        float scale = PAGE_WIDTH / maxWidth;

        PDDocument destinationDocument = new PDDocument();

        LayerUtility layerUtility = new LayerUtility(document);
        PDPage destinationPage = new PDPage(new

        float scaledHeight = r.getHeight() * scale;

        float verticalGap = (PAGE_HEIGHT - scaledHeight) /  2;

        PDPageContentStream contents = new
PDPageContentStream(destinationDocument, destinationPage);

        contents.transform(Matrix.getTranslateInstance(0f, PAGE_HEIGHT
- verticalGap));
        contents.transform(Matrix.getScaleInstance(scale, scale));

r.getLowerLeftY() - r.getHeight()));
        contents.drawForm(layerUtility.importPageAsForm(document, 0));


        PDAnnotationSquareCircle ann =




On Sat, Nov 18, 2017 at 2:23 PM, Tilman Hausherr <THausherr@t-online.de>

> I could have a look if you share the files and the code segment.
> Alternatively grab the source code and have a look at
> PDVisibleSignDesigner.java . While there's probably nothing to copy 1:1 as
> a solution, the code in adjustForRotation() might provide some inspiration.
> Tilman
> Am 18.11.2017 um 19:10 schrieb Dale King:
>> Still having no luck trying to figure out translating coordinates for the
>> copied annotations. I an create a new annotation and add it and it goes
>> where I want it, but the copied one seems to have some other transform
>> applied to it and I cannot figure out where that Matrix is coming from.
>> Setting the same rectangle on and annotation that I create vs. one that I
>> copy do not place it in the same place.
>> On Wed, Nov 15, 2017 at 8:50 PM, Dale King <dalewking@gmail.com> wrote:
>> First off, in my case the annotations are not widgets, so the if is not
>>> necessary. The annotations I am most interested in are rectangles to
>>> either
>>> highlight or white to block out content visually.
>>> But removing the if still did not allow it to work because I think I need
>>> to have the annotation take into account the matrix transformations
>>> applied
>>> at the time of adding the page. The annotations have rectangle relative
>>> to
>>> the original page, which and not relative to the transformed page. Looks
>>> like it will be a bit of work to track the many matrix transformations
>>> applied and reversed with restoring graphics state but it is doable.
>>> I will try that and see if it works
>>> On Wed, Nov 15, 2017 at 2:11 PM, Tilman Hausherr <THausherr@t-online.de>
>>> wrote:
>>> Please try this and tell if it works as wished. It copies all annotations
>>>> except those from Acroform.
>>>> List<PDAnnotation> annotations = page.getAnnotations();
>>>> for (PDAnnotation ann : doc.getPage(sourcePage - 1).getAnnotations())
>>>> {
>>>>      if (ann instanceof PDAnnotationWidget)
>>>>      {
>>>>           annotations.add(ann);
>>>>      }
>>>> }
>>>> Tilman
>>>> Am 15.11.2017 um 00:47 schrieb Dale King:
>>>> I am doing PDF imposition following the SuperImpose example (
>>>>> https://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main
>>>>> /java/org/apache/pdfbox/examples/pdmodel/SuperimposePage.jav
>>>>> a?view=markup)
>>>>> and the content is appearing. What doesn't appear on the superimposed
>>>>> page
>>>>> however are the annotations on the original page.
>>>>> Is it possible to do superimpostion including the annotations?
>>>>> ---------------------------------------------------------------------
>>>> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
>>>> For additional commands, e-mail: users-help@pdfbox.apache.org
>>> --
>>> Dale King
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
> For additional commands, e-mail: users-help@pdfbox.apache.org

Dale King

View raw message