Adding text to a page of existing PDF file

How do I add text to a page of an existing pdf when I don’t know what fonts exist in the document? Can I just use /F1, or would I need to add a font? If adding, how do I know what to name when the number of existing fonts (n) is unknown, /F(n+1)?

Thanks!

Hi Brian,

Are you perhaps referring to Content Stream example?

If yes, note that this example uses a simple Type1 Font (Helvetica) which is one of the Standard 14 Fonts that don’t need to be embedded in a PDF file. But for other fonts, their usage will be more complex.

Nevertheless, I’m afraid that current GemBox.Pdf lacks an easy-to-use API for creating new or editing existing PDF text content.
However, note that we’re actually working on it at this moment, these new planned improvements and API will simplify such tasks.

In the meantime, I would recommend you to try using both GemBox.Pdf and GemBox.Document to accomplish this. For example, please check the AddContent.zip project.

In short, this project uses GemBox.Document to create a new PDF file with the desired content and then uses GemBox.Pdf to append (or prepend) that content to the existing PDF. Using this approach you can add any content that you want (paragraphs, images, tables, hyperlinks, headers, footers, etc.).

As an FYI, for this requirement, you can feel free to use the GemBox.Document Free.
The Free mode enables you to create an unlimited number of documents that have up to 20 paragraphs and thus you can create an unlimited number of new PDF files and append their content to your existing PDF file.

Regards,
Mario

That sounds great. I am looking forward to the new features. As far as using GemBox.Document, we really can’t do that for this application since we receive the PDFs from third parties.

Hi,

Note, you won’t use GemBox.Document to open or read that existing PDF file from the third party.
Instead, you would use GemBox.Document as a sort of an intermediate for creating new text content (or any other content) for that existing PDF file.

In other words, you would use GemBox.Document to create a PDF that would have just the text that you want to insert and then you would use GemBox.Pdf to append the content of that new PDF into some page of the existing PDF.

I hope this explains better this approach, if not please let me know and I’ll try again.

Regards,
Mario

Hi @mario.gembox,

Could u please give an update on this easy-to-use API ? Is there any ETA ?

Thank you very much!

Regards,
Alex

Hi Alexander,

The implementation is done, we’re currently in process of updating examples and documentation.
Anyway, it should be released next week.

Regards,
Mario

The latest version of GemBox.Pdf includes the PdfPage.ConvertToForm method which simplifies this task, we can now easily append the content from one PDF page to another.

Here is a small example of how you can use it:

using (var destination = PdfDocument.Load("input.pdf"))
using (var source = PdfDocument.Load("source.pdf"))
{
    var form = source.Pages[0].ConvertToForm(destination);
    var group = destination.Pages[0].Content.Elements.AddGroup();
    group.Elements.AddForm(form);
    destination.Save("output.pdf");
}

This is working in that I can create a source pdf with a header in the top right and apply it to the destination pdf. If the destination has same dimensions it works great!

If the destination pdf has different dimensions it seems to place the header whatever distance the header is from the bottom of the page. So, if the source is say 8 inches tall and the destination is 10 inches tall, the header show up 8 inches from the bottom. If the destination is 7 inches tall, it doesn’t show up (because it is off what I will call the “stage”).

Is there a way that I can choose how the source is applied? Ideally, for this project it would be from the top right so that no matter the height or width of the destination it is always the same location including a (hopefully configurable) border of say 10.

Also noting that the pdf page dimensions in these documents can vary from page to page.

Thanks!

Hi Paul,

You can use PdfContentGroup.Transform to position the imported content.
For example, try this:

var sourceSize = source.Pages[0].Size;
var destinationSize = destination.Pages[0].Size;
double yOffset = destinationSize.Height - sourceSize.Height;
group.Transform = new PdfMatrix(1, 0, 0, 1, 0, yOffset);

Also just as an FYI, you can resize that imported content, as shown in this post:

Regards,
Mario

Thanks so much, this worked perfectly!