Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include example on adding to existing page #18

Open
JasonBarnabe opened this issue Jun 2, 2017 · 1 comment
Open

Include example on adding to existing page #18

JasonBarnabe opened this issue Jun 2, 2017 · 1 comment

Comments

@JasonBarnabe
Copy link

All the examples seem to be about adding a new page to a PDF and adding content to it. It would be helpful to have an example of adding content to an existing page. It took me numerous tries before finding something that would work:

      pdf      = Origami::PDF.read(path)
      contents = Origami::ContentStream.new
      contents.write('some text', {
          x: 200,
          y: 200,
      })
      pdf.get_page(1).setContents([pdf.get_page(1).Contents, contents])
      pdf.save(path)
@sudhanshug16
Copy link

sudhanshug16 commented Feb 18, 2021

Hey @JasonBarnabe, I know it has been a long time since you wrote this but it will be great if you can help.
I am trying to do the same and in the output the fields are showing up mirrored for some reason 🤔
Do you know the fix for this?
An example for the same:
image
cc @gdelugre

Code sample:

pdf = Origami::PDF.read file.path

page_count = pdf.pages.size

page_count.times do |i|
  page = pdf.get_page i + 1
  page.add_font Origami::Font::Type1::Standard::Helvetica.new.pre_build, :Helvetica
  contents = Origami::ContentStream.new
  contents.write("#nice text{i}", {
    x: x,
    y: y,
    font: :Helvetica
  })
  page.setContents([page.Contents, contents])
end

buffer = StringIO.new
pdf.write buffer
buffer.rewind

send_data buffer.string

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants