is there any way to combine two b5 size pdf into single legal size pdf. after googling i did not find any solution. how to approach this problem. what i can use to do it. i am developing c# desktop application which will combine two b5 size pdf to single legal page. one left size and another is right side
e.g. input
1. b5first.pdf
1234
2. b5second
567
Output should be
3. legal.pdf
1234 567
You can get the text from the second pdf and put it in first, in the place you want to put.
If you are using iTextsharp you can do something like
String text += PdfTextExtractor.GetTextFromPage(reader, pageno ,new LocationTextExtractionStrategy());
Then can put the string wherever you want.
If you are looking for open-source solution then check this MetafileToEPSConverter to convert metafile into EPS and then convert EPS into PDF using epstopdf (included into LyX) or similar tools.
public static void somefunction(string oldFile,string oldFile1,string pathout)
{
// open the reader
PdfReader reader = new PdfReader(oldFile);
PdfReader reader1 = new PdfReader(oldFile1);
Document document = new Document(PageSize.LEGAL.Rotate());
// open the writer
FileStream fs = new FileStream(pathout, FileMode.Create, FileAccess.Write);
PdfWriter writer = PdfWriter.GetInstance(document, fs);
document.Open();
// the pdf content
PdfContentByte cb = writer.DirectContent;
// create the new page and add it to the pdf
PdfImportedPage page = writer.GetImportedPage(reader, 1);
PdfImportedPage page1 = writer.GetImportedPage(reader1, 1);
cb.AddTemplate(page, 0, 0);
cb.AddTemplate(page1, 500, 0);
// close the streams and voilá the file should be changed :)
document.Close();
fs.Close();
writer.Close();
reader.Close();
reader1.Close();
}
Related
There is an existing table/image. When I write text using pdfcontentbyte, it is written behind this table/image.
I also want to write the text from the right side of this table/column.
The code I'm currently using to produce the image above:
// open the reader
PdfReader reader = new PdfReader(oldFile);
iTextSharp.text.Rectangle size = reader.GetPageSizeWithRotation(1);
Document document = new Document(size);
// open the writer
FileStream fs = new FileStream(newFile, FileMode.Create, FileAccess.Write);
PdfWriter writer = PdfWriter.GetInstance(document, fs);
document.Open();
PdfContentByte cb = writer.DirectContent;
BaseFont bf = BaseFont.CreateFont(BaseFont.COURIER_BOLD,BaseFont.CP1252, BaseFont.EMBEDDED);
string text = "WV0501";
cb.BeginText();
// put the alignment and coordinates here
cb.ShowTextAligned(2, text, 155, 655, 0);
cb.EndText();
// create the new page and add it to the pdf
PdfImportedPage page = writer.GetImportedPage(reader, 1);
cb.AddTemplate(page, 0, 0);
document.Close();
fs.Close();
writer.Close();
You don't show us your code, so we have to guess what you are doing wrong.
The content isn't showing:
You probably have this line in your code:
PdfContentByte canvas = pdfStamper.GetUnderContent(page);
If so, you should replace it with this line:
PdfContentByte canvas = pdfStamper.GetOverContent(page);
Update after you showed us your code:
You want to add content to an existing document, yet you are using a combination of Document and PdfWriter. Why are you doing this? Please read chapter 6 of my book where you'll learn about PdfStamper.
Right now, you are adding the text first and then you are covering it with a page from an existing document. This means that the existing page will cover the text.
You can switch this around like this:
// create the new page and add it to the pdf
PdfImportedPage page = writer.GetImportedPage(reader, 1);
cb.AddTemplate(page, 0, 0);
BaseFont bf = BaseFont.CreateFont(BaseFont.COURIER_BOLD,BaseFont.CP1252, BaseFont.EMBEDDED);
string text = "WV0501";
cb.BeginText();
// put the alignment and coordinates here
cb.ShowTextAligned(2, text, 155, 655, 0);
cb.EndText();
Now the text will cover the page, but that doesn't mean your code is better. You should really use PdfStamper instead of PdfWriter:
PdfReader reader = new PdfReader(oldFile);
FileStream fs = new FileStream(newFile, FileMode.Create, FileAccess.Write);
PdfStamper stamper = new PdfStamper(reader, fs);
PdfContentByte canvas = stamper.GetOverContent(1);
BaseFont bf = BaseFont.CreateFont(BaseFont.COURIER_BOLD,BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
ColumnText.ShowTextAligned(
canvas,
Element.ALIGN_RIGHT,
new Phrase("WV0501", new Font(bf, 9)),
155, 655, 0
);
stamper.Close();
Don't you agree that this is more elegant?
IMPORTANT:
In your code you use:
BaseFont bf = BaseFont.CreateFont(BaseFont.COURIER_BOLD,BaseFont.CP1252, BaseFont.EMBEDDED);
However, that doesn't make much sense: COURIER_BOLD is one of the Standard Type 1 fonts and because of that the embedded parameter is ignored. I changed this into NOT_EMBEDDED because if you use EMBEDDED developers who read your code and who don't know anything about PDF and iText may get confused. They might ask: Why is the font not getting embedded when the parameter says it should be embedded?
BaseFont bf = BaseFont.CreateFont(BaseFont.COURIER_BOLD,BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
Writing from the right side:
You are defining the alignment using a number: 2. I suggest that you don't use a number in your code, but a constant: ALIGN_RIGHT. This way, we see that you want to right align the text.
The text is right aligned with respect to the coordinates you've defined:
x = 155
y = 655
If you are not happy with the position of your text, you should change these hard-coded coordinates. For instance: increase x and decrease y.
You probably want the text to be relative to the border of a table cell or an image. If that is the case, you should not hard-code the coordinates.
Retrieving the coordinates of an image is discussed in another question on SO. Retrieving the coordinates of a table might very well be impossible. It all depends on the nature of the original PDF.
I want to add variable text and thumbnail images on specific position.
Text with formatting like fontstyles,rotation etc.
iTextSharp.text.pdf.PdfReader reader = null;
Document document = new Document();
PdfWriter writer;
writer = PdfWriter.GetInstance(document,
new FileStream(temp,
FileMode.Create, FileAccess.Write));
reader = new iTextSharp.text.pdf.PdfReader(file);
size = reader.GetCropBox(1);
PdfContentByte cb = writer.DirectContent;
document.NewPage();
PdfImportedPage page = writer.GetImportedPage(reader,
pageNumber);
cb.AddTemplate(page, 0, 0);
document.Close();
This will copy first page to new document. I want to add text and images on new document. how to do that?
Many people will stop reading your question after your first sentence:
I want to Edit existing PDF
If you read the intro of chapter 6 of my book, you'll understand that the word Edit is not the right word to use.
I did read on, and that I saw that you were using Document and PdfWriter to stamp new content on an existing PDF. If you do the effort of reading chapter 6 of my book, you'll understand that this is wrong. You should use PdfStamper instead.
This is one of the examples provided online:
public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
PdfReader reader = new PdfReader(src);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
Phrase header = new Phrase("Copy", new Font(FontFamily.HELVETICA, 14));
for (int i = 1; i <= reader.getNumberOfPages(); i++) {
float x = reader.getPageSize(i).getWidth() / 2;
float y = reader.getPageSize(i).getTop(20);
ColumnText.showTextAligned(
stamper.getOverContent(i), Element.ALIGN_CENTER,
header, x, y, 0);
}
stamper.close();
reader.close();
}
You can find these examples on the following URLs:
http://itextpdf.com/sandbox/stamper
http://itextpdf.com/book/chapter.php?id=6
If you need the examples from chapter 6 ported to C#, please visit this URL: http://tinyurl.com/itextsharpIIA2C06
Update
Does PdfStamper support:
font embedding: yes, when done correctly, fonts are embedded,
font size: yes, that's obvious,
font family: you provide the font program, the font program knows the font family,
alignments: for single lines: use showTextAligned(); for text blocks use ColumnText (caveat: read about the difference between text mode and composite mode)
rotation: for single lines, this is only a matter of providing a rotation angle; for text blocks, create a Form XObject and add this object using a rotation,
...
I want to add a text to an existing PDF file using iTextSharp, I found different ways but in all of them the writer and reader are separate pdf files.
I want a way so I can open a pdf then write different things in different positions.
right now I have this code, but it makes a new file.
using (FileStream stream1 = File.Open(path, FileMode.OpenOrCreate))
{
BaseFont bf = BaseFont.CreateFont(fontPath, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
PdfReader reader = new PdfReader("C:\\26178DATA\\pdf\\holding.pdf");
var pageSize = reader.GetPageSize(1);
PdfStamper stamper = new PdfStamper(reader, stream1);
iTextSharp.text.Font tmpFont = new iTextSharp.text.Font(bf, fontSize);
PdfContentByte canvas = stamper.GetOverContent(1);
Phrase ph = new Phrase(words[1], tmpFont);
ph.Font = tmpFont;
canvas.SetFontAndSize(bf, fontSize);
ColumnText.ShowTextAligned(canvas, Element.ALIGN_LEFT, ph, iTextSharp.text.Utilities.MillimetersToPoints(x * 10), pageSize.GetTop(iTextSharp.text.Utilities.MillimetersToPoints(y * 10)), 0);
stamper.Close();
}
You want to add a text to an existing PDF file using iTextSharp, found different ways but in all of them the writer and reader are separate pdf files.
As the normal way in which iText(Sharp) manipulates a PDF using a PdfStamper, can involve major reorganization of existing PDF elements, iText does not edit a file in place. The other way, using append mode, would allow for editing in place; but such an option is not implemented. A big draw-back of in-place editing is that in case of some program failure, the file in question might remain in an intermediary, unusable state.
That being said, you can save the new file to the path of the original file by first reading the file into memory completely and then starting to create the output with the same path. In case of your sample code that would imply at least moving the PdfReader constructor use before the creation of the output stream:
PdfReader reader = new PdfReader(path);
using (FileStream stream1 = File.Open(path, FileMode.OpenOrCreate))
{
BaseFont bf = BaseFont.CreateFont(fontPath, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
...
Alternatively you could create the result file in memory, i.e. in a MemoryStream instead of a FileStream, and, when finished, write the contents of the memory stream to your source file path.
I need to add a user's signature (like your signature on a credit card, for example) to a PDF document online. How can I do it? What is the best practice for this kind of tasks?
You can use iTextSharp to do it. Something like this will work for you:
PdfReader pdfReader = null;
PdfStamper pdfStamper = null;
// Open the PDF file to be signed
pdfReader = new PdfReader(this.signFile.FullName);
// Output stream to write the stamped PDF to
using (FileStream outStream = new FileStream(targetFilename, FileMode.Create))
{
try
{
// Stamper to stamp the PDF with a signature
pdfStamper = new PdfStamper(pdfReader, outStream);
// Load signature image
iTextSharp.text.Image sigImg = iTextSharp.text.Image.GetInstance(SIGNATURE_IMAGE_PATH);
// Scale image to fit
sigImg.ScaleToFit(MAX_WIDTH, MAX_HEIGHT);
// Set signature position on page
sigImg.SetAbsolutePosition(POS_X, POS_X);
// Add signatures to desired page
PdfContentByte over = pdfStamper.GetOverContent(PAGE_NUM_TO_SIGN);
over.AddImage(sigImg);
}
finally
{
// Clean up
if (pdfStamper != null)
pdfStamper.Close();
if (pdfReader != null)
pdfReader.Close();
}
}
You need to implement some action with JavaScript. You can use some JQuery pluging for that. Here is an example;
http://motyar.blogspot.com/2010/01/draw-with-jquery.html
For the pdf part, you can use some pdf converter tool Like ITextSharp
I tried to do it using this tutorial as a base, but it's throwing a null reference exception at the line specified below. Should I be doing this a different way? If not, why would it throw an null reference exception (both page and cb are NOT null). Code:
string filePath = #"c:\temp\test_new.pdf";
string attachPath = #"c:\temp\test.pdf";
Console.WriteLine("Begin!");
Document d = new Document();
if(File.Exists(filePath)){File.Delete(filePath);}
FileStream fs = new FileStream(filePath, FileMode.Create);
PdfWriter pw = PdfWriter.GetInstance(d, fs);
d.Open();
d.Add(new Paragraph("New document! Now lets add an attachment!"));
PdfReader pRdr = new PdfReader(new FileStream(attachPath,FileMode.Open));
PdfReaderContentParser parser = new PdfReaderContentParser(pRdr);
MemoryStream ms = new MemoryStream();
PdfWriter writer = PdfWriter.GetInstance(d, ms);
writer.Open();
PdfContentByte cb = writer.DirectContent;
PdfImportedPage page;
int rotation;
d.SetPageSize(PageSize.LETTER);
for (int i = 1; i <= pRdr.NumberOfPages; i++)
{
d.NewPage();
page = writer.GetImportedPage(pRdr, i);
rotation = pRdr.GetPageRotation(i);
if (rotation == 90 || rotation == 270)
{
cb.AddTemplate(page, 0, -1.0F, 1.0F, 0, 0, pRdr.GetPageSizeWithRotation(i).Height);
}
else
{
/*NULL EXCEPTION HERE!!!*/cb.AddTemplate(page, 1.0F, 0, 0, 1.0F, 0, 0); //NULL EXCEPTION HERE!!!
}
}
1) Use PdfCopy not PdfWriter. PdfWriter is for writing generated PDFs from a Document. PdfCopy is made for copying pages from A to B.
2) If you're problem is the result of an exception PLEASE post the exception. It'll remove much of the guesswork you see in the comments.
3) PdfImportedPage is just that page's contents and resources. You lose annotations (form fields and the like), bookmarks, and so forth. PdfCopy can help with some of that, but not all.
OK. I might get blasted for not answering your question, but there is a simpler way to merge two PDFs: don't use iTextSharp, use iTextDotNet
I found a post on how to do it: http://alex.buayacorp.com/merge-pdf-files-with-itextdotnet-and-net.html
I remembered this because I had to do this a couple of years ago. It does work, and well.
Check out my simple embPDFUtils library, where you can configure the merging and splitting process through an XML-Configuration file. There are methods to concatenate pdf files, split them and create pdf files from images. It is free. Check it out here: http://blog.mecum.biz/2011/11/how-master-xml-and-mistress-xsd-helped-itextsharp-out-of-the-claws-of-hippi-o-cratic-chaos-huggermugger/