Add a footer like “page x of y” when creating a PDF using UIGraphicsPDFRenderer
I'm working on a function in my app that lets users export their data into a nicely designed PDF.
I'm using UIGraphicsPDFRenderer
for this and I went through Apple's documentation.
I'm having a problem adding a footer like "page x of y". While "x" is easy - I'm having troubles determining the "y" since I only know how many pages my document has once I have fully rendered the PDF. I cannot determine the number of pages in advance because of the rather complex layout.
Now I also know that new pages are created with beginPage()
. Is there also a way to go to the previous page? - Because then I could simply go through the document and add the missing footer.
Here's the code I use (very much simplified, but it should be enough to get the idea) in Swift 4:
let pdf = renderer.pdfData { (context) in
let attributes = [ NSFontAttributeName : UIFont.boldSystemFont(ofSize: 15) ]
// page 1
context.beginPage()
NSAttributedString(string: "Page 1", attributes: attributes).draw(in: CGRect(x: 0, y: 0, width: 500, height: 200))
// page 2
context.beginPage()
NSAttributedString(string: "Page 2", attributes: attributes).draw(in: CGRect(x: 0, y: 0, width: 500, height: 200))
// page 3
context.beginPage()
NSAttributedString(string: "Page 3", attributes: attributes).draw(in: CGRect(x: 0, y: 0, width: 500, height: 200))
// Now that I know that the PDF will have 3 pages, add the footer on all 3 pages - but how?
}
}
ios swift pdf
|
show 2 more comments
I'm working on a function in my app that lets users export their data into a nicely designed PDF.
I'm using UIGraphicsPDFRenderer
for this and I went through Apple's documentation.
I'm having a problem adding a footer like "page x of y". While "x" is easy - I'm having troubles determining the "y" since I only know how many pages my document has once I have fully rendered the PDF. I cannot determine the number of pages in advance because of the rather complex layout.
Now I also know that new pages are created with beginPage()
. Is there also a way to go to the previous page? - Because then I could simply go through the document and add the missing footer.
Here's the code I use (very much simplified, but it should be enough to get the idea) in Swift 4:
let pdf = renderer.pdfData { (context) in
let attributes = [ NSFontAttributeName : UIFont.boldSystemFont(ofSize: 15) ]
// page 1
context.beginPage()
NSAttributedString(string: "Page 1", attributes: attributes).draw(in: CGRect(x: 0, y: 0, width: 500, height: 200))
// page 2
context.beginPage()
NSAttributedString(string: "Page 2", attributes: attributes).draw(in: CGRect(x: 0, y: 0, width: 500, height: 200))
// page 3
context.beginPage()
NSAttributedString(string: "Page 3", attributes: attributes).draw(in: CGRect(x: 0, y: 0, width: 500, height: 200))
// Now that I know that the PDF will have 3 pages, add the footer on all 3 pages - but how?
}
}
ios swift pdf
1
Please, provide a code you tried so far.
– fewlinesofcode
Nov 15 '18 at 11:39
Can't you create a pdf without the footer, get the pageCount, discard that pdf and create another one or simply edit the footer pages count?
– Leo Dabus
Nov 15 '18 at 12:20
@fewlinesofcode I've added a sample code to get the idea of what I'm trying to do.
– Chris
Nov 15 '18 at 13:02
@LeoDabus Thanks, that's a good idea - that will work for sure. Only thing I do not so much like about it is, that I more or less create the same document twice. I'm going to have to test it with larger amounts of data to see how it's going to work out :)
– Chris
Nov 15 '18 at 13:06
1
@LeoDabus thanks for the hints - added the Swift version (version 4) and updated the code to use NSAttributedString.
– Chris
Nov 15 '18 at 14:05
|
show 2 more comments
I'm working on a function in my app that lets users export their data into a nicely designed PDF.
I'm using UIGraphicsPDFRenderer
for this and I went through Apple's documentation.
I'm having a problem adding a footer like "page x of y". While "x" is easy - I'm having troubles determining the "y" since I only know how many pages my document has once I have fully rendered the PDF. I cannot determine the number of pages in advance because of the rather complex layout.
Now I also know that new pages are created with beginPage()
. Is there also a way to go to the previous page? - Because then I could simply go through the document and add the missing footer.
Here's the code I use (very much simplified, but it should be enough to get the idea) in Swift 4:
let pdf = renderer.pdfData { (context) in
let attributes = [ NSFontAttributeName : UIFont.boldSystemFont(ofSize: 15) ]
// page 1
context.beginPage()
NSAttributedString(string: "Page 1", attributes: attributes).draw(in: CGRect(x: 0, y: 0, width: 500, height: 200))
// page 2
context.beginPage()
NSAttributedString(string: "Page 2", attributes: attributes).draw(in: CGRect(x: 0, y: 0, width: 500, height: 200))
// page 3
context.beginPage()
NSAttributedString(string: "Page 3", attributes: attributes).draw(in: CGRect(x: 0, y: 0, width: 500, height: 200))
// Now that I know that the PDF will have 3 pages, add the footer on all 3 pages - but how?
}
}
ios swift pdf
I'm working on a function in my app that lets users export their data into a nicely designed PDF.
I'm using UIGraphicsPDFRenderer
for this and I went through Apple's documentation.
I'm having a problem adding a footer like "page x of y". While "x" is easy - I'm having troubles determining the "y" since I only know how many pages my document has once I have fully rendered the PDF. I cannot determine the number of pages in advance because of the rather complex layout.
Now I also know that new pages are created with beginPage()
. Is there also a way to go to the previous page? - Because then I could simply go through the document and add the missing footer.
Here's the code I use (very much simplified, but it should be enough to get the idea) in Swift 4:
let pdf = renderer.pdfData { (context) in
let attributes = [ NSFontAttributeName : UIFont.boldSystemFont(ofSize: 15) ]
// page 1
context.beginPage()
NSAttributedString(string: "Page 1", attributes: attributes).draw(in: CGRect(x: 0, y: 0, width: 500, height: 200))
// page 2
context.beginPage()
NSAttributedString(string: "Page 2", attributes: attributes).draw(in: CGRect(x: 0, y: 0, width: 500, height: 200))
// page 3
context.beginPage()
NSAttributedString(string: "Page 3", attributes: attributes).draw(in: CGRect(x: 0, y: 0, width: 500, height: 200))
// Now that I know that the PDF will have 3 pages, add the footer on all 3 pages - but how?
}
}
ios swift pdf
ios swift pdf
edited Nov 15 '18 at 13:59
Chris
asked Nov 15 '18 at 11:26
ChrisChris
136
136
1
Please, provide a code you tried so far.
– fewlinesofcode
Nov 15 '18 at 11:39
Can't you create a pdf without the footer, get the pageCount, discard that pdf and create another one or simply edit the footer pages count?
– Leo Dabus
Nov 15 '18 at 12:20
@fewlinesofcode I've added a sample code to get the idea of what I'm trying to do.
– Chris
Nov 15 '18 at 13:02
@LeoDabus Thanks, that's a good idea - that will work for sure. Only thing I do not so much like about it is, that I more or less create the same document twice. I'm going to have to test it with larger amounts of data to see how it's going to work out :)
– Chris
Nov 15 '18 at 13:06
1
@LeoDabus thanks for the hints - added the Swift version (version 4) and updated the code to use NSAttributedString.
– Chris
Nov 15 '18 at 14:05
|
show 2 more comments
1
Please, provide a code you tried so far.
– fewlinesofcode
Nov 15 '18 at 11:39
Can't you create a pdf without the footer, get the pageCount, discard that pdf and create another one or simply edit the footer pages count?
– Leo Dabus
Nov 15 '18 at 12:20
@fewlinesofcode I've added a sample code to get the idea of what I'm trying to do.
– Chris
Nov 15 '18 at 13:02
@LeoDabus Thanks, that's a good idea - that will work for sure. Only thing I do not so much like about it is, that I more or less create the same document twice. I'm going to have to test it with larger amounts of data to see how it's going to work out :)
– Chris
Nov 15 '18 at 13:06
1
@LeoDabus thanks for the hints - added the Swift version (version 4) and updated the code to use NSAttributedString.
– Chris
Nov 15 '18 at 14:05
1
1
Please, provide a code you tried so far.
– fewlinesofcode
Nov 15 '18 at 11:39
Please, provide a code you tried so far.
– fewlinesofcode
Nov 15 '18 at 11:39
Can't you create a pdf without the footer, get the pageCount, discard that pdf and create another one or simply edit the footer pages count?
– Leo Dabus
Nov 15 '18 at 12:20
Can't you create a pdf without the footer, get the pageCount, discard that pdf and create another one or simply edit the footer pages count?
– Leo Dabus
Nov 15 '18 at 12:20
@fewlinesofcode I've added a sample code to get the idea of what I'm trying to do.
– Chris
Nov 15 '18 at 13:02
@fewlinesofcode I've added a sample code to get the idea of what I'm trying to do.
– Chris
Nov 15 '18 at 13:02
@LeoDabus Thanks, that's a good idea - that will work for sure. Only thing I do not so much like about it is, that I more or less create the same document twice. I'm going to have to test it with larger amounts of data to see how it's going to work out :)
– Chris
Nov 15 '18 at 13:06
@LeoDabus Thanks, that's a good idea - that will work for sure. Only thing I do not so much like about it is, that I more or less create the same document twice. I'm going to have to test it with larger amounts of data to see how it's going to work out :)
– Chris
Nov 15 '18 at 13:06
1
1
@LeoDabus thanks for the hints - added the Swift version (version 4) and updated the code to use NSAttributedString.
– Chris
Nov 15 '18 at 14:05
@LeoDabus thanks for the hints - added the Swift version (version 4) and updated the code to use NSAttributedString.
– Chris
Nov 15 '18 at 14:05
|
show 2 more comments
1 Answer
1
active
oldest
votes
UIGraphicsPDFRenderer îs a forward only pdf generator.
You have 2 options:
- Analyze the data before writing it to pdf to determine on how many pages it fits. Since your code performs the page breaks you should be able to compute how many pages are required for your data.
- Create your document without ‘page x of y’ and save it. Create a new document, open the previously saved document and draw its pages on the pages of the new document and then add the ‘page x of y’.
2 is more cumbersome and inefficient so I would go with 1.
Thanks, Mihai! I did go with option 1 and it works pretty fast - even with large data. :)
– Chris
Nov 20 '18 at 16:52
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53318425%2fadd-a-footer-like-page-x-of-y-when-creating-a-pdf-using-uigraphicspdfrenderer%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
UIGraphicsPDFRenderer îs a forward only pdf generator.
You have 2 options:
- Analyze the data before writing it to pdf to determine on how many pages it fits. Since your code performs the page breaks you should be able to compute how many pages are required for your data.
- Create your document without ‘page x of y’ and save it. Create a new document, open the previously saved document and draw its pages on the pages of the new document and then add the ‘page x of y’.
2 is more cumbersome and inefficient so I would go with 1.
Thanks, Mihai! I did go with option 1 and it works pretty fast - even with large data. :)
– Chris
Nov 20 '18 at 16:52
add a comment |
UIGraphicsPDFRenderer îs a forward only pdf generator.
You have 2 options:
- Analyze the data before writing it to pdf to determine on how many pages it fits. Since your code performs the page breaks you should be able to compute how many pages are required for your data.
- Create your document without ‘page x of y’ and save it. Create a new document, open the previously saved document and draw its pages on the pages of the new document and then add the ‘page x of y’.
2 is more cumbersome and inefficient so I would go with 1.
Thanks, Mihai! I did go with option 1 and it works pretty fast - even with large data. :)
– Chris
Nov 20 '18 at 16:52
add a comment |
UIGraphicsPDFRenderer îs a forward only pdf generator.
You have 2 options:
- Analyze the data before writing it to pdf to determine on how many pages it fits. Since your code performs the page breaks you should be able to compute how many pages are required for your data.
- Create your document without ‘page x of y’ and save it. Create a new document, open the previously saved document and draw its pages on the pages of the new document and then add the ‘page x of y’.
2 is more cumbersome and inefficient so I would go with 1.
UIGraphicsPDFRenderer îs a forward only pdf generator.
You have 2 options:
- Analyze the data before writing it to pdf to determine on how many pages it fits. Since your code performs the page breaks you should be able to compute how many pages are required for your data.
- Create your document without ‘page x of y’ and save it. Create a new document, open the previously saved document and draw its pages on the pages of the new document and then add the ‘page x of y’.
2 is more cumbersome and inefficient so I would go with 1.
answered Nov 16 '18 at 16:52
Mihai IancuMihai Iancu
1,373289
1,373289
Thanks, Mihai! I did go with option 1 and it works pretty fast - even with large data. :)
– Chris
Nov 20 '18 at 16:52
add a comment |
Thanks, Mihai! I did go with option 1 and it works pretty fast - even with large data. :)
– Chris
Nov 20 '18 at 16:52
Thanks, Mihai! I did go with option 1 and it works pretty fast - even with large data. :)
– Chris
Nov 20 '18 at 16:52
Thanks, Mihai! I did go with option 1 and it works pretty fast - even with large data. :)
– Chris
Nov 20 '18 at 16:52
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53318425%2fadd-a-footer-like-page-x-of-y-when-creating-a-pdf-using-uigraphicspdfrenderer%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Please, provide a code you tried so far.
– fewlinesofcode
Nov 15 '18 at 11:39
Can't you create a pdf without the footer, get the pageCount, discard that pdf and create another one or simply edit the footer pages count?
– Leo Dabus
Nov 15 '18 at 12:20
@fewlinesofcode I've added a sample code to get the idea of what I'm trying to do.
– Chris
Nov 15 '18 at 13:02
@LeoDabus Thanks, that's a good idea - that will work for sure. Only thing I do not so much like about it is, that I more or less create the same document twice. I'm going to have to test it with larger amounts of data to see how it's going to work out :)
– Chris
Nov 15 '18 at 13:06
1
@LeoDabus thanks for the hints - added the Swift version (version 4) and updated the code to use NSAttributedString.
– Chris
Nov 15 '18 at 14:05