Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# iTextsharp Replace Page of a multi-page PDF

Say, I now have a 5-page PDF called 'a.pdf' which page 2 and 4 are empty. And another 2-page PDF called 'b.pdf'. Now what I want is to copy the the first page of 'b.pdf' to page2 of 'a.pdf' and second page of 'b.pdf' to page 4 of 'a.pdf'.

I found it's quite hard to find any examples, what I found is someone provided here, http://itextsharp.10939.n7.nabble.com/Replace-Pages-with-ItextSharp-td2956.html Called 'PdfStamper.ReplacePage()', I guess this is what I'm looking for, but I did a simple demo but didn't work out. Can someone have a check for me?

string _outMergeFile = Server.MapPath("~/11/a.pdf");

string file2 = Server.MapPath("~/11/b.pdf");
PdfReader readerA = new PdfReader(_outMergeFile);
PdfReader readerB = new PdfReader(file2);

PdfStamper cc = new PdfStamper(readerA,new MemoryStream());

cc.ReplacePage(readerB, 1, 2);
cc.ReplacePage(readerB, 2, 4);
cc.Close();

Thanks in advance.

================================================================================= Thanks to Jose's suggestion. The code works now. I'm now providing a simple sample here for others to reference.

public void MyFunction()
{
    string _outMergeFile = Server.MapPath("~/11/a.pdf");

    string file2 = Server.MapPath("~/11/b.pdf");
    PdfReader readerA = new PdfReader(_outMergeFile);
    PdfReader readerB = new PdfReader(file2);

    PdfStamper cc = new PdfStamper(readerA, new FileStream(Server.MapPath("~/11/result.pdf"), FileMode.Append));

    cc.ReplacePage(readerB, 1, 2);
    cc.Close();
}
like image 695
JunglerSens Avatar asked Oct 07 '13 00:10

JunglerSens


People also ask

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What do you mean by C?

C is a structured, procedural programming language that has been widely used both for operating systems and applications and that has had a wide following in the academic community. Many versions of UNIX-based operating systems are written in C.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is C language used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...


1 Answers

OK, I think I've found your problem. cc is created in memory, and I don't see any code to save the actual changes to the file before you close it, so the alterations made to the in-memory file are lost. One option is to create it with a new FileStream () instead of a memory stream

like image 196
jose Avatar answered Sep 23 '22 06:09

jose