I have a usecase where i need to add header to PDF files. Previously the project build in windows legacy, and i used iTextSharp and able to add Header. But now the project version is windows, and iTextSharp isn’t has any compatible version, due to which i am facing issue. I tried pdfSharp and iText7 but unable to add header to pdf file. Any suggestion for the solution approach? Thanks in Advance
Let us know what was tried with PDFsharp in detail so far
pdfSharp:
- addSection() to a document like: Dim mySect As Section = myDoc.AddSection()
- then work with the header e.g. mySect.Headers.Primary.AddParagraph()
Already installed but facing difficulty while it create/update the pdf. unable to acheive the task
i have used the below code:
try
{
void TestPdfWriterInitialization(string outputFilePath)
{
try
{
Console.WriteLine(“Starting TestPdfWriterInitialization method…”);
if (File.Exists(outputFilePath))
{
Console.WriteLine("Deleting existing output file...");
File.Delete(outputFilePath); // Ensure the output file does not already exist
}
Console.WriteLine($"Creating output file: {outputFilePath}");
using (FileStream fsOutput = new FileStream(outputFilePath, FileMode.Create, FileAccess.Write))
{
PdfWriter pdfWriter = new PdfWriter(fsOutput);
Console.WriteLine("Creating PdfDocument...");
PdfDocument pdfDoc = new PdfDocument(pdfWriter);
Document document = new Document(pdfDoc);
Console.WriteLine("Adding a simple paragraph...");
document.Add(new Paragraph("Hello, World!"));
Console.WriteLine("Closing PdfDocument...");
document.Close();
}
}
catch (Exception ex)
{
Console.WriteLine("An error occurred: " + ex.Message);
Console.WriteLine(ex.StackTrace);
throw;
}
}
// Call the method with the output file path
string FilePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "TestOutput.pdf");
TestPdfWriterInitialization(FilePath);
}
catch (Exception ex)
{
// Handle the exception (for example, log it or rethrow it)
Console.WriteLine("An error occurred in the outer try-catch: " + ex.Message);
Console.WriteLine(ex.StackTrace);
throw;
}
is it posisble for you to share a sample workflow (windows comaptability) for just adding a header to a pdf?
feel free to research more by your own e.g. on pdfsharp resources like:
http://forum.pdfsharp.de/viewtopic.php?f=2&t=3238