View previous topic :: View next topic |
Author |
Message |
sgirard
Joined: 19 Nov 2007 Posts: 2
|
Posted: Mon Nov 19, 2007 6:05 pm Post subject: Merge PDF documents, and use same background for all pages |
|
|
Hi, I am new to PdfSharp, and I have one question.
I have several PDF files, with the same background. My pdf size is about 700kb. I have to merge multiple files, but I don't want to repeat the backgound for each. Is there a way to add a page to another document, and use the original background for all pages.
Thanks for your help
Sebastien |
|
Back to top |
|
|
Thomas Hoevel
Joined: 16 Oct 2006 Posts: 387 Location: Cologne, Germany
|
Posted: Tue Nov 20, 2007 9:19 am Post subject: |
|
|
Not sure if I understood your question correctly.
If you use one XGraphics object to draw the background of all pages, only one copy of the background will be in the resulting PDF file.
So if you have one file with the background and several files with the content and merge them, then you will get a small PDF file with only one copy of the background. _________________ Regards
Thomas Hoevel
PDFsharp Team |
|
Back to top |
|
|
sgirard
Joined: 19 Nov 2007 Posts: 2
|
Posted: Tue Nov 20, 2007 2:21 pm Post subject: |
|
|
Sorry for by english, I speak french!
I have two pdf. These pdf have the same background image, (logo, images....). In average, a pdf in this format is about 600kb.
When merging these 2 files, the new file becomes 1200kb, because the background is duplicated for 2 files. What I want to do, is creating a pdf files that will repeat this background for all pages, and then only import data of pdfs. If I'm able to do that, maybe the result file should be 615kb, instead of 1200kb. I don't know if you understand. |
|
Back to top |
|
|
Thomas Hoevel
Joined: 16 Oct 2006 Posts: 387 Location: Cologne, Germany
|
Posted: Tue Nov 20, 2007 2:50 pm Post subject: |
|
|
There is no simple solution for that if the two source files are given and cannot be re-created.
It would work the other way around: start with one big file and split it into two ...
It would also work with 3 files: one with images, two with text.
You could create three files: Part A with images, Part B with images, complete file with images.
PDFsharp does not (yet) detect duplicated resources (images, fonts, ...) when merging PDF files. _________________ Regards
Thomas Hoevel
PDFsharp Team |
|
Back to top |
|
|
brendalisa
Joined: 07 Feb 2008 Posts: 2
|
Posted: Thu Feb 07, 2008 5:08 pm Post subject: |
|
|
I'm trying to merge 2 documents into 1. One is a template and the other just has text. It sounds exactly like what you are already doing. When you get a second, can you show me some sample code that will do that? I would totally appreciate it. Thanks!
Brenda |
|
Back to top |
|
|
trih
Joined: 11 Mar 2008 Posts: 2
|
Posted: Tue Mar 11, 2008 6:10 pm Post subject: |
|
|
brendalisa wrote: | I'm trying to merge 2 documents into 1. One is a template and the other just has text. It sounds exactly like what you are already doing. When you get a second, can you show me some sample code that will do that? I would totally appreciate it. Thanks!
Brenda |
Maybe I am little late with it, anyway here a solution how i did it. (Edited code from the "TwoOnOnePage-Tutorial")
This is just a quick hack. Currently you can only use it if both documents are in the same format, otherwise it will look ugly
Look for this part in the code to fix that.
Code: |
XRect size_box = new XRect(0, 0, page.Width, page.Height);
gfx.DrawImage(pdf_doc_background, size_box);
|
Code: |
using System;
using System.Diagnostics;
using System.IO;
using PdfSharp.Drawing;
using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;
namespace yourNameSpace
{
class watermarker
{
public watermarker(string input_path, string input_filename, string background_path, string background_filename, string output_path_and_filename)
{
string background = Path.Combine(background_path, background_filename);
string input = Path.Combine(input_path, input_filename);
string output = output_path_and_filename;
File.Copy(input, output ,true); // Copy the input PDF file to the output source, to edit it.
PdfDocument pdf_doc_output = PdfReader.Open(output); //Open the output document to edit it.
// Open the background PDF as XpdfForm (used to draw like an Image)
XPdfForm pdf_doc_background = XPdfForm.FromFile(background);
// Set version to PDF 1.4 (Acrobat 5) because we use transparency.
if (pdf_doc_output.Version < 14)
pdf_doc_output.Version = 14;
for (int idx = 0; idx < pdf_doc_output.Pages.Count; idx++) // Loop threw all input pages
{
PdfPage page = pdf_doc_output.Pages[idx]; //Create a link to the current page
// Get an XGraphics object for drawing beneath the existing content
XGraphics gfx = XGraphics.FromPdfPage(page, XGraphicsPdfPageOptions.Prepend);
XRect size_box = new XRect(0, 0, page.Width, page.Height);
gfx.DrawImage(pdf_doc_background, size_box);
}
// Save your Document
pdf_doc_output.Save(output);
// Start a Viewer
Process.Start(output);
}
}
}
|
|
|
Back to top |
|
|
Hugo
Joined: 10 Dec 2008 Posts: 1
|
Posted: Wed Dec 10, 2008 4:57 pm Post subject: XPdfForm bug? |
|
|
Hello all,
I think that XPdfForm class as a bug.
I'm using it in a GUI application that allows the users to change the background but if the user changes it more that one time the changes do not work. The background does not change.
Best regards,
Hugo |
|
Back to top |
|
|
Thomas Hoevel
Joined: 16 Oct 2006 Posts: 387 Location: Cologne, Germany
|
Posted: Thu Dec 11, 2008 9:10 am Post subject: Re: XPdfForm bug? |
|
|
Hi!
Hugo wrote: | I think that XPdfForm class as a bug. |
Is it a bug or a feature?
XPdfForm caches the PDFfile. If the PDFfile is changed after it was already read by the XPdfForm, then this change will not be visible in the final PDF file. Free all XPdfForms when the final document is done - and they will be read again on the next run.
This is the only "problem" I had with XPdfForms - and this is a feature!
If you think there is another bug: without further information we can't do anything about it. _________________ Regards
Thomas Hoevel
PDFsharp Team |
|
Back to top |
|
|
|