C# using iText7 to generate a PDF positioning elements

LabProARW

Well-known member
Joined
Nov 4, 2019
Messages
52
Programming Experience
1-3
I have looked all over the internet and found examples with iText and Java, but I am not using Java (and don't speak it). Is there a clear site with sample code snippets to position elements? What I need to do is have a logo at the top in the header section of a PDF, underneath that a line of bold text for product, then under that a line of text for "Certificate of Analysis". More text sections and a table showing lines, and a text footer. Positioning these things are not intuitive at all - or my brain is too small, so I need help. All of this will be on A4 size paper.
Any guidance on positioning? I only have found a set fixed position of 100,250 which centers one object on the page. My failed attempts have text overwriting my top header logo.

Thanks in advance.
 
What you are asking us, or how it comes across, is you want us to write you an application in whole? The only way we can do what you have asked is by writing an application example in whole. In which case, I can send you an invoice for services rendered if you'd like?

Otherwise, I suggest that you be a bit more diligent in your own research, and look for examples on working with PDF's in C#. Within 20 seconds, I was able to retrieve this from a search engine : Generating a PDF Document Using C#, .NET, and iText 7

Might I suggest you research by yourself before coming to a public forum and asking people to write application examples for you, as you are more than capable of pulling up this information yourself from any online web search.
 
What you are asking us, or how it comes across, is you want us to write you an application in whole? The only way we can do what you have asked is by writing an application example in whole. In which case, I can send you an invoice for services rendered if you'd like?

Otherwise, I suggest that you be a bit more diligent in your own research, and look for examples on working with PDF's in C#. Within 20 seconds, I was able to retrieve this from a search engine : Generating a PDF Document Using C#, .NET, and iText 7

Might I suggest you research by yourself before coming to a public forum and asking people to write application examples for you, as you are more than capable of pulling up this information yourself from any online web search.
In my post I was attempting to communicate the scope of the information that I was going to need. I have looked at your 20 second search example yesterday as well as 10 others. This nice Code Guru sample was not helping me place the graphic in the header section of a document centered. My graphic does not occupy the entire width of the page. The Code Guru sample using SetTexAlignment CENTER for their graphic does not work to center my smaller graphic on my page... nor do the other examples which I have found of various relevance. The SetFixedPosition has been a lot of trial and error time suck and seems backwards to me regarding the vertical positioning. Thus my one and only question of my post was "guidance on positioning"... not code or an application.

My apologies for giving context to what I wanted to ultimately achieve in my project.
 
It sounds to me like you first need to understand how layout works in PDF files. If you understand how the coordinate system (and page resolution) works within PDF files, then setting by absolute position will not become a trial and error time suck -- you can compute directly where you are putting an element.

As a freebie: try reading section 8.3 of the PDF spec.
 
Last edited:
It sounds to me like you first need to understand how layout works in PDF files. If you understand how the coordinate system (and page resolution) works within PDF files, then setting by absolute position will not become a trial and error time suck -- you can compute directly where you are putting an element.

As a freebie: try reading section 8.3 of the PDF spec.
Thank you for your advice! I will spend some time with the spec.
 
You can use SetVerticalAlignment on the container block element. For example you add a Div or Paragraph with height and vertical align its contents to MIDDLE, then add the image to that element.
 
You don't need to learn or use Java, you just need to use a C# PDF generation library, you can use iText with C# too but I prefer PDFFlow, you can easily set padding, add header and footer, and create tables and fill it with data dynamically. you can find all your requirements on this example page

and btw you can contact them if you need their team to write the code for you!

for example this is how to apply a style to a paragraph :


C#:
         //Create a style:
            var styleHeader = StyleBuilder.New()
                .SetFontName(FontNames.Helvetica)
                .SetFontSize(16)
                .SetFontBold()
                .SetHorizontalAlignment(HorizontalAlignment.Center);
         //Create a document:
            DocumentBuilder.New()
         //Add content and apply the style to the paragraph:
            .AddSection().AddParagraph("Header").ApplyStyle(styleHeader)
         //Build a file:
            .ToDocument().Build("Result.pdf");

good luck ;)
 
Last edited:
My reading of your licensing page is that for the free version or for a single developer version of $150, I would have to rely on community support. To have your developers write the code, it would cost something above $150 and less than $500.

 
Back
Top Bottom