4

I have a list of images:

images = {im1,im2,im3...}

and a list of text, that foes with the image:

text = {text1,text2,text3,...}

Now I would like to create one single PDF containing the images and the text. Each pair on a separate page.

So what I did was that...

The code is based on those question:

To make the layout: Layout: Images and Text

To create one single pdf: Export list elements each to it's own page in a multi-paged PDF document

myList = panels ;
report = CreateDocument[Null, 
   PageHeaders -> {{None, None, None}, {None, None, None}}];

SetOptions[report, "PageSize" -> {210, 297}*2, 
 "PaperSize" -> {210, 297}*2]

Do[Paste[report, i];
 NotebookWrite[report, 
  Cell["", "PageBreak", PageBreakBelow -> True]];, {i, myList}] 
SetOptions[report, 
"PageSize" -> {210, 297}*2]

Export["myList.pdf", report, 
 "PageSize" -> {210, 297}*2]; NotebookClose[report]; Clear[report];

{210, 297}*2 is the size of the images...

However my result is:

enter image description here

How can I make the PDF page have the same size as the panel ? The resulting image should fill the complete PDF page

EDIT: @andre enter image description here

Alexey Popkov
  • 61,809
  • 7
  • 149
  • 368
james
  • 3,043
  • 13
  • 29
  • @andre thanks ! I think you are missing an } before the &, right ? Ah, I forgot to mention that in fact the list contains Panel objects – james Oct 27 '17 at 18:39
  • @andre okay, great ! Thanks a lot. Do you know how to handle the same task, if instead of images, I have a list of "panel" objects (http://reference.wolfram.com/language/ref/Panel.html) ? – james Oct 27 '17 at 19:00
  • @andre thank you a lot for trying !! The content is image and text. This is how I create it: Panel[Style[Grid[{ {images[[#]], SpanFromLeft}, {TextCell[Row[{text[[#]]}], TextJustification -> 1, Hyphenation -> False], SpanFromLeft}, {}, {"Notes: "}}, Frame -> {{False, False}, {True, True, True, False}, {False}}, BaseStyle -> ImageSizeMultipliers -> 1], 7, FontFamily -> "Helvetica", Background -> White], Background -> White, ImageSize -> {210, 297}*2] & /@ Range[20] (there are 20 images and text blocs) – james Oct 27 '17 at 19:26

1 Answers1

3

I guess you want to make a PDF file with what you have described here

Try this :

images=Image[ImageTake[#,200+{0,100}],ImageSize->Full]& /@ {ExampleData[{"TestImage","House"}],ExampleData[{"TestImage","Lena"}]};
text={StringTake[ExampleData[{"Text","PlatoMenoEnglish"}],1000],StringTake[ExampleData[{"Text","OriginOfSpecies"}],200]};
pagesToExport=Column[{images[[#]],TextCell[text[[#]],Hyphenation -> False,TextJustification -> 1,CellFrame->True],"Notes:"},Dividers->None]& /@ Range[2];

report = CreateDocument[Null, PageHeaders -> {{None, None, None}, {None, None, None}}];

SetOptions[report, "PageSize" -> {210, 297}*2, "PaperSize" -> {210, 297}*2]
Do[Paste[report, i];
 NotebookWrite[report, 
  Cell["", "PageBreak", PageBreakBelow -> True]];, {i,   pagesToExport}] 

SetOptions[report, "PageSize" -> {210, 297}*2]

fileName="testExportPDF "<>StringReplace[DateString[],":"-> " "]<>".pdf"

Export[fileName, report, "PageSize" -> {210, 297}*2](*; NotebookClose[report]; Clear[report]*);

SystemOpen[fileName]  

enter image description here

EDIT

The control of margins involves 2 kinds of margins :

  • PrintingMargins

  • CellMargins

In the code below, I have reduced all the margins to 0 :

images=Image[ImageTake[#,200+{0,100}],ImageSize->Full]& /@ {ExampleData[{"TestImage","House"}],ExampleData[{"TestImage","Lena"}]};
text={StringTake[ExampleData[{"Text","PlatoMenoEnglish"}],1000],StringTake[ExampleData[{"Text","OriginOfSpecies"}],200]};
pagesToExport=Column[{images[[#]],TextCell[text[[#]],Hyphenation -> False,TextJustification -> 1,CellFrame->True],"Notes:"},Dividers->None]& /@ Range[2];

report = CreateDocument[Null, PageHeaders -> {{None, None, None}, {None, None, None}}];
CurrentValue[report, {PrintingOptions, "PrintingMargins"}]= {{0,0},{0,0}};
SetOptions[report, "PageSize" -> {210, 297}*2, "PaperSize" -> {210, 297}*2]; (* ??? not sure this works ??? *)

Do[Paste[report, i];
 NotebookWrite[report, 
  Cell["", "PageBreak", PageBreakBelow -> True]];, {i,   ExpressionCell[#,CellMargins->{{0,0},{0,0}},ShowStringCharacters->False]& /@ pagesToExport}] 


fileName="testExportPDF "<>StringReplace[DateString[],":"-> " "]<>".pdf"

Export[fileName, report, "PageSize" -> {210, 297}*2](*; NotebookClose[report]; Clear[report]*);

SystemOpen[fileName]   

enter image description here

The relevant modifications are :

  • CurrentValue[report, {PrintingOptions, "PrintingMargins"}]= {{0,0},{0,0}}

  • ExpressionCell[#,CellMargins->{{0,0},{0,0}},ShowStringCharacters->False]& /@ pagesToExport

EDIT 2

Previous code improved to remove the final extraneous pagebreak :

images=Image[ImageTake[#,200+{0,100}],ImageSize->Full]& /@ {ExampleData[{"TestImage","House"}],ExampleData[{"TestImage","Lena"}]};
text={StringTake[ExampleData[{"Text","PlatoMenoEnglish"}],1000],StringTake[ExampleData[{"Text","OriginOfSpecies"}],200]};
pagesToExport=Column[{images[[#]],TextCell[text[[#]],Hyphenation -> False,TextJustification -> 1,CellFrame->True],"Notes:"},Dividers->None]& /@ Range[2];

report = CreateDocument[Null, PageHeaders -> {{None, None, None}, {None, None, None}}];
CurrentValue[report, {PrintingOptions, "PrintingMargins"}]= {{0,0},{0,0}};
SetOptions[report, "PageSize" -> {210, 297}*2, "PaperSize" -> {210, 297}*2]; (* ??? not sure this works ??? *)

reportAndPageBreaks=Riffle[
ExpressionCell[#,CellMargins->{{0,0},{0,0}},ShowStringCharacters->False]& /@ pagesToExport,
Cell["", "PageBreak", PageBreakBelow -> True]]

Do[Paste[report, i];, {i,reportAndPageBreaks}] 

fileName="testExportPDF "<>StringReplace[DateString[],":"-> " "]<>".pdf"

Export[fileName, report, "PageSize" -> {210, 297}*2](*; NotebookClose[report]; Clear[report]*);

SystemOpen[fileName] 
andre314
  • 18,474
  • 1
  • 36
  • 69
  • thank you very much !! Indeed, you guessed right about the purpose of the question. :) I tried your approach, but the images are cropped, as you can see in my edited question. Do you know how to fix it ? If you want to have a look a the images, that I use: https://www.dropbox.com/sh/dobup8qqoz2iq08/AACjrwQ145s0Ou4C2HbXnVpea?dl=0 Another thing, I also would like to decrease the margins on each side and to not show the vertical lines of the text box, basically to have it look as close as possible to here: https://mathematica.stackexchange.com/questions/158664/layout-images-and-text – james Oct 28 '17 at 06:50
  • @totyped I have deliberately cropped the image in my code. To remove this effect just replace the code Image[ImageTake[#,200+{0,100}],ImageSize->Full]& by Image[#,ImageSize->Full]& above. – andre314 Oct 28 '17 at 18:20
  • Note : I don't know if I can make some Mathematica code that download your images from Dropbox. (Without account. I don't want to create a account) – andre314 Oct 28 '17 at 18:23
  • Okay, I will try that. Btw, you do not need a Dropbox account. You can simply download all the pictures as a zip. manually. ...and do you also know, how to handle the margins ? – james Oct 28 '17 at 18:46
  • @totyped Concering the margins, see my Edit – andre314 Oct 28 '17 at 19:46
  • Awesome !! Thank you so much! :)) This really helps ! Maybe you can also poste your answer here: https://mathematica.stackexchange.com/questions/158664/layout-images-and-text – james Oct 28 '17 at 22:25
  • @totyped I could answer the other question, but I would like to propose a block of code that automatically load 2 of your images and do the requested formatting. The target is that people can quickly test the code on their computer (with a simple copy-paste of the block of code) – andre314 Oct 28 '17 at 22:35
  • I see!

    I spotted one little problem, that I don't know how to solve. When I execute the code, it actually prints 3 pages instead of one (the last one is simply an empty page). I think, that is probably due to the page break. After the las image, it adds a page break, effectivly starting a new cell (which is empty). Do you know how to resolve also this problem ?

    – james Oct 29 '17 at 06:43
  • @totyped Concerning the removal of the final pagebreak, see my second EDIT (EDIT 2). Note also : I try to follow your approach, but personally, my approach would have been to use DocumentNotebook because it's more recent. – andre314 Oct 30 '17 at 19:36
  • The page orientation switches unpredictably from "Portrait" to "Landscape". I'm not able to impose "Portrait", even with the option "PageOrientation"->"Portrait". I would be happy to resolve that. (Mma 11.2, Windows 7) – andre314 Oct 30 '17 at 19:45
  • Strange, I am using Mma 10.4 and I did not experience any problems. The code works fine for me. – james Nov 02 '17 at 09:50