Set page margins with iTextSharp

Set page margins with iTextSharp

To set page margins using iTextSharp, you can manipulate the Document and PdfWriter objects. Here's an example that demonstrates how to set page margins:

using iTextSharp.text; using iTextSharp.text.pdf; class Program { static void Main() { // Create a new document Document document = new Document(); // Set the page margins document.SetMargins(36, 36, 72, 72); // Left, Right, Top, Bottom // Create a PdfWriter instance to write the PDF document PdfWriter writer = PdfWriter.GetInstance(document, new FileStream("output.pdf", FileMode.Create)); // Open the document for writing document.Open(); // Add content to the document document.Add(new Paragraph("Hello, world!")); // Close the document and writer document.Close(); writer.Close(); } } 

In this example, we create a new Document instance and then use the SetMargins method to set the page margins. The parameters of SetMargins represent the left, right, top, and bottom margins respectively.

After setting the margins, we create a PdfWriter instance to write the PDF document. The GetInstance method takes the Document object and a FileStream to specify the output file.

Next, we open the document using document.Open() and add content to it. In this example, we add a simple paragraph with the text "Hello, world!" using document.Add().

Finally, we close the document and writer instances to complete the PDF creation process.

Adjust the margin values (36, 36, 72, 72 in the example) according to your desired measurements, such as inches or points.

Make sure you have the iTextSharp library properly referenced in your project.

Examples

  1. "Set page margins with iTextSharp in C#"

    • Description: Learn how to set page margins for a PDF document using iTextSharp in C#.
    // Example of setting page margins with iTextSharp in C# Document document = new Document(); PdfWriter writer = PdfWriter.GetInstance(document, new FileStream("output.pdf", FileMode.Create)); document.SetMargins(36, 72, 108, 180); // Left, Right, Top, Bottom margins in points document.Open(); // Add content to the document document.Close(); 
  2. "iTextSharp set different margins for odd and even pages"

    • Description: Explore how to set different margins for odd and even pages in a PDF document using iTextSharp in C#.
    // Example of setting different margins for odd and even pages with iTextSharp in C# Document document = new Document(); PdfWriter writer = PdfWriter.GetInstance(document, new FileStream("output.pdf", FileMode.Create)); document.SetMarginMirroring(true); // Enable mirroring for odd and even pages document.SetMargins(36, 72, 108, 180); // Left, Right, Top, Bottom margins in points for odd pages document.Open(); // Add content to the document document.Close(); 
  3. "C# iTextSharp set page margins for specific section"

    • Description: Understand how to set page margins for a specific section of a PDF document using iTextSharp in C#.
    // Example of setting page margins for a specific section with iTextSharp in C# Document document = new Document(); PdfWriter writer = PdfWriter.GetInstance(document, new FileStream("output.pdf", FileMode.Create)); document.SetMargins(36, 72, 108, 180); // Default margins // Add content to the first section document.SetMargins(72, 36, 180, 108); // New margins for the second section // Add content to the second section document.Close(); 
  4. "iTextSharp set margin for specific page in C#"

    • Description: Learn how to set different margins for a specific page in a PDF document using iTextSharp in C#.
    // Example of setting margins for a specific page with iTextSharp in C# Document document = new Document(); PdfWriter writer = PdfWriter.GetInstance(document, new FileStream("output.pdf", FileMode.Create)); document.SetMargins(36, 72, 108, 180); // Default margins // Add content to the first page document.NewPage(); document.SetMargins(72, 36, 180, 108); // New margins for the second page // Add content to the second page document.Close(); 
  5. "C# iTextSharp set margin for header and footer"

    • Description: Explore how to set margins specifically for the header and footer sections in a PDF document using iTextSharp in C#.
    // Example of setting margins for header and footer with iTextSharp in C# Document document = new Document(); PdfWriter writer = PdfWriter.GetInstance(document, new FileStream("output.pdf", FileMode.Create)); document.SetMargins(36, 72, 108, 180); // Default margins // Set margins for the header writer.PageEvent = new CustomHeaderFooter() { MarginTop = 36 }; // Set margins for the footer writer.PageEvent = new CustomHeaderFooter() { MarginBottom = 36 }; document.Open(); // Add content to the document document.Close(); 
    // CustomHeaderFooter class for setting margins public class CustomHeaderFooter : PdfPageEventHelper { public float MarginTop { get; set; } = 36; public float MarginBottom { get; set; } = 36; public override void OnStartPage(PdfWriter writer, Document document) { document.SetMargins(36, 72, MarginTop, 180); // Set margins for the header base.OnStartPage(writer, document); } public override void OnEndPage(PdfWriter writer, Document document) { document.SetMargins(36, 72, 108, MarginBottom); // Set margins for the footer base.OnEndPage(writer, document); } } 
  6. "iTextSharp set page size and margins in C#"

    • Description: Learn how to set both the page size and margins for a PDF document using iTextSharp in C#.
    // Example of setting page size and margins with iTextSharp in C# Document document = new Document(PageSize.A4, 36, 72, 108, 180); // Page size and margins in points PdfWriter writer = PdfWriter.GetInstance(document, new FileStream("output.pdf", FileMode.Create)); document.Open(); // Add content to the document document.Close(); 
  7. "C# iTextSharp set margins for landscape orientation"

    • Description: Understand how to set specific margins for a PDF document in landscape orientation using iTextSharp in C#.
    // Example of setting margins for landscape orientation with iTextSharp in C# Document document = new Document(PageSize.A4.Rotate(), 36, 72, 108, 180); // Landscape orientation with margins PdfWriter writer = PdfWriter.GetInstance(document, new FileStream("output.pdf", FileMode.Create)); document.Open(); // Add content to the document document.Close(); 
  8. "C# iTextSharp set margin for specific paragraph"

    • Description: Explore how to set specific margins for a paragraph within a PDF document using iTextSharp in C#.
    // Example of setting margins for a specific paragraph with iTextSharp in C# Document document = new Document(); PdfWriter writer = PdfWriter.GetInstance(document, new FileStream("output.pdf", FileMode.Create)); document.Open(); Paragraph paragraph = new Paragraph("This is a paragraph with custom margins"); paragraph.SetMargins(50, 50, 20, 20); // Left, Right, Top, Bottom margins for the paragraph document.Add(paragraph); document.Close(); 
  9. "C# iTextSharp set margin for specific element"

    • Description: Learn how to set specific margins for a specific element, such as an image or table, within a PDF document using iTextSharp in C#.
    // Example of setting margins for a specific element with iTextSharp in C# Document document = new Document(); PdfWriter writer = PdfWriter.GetInstance(document, new FileStream("output.pdf", FileMode.Create)); document.Open(); Image image = Image.GetInstance("image.jpg"); image.SetAbsolutePosition(50, 50); // Set position for the image image.ScaleToFit(100, 100); // Set size for the image document.Add(image); document.Close(); 
  10. "C# iTextSharp set margin for specific table cell"

    • Description: Understand how to set specific margins for a cell within a table in a PDF document using iTextSharp in C#.
    // Example of setting margins for a specific table cell with iTextSharp in C# Document document = new Document(); PdfWriter writer = PdfWriter.GetInstance(document, new FileStream("output.pdf", FileMode.Create)); document.Open(); PdfPTable table = new PdfPTable(2); PdfPCell cell = new PdfPCell(new Phrase("Cell content")); // Set margins for the cell cell.PaddingLeft = 10; cell.PaddingRight = 10; cell.PaddingTop = 5; cell.PaddingBottom = 5; table.AddCell(cell); document.Add(table); document.Close(); 

More Tags

iokit iterable google-kubernetes-engine spelevaluationexception relative-path networkx can-bus uipath batch-insert grunt-contrib-watch

More C# Questions

More Everyday Utility Calculators

More Mixtures and solutions Calculators

More Date and Time Calculators

More Geometry Calculators