public SetPageSize ( |
||
pageSize | the new pagesize | |
return | bool |
using (var fileStream = new FileStream("output.pdf", FileMode.Create)) { var document = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4); var writer = PdfWriter.GetInstance(document, fileStream); // Add content to the document document.Close(); }
using (var fileStream = new FileStream("input.pdf", FileMode.Open)) { var reader = new PdfReader(fileStream); var document = new iTextSharp.text.Document(); using (var outputStream = new FileStream("output.pdf", FileMode.Create)) { var writer = PdfWriter.GetInstance(document, outputStream); document.Open(); // Loop through the pages of the input document for (int pageNumber = 1; pageNumber <= reader.NumberOfPages; pageNumber++) { // Get the page size of the current page var pageSize = reader.GetPageSize(pageNumber); // Change the page size to letter (if it's not already) if (pageSize.Width != iTextSharp.text.PageSize.LETTER.Width || pageSize.Height != iTextSharp.text.PageSize.LETTER.Height) { document.SetPageSize(iTextSharp.text.PageSize.LETTER); } // Add the page from the input document to the output document var importedPage = writer.GetImportedPage(reader, pageNumber); writer.DirectContent.AddTemplate(importedPage, 0, 0); document.NewPage(); } document.Close(); } }In this example, we open an existing PDF document and loop through its pages. For each page, we get its page size and check if it's already set to letter size. If not, we change the page size to letter using the SetPageSize() method. We then add the page to a new PDF document with the updated page size and save it to a file. Overall, iTextSharp is a powerful tool for working with PDF documents in C# applications, and its Document class provides many useful methods and properties for manipulating PDF pages and content.
public SetPageSize ( |
||
pageSize | the new pagesize | |
return | bool |