c# - append image to pages of PDF using iTextSharp - Stack Overflow
sign up
log in
tour
Questions
help
stack overflow careers
Tags
Users
Badges
search
Unanswered
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no
registration required.
Ask Question
Take the 2-minute tour
append image to pages of PDF using iTextSharp
asked
1 year ago
viewed 3556 times
active
9 months ago
I am attempting to append images to an existing PDF using the following.
Upcoming Events
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
http://stackoverflow.com/questions/15948097/append-image-to-pages-of-pdf-using-itextsharp[10/04/2015 21:09:56]
2015 Community Moderator
Election
ends Apr 21
c# - append image to pages of PDF using iTextSharp - Stack Overflow
: 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);
Linked
}
pdf.Close();
23
How can I insert an image
with iTextSharp in an
existing PDF?
}
ms.Flush();
return ms.GetBuffer();
Related
The result value is not used, I was debugging it. The value is always true, so the add page is
working.
23 How can I insert an image
The resulting PDF is the same size as the original, but is not readable. I get invalid root object
errors when opening it.
0 convert pdf page to image
Any suggestions?
7 Extract image from PDF
image
iTextSharp c#
using itextsharp
Thanks
c#
with iTextSharp in an
existing PDF?
4 iTextSharp - PDF - Resize
pdf
Document to Accomodate
a Large Image
itextsharp
share improve this question
asked Apr 11 '13 at 11:53
Jim
1,161
14
2 Add an existing PDF from
file to an unwritten
document using iTextSharp
40
0 Get pdf embedded image
width using iTextsharp
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
http://stackoverflow.com/questions/15948097/append-image-to-pages-of-pdf-using-itextsharp[10/04/2015 21:09:56]
1 How do you set a
c# - append image to pages of PDF using iTextSharp - Stack Overflow
background image on
every page of a PDF in
iTextSharp?
add a comment
0 iTextSharp is giving me the
2
Answers
active
oldest
votes
error: PDF header
signature not found
0 How to draw a PDF page
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);
http://stackoverflow.com/questions/15948097/append-image-to-pages-of-pdf-using-itextsharp[10/04/2015 21:09:56]
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
c# - append image to pages of PDF using iTextSharp - Stack Overflow
sounding like ga-yeah?
doc.Add(pdfImage);
}
doc.Close();
writer.Close();
How does the high gravity on
Miller's planet create big waves?
What is this book about psychic
juvenile delinquents?
Method 2 (using PdfStamper)
QR code reader on ubuntu
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
Jim
1,161
add a comment
http://stackoverflow.com/questions/15948097/append-image-to-pages-of-pdf-using-itextsharp[10/04/2015 21:09:56]
14
40
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?
c# - append image to pages of PDF using iTextSharp - Stack Overflow
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
Bruno Lowagie
26.1k
http://stackoverflow.com/questions/15948097/append-image-to-pages-of-pdf-using-itextsharp[10/04/2015 21:09:56]
22
46
c# - append image to pages of PDF using iTextSharp - Stack Overflow
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
add a comment
Your Answer
Sign up or log in
Sign up using Google
Sign up using Facebook
Sign up using Stack Exchange
Post as a guest
Name
Email
required, but never shown
http://stackoverflow.com/questions/15948097/append-image-to-pages-of-pdf-using-itextsharp[10/04/2015 21:09:56]
c# - append image to pages of PDF using iTextSharp - Stack Overflow
Post Your Answer
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
Stack Overflow
Programmers
Server Fault
Unix & Linux
Super User
Web Applications
Ask Ubuntu
Ask Different (Apple)
WordPress
Development
Webmasters
Geographic Information
Systems
Game Development
Electrical Engineering
TeX - LaTeX
Android Enthusiasts
Information Security
Database
Administrators
Photography
CULTURE /
RECREATION
English Language &
Usage
SCIENCE
OTHER
Mathematics
Stack Apps
Cross Validated (stats)
Meta Stack Exchange
Area 51
Drupal Answers
Science Fiction &
Fantasy
SharePoint
Graphic Design
Mi Yodeya (Judaism)
Theoretical Computer
Science
User Experience
Seasoned Advice
(cooking)
Travel
Physics
Christianity
MathOverflow
Arqade (gaming)
more (7)
Mathematica
Salesforce
more (14)
Home Improvement
Skeptics
Personal Finance &
Money
Bicycles
Academia
Role-playing Games
more (10)
more (21)
site design / logo 2015 stack exchange inc; user contributions licensed under cc by-sa 3.0 with attribution required
rev 2015.4.10.2461
http://stackoverflow.com/questions/15948097/append-image-to-pages-of-pdf-using-itextsharp[10/04/2015 21:09:56]
Stack Overflow Careers
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]