sumatrapdfreader / sumatrapdf

SumatraPDF reader
http://www.sumatrapdfreader.org
GNU General Public License v3.0
13.7k stars 1.73k forks source link

Fit width to content width (page crop view) #767

Open Turbine1991 opened 7 years ago

Turbine1991 commented 7 years ago

Hi, I'm a user of a fairly small high DPI device. I find myself with a considerable amount of margin padding and smaller text that I'd like.

Could you please provide a way for each page to scale according to the content with extent. This would make this program perfect on my device.

GitHubRulesOK commented 4 years ago

SumatraPDF does have fit content but many files declare their contents equal full page to include footers and headings even if not populated ! Fit to content width in many cases may not differ from fit page width. Related to #41

yatli commented 3 years ago

@GitHubRulesOK many e-books are typeset like this:

image

So even if we somehow figure out the actual width of the content, we still get huge top/bottom margins:

image

We can display more content if we actually crop the pages:

image

and allow continuously display of pages:

image

... and of course there's a subtle situation if the even and odd pages are typeset differently:

image

GitHubRulesOK commented 3 years ago

SumatraPDF will fit content perfectly it is the source file that dictates how much For example

image

What is missing as requested in this issue by Turbine1991 is to start off based on width then constrain to ONLY by width

yatli commented 3 years ago

Thanks for the info. I'm more interested in the continuous mode cropping though. For example, a page looks like this: image

With width fitting, I still have large margins between two pages: image

Cropping, on the other hand, will result in something like this: image

The viewport is more efficient that it can display more content across pages -- new content not shown in the non-cropping view is marked in green.

GitHubRulesOK commented 3 years ago

I think the request to ignore white space between pages is raised as a separate issue elsewhere. This one is to only ignore left and right margin whitespace

yatli commented 3 years ago

https://github.com/sumatrapdfreader/sumatrapdf/issues/800 is closed as a dupe for this and that's why I'm trying to elaborate it here.

GitHubRulesOK commented 3 years ago

Ok agreed the need for top and bottom margin clipping in the view window, has been lost in deduplication but should be covered by https://github.com/sumatrapdfreader/sumatrapdf/issues/1183

Whilst the two requirements are related this width one is potentially easier to close by combining "fit content & width only " IMHO Cropping whitespace between pages is potentially more complex. but I would have to defer to the developer @kjk to say if so and why.

Whilst not directly related to these issues, In some usage cases a similar need to jump with a set width/zoom per page and a constant margin/offset down from the page top can be assigned to a single shortcut, so for example workaround see https://github.com/sumatrapdfreader/sumatrapdf/issues/1500#

yatli commented 3 years ago

Whilst the two requirements are related this width one is potentially easier to close by combining "fit content & width only "

Yup I guess so, because horizontal stuff doesn't involve continuous display.

Whilst not directly related to these issues, In some usage cases a similar need to jump with a set width/zoom per page and a constant margin/offset down from the page top can be assigned to a single shortcut, so for example workaround see #1500

May I suggest "horizontal lock" which restricts the panning to content width? Could be useful if one wants a zoomed view, but also wants the width boundary.

yatli commented 3 years ago

Also, https://github.com/mbaeuerle/Briss-2.0 is good. Mentioned in the other thread, but this is 2.0 and on Github now.

GitHubRulesOK commented 3 years ago

Briss is certainly the one to use as a yardstick for competing cropping tools (thanks for the updated link) There are some good virtual printer options that can add a cropbox on a fixed margin basis say blindly removing 20mm all round (European users can for example use PDF24 desktop) but those may not trim odd and even differently or change each page length to the mimimum required.

GitHubRulesOK commented 3 years ago

@yatli

Not the answer as SumatraPDF will not usually alter the source file (except for annotation), but perhaps an interim hotfix for some types of file.

I tried Briss CLI autocropping whilst viewing with above test file and although it had problems trimming the 1st 2nd and 5th pages however the 3rd and 4th simpler text were correctly trimmed to only the text region. I may need to try looking if there are any settings to tweak or perhaps try other CLI apps

Briss will understandably not overwrite the source file, so command line needs to be copy "%1" "%1"-backup.pdf & "path to Briss-2.0/bin/Briss-2.0.bat" -s "%1"-backup.pdf -d "%1"

To crop whilst viewing current PDF in SumatraPDF replace "%1" with "%f" in ExternalViewers command line (Reminder: there is? a 32MB limit on changing the file you are viewing, question to self how does that apply to annotation ?)

before shortcut image

after shortcut image

My desired result like this I have only found using either free online croppers or $79 PDFMC (VeryDOCPDFMarginCrop) image

yatli commented 3 years ago

I see. Your use case requires heterogeneous cropping for different pages. Briss's algorithm simply unions the bounds and then calculate "bound of bounds".

GitHubRulesOK commented 3 years ago

I never advocate VeryPDF as they rip-off SumatraPDF as their own viewer, however so far they appear to best match the core need offline to remove all margins whilst retaining bookmarks and internal links. So for example the page numbers here are functional. (the visible page breaks will not show if you set PageSpacing = 0 0 image

yatli commented 3 years ago

the visible page breaks will not show if you set PageSpacing = 0 0

Some proper spacing is good for health though :)

.. wait, why does your sumatra have these nice looking mono icons?

GitHubRulesOK commented 3 years ago

".. wait, why does your sumatra have these nice looking mono icons?"

The SVG icons have been there long time since after version 3.2 I use the daily build as its usually stable and has so many improvements

GitHubRulesOK commented 3 years ago

OK only using windows CLI I can get something very close that does those 5 pages in a few seconds after a shortcut so just need to script round that for more info on the gui version see https://github.com/abarker/pdfCropMargins

I can also clip bad edges combined with a uniform automated width and remove top/bottom headers etc, just by using a small set of optional CLI preferences, so for me this FreeOpenSourceSoftware powerfull option seems to address any of my current/future needs and I can throw away the Very expensive version.

image

requires first time waiting for the updates

python wait for complete MS store (NO NEED TO SIGN IN) download before next step pause

rem confirm installer updates and wait till finished python.exe -m pip install --upgrade pip pause

rem lets get the full gui version with cli support pip3 install pdfCropMargins[gui] --user --upgrade pause

rem just for testing only prefix to path but will need SETX for permanent use path=C:\Users\your name goes here\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\Scripts;%path%

rem here is the guts for the scripts we will need to work it around %f in externalviewers command pdf-crop-margins "%1"

GitHubRulesOK commented 3 years ago

Finally I think I can get a good result inside SumatraPDF where the page width is a constant (with minimum side margins) and all the white space between pages is set to be invisible. (note the file viewports are modified, so original can be restored, but easier to just keep source as a backup) image

yatli commented 3 years ago

Hmm. Curious if font size consistency is on the top of your requirements. I would happily accept some automatic padding to maintain that.

GitHubRulesOK commented 3 years ago

The fonts should be unaffected as there is no editing involved it is just the shape of the cropbox that changes. So maintaining constant paper width will not affect source font size Hence my query with the margin cropping author was how to keep width constant especially for that reason

yatli commented 3 years ago

Oh ok. I thought the content is zoomed to the full width.

GitHubRulesOK commented 3 years ago

The earlier attempts were crop all white space but it became apparent there was then misalignment between column of text cant compensate for bad placements but if all page widths are equal the effect was minimal even on staggered odd and even. My main issue is that my reboot system wipes out any permanent Python install so am working on making that part networked/portable. If your need is great enough and are happy to try it out after setting a restore point then install the Full Microsoft version as it could well be worth it just for this application.

yatli commented 3 years ago

If anyone can give me some guidance I can take a stab at integrating this feature into sumatra.

GitHubRulesOK commented 3 years ago

Direct integration using C++ is way beyond me, it would be down to @kjk to provide guidance, however I can run external scripting, such as here, using an independent CMD>CLI from within SumatraPDF. The main issue so far is the heavy overhead of installing the means to run a Python / Wheel script, thus my personal approach is to find a minimal install of Python, which then could also be used ad hoc for any other similar cases.

yatli commented 3 years ago

I am familiar with win32 programming, have some limited experience with MuPDF. Haven't dig into the sumatra codebase, so I need to know where to look for insertion points.

GitHubRulesOK commented 3 years ago

Ah that may help Since the python script uses GS/MuPDF you might be able to pick out how it uses them and emulate a similar independent solution without the need for the python wrapping

yatli commented 3 years ago

After skimming through the code I think the place to add crop info is DisplayModel.h, PageInfo

Just place it aside the content box:

    /* data that is calculated when needed. actual content size within a page (View target) */
    RectF contentBox{};

    /* data that is calculated when needed. cropped bounding box of a page */
    RectF cropBounds{};
yatli commented 3 years ago

I don't think we should go into MuPDF because a generic cropper for pages is preferable.

yatli commented 3 years ago

Things are robust enough for me to mess around:

image

GitHubRulesOK commented 3 years ago

Looks like your headed in the right (Pun) direction, just unclear how that needs to be adjustable, since I can almost hear the request for each user wanting different amounts of padding or a width setting, interesting to see if there is an easy way to override speckled edge pages.

GitHubRulesOK commented 3 years ago

@yatli

Some proper spacing is good for health though :)

I now see that some is needed when a larger space between paragraphs is cut by a page boundary, however that would require some very clever AI to allow for. image

So its much simpler to keep just a little each side of a page break, say +-4points then user can gloss over those within paragraphs and accept the intentional smaller gap between paragraphs.

I am happy SumatraPDF running inside Portable Python can provide for my needs within seconds using 2 steps along the lines of pdf-crop-margins -o temp.pdf -p4 100 0 100 0 sample.pdf & pdf-crop-margins -o sample.pdf -u temp.pdf with perhaps a third step to add a little back top and bottom, but then if my background was white I would not notice.

I am sure I can run it the other way round so SumatraPDF runs Portable Python with the script on one or more files/tabs. But it feels like a hammer to crack a nut. If you can achieve similar "On the Fly" in native SumatraPDF it should close this issue and possibly the other one.

image

yujiewu66 commented 3 years ago

Okular PDF has the option to trim view and you can select your own margins even. It also allows change paper color, change font/background color in its accessability settings. The only downside for me is that it is not as smooth as Sumatra when it comes to processing the files. Still a good option before the trim view becomes available in Sumatra.