PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Fri Nov 01, 2024 10:52 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: Thu Oct 10, 2024 10:24 am 
Offline

Joined: Thu Oct 03, 2024 1:22 pm
Posts: 2
Hi,

I added text to every page of a PDF by writing a short script.
However, I would like to add some additional space at the top for text so that it doesn't overlap with the PDF's original content.
Also, if the text is longer then, it will not continue onto the following line at that point, instead cutting off the text in between.

Currently I am using PDFsharp's version 6.1.1.0.

Any ideas to fix this? I'm new to PDFsharp, so any advice is welcome.

I am sharing the code I am using for this

using (MemoryStream inputStream = new MemoryStream(pdfContent))
{
using (PdfDocument document = PdfReader.Open(inputStream, PdfDocumentOpenMode.Modify))
{
foreach (PdfPage page in document.Pages)
{
if (page.Width <= XUnit.Zero || page.Height <= XUnit.Zero)
{
page.Size = PdfSharp.PageSize.A4;
}

// Create a graphics object for the current page
XGraphics gfx = XGraphics.FromPdfPage(page);

XFont font = new XFont("Arial", 16, XFontStyleEx.Regular);

// Calculate the position for the annotation
double x = 28.35;
double y = 28.35;

// Draw the annotation text on the PDF
gfx.DrawString(annotationText, font, XBrushes.Black, new XPoint(x, y));
}

using (MemoryStream outputStream = new MemoryStream())
{
document.Save(outputStream);
return outputStream.ToArray();
}
}
}


Attachments:
File comment: as shown in this image text is overlapping with the PDF's original content
1.PNG
1.PNG [ 24.77 KiB | Viewed 7678 times ]
File comment: As shown above content is not going on 2nd line its cutting in between.
text added here to test it -"I would like to add some additional space at the top for text so that it doesn't overlap with the PDF's original content"

2.PNG
2.PNG [ 25.37 KiB | Viewed 7678 times ]
Top
 Profile  
Reply with quote  
PostPosted: Thu Oct 10, 2024 10:54 am 
Offline
PDFsharp Guru
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 1000
Location: CCAA
Extra room:
Create a new PdfPage and draw the existing PdfPage where you want to have it with white space where you need it.
See also:
https://www.pdfsharp.net/wiki/XForms-sample.ashx

Make sure that text fits on a page and add line breaks as needed.
Maybe try XTextFormatter class:
https://www.pdfsharp.net/wiki/TextLayout-sample.ashx

Or maybe use XTextFormatterEx2 class:
viewtopic.php?f=8&t=3192

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
PostPosted: Tue Oct 15, 2024 2:16 pm 
Offline

Joined: Thu Oct 03, 2024 1:22 pm
Posts: 2
Hi,

I tried the solution you provided, but still my PDF content is overlapping and the original PDF content is not visible where the textbox is added. Is there any way to make the original PDF content smaller so that original PDF content will be visible?

as I am using byte[] pdfContent, string annotationText

using (MemoryStream inputStream = new MemoryStream(pdfContent))
{
PdfDocument inputDocument = PdfReader.Open(inputStream, PdfDocumentOpenMode.Import);
PdfDocument outputDocument = new PdfDocument();

// Set up the font and initial parameters
XFont font = new XFont("Arial", 12, XFontStyleEx.Regular);
double lineHeight = 15; // Approximate height of one line of text

// Loop through each page of the input document
for (int pageIndex = 0; pageIndex < inputDocument.PageCount; pageIndex++)
{
PdfPage inputPage = inputDocument.Pages[pageIndex];

PdfPage outputPage = outputDocument.AddPage(inputPage);

var mediaBox = inputPage.MediaBox;
var pageRotation = inputPage.Rotate;

double pageWidth = mediaBox.Width;
double pageHeight = mediaBox.Height;

if (pageRotation == 90 || pageRotation == 270)
{
pageWidth = mediaBox.Height;
pageHeight = mediaBox.Width;
}

double maxTextWidth = pageWidth - 10; // Keep padding of 5 points on each side

List<string> lines = new List<string>();
using (XGraphics gfxs = XGraphics.FromPdfPage(outputPage))
{
string[] words = annotationText.Split(' ');
StringBuilder currentLine = new StringBuilder();

foreach (string word in words)
{
string testLine = currentLine.Length == 0 ? word : currentLine + " " + word;
double width = gfxs.MeasureString(testLine, font).Width;

if (width < maxTextWidth)
{
currentLine.Append(currentLine.Length == 0 ? word : " " + word);
}
else
{
lines.Add(currentLine.ToString());
currentLine.Clear();
currentLine.Append(word);
}
}

if (currentLine.Length > 0)
{
lines.Add(currentLine.ToString()); // Add the remaining text as a line
}
}
double totalTextHeight = lines.Count * lineHeight + 10; // Add 10 for padding at top and bottom

outputPage.Height += XUnit.FromPoint(totalTextHeight);

XGraphics gfx = XGraphics.FromPdfPage(outputPage);

gfx.TranslateTransform(0, totalTextHeight);

XColor back = XColors.White; // Background color for the top space
XSolidBrush brush = new XSolidBrush(back);

gfx.DrawRectangle(brush, new XRect(0, 0, pageWidth, totalTextHeight));

double textXPosition = 5;
double textYPosition = 10;

foreach (string line in lines)
{
gfx.DrawString(line, font, XBrushes.Black, new XPoint(textXPosition, textYPosition));
textYPosition += lineHeight; // Move down for the next line
}
}

using (MemoryStream stream = new MemoryStream())
{
outputDocument.Save(stream);
return stream.ToArray();
}
}

I tried with ScaleFactor as well but it doesn't works for me.


Top
 Profile  
Reply with quote  
PostPosted: Tue Oct 15, 2024 3:48 pm 
Offline
PDFsharp Guru
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 1000
Location: CCAA
You still do not use DrawImage to draw the existing page at reduced size on the new page.

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 11 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group