However: as you didn't add any content to the first page yet, the NewPage() method will be ignored and the page size will not be changed. Afterwards, you try changing the page size like this: doc.SetPageSize(new (0, 0, image.Width, image.Height, 0)) You are creating a document with page size A4: Document doc = new Document()Ī4 is the default size when you don't pass a parameter to the Document constructor. Larger inline images will be forbidden in PDF 2.0. Inline images can only be used for images with a size of 4 KB or less. I guess you'll have to preprocess your JPG in a similar way.ĭon't use inline images: using inline images means that the images are stored in the content stream of the PDF as opposed to being stored as an Image XObject (which is the optimal way of storing images in a PDF). The opaque image is added with the monochrome image as its mask to obtain transparency. ![]() It creates two images: one opaque image using /FlateDecode and one monochrome image using /FlateDecode. When iText encounters a transparent PNG, it processes the PNG. As you may know, PDF doesn't support PNGs and PNG supports transparency. You will have to apply the clipping path before adding the image to the PDF. iText doesn't even look at the JPG bytes: it just creates a PDF stream object with the filter DCTDecode. If you say that your JPGs have clipping paths (I've never heard of such a thing) and you don't see that feature in the PDF, you are being confronted with a limitation inherent to PDF, not to iText. ![]() put the alignment and coordinates hereĬb.ShowTextAligned(1, text, 520, 640, 0) Ĭb.IText copies the bytes of a JPG straight into the PDF. String text = "Some random blablablabla." PdfWriter writer = PdfWriter.GetInstance(document, fs) īaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED) Rectangle size = reader.GetPageSizeWithRotation(1) įileStream fs = new FileStream(newFile, FileMode.Create, FileAccess.Write) PdfReader reader = new PdfReader(oldFile) String newFile = "C:\Hola.pdf" // open the reader PdfWriter writer = PdfWriter.GetInstance(doc, ms) Ĭopier.AddPage(copier.GetImportedPage(rd, pageCounter)) PageStamp ps = pdfCopy.CreatePageStamp(page) īaseFont baseFont = BaseFont.CreateFont(string.Format("\\Fonts\\arialuni.ttf", Environment.GetEnvironmentVariable("windir"), BaseFont.CP1252, BaseFont.NOT_EMBEDDED) Ĭb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, printLangString, 100f, 40f, 0f) ĭocument doc = new Document(PageSize.A4) PdfImportedPage page = copier.GetImportedPage(reader, pageCounter) PdfCopy copier = new PdfCopy(doc, new FileStream("2.pdf", FileMode.Create)) įor (int pageCounter = 1 pageCounter < reader.NumberOfPages 1 pageCounter ) I've added an example how to add a footer too: PdfReader reader = new PdfReader("1.pdf") ĭocument document = new Document(reader.GetPageSize(1)) You will need to create a new pdf and add it If you need to do that (PdfCopy is the best for merging pdf). Under.ShowTextAligned(PdfContentByte.ALIGN_CENTER, userComments, xPosition, yPosition, angle) Under.SetFontAndSize(baseFont, fontSize) Under.SetColorFill(BaseColor.LIGHT_GRAY) ![]() ![]() PdfContentByte under = writer.DirectContentUnder īaseFont baseFont = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.EMBEDDED) String outputFileName = Path.GetTempFileName() ".pdf" įloat xPosition = 4.Width / 2 įloat yPosition = (4.Height - 140f) / 2 private string AddWaterMarkPageToFile(string fileName, string userComments) Try This code: It adds a new page with existing pdf and add watermark text to that last page. Int rotation = reader.GetPageRotation(pageNumber) Ĭb.AddTemplate(page, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation(pageNumber).Height) Ĭb.AddTemplate(page, 1f, 0, 0, 1f, 0, 0) įont titleFont = new Font(.HELVETICAĬhunk titleChunk = new Chunk("Comments", titleFont) įont textFont = new Font(.HELVETICAĬhunk textChunk = new Chunk(userComments, textFont) PdfImportedPage page = writer.GetImportedPage(reader, pageNumber) Insert to Destination on the first page PdfReader reader = new PdfReader(filename) įor (int pageNumber = 1 pageNumber < reader.NumberOfPages 1 pageNumber )ĭocument.SetPageSize(reader.GetPageSizeWithRotation(1)) PdfContentByte cb = writer.DirectContent PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(outputFileName, FileMode.Create)) Step 2: we create a writer that listens to the document String outputFileName = Path.GetTempFileName() Private static string AddCommentsToFile(string fileName, string userComments)
0 Comments
Leave a Reply. |