4
$\begingroup$

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

$\endgroup$
3
  • $\begingroup$ @andre thanks ! I think you are missing an } before the &, right ? Ah, I forgot to mention that in fact the list contains Panel objects $\endgroup$
    – james
    Commented Oct 27, 2017 at 18:39
  • $\begingroup$ @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 (reference.wolfram.com/language/ref/Panel.html) ? $\endgroup$
    – james
    Commented Oct 27, 2017 at 19:00
  • $\begingroup$ @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) $\endgroup$
    – james
    Commented Oct 27, 2017 at 19:26

1 Answer 1

3
$\begingroup$

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] 
$\endgroup$
11
  • $\begingroup$ 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: 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: mathematica.stackexchange.com/questions/158664/… $\endgroup$
    – james
    Commented Oct 28, 2017 at 6:50
  • $\begingroup$ @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. $\endgroup$
    – andre314
    Commented Oct 28, 2017 at 18:20
  • $\begingroup$ 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) $\endgroup$
    – andre314
    Commented Oct 28, 2017 at 18:23
  • $\begingroup$ 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 ? $\endgroup$
    – james
    Commented Oct 28, 2017 at 18:46
  • $\begingroup$ @totyped Concering the margins, see my Edit $\endgroup$
    – andre314
    Commented Oct 28, 2017 at 19:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.