Send file to XPS Printer - c#

XPS Printer allows us to create xps file no matter if from an image or txt or doc file.
I would like to do the same just programmatically in c#.
How do I send a file to xps printer and let the printer convert it to .xps file?
Any ideas?
I Google this but haven't found much so far.

It is possible also to use print queue to print to the XPS document writer but it will always show the file dialog.
See below other alternatives to convert and print to XPS file.
Programmatically converting files to XPS
This is not as simple as you wish many users out there have tried and there is many different ways to accomplish it all which arent the best.
One way (easiest way) to convert documents to XPS would be to use WORD 2007+ API to do it.
See below a snipped from this MSDN forum question
To programmatically convert docx to xps using Word 2007 see
Document.ExportAsFixedFormat in the Word Object Model Reference
(http://msdn2.microsoft.com/en-us/library/Bb256835.aspx). However,
since this is a server-side scenario you should take note of KB 257757
Considerations for server-side Automation of Office (see
http://support.microsoft.com/kb/257757).
Printing Images to XPS
You can easily print an Image to XPS file using the code below.
The code below is WPF example the image you pass into the write method needs to be wrapped in a canvas see this post for an example: http://denisvuyka.wordpress.com/2007/12/03/wpf-diagramming-saving-you-canvas-to-image-xps-document-or-raw-xaml/
XpsDocument xpsd = new XpsDocument("C:\\YourXPSFileName.xps", FileAccess.ReadWrite);
System.Windows.Xps.XpsDocumentWriter xw = XpsDocument.CreateXpsDocumentWriter(xpsd);
xw.Write(YourImageYouWishToWrite);
See an extended example below:
public void Export(Uri path, Canvas surface)
{
if (path == null) return;
// Save current canvas transorm
Transform transform = surface.LayoutTransform;
// Temporarily reset the layout transform before saving
surface.LayoutTransform = null;
// Get the size of the canvas
Size size = new Size(surface.Width, surface.Height);
// Measure and arrange elements
surface.Measure(size);
surface.Arrange(new Rect(size));
// Open new package
Package package = Package.Open(path.LocalPath, FileMode.Create);
// Create new xps document based on the package opened
XpsDocument doc = new XpsDocument(package);
// Create an instance of XpsDocumentWriter for the document
XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(doc);
// Write the canvas (as Visual) to the document
writer.Write(surface);
// Close document
doc.Close();
// Close package
package.Close();
// Restore previously saved layout
surface.LayoutTransform = transform;
}
There are third party tools that allow you to print PDFs and other file formats to XPS.
PDF to XPS
Printing XPS files
It is possible to print XPS Documents programmatically You will need .Net 4 at least for this solution.
The example below uses the Print dialog from WPF and some of the classes from System.Windows.Xps and System.Printing.
The code below will print the Xps file to the default printer on the system however if you want to print to a different printer or even print to a print server you need to change the PrintQueue object on the print dialog.
Which is quite simple using the System.Printing namespace.
See the example below.
Please bear in mind that because it is using the WPF dialog it needs to run on a STATThread model.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Printing;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Xps.Packaging;
namespace ConsoleApplication
{
class Program
{
[STAThread]
static void Main(string[] args)
{
PrintDialog dlg = new PrintDialog();
XpsDocument xpsDoc = new XpsDocument(#"C:\DATA\personal\go\test.xps", System.IO.FileAccess.Read);
dlg.PrintDocument(xpsDoc.GetFixedDocumentSequence().DocumentPaginator, "Document title");
}
}
}
See below some Helpful links.
Xps Document documentation
Print Dialog from WPF documentation
System.Printing namespace documentation
Hope this helps and fit your needs

class Program
{
[System.MTAThreadAttribute()] // Added for clarity, but this line is redundant because MTA is the default.
static void Main(string[] args)
{
// Create the secondary thread and pass the printing method for
// the constructor's ThreadStart delegate parameter. The BatchXPSPrinter
// class is defined below.
Thread printingThread = new Thread(BatchXPSPrinter.PrintXPS);
// Set the thread that will use PrintQueue.AddJob to single threading.
printingThread.SetApartmentState(ApartmentState.STA);
// Start the printing thread. The method passed to the Thread
// constructor will execute.
printingThread.Start();
}//end Main
}//end Program class
public class BatchXPSPrinter
{
public static void PrintXPS()
{
// Create print server and print queue.
LocalPrintServer localPrintServer = new LocalPrintServer();
PrintQueue defaultPrintQueue = LocalPrintServer.GetDefaultPrintQueue();
// Prompt user to identify the directory, and then create the directory object.
Console.Write("Enter the directory containing the XPS files: ");
String directoryPath = Console.ReadLine();
DirectoryInfo dir = new DirectoryInfo(directoryPath);
// If the user mistyped, end the thread and return to the Main thread.
if (!dir.Exists)
{
Console.WriteLine("There is no such directory.");
}
else
{
// If there are no XPS files in the directory, end the thread
// and return to the Main thread.
if (dir.GetFiles("*.xps").Length == 0)
{
Console.WriteLine("There are no XPS files in the directory.");
}
else
{
Console.WriteLine("\nJobs will now be added to the print queue.");
Console.WriteLine("If the queue is not paused and the printer is working, jobs will begin printing.");
// Batch process all XPS files in the directory.
foreach (FileInfo f in dir.GetFiles("*.xps"))
{
String nextFile = directoryPath + "\\" + f.Name;
Console.WriteLine("Adding {0} to queue.", nextFile);
try
{
// Print the Xps file while providing XPS validation and progress notifications.
PrintSystemJobInfo xpsPrintJob = defaultPrintQueue.AddJob(f.Name, nextFile, false);
}
catch (PrintJobException e)
{
Console.WriteLine("\n\t{0} could not be added to the print queue.", f.Name);
if (e.InnerException.Message == "File contains corrupted data.")
{
Console.WriteLine("\tIt is not a valid XPS file. Use the isXPS Conformance Tool to debug it.");
}
Console.WriteLine("\tContinuing with next XPS file.\n");
}
}// end for each XPS file
}//end if there are no XPS files in the directory
}//end if the directory does not exist
Console.WriteLine("Press Enter to end program.");
Console.ReadLine();
}// end PrintXPS method
}// end BatchXPSPrinter class

Related

How to solve the error of Word opening in background when trying to read text from Word documents?

I'm trying to read the string of text from word documents into a List Array, and then search for the word in these string of text. The problem, however, is that the word documents kept on running continuously in the windows background when opened, even though I close the document after reading the text.
Parallel.ForEach(files, file =>
{
switch (System.IO.Path.GetExtension(file))
{
case ".docx":
List<string> Word_list = GetTextFromWord(file);
SearchForWordContent(Word_list, file);
break;
}
});
static List<string> GetTextFromWord(string direct)
{
if (string.IsNullOrEmpty(direct))
{
throw new ArgumentNullException("direct");
}
if (!File.Exists(direct))
{
throw new FileNotFoundException("direct");
}
List<string> word_List = new List<string>();
try
{
Microsoft.Office.Interop.Word.Application app =
new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Document doc = app.Documents.Open(direct);
int count = doc.Words.Count;
for (int i = 1; i <= count; i++)
{
word_List.Add(doc.Words[i].Text);
}
((_Application)app).Quit();
}
catch (System.Runtime.InteropServices.COMException e)
{
Console.WriteLine("Error: " + e.Message.ToString());
}
return word_List;
}
When you use Word Interop you're actually starting the Word application and talk to it using COM. Every call, even reading a property, is an expensive cross-process call.
You can read a Word document without using Word. A docx document is a ZIP package containing well-defined XML files. You could read those files as XML directly, you can use the Open XML SDK to read a docx file or use a library like NPOI which simplifies working with Open XML.
The word count is a property of the document itself. To read it using the Open XML SDK you need to check the document's ExtendedFileProperties part :
using (var document = WordprocessingDocument.Open(fileName, false))
{
var words = (int) document.ExtendedFilePropertiesPart.Properties.Words.Text;
}
You'll find the Open XML documentation, including the strucrure of Word documents at MSDN
Avoiding Owner Files
Word or Excel Files that start with ~ are owner files. These aren't real Word or Excel files. They're temporary files created when someone opens a document for editing and contain the logon name of that user. These files are deleted when Word closes gracefully but may be left behind if Word crashes or the user has no DELETE permissions, eg in a shared folder.
To avoid these one only needs to check whether the filename starts with ~.
If the fileName is only the file name and extension, fileName.StartsWith("~") is enough
If fileName is an absolute path, `Path.GetFileName(fileName).StartsWith("~")
Things get trickier when trying to filter such files in a folder. The patterns used in Directory.EnumerateFiles or DirectoryInfo.EnumerateFiles are simplistic and can't exclude characters. The files will have to be filtered after the call to EnumerateFiles, eg :
var dir=new DirectoryInfo(folderPath);
foreach(var file in dir.EnumerateFiles("*.docx"))
{
if (!file.Name.StartsWith("~"))
{
...
}
}
or, using LINQ :
var dir=new DirectoryInfo(folderPath);
var files=dir.EnumerateFiles("*.docx")
.Where(file=>!file.Name.StartsWith("~"));
foreach(var file in files)
{
...
}
Enumeration can still fail if a file is opened exclusively for editing. To avoid exceptions, the EnumerationOptions.IgnoreInaccessible parameter can be used to skip over locked files:
var dir=new DirectoryInfo(folderPath);
var options=new EnumerationOptions
{
IgnoreInaccessible =true
};
var files=dir.EnumerateFiles("*.docx",options)
.Where(file=>!file.Name.StartsWith("~"));
One option is to
List item
List item

Unable to merge 2 PDFs using MemoryStream

I have a c# class that takes an HTML and converts it to PDF using wkhtmltopdf.
As you will see below, I am generating 3 PDFs - Landscape, Portrait, and combined of the two.
The properties object contains the html as a string, and the argument for landscape/portrait.
System.IO.MemoryStream PDF = new WkHtmlToPdfConverter().GetPdfStream(properties);
System.IO.FileStream file = new System.IO.FileStream("abc_landscape.pdf", System.IO.FileMode.Create);
PDF.Position = 0;
properties.IsHorizontalOrientation = false;
System.IO.MemoryStream PDF_portrait = new WkHtmlToPdfConverter().GetPdfStream(properties);
System.IO.FileStream file_portrait = new System.IO.FileStream("abc_portrait.pdf", System.IO.FileMode.Create);
PDF_portrait.Position = 0;
System.IO.MemoryStream finalStream = new System.IO.MemoryStream();
PDF.CopyTo(finalStream);
PDF_portrait.CopyTo(finalStream);
System.IO.FileStream file_combined = new System.IO.FileStream("abc_combined.pdf", System.IO.FileMode.Create);
try
{
PDF.WriteTo(file);
PDF.Flush();
PDF_portrait.WriteTo(file_portrait);
PDF_portrait.Flush();
finalStream.WriteTo(file_combined);
finalStream.Flush();
}
catch (Exception)
{
throw;
}
finally
{
PDF.Close();
file.Close();
PDF_portrait.Close();
file_portrait.Close();
finalStream.Close();
file_combined.Close();
}
The PDFs "abc_landscape.pdf" and "abc_portrait.pdf" generate correctly, as expected, but the operation fails when I try to combine the two in a third pdf (abc_combined.pdf).
I am using MemoryStream to preform the merge, and at the time of debug, I can see that the finalStream.length is equal to the sum of the previous two PDFs. But when I try to open the PDF, I see the content of just 1 of the two PDFs.
The same can be seen below:
Additionally, when I try to close the "abc_combined.pdf", I am prompted to save it, which does not happen with the other 2 PDFs.
Below are a few things that I have tried out already, to no avail:
Change CopyTo() to WriteTo()
Merge the same PDF (either Landscape or Portrait one) with itself
In case it is required, below is the elaboration of the GetPdfStream() method.
var htmlStream = new MemoryStream();
var writer = new StreamWriter(htmlStream);
writer.Write(htmlString);
writer.Flush();
htmlStream.Position = 0;
return htmlStream;
Process process = Process.Start(psi);
process.EnableRaisingEvents = true;
try
{
process.Start();
process.BeginErrorReadLine();
var inputTask = Task.Run(() =>
{
htmlStream.CopyTo(process.StandardInput.BaseStream);
process.StandardInput.Close();
});
// Copy the output to a memorystream
MemoryStream pdf = new MemoryStream();
var outputTask = Task.Run(() =>
{
process.StandardOutput.BaseStream.CopyTo(pdf);
});
Task.WaitAll(inputTask, outputTask);
process.WaitForExit();
// Reset memorystream read position
pdf.Position = 0;
return pdf;
}
catch (Exception ex)
{
throw ex;
}
finally
{
process.Dispose();
}
Merging pdf in C# or any other language is not straight forward with out using 3rd party library.
I assume your requirement for not using library is that most Free libraries, nuget packages has limitation or/and cost money for commercial use.
I have made research and found you an Open Source library called PdfClown with nuget package, it is also available for Java. It is Free with out limitation (donate if you like). The library has a lot of features. One such you can merge 2 or more documents to one document.
I supply my example that take a folder with multiple pdf files, merged it and save it to same or another folder. It is also possible to use MemoryStream, but I do not find it necessary in this case.
The code is self explaining, the key point here is using SerializationModeEnum.Incremental:
public static void MergePdf(string srcPath, string destFile)
{
var list = Directory.GetFiles(Path.GetFullPath(srcPath));
if (string.IsNullOrWhiteSpace(srcPath) || string.IsNullOrWhiteSpace(destFile) || list.Length <= 1)
return;
var files = list.Select(File.ReadAllBytes).ToList();
using (var dest = new org.pdfclown.files.File(new org.pdfclown.bytes.Buffer(files[0])))
{
var document = dest.Document;
var builder = new org.pdfclown.tools.PageManager(document);
foreach (var file in files.Skip(1))
{
using (var src = new org.pdfclown.files.File(new org.pdfclown.bytes.Buffer(file)))
{ builder.Add(src.Document); }
}
dest.Save(destFile, SerializationModeEnum.Incremental);
}
}
To test it
var srcPath = #"C:\temp\pdf\input";
var destFile = #"c:\temp\pdf\output\merged.pdf";
MergePdf(srcPath, destFile);
Input examples
PDF doc A and PDF doc B
Output example
Links to my research:
https://csharp-source.net/open-source/pdf-libraries
https://sourceforge.net/projects/clown/
https://www.oipapio.com/question-3526089
Disclaimer: A part of this answer is taken from my my personal web site https://itbackyard.com/merge-multiple-pdf-files-to-one-pdf-file-in-c/ with source code to github.
This answer from Stack Overflow (Combine two (or more) PDF's) by Andrew Burns works for me:
using (PdfDocument one = PdfReader.Open("pdf 1.pdf", PdfDocumentOpenMode.Import))
using (PdfDocument two = PdfReader.Open("pdf 2.pdf", PdfDocumentOpenMode.Import))
using (PdfDocument outPdf = new PdfDocument())
{
CopyPages(one, outPdf);
CopyPages(two, outPdf);
outPdf.Save("file1and2.pdf");
}
void CopyPages(PdfDocument from, PdfDocument to)
{
for (int i = 0; i < from.PageCount; i++)
{
to.AddPage(from.Pages[i]);
}
}
That's not quite how PDFs work. PDFs are structured files in a specific format.
You can't just append the bytes of one to the other and expect the result to be a valid document.
You're going to have to use a library that understands the format and can do the operation for you, or developing your own solution.
PDF files aren't just text and images. Behind the scenes there is a strict file format that describes things like PDF version, the objects contained in the file and where to find them.
In order to merge 2 PDFs you'll need to manipulate the streams.
First you'll need to conserve the header from only one of the files. This is pretty easy since it's just the first line.
Then you can write the body of the first page, and then the second.
Now the hard part, and likely the part that will convince you to use a library, is that you have to re-build the xref table. The xref table is a cross reference table that describes the content of the document and more importantly where to find each element. You'd have to calculate the byte offset of the second page, shift all of the elements in it's xref table by that much, and then add it's xref table to the first. You'll also need to ensure you create objects in the xref table for the page break.
Once that's done, you need to re-build the document trailer which tells an application where the various sections of the document are among other things.
See https://resources.infosecinstitute.com/pdf-file-format-basic-structure/
This is not trivial and you'll end up re-writing lots of code that already exists.

How can I send docx and pdf file to printer?

I have the following code which prints text file from C# to printer its based on the this article it prints plain text perfect but when I try to print .docx and .pdf file it prints but convert the content to some-kind of encoded characters. How can I fix this to print pdf and doc file?
private void btnPrint_Click(object sender, EventArgs e)
{
// Select the desired printer. ps.Duplex = Duplex.Simplex; // This works
pdocFile.PrinterSettings.PrinterName = cboPrinter.Text;
pdocFile.PrinterSettings.Duplex = Duplex.Horizontal;
// Print the checked files.
foreach (string filename in clbFiles.CheckedItems)
{
Console.WriteLine("Printing: " + filename);
// Get the file's name without the path.
FileInfo file_into = new FileInfo(filename);
string short_name = file_into.Name;
// Set the PrintDocument's name for use by the printer queue.
pdocFile.DocumentName = short_name;
// Read the file's contents.
try
{
FileContents = File.ReadAllText(filename).Trim();
}
catch (Exception ex)
{
MessageBox.Show("Error reading file " + filename +
".\n" + ex.Message);
return;
}
// Print.
pdocFile.Print();
}
MessageBox.Show("Spooled " + clbFiles.CheckedItems.Count +
" files for printing.");
}
//
private string FileContents;
// Print a page of the text file.
private void pdocTextFile_PrintPage(object sender, PrintPageEventArgs e)
{
// Make a font for printing.
using (Font font = new Font("Courier New", 10))
{
// Make a StringFormat to align text normally.
using (StringFormat string_format = new StringFormat())
{
// See how much of the remaining text will fit.
SizeF layout_area = new SizeF(e.MarginBounds.Width, e.MarginBounds.Height);
int chars_fitted, lines_filled;
e.Graphics.MeasureString(FileContents, font,
layout_area, string_format,
out chars_fitted, out lines_filled);
// Print as much as will fit.
e.Graphics.DrawString(
FileContents.Substring(0, chars_fitted),
font, Brushes.Black, e.MarginBounds,
string_format);
// Remove the printed text from the string.
FileContents = FileContents.Substring(chars_fitted).Trim();
}
}
// See if we are done.
e.HasMorePages = FileContents.Length > 0;
}
see Image link below
Your example above is taking a binary file format and trying to print it using a method that uses plain text, which will not work. You have a few options on how you could approach this.
Some printers allow you to submit various file types directly to them over a protocol like FTP. And example of this can be seen here. This method works great in enterprise environments which have business printers but is limited to the file types supported by each printer, and each printer's unique requirements.
For some formats, you can use third-party libraries like iText in your C# code to handle the actual printing. This option gives you a ton of control over the formatting, with the overhead of having to maintain additional code for every file type you wish to support.
You can also use the example code posted here to utilize already installed applications. In this example, it takes advantage of the Print verb made available by Adobe Acrobat, Word, etc. You'll need to make sure the applications have their defaults and surface the correct verb (which typically correlates with the context menu when right-clicking on a file name). This method is probably the most straight-forward option to handle files as-is.

Print Word document from Asp.net c# Without MS Office Installed

I need to print Word document without installing MS Office.
I am using WordprocessingDocument to manipulate Word file.
Now I need to print it.
I have tried:
System.Diagnostics.Process printProcess = new System.Diagnostics.Process();
printProcess.StartInfo.FileName = "D:/testWordPad1.docx";
printProcess.StartInfo.Verb = "Print";
printProcess.StartInfo.CreateNoWindow = true;
printProcess.Start();
printProcess.WaitForExit();
It works in my local system which has MS Office installed. But it does not work at my server which does not have MS office installed.
It shows me:
No application is associated with the specified file for this operation - at System.Diagnostics.Process.StartWithShellExecuteEx(ProcessStartInfo startInfo)
Please give me your suggestions regarding this.
In order to print textual files [which is what it seems you try to do], you can use notepad's command line arguments [read more here]
The following will send the document to the default printer:
System.Diagnostics.Process printProcess = new System.Diagnostics.Process();
printProcess.StartInfo.FileName = "notepad.exe";
printProcess.StartInfo.Parameters = "/P D:\\testWordPad1.txt";
printProcess.StartInfo.CreateNoWindow = true;
printProcess.Start();
printProcess.WaitForExit();
If you want to define the printer to which the file will be sent, use the following parameter:
/PT [filename] [printername] [driverdll] [port]
The Error means, that the Verb is not available.
First you can check the available Verbs like this:
foreach (String verb in printProcess.StartInfo.Verbs)
{
System.Diagnostics.Debug.WriteLine(verb);
}
Are you sure that there any printing facilities available on your Server?
If you do a right click on that file on your server, does it show "Print" in the context menu?
To print .docx files on web server without MS Office installed, a third party library might be the best choice. I tried a free component (nuget package) with 100 passages and 5 tables limits for small project and it works.
using System;
using System.Collections.Generic;
using System.Text;
using Spire.Doc;
using System.Windows.Forms;
using System.Drawing.Printing;
namespace Doc_Print
{
class Program
{
static void Main(string[] args)
{
Document doc = new Document();
doc.LoadFromFile("sample.doc");
PrintDialog dialog = new PrintDialog();
dialog.AllowPrintToFile = true;
dialog.AllowCurrentPage = true;
dialog.AllowSomePages = true;
dialog.UseEXDialog = true;
doc.PrintDialog = dialog;
PrintDocument printDoc = doc.PrintDocument;
printDoc.Print();
if (dialog.ShowDialog() == DialogResult.OK)
{
printDoc.Print();
}
}
}
}

How to insert files into folders of an existing PDF portfolio with folders using iTextsharp

Using the answers in How to merge PDFs into a PDF Portfolio?, I've been able to create an PDF portfolio using iTextSharp. However, using Adobe Acrobat I'm able to create folders, and I'm able to put files in those folders.
How do I create folders and how do I put files in those folders in a PDF portfolio using iTextSharp?
I've tried using a pdf inspector program to see the differences between a portfolio with and without folders, but I haven't been able to see any. I guess I'm looking in the wrong places
EDIT
For this specific use case of mine it is actually possible to create the PDF portfolio with folder up front. So it's way more important to be able to insert files in folders in an existing PDF portfolio as opposed to actually creating the folders themselves.
Here is some proof of concept code that creates a new portfolio pdf with couple of folders and inserts an existing pdf file into each of those folders. This approach is based on using a text editor to look at a pdf file created using the sample code linked to in the original post, then using Acrobat to create a folder and move the embedded file into that folder, saving the pdf, then looking at the changes with a text editor. In the code I'm recreating the changes found while comparing the two versions of the portfolio pdf, so while it works (at least on my machine), it may not be the best way to accomplish the task.
If you want to view the before/after files like I did, create the portfolio using iTextsharp, then open with Acrobat and create a folder and move the embedded file(s) into the folder, then just save the file again using the save icon on the toolbar. Do not use the File -> Save As... option to save the file as a Portfolio PDF. Acrobat reorganizes the file and compresses or in some other way converts a lot of the file to binary data that you won't be able read in a text editor. I found that deleting the binary stream data made the structure much easier to follow. Just get rid of everything between each pair of stream/endstream keywords.
One thing that Acrobat does in portfolio pdfs is embed a flash file that provides animation and a more attractive theme for the portfolio. I wasn't able to figure out how to do that, so the resulting file from this code is a bit plain looking.
using System;
using System.IO;
using System.Linq;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.pdf.collection;
public class FolderWriter {
private const string Folder = #"C:\Path\to\your\pdf\files";
private const string File1 = #"Pdf File 1.pdf";
private const string File2 = #"Pdf File 2.pdf";
private readonly string file1Path = Path.Combine(Folder, File1);
private readonly string file2Path = Path.Combine(Folder, File2);
private readonly string[] keys = new[] {
"Type",
"File"
};
public void Write(Stream stream) {
using (Document document = new Document()) {
PdfWriter writer = PdfWriter.GetInstance(document, stream);
document.Open();
document.Add(new Paragraph("This document contains a collection of PDFs"));
PdfIndirectReference parentFolderObjectReference = writer.PdfIndirectReference;
PdfIndirectReference childFolder1ObjectReference = writer.PdfIndirectReference;
PdfIndirectReference childFolder2ObjectReference = writer.PdfIndirectReference;
PdfDictionary parentFolderObject = GetFolderDictionary(0);
parentFolderObject.Put(new PdfName("Child"), childFolder1ObjectReference);
parentFolderObject.Put(PdfName.NAME, new PdfString());
PdfDictionary childFolder1Object = GetFolderDictionary(1);
childFolder1Object.Put(PdfName.NAME, new PdfString("Folder 1"));
childFolder1Object.Put(PdfName.PARENT, parentFolderObjectReference);
childFolder1Object.Put(PdfName.NEXT, childFolder2ObjectReference);
PdfDictionary childFolder2Object = GetFolderDictionary(2);
childFolder2Object.Put(PdfName.NAME, new PdfString("Folder 2"));
childFolder2Object.Put(PdfName.PARENT, parentFolderObjectReference);
PdfCollection collection = new PdfCollection(PdfCollection.DETAILS);
PdfCollectionSchema schema = CollectionSchema();
collection.Schema = schema;
collection.Sort = new PdfCollectionSort(keys);
collection.Put(new PdfName("Folders"), parentFolderObjectReference);
writer.Collection = collection;
PdfFileSpecification fs;
PdfCollectionItem item;
fs = PdfFileSpecification.FileEmbedded(writer, file1Path, File1, null);
item = new PdfCollectionItem(schema);
item.AddItem("Type", "pdf");
fs.AddCollectionItem(item);
// the description is apparently used to place the
// file in a particular folder. The number between the < and >
// is used to put the file in the folder that has the matching id
fs.AddDescription(GetDescription(1, File1), false);
writer.AddFileAttachment(fs);
fs = PdfFileSpecification.FileEmbedded(writer, file2Path, File2, null);
item = new PdfCollectionItem(schema);
item.AddItem("Type", "pdf");
fs.AddCollectionItem(item);
fs.AddDescription(GetDescription(2, File2), false);
writer.AddFileAttachment(fs);
writer.AddToBody(parentFolderObject, parentFolderObjectReference);
writer.AddToBody(childFolder1Object, childFolder1ObjectReference);
writer.AddToBody(childFolder2Object, childFolder2ObjectReference);
document.Close();
}
}
private static string GetDescription(int id, string fileName) {
return string.Format("<{0}>{1}", id, fileName);
}
private static PdfDictionary GetFolderDictionary(int id) {
PdfDictionary dic = new PdfDictionary(new PdfName("Folder"));
dic.Put(PdfName.CREATIONDATE, new PdfDate(DateTime.Now));
dic.Put(PdfName.MODDATE, new PdfDate(DateTime.Now));
dic.Put(PdfName.ID, new PdfNumber(id));
return dic;
}
private static PdfCollectionSchema CollectionSchema() {
PdfCollectionSchema schema = new PdfCollectionSchema();
PdfCollectionField type = new PdfCollectionField("File type", PdfCollectionField.TEXT);
type.Order = 0;
schema.AddField("Type", type);
PdfCollectionField filename = new PdfCollectionField("File", PdfCollectionField.FILENAME);
filename.Order = 1;
schema.AddField("File", filename);
return schema;
}
}
iTextsharp can't create folders, but you can create and find PDF bookmark.
See:
http://www.mikesdotnetting.com/Article/84/iTextSharp-Links-and-Bookmarks
http://www.geek-tutorials.com/java/itext/itext_bookmark_anchor.php

Categories

Resources