append image to pages of pdf using itextsharp
DESCRIPTION
Append Image to Pages of PDF Using ITextSharpTRANSCRIPT
c# - append image to pages of PDF using iTextSharp - Stack Overflow
http://stackoverflow.com/questions/15948097/append-image-to-pages-of-pdf-using-itextsharp[10/04/2015 21:09:56]
Take the 2-minute tour ×
0
1
I am attempting to append images to an existing PDF using the following.
public static byte[] Append(byte[] inputPdf, params Image[] images){ var ms = new MemoryStream(); ms.Write(inputPdf, 0, inputPdf.Length); ms.Seek(0, SeekOrigin.Begin);
using (Document pdf = new Document(iTextSharp.text.PageSize.A4, 10, 10, 10, 10)) using (PdfWriter writer = PdfWriter.GetInstance(pdf, ms)) { pdf.Open(); foreach (var image in images) {
var result = pdf.NewPage();
ImageFormat format = image.PixelFormat == PixelFormat.Format1bppIndexed || image.PixelFormat == PixelFormat.Format4bppIndexed || image.PixelFormat == PixelFormat.Format8bppIndexed ? ImageFormat.Tiff
2015 Community Moderator Electionends Apr 21
asked 1 year ago
viewed 3556 times
active 9 months ago
Upcoming Events
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.
append image to pages of PDF using iTextSharp
sign up log in tour help stack overflow careers
Questions Tags Users Badges Unanswered Ask Question
c# - append image to pages of PDF using iTextSharp - Stack Overflow
http://stackoverflow.com/questions/15948097/append-image-to-pages-of-pdf-using-itextsharp[10/04/2015 21:09:56]
Jim1,161 14 40
: ImageFormat.Jpeg; var pdfImage = iTextSharp.text.Image.GetInstance(image, format); pdfImage.Alignment = Element.ALIGN_CENTER; pdfImage.ScaleToFit(pdf.PageSize.Width, pdf.PageSize.Height); pdf.Add(pdfImage); } pdf.Close(); } ms.Flush(); return ms.GetBuffer();}
The result value is not used, I was debugging it. The value is always true, so the add page is working.
The resulting PDF is the same size as the original, but is not readable. I get invalid root object errors when opening it.
Any suggestions?
Thanks
c# image pdf itextsharp
share improve this question asked Apr 11 '13 at 11:53
When working with an existing PDF you need to use a PdfStamper, see How can I insert an image with iTextSharp in an existing PDF? – Chris Haas Apr 11 '13 at 13:02
Linked
23 How can I insert an image with iTextSharp in an existing PDF?
Related
23 How can I insert an image with iTextSharp in an existing PDF?
0 convert pdf page to image iTextSharp c#
7 Extract image from PDF using itextsharp
4 iTextSharp - PDF - Resize Document to Accomodate a Large Image
2 Add an existing PDF from file to an unwritten document using iTextSharp
0 Get pdf embedded image width using iTextsharp
1 How do you set a
c# - append image to pages of PDF using iTextSharp - Stack Overflow
http://stackoverflow.com/questions/15948097/append-image-to-pages-of-pdf-using-itextsharp[10/04/2015 21:09:56]
2 Answers
active oldest votes
1
Method 1 (without PdfStamper)
using (var ms = new MemoryStream()){ var pdf = new PdfReader(inputPdf); var doc = new Document(pdf.GetPageSizeWithRotation(1)); using (var writer = PdfWriter.GetInstance(doc, ms)) { doc.Open();
for (int page = 0; page < pdf.NumberOfPages; page++) { doc.SetPageSize(pdf.GetPageSizeWithRotation(page + 1)); doc.NewPage(); var pg = writer.GetImportedPage(pdf, page + 1); int rotation = pdf.GetPageRotation(page + 1); if (rotation == 90 || rotation == 270) writer.DirectContent.AddTemplate( pg, 0, -1f, 1f, 0, 0, pdf.GetPageSizeWithRotation(page).Height); else writer.DirectContent.AddTemplate(pg, 1f, 0, 0, 1f, 0, 0); } foreach (var image in images) { doc.NewPage();
ImageFormat format = image.PixelFormat == PixelFormat.Format1bppIndexed || image.PixelFormat == PixelFormat.Format4bppIndexed || image.PixelFormat == PixelFormat.Format8bppIndexed ? ImageFormat.Tiff : ImageFormat.Jpeg; var pdfImage = iTextSharp.text.Image.GetInstance(image, format); pdfImage.Alignment = Element.ALIGN_CENTER; pdfImage.ScaleToFit(doc.PageSize.Width - 10, doc.PageSize.Height - 10);
background image on every page of a PDF in iTextSharp?
0 iTextSharp is giving me the error: “PDF header signature not found”
0 How to draw a PDF page into a System.Drawing.Image using iTextSharp?
0 Export big PNG to PDF with ItextSharp
Hot Network Questions
Cryptic Crossword Clue: Mind reading Pop Star is fashionable (7)
What methods can I use to create balance and consistency between a group of differing logos?
Help me learn to speak Susan's language
What is the difference between "curing" and "drying"?
What is a good (and affordable) sleeping bag for someone who is beginning to backpack?
How to easily cherry pick with magit?
Are there any scales other than temperature that have different zero points?
What are NoScript surrogates and should one disable them for more privacy in browsing?
Candidate quitting and now asking to rejoin
German word for “awesome”
add a comment
c# - append image to pages of PDF using iTextSharp - Stack Overflow
http://stackoverflow.com/questions/15948097/append-image-to-pages-of-pdf-using-itextsharp[10/04/2015 21:09:56]
Jim1,161 14 40
doc.Add(pdfImage); } doc.Close(); writer.Close();
Method 2 (using PdfStamper)
var pdfReader = new PdfReader(inputPdf);using (var ms = new MemoryStream()){ using (var stamp = new PdfStamper(pdfReader, ms)) { foreach (var image in images) { var size = pdfReader.GetPageSize(1); var page = pdfReader.NumberOfPages + 1; stamp.InsertPage(page, pdfReader.GetPageSize(1)); ImageFormat format = image.PixelFormat == PixelFormat.Format1bppIndexed || image.PixelFormat == PixelFormat.Format4bppIndexed || image.PixelFormat == PixelFormat.Format8bppIndexed ? ImageFormat.Tiff : ImageFormat.Jpeg; var pdfImage = iTextSharp.text.Image.GetInstance(image, format); pdfImage.Alignment = Element.ALIGN_CENTER; pdfImage.SetAbsolutePosition(0, size.Height - pdfImage.Height); pdfImage.ScaleToFit(size.Width, size.Height); stamp.GetOverContent(page).AddImage(pdfImage); } } ms.Flush(); return ms.GetBuffer();}
share improve this answer answered Apr 12 '13 at 5:53
sounding like “ga-yeah”?
How does the high gravity on Miller's planet create big waves?
What is this book about psychic juvenile delinquents?
QR code reader on ubuntu
Alternative to "daydream" without the pleasant connotation
Why did Obi Wan use his lightsaber in the Mos Eisley Cantina?
Could the LHC be used for fusion experiments?
Generate a pair of integers from a non-negative one
What is the picture on the front of this edition of "The Two Towers"?
Bootstrap styles not being applied to Visualforce page as static resource
Interpreting Fish (no, not that Fish)
What does the term "undefined" actually mean?
Did Darth Vader's TIE fighter appear in any version of Empire Strikes Back?
Keeping noodles from eating all the soup
Do effects that reduce a spell's casting cost also reduce its converted mana cost?
add a comment
c# - append image to pages of PDF using iTextSharp - Stack Overflow
http://stackoverflow.com/questions/15948097/append-image-to-pages-of-pdf-using-itextsharp[10/04/2015 21:09:56]
Bruno Lowagie26.1k 3 22 46
0
You are making the wrong assumption that you can glue the bytes of two PDF documents together.
You have one PDF that looks like this:
%PDF-1.6%âãÏÓ1 0 obj <<... PDF syntax%%EOF
With another one that looks like this:
%PDF-1.6%âãÏÓ1 0 obj <<... PDF syntax%%EOF
Resulting in a file that looks like this:
%PDF-1.6%âãÏÓ1 0 obj <<... PDF syntax%%EOF%PDF-1.6%âãÏÓ1 0 obj <<... PDF syntax%%EOF
You really shouldn't expect this to work! Please start by reading chapter 6 of my book and read about called PdfStamper . Then go to this question: How can I insert an image with iTextSharp in an existing PDF?
share improve this answer answered Apr 11 '13 at 15:18
c# - append image to pages of PDF using iTextSharp - Stack Overflow
http://stackoverflow.com/questions/15948097/append-image-to-pages-of-pdf-using-itextsharp[10/04/2015 21:09:56]
I am trying to add new pages to the PDF, not stamp existing pages on the pdf. – Jim Apr 12 '13 at 5:10
I know, but you still need PdfStamper and its insertPage() method (read section 6.3.4 of the free chapter). You certainly don't want to concatenate the bytes of one file to the bytes of another file. – Bruno Lowagie Apr 12 '13 at 7:07
Your Answer
add a comment
Post as a guest
Name
Sign up or log in
Sign up using Google
Sign up using Facebook
Sign up using Stack Exchange
c# - append image to pages of PDF using iTextSharp - Stack Overflow
http://stackoverflow.com/questions/15948097/append-image-to-pages-of-pdf-using-itextsharp[10/04/2015 21:09:56]
By posting your answer, you agree to the privacy policy and terms of service.
Not the answer you're looking for? Browse other questions tagged c# image pdf itextsharp or ask your own question.
question feed
tour help blog chat data legal privacy policy work here advertising info mobile contact us feedback
TECHNOLOGY LIFE / ARTS CULTURE / RECREATION SCIENCE OTHER
Stack Overflow
Server Fault
Super User
Web Applications
Ask Ubuntu
Webmasters
Game Development
TeX - LaTeX
Programmers
Unix & Linux
Ask Different (Apple)
WordPress Development
Geographic Information Systems
Electrical Engineering
Android Enthusiasts
Information Security
Database Administrators
Drupal Answers
SharePoint
User Experience
Mathematica
Salesforce
more (14)
Photography
Science Fiction & Fantasy
Graphic Design
Seasoned Advice (cooking)
Home Improvement
Personal Finance & Money
Academia
more (10)
English Language & Usage
Skeptics
Mi Yodeya (Judaism)
Travel
Christianity
Arqade (gaming)
Bicycles
Role-playing Games
more (21)
Mathematics
Cross Validated (stats)
Theoretical Computer Science
Physics
MathOverflow
more (7)
Stack Apps
Meta Stack Exchange
Area 51
Stack Overflow Careers
site design / logo © 2015 stack exchange inc; user contributions licensed under cc by-sa 3.0 with attribution required
rev 2015.4.10.2461
c# - append image to pages of PDF using iTextSharp - Stack Overflow
http://stackoverflow.com/questions/15948097/append-image-to-pages-of-pdf-using-itextsharp[10/04/2015 21:09:56]