Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
This Gist contains examples of Aspose.Pdf for .Net
This gist exceeds the recommended number of files (~10). To access all files, please clone this gist.
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_Annotations();
// Open document
Document pdfDocument = new Document(dataDir + "AddAnnotation.pdf");
// Create annotation
TextAnnotation textAnnotation = new TextAnnotation(pdfDocument.Pages[1], new Aspose.Pdf.Rectangle(200, 400, 400, 600));
textAnnotation.Title = "Sample Annotation Title";
textAnnotation.Subject = "Sample Subject";
textAnnotation.State = AnnotationState.Accepted;
textAnnotation.Contents = "Sample contents for the annotation";
textAnnotation.Open = true;
textAnnotation.Icon = TextIcon.Key;
Border border = new Border(textAnnotation);
border.Width = 5;
border.Dash = new Dash(1, 1);
textAnnotation.Border = border;
textAnnotation.Rect = new Aspose.Pdf.Rectangle(200, 400, 400, 600);
// Add annotation in the annotations collection of the page
pdfDocument.Pages[1].Annotations.Add(textAnnotation);
dataDir = dataDir + "AddAnnotation_out.pdf";
// Save output file
pdfDocument.Save(dataDir);
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_Annotations();
Document doc = new Document();
Page pdfPage = doc.Pages.Add();
System.Drawing.Rectangle drect = new System.Drawing.Rectangle();
drect.Height = (int)pdfPage.Rect.Height;
drect.Width = (int)pdfPage.Rect.Width;
drect.X = 0;
drect.Y = 0;
Aspose.Pdf.Rectangle arect = Aspose.Pdf.Rectangle.FromRect(drect);
IList<Point[]> inkList = new List<Point[]>();
Aspose.Pdf.Point[] arrpt = new Aspose.Pdf.Point[3];
inkList.Add(arrpt);
arrpt[0] = new Aspose.Pdf.Point(100, 800);
arrpt[1] = new Aspose.Pdf.Point(200, 800);
arrpt[2] = new Aspose.Pdf.Point(200, 700);
InkAnnotation ia = new InkAnnotation(pdfPage, arect, inkList);
ia.Title = "XXX";
ia.Color = Aspose.Pdf.Color.LightBlue; // (GetColorFromString(stroke.InkColor));
ia.CapStyle = CapStyle.Rounded;
Border border = new Border(ia);
border.Width = 25;
ia.Opacity = 0.5;
pdfPage.Annotations.Add(ia);
dataDir = dataDir + "AddlnkAnnotation_out.pdf";
// Save output file
doc.Save(dataDir);
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_Annotations();
// Open the PDF document
Document doc = new Document(dataDir + "AddSwfFileAsAnnotation.pdf");
// Get reference of the page to which you need to add the annotation
Page page = doc.Pages[1];
// Create ScreenAnnotation object with .swf multimedia file as an argument
ScreenAnnotation annotation = new ScreenAnnotation(page, new Aspose.Pdf.Rectangle(0, 400, 600, 700), dataDir + "input.swf");
// Add the annotation to annotations collection of page
page.Annotations.Add(annotation);
dataDir = dataDir + "AddSwfFileAsAnnotation_out.pdf";
// Save the update PDF document with annotation
doc.Save(dataDir);
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_Annotations();
// Open document
Document pdfDocument = new Document(dataDir + "DeleteAllAnnotationsFromPage.pdf");
// Delete particular annotation
pdfDocument.Pages[1].Annotations.Delete();
dataDir = dataDir + "DeleteAllAnnotationsFromPage_out.pdf";
// Save updated document
pdfDocument.Save(dataDir);
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_Annotations();
// Open document
Document pdfDocument = new Document(dataDir + "DeleteParticularAnnotation.pdf");
// Delete particular annotation
pdfDocument.Pages[1].Annotations.Delete(1);
dataDir = dataDir + "DeleteParticularAnnotation_out.pdf";
// Save updated document
pdfDocument.Save(dataDir);
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_Annotations();
Document doc = new Document(dataDir + "ExtractHighlightedText.pdf");
// Loop through all the annotations
foreach (Annotation annotation in doc.Pages[1].Annotations)
{
// Filter TextMarkupAnnotation
if (annotation is TextMarkupAnnotation)
{
TextMarkupAnnotation highlightedAnnotation = annotation as TextMarkupAnnotation;
// Retrieve highlighted text fragments
TextFragmentCollection collection = highlightedAnnotation.GetMarkedTextFragments();
foreach (TextFragment tf in collection)
{
// Display highlighted text
Console.WriteLine(tf.Text);
}
}
}
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_Annotations();
// Open document
Document pdfDocument = new Document(dataDir + "GetAllAnnotationsFromPage.pdf");
// Loop through all the annotations
foreach (MarkupAnnotation annotation in pdfDocument.Pages[1].Annotations)
{
// Get annotation properties
Console.WriteLine("Title : {0} ", annotation.Title);
Console.WriteLine("Subject : {0} ", annotation.Subject);
Console.WriteLine("Contents : {0} ", annotation.Contents);
}
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_Annotations();
// Open document
Document pdfDocument = new Document(dataDir + "GetParticularAnnotation.pdf");
// Get particular annotation
TextAnnotation textAnnotation = (TextAnnotation)pdfDocument.Pages[1].Annotations[1];
// Get annotation properties
Console.WriteLine("Title : {0} ", textAnnotation.Title);
Console.WriteLine("Subject : {0} ", textAnnotation.Subject);
Console.WriteLine("Contents : {0} ", textAnnotation.Contents);
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_Annotations();
// Open document
Document doc = new Document(dataDir + "input.pdf");
FreeTextAnnotation annotation = new FreeTextAnnotation(doc.Pages[1], new Aspose.Pdf.Rectangle(50, 600, 250, 650), new DefaultAppearance("Helvetica", 16, System.Drawing.Color.Red));
annotation.Contents = "ABCDEFG";
annotation.Characteristics.Border = System.Drawing.Color.Red;
annotation.Flags = AnnotationFlags.Print | AnnotationFlags.NoView;
doc.Pages[1].Annotations.Add(annotation);
dataDir = dataDir + "InvisibleAnnotation_out.pdf";
// Save output file
doc.Save(dataDir);
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_Annotations();
Document doc = new Document();
doc.Pages.Add();
IList<Point[]> inkList = new List<Point[]>();
LineInfo lineInfo = new LineInfo();
lineInfo.VerticeCoordinate = new float[] { 55, 55, 70, 70, 70, 90, 150, 60 };
lineInfo.Visibility = true;
lineInfo.LineColor = System.Drawing.Color.Red;
lineInfo.LineWidth = 2;
int length = lineInfo.VerticeCoordinate.Length / 2;
Aspose.Pdf.Point[] gesture = new Aspose.Pdf.Point[length];
for (int i = 0; i < length; i++)
{
gesture[i] = new Aspose.Pdf.Point(lineInfo.VerticeCoordinate[2 * i], lineInfo.VerticeCoordinate[2 * i + 1]);
}
inkList.Add(gesture);
InkAnnotation a1 = new InkAnnotation(doc.Pages[1], new Aspose.Pdf.Rectangle(100, 100, 300, 300), inkList);
a1.Subject = "Test";
a1.Title = "Title";
a1.Color = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Green);
Border border = new Border(a1);
border.Width = 3;
border.Effect = BorderEffect.Cloudy;
border.Dash = new Dash(1, 1);
border.Style = BorderStyle.Solid;
doc.Pages[1].Annotations.Add(a1);
dataDir = dataDir + "lnkAnnotationLineWidth_out.pdf";
// Save output file
doc.Save(dataDir);
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_Annotations();
Aspose.Pdf.Facades.PdfAnnotationEditor editor = new Aspose.Pdf.Facades.PdfAnnotationEditor();
// Redact certain page region
editor.RedactArea(1, new Aspose.Pdf.Rectangle(100, 100, 20, 70), System.Drawing.Color.White);
editor.BindPdf(dataDir + "input.pdf");
editor.Save( dataDir + "FacadesApproach_out.pdf");
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_Annotations();
// Open document
Document doc = new Document(dataDir + "input.pdf");
// Create RedactionAnnotation instance for specific page region
RedactionAnnotation annot = new RedactionAnnotation(doc.Pages[1], new Aspose.Pdf.Rectangle(200, 500, 300, 600));
annot.FillColor = Aspose.Pdf.Color.Green;
annot.BorderColor = Aspose.Pdf.Color.Yellow;
annot.Color = Aspose.Pdf.Color.Blue;
// Text to be printed on redact annotation
annot.OverlayText = "REDACTED";
annot.TextAlignment = Aspose.Pdf.HorizontalAlignment.Center;
// Repat Overlay text over redact Annotation
annot.Repeat = true;
// Add annotation to annotations collection of first page
doc.Pages[1].Annotations.Add(annot);
// Flattens annotation and redacts page contents (i.e. removes text and image
// Under redacted annotation)
annot.Redact();
dataDir = dataDir + "RedactPage_out.pdf";
doc.Save(dataDir);
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_Annotations();
// Open document
Document pdfDocument = new Document(dataDir + "SetFreeTextAnnotationFormatting.pdf");
// Instantiate DefaultAppearance object
DefaultAppearance default_appearance = new DefaultAppearance("Arial", 28, System.Drawing.Color.Red);
// Create annotation
FreeTextAnnotation freetext = new FreeTextAnnotation(pdfDocument.Pages[1], new Aspose.Pdf.Rectangle(200, 400, 400, 600), default_appearance);
// Specify the contents of annotation
freetext.Contents = "Free Text";
// Add anootation to annotations collection of page
pdfDocument.Pages[1].Annotations.Add(freetext);
dataDir = dataDir + "SetFreeTextAnnotationFormatting_out.pdf";
// Save the updated document
pdfDocument.Save(dataDir);
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_Annotations();
// Open document
Document document = new Document(dataDir + "input.pdf");
// Create TextFragment Absorber instance to search particular text fragment
Aspose.Pdf.Text.TextFragmentAbsorber textFragmentAbsorber = new Aspose.Pdf.Text.TextFragmentAbsorber("Estoque");
// Iterate through pages of PDF document
for (int i = 1; i <= document.Pages.Count; i++)
{
// Get first page of PDF document
Page page = document.Pages[1];
page.Accept(textFragmentAbsorber);
}
// Create a collection of Absorbed text
Aspose.Pdf.Text.TextFragmentCollection textFragmentCollection = textFragmentAbsorber.TextFragments;
// Iterate on above collection
for (int j = 1; j <= textFragmentCollection.Count; j++)
{
Aspose.Pdf.Text.TextFragment textFragment = textFragmentCollection[j];
// Get rectangular dimensions of TextFragment object
Aspose.Pdf.Rectangle rect = new Aspose.Pdf.Rectangle(
(float)textFragment.Position.XIndent,
(float)textFragment.Position.YIndent,
(float)textFragment.Position.XIndent +
(float)textFragment.Rectangle.Width,
(float)textFragment.Position.YIndent +
(float)textFragment.Rectangle.Height);
// Instantiate StrikeOut Annotation instance
StrikeOutAnnotation strikeOut = new StrikeOutAnnotation(textFragment.Page, rect);
// Set opacity for annotation
strikeOut.Opacity = .80f;
// Set the border for annotation instance
strikeOut.Border = new Border(strikeOut);
// Set the color of annotation
strikeOut.Color = Aspose.Pdf.Color.Red;
// Add annotation to annotations collection of TextFragment
textFragment.Page.Annotations.Add(strikeOut);
}
dataDir = dataDir + "StrikeOutWords_out.pdf";
document.Save(dataDir);
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_Annotations();
// Open document
Document doc1 = new Document(dataDir + "input.pdf");
// Set font size and color of the annotation:
(doc1.Pages[1].Annotations[0] as FreeTextAnnotation).TextStyle.FontSize = 18;
(doc1.Pages[1].Annotations[0] as FreeTextAnnotation).TextStyle.Color = System.Drawing.Color.Green;
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_Attachments();
// Open document
Document pdfDocument = new Document(dataDir + "AddAttachment.pdf");
// Setup new file to be added as attachment
FileSpecification fileSpecification = new FileSpecification(dataDir + "test.txt", "Sample text file");
// Add attachment to document's attachment collection
pdfDocument.EmbeddedFiles.Add(fileSpecification);
dataDir = dataDir + "AddAttachment_out.pdf";
// Save new output
pdfDocument.Save(dataDir);
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_Attachments();
// Open document
Document pdfDocument = new Document(dataDir + "DeleteAllAttachments.pdf");
// Delete all attachments
pdfDocument.EmbeddedFiles.Delete();
dataDir = dataDir + "DeleteAllAttachments_out.pdf";
// Save updated file
pdfDocument.Save(dataDir);
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_Attachments();
Document pdfDocument = new Document(dataDir + "GetAlltheAttachments.pdf");
// Setup new file to be added as attachment
FileSpecification fileSpecification = new FileSpecification("test_out.txt", "Sample text file");
// Specify Encoding proparty setting it to FileEncoding.None
fileSpecification.Encoding = FileEncoding.None;
// Add attachment to document's attachment collection
pdfDocument.EmbeddedFiles.Add(fileSpecification);
dataDir = dataDir + "DisableFilesCompression_out.pdf";
// Save new output
pdfDocument.Save(dataDir);
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_Attachments();
// Open document
Document pdfDocument = new Document(dataDir + "GetAlltheAttachments.pdf");
// Get embedded files collection
EmbeddedFileCollection embeddedFiles = pdfDocument.EmbeddedFiles;
// Get count of the embedded files
Console.WriteLine("Total files : {0}", embeddedFiles.Count);
int count = 1;
// Loop through the collection to get all the attachments
foreach (FileSpecification fileSpecification in embeddedFiles)
{
Console.WriteLine("Name: {0}", fileSpecification.Name);
Console.WriteLine("Description: {0}",
fileSpecification.Description);
Console.WriteLine("Mime Type: {0}", fileSpecification.MIMEType);
// Check if parameter object contains the parameters
if (fileSpecification.Params != null)
{
Console.WriteLine("CheckSum: {0}",
fileSpecification.Params.CheckSum);
Console.WriteLine("Creation Date: {0}",
fileSpecification.Params.CreationDate);
Console.WriteLine("Modification Date: {0}",
fileSpecification.Params.ModDate);
Console.WriteLine("Size: {0}", fileSpecification.Params.Size);
}
// Get the attachment and write to file or stream
byte[] fileContent = new byte[fileSpecification.Contents.Length];
fileSpecification.Contents.Read(fileContent, 0,
fileContent.Length);
FileStream fileStream = new FileStream(dataDir + count + "_out" + ".txt",
FileMode.Create);
fileStream.Write(fileContent, 0, fileContent.Length);
fileStream.Close();
count+=1;
}
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_Attachments();
// Open document
Document pdfDocument = new Document(dataDir + "GetAttachmentInfo.pdf");
// Get particular embedded file
FileSpecification fileSpecification = pdfDocument.EmbeddedFiles[1];
// Get the file properties
Console.WriteLine("Name: {0}", fileSpecification.Name);
Console.WriteLine("Description: {0}", fileSpecification.Description);
Console.WriteLine("Mime Type: {0}", fileSpecification.MIMEType);
// Check if parameter object contains the parameters
if (fileSpecification.Params != null)
{
Console.WriteLine("CheckSum: {0}",
fileSpecification.Params.CheckSum);
Console.WriteLine("Creation Date: {0}",
fileSpecification.Params.CreationDate);
Console.WriteLine("Modification Date: {0}",
fileSpecification.Params.ModDate);
Console.WriteLine("Size: {0}", fileSpecification.Params.Size);
}
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_Attachments();
// Open document
Document pdfDocument = new Document(dataDir + "GetIndividualAttachment.pdf");
// Get particular embedded file
FileSpecification fileSpecification = pdfDocument.EmbeddedFiles[1];
// Get the file properties
Console.WriteLine("Name: {0}", fileSpecification.Name);
Console.WriteLine("Description: {0}", fileSpecification.Description);
Console.WriteLine("Mime Type: {0}", fileSpecification.MIMEType);
// Check if parameter object contains the parameters
if (fileSpecification.Params != null)
{
Console.WriteLine("CheckSum: {0}",
fileSpecification.Params.CheckSum);
Console.WriteLine("Creation Date: {0}",
fileSpecification.Params.CreationDate);
Console.WriteLine("Modification Date: {0}",
fileSpecification.Params.ModDate);
Console.WriteLine("Size: {0}", fileSpecification.Params.Size);
}
// Get the attachment and write to file or stream
byte[] fileContent = new byte[fileSpecification.Contents.Length];
fileSpecification.Contents.Read(fileContent, 0, fileContent.Length);
FileStream fileStream = new FileStream(dataDir + "test_out" + ".txt", FileMode.Create);
fileStream.Write(fileContent, 0, fileContent.Length);
fileStream.Close();
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_Bookmarks();
// Open document
Document pdfDocument = new Document(dataDir + "AddBookmark.pdf");
// Create a bookmark object
OutlineItemCollection pdfOutline = new OutlineItemCollection(pdfDocument.Outlines);
pdfOutline.Title = "Test Outline";
pdfOutline.Italic = true;
pdfOutline.Bold = true;
// Set the destination page number
pdfOutline.Action = new GoToAction(pdfDocument.Pages[1]);
// Add bookmark in the document's outline collection.
pdfDocument.Outlines.Add(pdfOutline);
dataDir = dataDir + "AddBookmark_out.pdf";
// Save output
pdfDocument.Save(dataDir);
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_Bookmarks();
// Open document
Document pdfDocument = new Document(dataDir + "AddChildBookmark.pdf");
// Create a parent bookmark object
OutlineItemCollection pdfOutline = new OutlineItemCollection(pdfDocument.Outlines);
pdfOutline.Title = "Parent Outline";
pdfOutline.Italic = true;
pdfOutline.Bold = true;
// Create a child bookmark object
OutlineItemCollection pdfChildOutline = new OutlineItemCollection(pdfDocument.Outlines);
pdfChildOutline.Title = "Child Outline";
pdfChildOutline.Italic = true;
pdfChildOutline.Bold = true;
// Add child bookmark in parent bookmark's collection
pdfOutline.Add(pdfChildOutline);
// Add parent bookmark in the document's outline collection.
pdfDocument.Outlines.Add(pdfOutline);
dataDir = dataDir + "AddChildBookmark_out.pdf";
// Save output
pdfDocument.Save(dataDir);
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_Bookmarks();
// Open document
Document pdfDocument = new Document(dataDir + "DeleteAllBookmarks.pdf");
// Delete all bookmarks
pdfDocument.Outlines.Delete();
dataDir = dataDir + "DeleteAllBookmarks_out.pdf";
// Save updated file
pdfDocument.Save(dataDir);
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_Bookmarks();
// Open document
Document pdfDocument = new Document(dataDir + "DeleteParticularBookmark.pdf");
// Delete particular outline by Title
pdfDocument.Outlines.Delete("Child Outline");
dataDir = dataDir + "DeleteParticularBookmark_out.pdf";
// Save updated file
pdfDocument.Save(dataDir);
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_Bookmarks();
// Open document
Document doc = new Document(dataDir + "input.pdf");
// Set page view mode i.e. show thumbnails, full-screen, show attachment panel
doc.PageMode = PageMode.UseOutlines;
// Traverse through each Ouline item in outlines collection of PDF file
foreach (OutlineItemCollection item in doc.Outlines)
{
// Set open status for outline item
item.Open = true;
}
dataDir = dataDir + "ExpandBookmarks_out.pdf";
// Save output
doc.Save(dataDir);
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_Bookmarks();
// Create PdfBookmarkEditor
PdfBookmarkEditor bookmarkEditor = new PdfBookmarkEditor();
// Open PDF file
bookmarkEditor.BindPdf(dataDir + "GetBookmarks.pdf");
// Extract bookmarks
Aspose.Pdf.Facades.Bookmarks bookmarks = bookmarkEditor.ExtractBookmarks();
foreach (Aspose.Pdf.Facades.Bookmark bookmark in bookmarks)
{
string strLevelSeprator = string.Empty;
for (int i = 1; i < bookmark.Level; i++)
{
strLevelSeprator += "----";
}
Console.WriteLine("{0}Title: {1}", strLevelSeprator, bookmark.Title);
Console.WriteLine("{0}Page Number: {1}", strLevelSeprator, bookmark.PageNumber);
Console.WriteLine("{0}Page Action: {1}", strLevelSeprator, bookmark.Action);
}
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_Bookmarks();
// Open document
Document pdfDocument = new Document(dataDir + "GetBookmarks.pdf");
// Loop through all the bookmarks
foreach (OutlineItemCollection outlineItem in pdfDocument.Outlines)
{
Console.WriteLine(outlineItem.Title);
Console.WriteLine(outlineItem.Italic);
Console.WriteLine(outlineItem.Bold);
Console.WriteLine(outlineItem.Color);
}
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_Bookmarks();
// Open document
Document pdfDocument = new Document(dataDir + "GetChildBookmarks.pdf");
// Loop through all the bookmarks
foreach (OutlineItemCollection outlineItem in pdfDocument.Outlines)
{
Console.WriteLine(outlineItem.Title);
Console.WriteLine(outlineItem.Italic);
Console.WriteLine(outlineItem.Bold);
Console.WriteLine(outlineItem.Color);
if (outlineItem.Count > 0)
{
Console.WriteLine("Child Bookmarks");
// There are child bookmarks then loop through that as well
foreach (OutlineItemCollection childOutline in outlineItem)
{
Console.WriteLine(childOutline.Title);
Console.WriteLine(childOutline.Italic);
Console.WriteLine(childOutline.Bold);
Console.WriteLine(childOutline.Color);
}
}
}
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_Bookmarks();
// Open document
Document doc = new Document(dataDir + "input.pdf");
// Get outlines/bookmarks collection of PDF file
OutlineItemCollection item = new OutlineItemCollection(doc.Outlines);
// Set zoom level as 0
XYZExplicitDestination dest = new XYZExplicitDestination(2, 100, 100, 0);
// Add XYZExplicitDestination as action to outlines collection of PDF
item.Action = new GoToAction(dest);
// Add item to outlines collection of PDF file
doc.Outlines.Add(item);
dataDir = dataDir + "InheritZoom_out.pdf";
// Save output
doc.Save(dataDir);
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_Bookmarks();
// Open document
Document pdfDocument = new Document(dataDir + "UpdateBookmarks.pdf");
// Get a bookmark object
OutlineItemCollection pdfOutline = pdfDocument.Outlines[1];
pdfOutline.Title = "Updated Outline";
pdfOutline.Italic = true;
pdfOutline.Bold = true;
dataDir = dataDir + "UpdateBookmarks_out.pdf";
// Save output
pdfDocument.Save(dataDir);
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_Bookmarks();
// Open document
Document pdfDocument = new Document(dataDir + "UpdateChildBookmarks.pdf");
// Get a bookmark object
OutlineItemCollection pdfOutline = pdfDocument.Outlines[1];
// Get child bookmark object
OutlineItemCollection childOutline = pdfOutline[1];
childOutline.Title = "Updated Outline";
childOutline.Italic = true;
childOutline.Bold = true;
dataDir = dataDir + "UpdateChildBookmarks_out.pdf";
// Save output
pdfDocument.Save(dataDir);
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_DocumentConversion();
// Instantiate Document instance to load existing file
Aspose.Pdf.Document doc = new Document(dataDir + "input.pdf");
// Setup new file to be added as attachment
FileSpecification fileSpecification = new FileSpecification(dataDir + "aspose-logo.jpg", "Large Image file");
// Add attachment to document's attachment collection
doc.EmbeddedFiles.Add(fileSpecification);
// Perform conversion to PDF/A_3a so attachment is included in resultnat file
doc.Convert(dataDir + "log.txt", Aspose.Pdf.PdfFormat.PDF_A_3A, ConvertErrorAction.Delete);
// Save resultant file
doc.Save(dataDir + "AddAttachmentToPDFA_out.pdf");
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_DocumentConversion();
// Instantiate LoadOption object using CGMLoadOption
Aspose.Pdf.CgmLoadOptions cgmload = new Aspose.Pdf.CgmLoadOptions();
// Instantiate Document object
Document doc = new Document(dataDir + "CGMToPDF.CGM", cgmload);
// Save the resultant PDF document
doc.Save(dataDir+ "TECHDRAW_out.pdf");
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_DocumentConversion();
// Instantiate LoadOption object using EPUB load option
EpubLoadOptions epubload = new EpubLoadOptions();
// Create Document object
Aspose.Pdf.Document pdf = new Aspose.Pdf.Document(dataDir + "EPUBToPDF.epub", epubload);
// Save the resultant PDF document
pdf.Save(dataDir + "EPUBToPDF_out.pdf");
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_DocumentConversion();
var loadopt = new SvgLoadOptions();
loadopt.AdjustPageSize = true;
var svgDoc = new Document(dataDir + "GetSVGDimensions.svg", loadopt);
svgDoc.Pages[1].PageInfo.Margin.Top = 0;
svgDoc.Pages[1].PageInfo.Margin.Left = 0;
svgDoc.Pages[1].PageInfo.Margin.Bottom = 0;
svgDoc.Pages[1].PageInfo.Margin.Right = 0;
svgDoc.Save(dataDir + "GetSVGDimensions_out.pdf");
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_DocumentConversion();
HtmlLoadOptions options = new HtmlLoadOptions();
options.CustomLoaderOfExternalResources = new LoadOptions.ResourceLoadingStrategy(SamePictureLoader);
Document pdfDocument = new Document(dataDir + "HTMLToPDF.html", options);
pdfDocument.Save("HTMLToPDF_out.pdf");
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static LoadOptions.ResourceLoadingResult SamePictureLoader(string resourceURI)
{
string dataDir = RunExamples.GetDataDir_AsposePdf_DocumentConversion();
byte[] resultBytes = File.ReadAllBytes(dataDir + "aspose-logo.jpg");
LoadOptions.ResourceLoadingResult result = new LoadOptions.ResourceLoadingResult(resultBytes);
return result;
}
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_DocumentConversion();
MhtLoadOptions options = new MhtLoadOptions();
// Load document
Document document = new Document(dataDir + "test.mht", options);
// Save the output as PDF document
document.Save(dataDir + "MHTToPDF_out.pdf");
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_DocumentConversion();
Aspose.Pdf.Document doc = new Aspose.Pdf.Document();
// Retrieve names of all the JPG files in a particular Directory
string[] fileEntries = Directory.GetFiles(dataDir, "*.JPG");
int counter;
for (counter = 0; counter < fileEntries.Length - 1; counter++)
{
// Create a page object
Aspose.Pdf.Page page = doc.Pages.Add();
// Creat an image object
Aspose.Pdf.Image image1 = new Aspose.Pdf.Image();
image1.File = fileEntries[counter];
// Create a BitMap object in order to get the information of image file
Bitmap myimage = new Bitmap(fileEntries[counter]);
// Check if the width of the image file is greater than Page width or not
if (myimage.Width > page.PageInfo.Width)
// If the Image width is greater than page width, then set the page orientation to Landscape
page.PageInfo.IsLandscape = true;
else
// If the Image width is less than page width, then set the page orientation to Portrait
page.PageInfo.IsLandscape = false;
// Add the image to paragraphs collection of the PDF document
page.Paragraphs.Add(image1);
}
// Save the Pdf file
doc.Save(dataDir + "SetPageOrientation_out.pdf");
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_DocumentConversion();
using (FileStream fileStream = new FileStream(dataDir + "sample.pcl", FileMode.Open))
using (MemoryStream memoryStream = new MemoryStream())
{
fileStream.CopyTo(memoryStream);
memoryStream.Seek(0, SeekOrigin.Begin);
using (Document document = new Document(memoryStream, new PclLoadOptions()))
{
}
}
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_DocumentConversion();
// Instantiate LoadOption object using PCL load option
Aspose.Pdf.LoadOptions loadopt = new Aspose.Pdf.PclLoadOptions();
// Create Document object
Aspose.Pdf.Document doc = new Aspose.Pdf.Document(dataDir + "hidetext.pcl", loadopt);
// Save the resultant PDF document
doc.Save(dataDir + "PCLToPDF_out.pdf");
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_DocumentConversion();
// Open document
Document doc = new Document(dataDir + "PDFAToPDF.pdf");
// Remove PDF/A compliance information
doc.RemovePdfaCompliance();
// Save updated document
doc.Save(dataDir + "PDFAToPDF_out.pdf");
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_DocumentConversion();
// Open the source PDF document
Document pdfDocument = new Document(dataDir + "PDFToDOC.pdf");
// Instantiate DocSaveOptions object
DocSaveOptions saveOptions = new DocSaveOptions();
// Specify the output format as DOCX
saveOptions.Format = DocSaveOptions.DocFormat.DocX;
// Save document in docx format
pdfDocument.Save("ConvertToDOCX_out.docx", saveOptions);
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_DocumentConversion();
// Open the source PDF document
Document pdfDocument = new Document(dataDir + "PDFToDOC.pdf");
// Save the file into MS document format
pdfDocument.Save(dataDir + "PDFToDOC_out.doc", SaveFormat.Doc);
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_DocumentConversion();
// Open the source PDF document
Document pdfDocument = new Document(dataDir + "PDFToDOC.pdf");
// Save using save options
// Create DocSaveOptions object
DocSaveOptions saveOptions = new DocSaveOptions();
// Set the recognition mode as Flow
saveOptions.Mode = DocSaveOptions.RecognitionMode.Flow;
// Set the Horizontal proximity as 2.5
saveOptions.RelativeHorizontalProximity = 2.5f;
// Enable the value to recognize bullets during conversion process
saveOptions.RecognizeBullets = true;
// Save the resultant DOC file
pdfDocument.Save(dataDir + "saveOptionsOutput_out.doc", saveOptions);
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_DocumentConversion();
// Load PDF document
Document pdfDocument = new Document(dataDir + "PDFToEPUB.pdf");
// Instantiate Epub Save options
EpubSaveOptions options = new EpubSaveOptions();
// Specify the layout for contents
options.ContentRecognitionMode = EpubSaveOptions.RecognitionMode.Flow;
// Save the ePUB document
pdfDocument.Save(dataDir + "PDFToEPUB_out.epub", options);
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// Create HtmlSaveOption with tested feature
HtmlSaveOptions newOptions = new HtmlSaveOptions();
// Compress the SVG images if there are any
newOptions.CompressSvgGraphicsIfAny = true;
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_DocumentConversion();
Document doc = new Document(dataDir + "PDFToHTML.pdf");
HtmlSaveOptions options = new HtmlSaveOptions();
// This is the tested setting
options.HtmlMarkupGenerationMode = HtmlSaveOptions.HtmlMarkupGenerationModes.WriteOnlyBodyContent;
options.SplitIntoPages = true;
doc.Save(dataDir + "CreateSubsequentFiles_out.html", options);
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_DocumentConversion();
Document doc = new Document(dataDir + "PDFToHTML.pdf");
// Instantiate HTML SaveOptions object
HtmlSaveOptions htmlOptions = new HtmlSaveOptions();
// Specify to render PDF document layers separately in output HTML
htmlOptions.ConvertMarkedContentToLayers = true;
// Save the document
doc.Save(dataDir + "LayersRendering_out.html", htmlOptions);
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_DocumentConversion();
// Open the source PDF document
Document pdfDocument = new Document(dataDir + "PDFToHTML.pdf");
// Instantiate HTML SaveOptions object
HtmlSaveOptions htmlOptions = new HtmlSaveOptions();
// Specify to split the output into multiple pages
htmlOptions.SplitIntoPages = true;
// Save the document
pdfDocument.Save(@"MultiPageHTML_out.html", htmlOptions);
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_DocumentConversion();
// Open the source PDF document
Document pdfDocument = new Document(dataDir + "PDFToHTML.pdf");
// Save the file into MS document format
pdfDocument.Save(dataDir + "output_out.html", SaveFormat.Html);
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_DocumentConversion();
// Load the PDF file
Document doc = new Document(dataDir + "PDFToHTML.pdf");
// Instantiate HTML save options object
HtmlSaveOptions newOptions = new HtmlSaveOptions();
// Specify the folder where SVG images are saved during PDF to HTML conversion
newOptions.SpecialFolderForSvgImages = dataDir;
// Save the output file
doc.Save(dataDir + "SaveSVGFiles_out.html", newOptions);
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// Create HtmlSaveOption with tested feature
HtmlSaveOptions newOptions = new HtmlSaveOptions();
// Specify the separate folder to save images
newOptions.SpecialFolderForAllImages = dataDir;
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_DocumentConversion();
Document doc = new Document(dataDir + "PDFToHTML.pdf");
HtmlSaveOptions htmlOptions = new HtmlSaveOptions();
htmlOptions.SaveShadowedTextsAsTransparentTexts = true;
htmlOptions.SaveTransparentTexts = true;
doc.Save(dataDir + "TransparentTextRendering_out.html", htmlOptions);
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_DocumentConversion_PDFToHTMLFormat();
Document pdfDocument = new Document(dataDir + "input.pdf");
// Create HtmlSaveOption with tested feature
HtmlSaveOptions saveOptions = new HtmlSaveOptions();
saveOptions.FixedLayout = true;
saveOptions.RasterImagesSavingMode = HtmlSaveOptions.RasterImagesSavingModes.AsEmbeddedPartsOfPngPageBackground;
pdfDocument.Save(dataDir + "AvoidSavingImages_out.html", saveOptions);
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_DocumentConversion_PDFToHTMLFormat();
Document doc = new Document( dataDir + "input.pdf");
// Tune conversion params
HtmlSaveOptions newOptions = new HtmlSaveOptions();
newOptions.RasterImagesSavingMode = HtmlSaveOptions.RasterImagesSavingModes.AsEmbeddedPartsOfPngPageBackground;
newOptions.FontSavingMode = HtmlSaveOptions.FontSavingModes.SaveInAllFormats;
newOptions.PartsEmbeddingMode = HtmlSaveOptions.PartsEmbeddingModes.EmbedAllIntoHtml;
newOptions.LettersPositioningMethod = HtmlSaveOptions.LettersPositioningMethods.UseEmUnitsAndCompensationOfRoundingErrorsInCss;
newOptions.SplitIntoPages = false;// Force write HTMLs of all pages into one output document
newOptions.CustomHtmlSavingStrategy = new HtmlSaveOptions.HtmlPageMarkupSavingStrategy(SavingToStream);
// We can use some non-existing puth as result file name - all real saving will be done
// In our custom method SavingToStream() (it's follows this one)
doc.Save(dataDir + "OutPutToStream_out.html", newOptions);
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
static string _folderForReferencedResources_34748;
public static void PDFNEWNET_34748()
{
//-----------------------------------------------------
// 1) Tune paths and set license
//-----------------------------------------------------
(new Aspose.Pdf.License()).SetLicense(@"F:\_Sources\Aspose_5\trunk\testdata\License\Aspose.Total.lic");
Document pdfDocument = new Document(@"F:\ExternalTestsData\34748_36189.pdf");
string outHtmlFile = @"F:\ExternalTestsData\34748.html";
_folderForReferencedResources_34748 = @"F:\ExternalTestsData\out_34748\";
//-----------------------------------------------------
// 2) Clean results if they already present
//-----------------------------------------------------
if (Directory.Exists(_folderForReferencedResources_34748))
{
Directory.Delete(_folderForReferencedResources_34748, true);
}
File.Delete(outHtmlFile);
//-----------------------------------------------------
// Create HtmlSaveOption with tested feature
//-----------------------------------------------------
HtmlSaveOptions saveOptions = new HtmlSaveOptions();
saveOptions.CustomResourceSavingStrategy = new HtmlSaveOptions.ResourceSavingStrategy(Strategy_11_CUSTOM_SAVE_OF_FONTS_AND_IMAGES);
saveOptions.CustomCssSavingStrategy = new HtmlSaveOptions.CssSavingStrategy(Strategy_11_CSS_WriteCssToPredefinedFolder);
saveOptions.CustomStrategyOfCssUrlCreation = new HtmlSaveOptions.CssUrlMakingStrategy(Strategy_11_CSS_ReturnResultPathInPredefinedTestFolder);
using (Stream outStream = File.OpenWrite(outHtmlFile))
{
pdfDocument.Save(outStream, saveOptions);
}
}
private static void Strategy_11_CSS_WriteCssToPredefinedFolder(HtmlSaveOptions.CssSavingInfo resourceInfo)
{
if (!Directory.Exists(_folderForReferencedResources_34748))
{
Directory.CreateDirectory(_folderForReferencedResources_34748);
}
string path = _folderForReferencedResources_34748 + Path.GetFileName(resourceInfo.SupposedURL);
System.IO.BinaryReader reader = new BinaryReader(resourceInfo.ContentStream);
System.IO.File.WriteAllBytes(path, reader.ReadBytes((int)resourceInfo.ContentStream.Length));
}
private static string Strategy_11_CSS_ReturnResultPathInPredefinedTestFolder(HtmlSaveOptions.CssUrlRequestInfo requestInfo)
{
return "file:///" + _folderForReferencedResources_34748.Replace(@"\", "/") + "css_style{0}.css";
}
private static string Strategy_11_CUSTOM_SAVE_OF_FONTS_AND_IMAGES(SaveOptions.ResourceSavingInfo resourceSavingInfo)
{
if (!Directory.Exists(_folderForReferencedResources_34748))
{
Directory.CreateDirectory(_folderForReferencedResources_34748);
}
string path = _folderForReferencedResources_34748 + Path.GetFileName(resourceSavingInfo.SupposedFileName);
// The first path of this method is for saving fonts
System.IO.BinaryReader contentReader = new BinaryReader(resourceSavingInfo.ContentStream);
System.IO.File.WriteAllBytes(path, contentReader.ReadBytes((int)resourceSavingInfo.ContentStream.Length));
string urlThatWillBeUsedInHtml = "file:///" + _folderForReferencedResources_34748.Replace(@"\", "/") + Path.GetFileName(resourceSavingInfo.SupposedFileName);
return urlThatWillBeUsedInHtml;
}
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void SavingToStream(HtmlSaveOptions.HtmlPageMarkupSavingInfo htmlSavingInfo)
{
byte[] resultHtmlAsBytes = new byte[htmlSavingInfo.ContentStream.Length];
htmlSavingInfo.ContentStream.Read(resultHtmlAsBytes, 0, resultHtmlAsBytes.Length);
// Here You can use any writable stream, file stream is taken just as example
string fileName = "stream_out.html";
Stream outStream = File.OpenWrite(fileName);
outStream.Write(resultHtmlAsBytes, 0, resultHtmlAsBytes.Length);
}
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_DocumentConversion_PDFToHTMLFormat();
Document pdfDocument = new Document(dataDir + "input.pdf");
string outHtmlFile = dataDir + "PrefixCSSClassNames_out.html";
HtmlSaveOptions saveOptions = new HtmlSaveOptions();
saveOptions.CssClassNamesPrefix = ".gDV__ .stl_";
pdfDocument.Save(outHtmlFile, saveOptions);
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_DocumentConversion_PDFToHTMLFormat();
Document doc = new Document(dataDir + "input.pdf");
string outHtmlFile = dataDir + "PrefixForFonts_out.html";
_desiredFontDir = Path.GetDirectoryName(outHtmlFile) + @"\36296_files\";
if (!Directory.Exists(_desiredFontDir))
{
Directory.CreateDirectory(_desiredFontDir);
}
// Reset counter for font names - this counter will be used in our custom code
// To generate unigue font file names
_fontNumberForUniqueFontFileNames = 0;
// Create HtmlSaveOption with custom saving strategy that will do all the job
HtmlSaveOptions saveOptions = new HtmlSaveOptions();
saveOptions.CustomResourceSavingStrategy = new HtmlSaveOptions.ResourceSavingStrategy(CustomResourcesProcessing);
doc.Save(outHtmlFile, saveOptions);
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static string CustomResourcesProcessing(SaveOptions.ResourceSavingInfo resourceSavingInfo)
{
//-----------------------------------------------------------------------------
// It's just example of possible realization of cusstom processing of resources
// Referenced in result HTML
//-----------------------------------------------------------------------------
// 1) In this case we need only do something special
// with fonts, so let's leave processing of all other resources
// to converter itself
if (resourceSavingInfo.ResourceType != SaveOptions.NodeLevelResourceType.Font)
{
resourceSavingInfo.CustomProcessingCancelled = true;
return "";
}
// If supplied font resource, process it ourselves
// 1) Write supplied font with short name to desired folder
// You can easily do anythings - it's just one of realizations
_fontNumberForUniqueFontFileNames++;
string shortFontFileName = (_fontNumberForUniqueFontFileNames.ToString() + Path.GetExtension(resourceSavingInfo.SupposedFileName));
string outFontPath = _desiredFontDir + "\\" + shortFontFileName;
System.IO.BinaryReader fontBinaryReader = new BinaryReader(resourceSavingInfo.ContentStream);
System.IO.File.WriteAllBytes(outFontPath, fontBinaryReader.ReadBytes((int)resourceSavingInfo.ContentStream.Length));
// 2) Return the desired URI with which font will be referenced in CSSes
string fontUrl = "http:// Localhost:255/document-viewer/GetFont/" + shortFontFileName;
return fontUrl;
}
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_DocumentConversion_PDFToHTMLFormat();
Document testDoc = new Document(dataDir + "input.pdf");
HtmlSaveOptions options = new HtmlSaveOptions();
// This is main setting that allows work and testing of tested feature
options.RasterImagesSavingMode = HtmlSaveOptions.RasterImagesSavingModes.AsExternalPngFilesReferencedViaSvg;//
options.CustomResourceSavingStrategy = new HtmlSaveOptions.ResourceSavingStrategy(Custom_processor_of_embedded_images);
// Do conversion
testDoc.Save(dataDir + @"PrefixForURLs_out.html", options);
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static string Custom_processor_of_embedded_images(SaveOptions.ResourceSavingInfo resourceSavingInfo)
{
// ____________________________________________________________________________
// This sample method saving strategy method saves image-files in some folder
// (including raster image files that are exctracted from that SVGs)
// Then it returns specific custom artificial path
// to be used as value of 'src' or 'data' relevant attribute in generated host-SVG(or HTML)
// ____________________________________________________________________________
//---------------------------------------------------------
// 1) All other files(f.e. fonts) will be processed with converter itself cause for them flag
// resourceSavingInfo.CustomProcessingCancelled is set to 'true'
//---------------------------------------------------------
if (!(resourceSavingInfo is HtmlSaveOptions.HtmlImageSavingInfo))
{
resourceSavingInfo.CustomProcessingCancelled = true;
return "";
}
//---------------------------------------------------------
// 1) Create target folder if not created yet
//---------------------------------------------------------
string dataDir = RunExamples.GetDataDir_AsposePdf_DocumentConversion_PDFToHTMLFormat();
string outDir = dataDir + @"output\36297_files\";
string outPath = outDir + Path.GetFileName(resourceSavingInfo.SupposedFileName);
if (!Directory.Exists(outDir))
{
Directory.CreateDirectory(outDir);
}
//---------------------------------------------------------
// 3) Write supplied image to that folder
//---------------------------------------------------------
System.IO.BinaryReader reader = new BinaryReader(resourceSavingInfo.ContentStream);
System.IO.File.WriteAllBytes(dataDir, reader.ReadBytes((int)resourceSavingInfo.ContentStream.Length));
//---------------------------------------------------------
// 4) Return customized specific URL to be used to refer
// just created image in parent SVG (or HTML)
//---------------------------------------------------------
HtmlSaveOptions.HtmlImageSavingInfo asHtmlImageSavingInfo = resourceSavingInfo as HtmlSaveOptions.HtmlImageSavingInfo;
if (asHtmlImageSavingInfo.ParentType == HtmlSaveOptions.ImageParentTypes.SvgImage)
{
return "http:// Localhost:255/" + resourceSavingInfo.SupposedFileName;
}
else
{
return "http:// Localhost:GetImage/imageID=" + resourceSavingInfo.SupposedFileName;
}
}
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_DocumentConversion_PDFToHTMLFormat();
string linceseFile = ""; // E.g @"F:\_Sources\Aspose.Total.lic"
(new Aspose.Pdf.License()).SetLicense(linceseFile);
Document pdfDocument = new Document(dataDir + "input.pdf");
string outHtmlFile = dataDir + "PrefixToImportDirectives_out.html";
_folderForReferencedResources_36435 = dataDir;
// Create HtmlSaveOption with tested feature
HtmlSaveOptions saveOptions = new HtmlSaveOptions();
saveOptions.CustomStrategyOfCssUrlCreation = new HtmlSaveOptions.CssUrlMakingStrategy(Strategy_10_CSS_ReturnResultPathInPredefinedTestFolder);
saveOptions.CustomCssSavingStrategy = new HtmlSaveOptions.CssSavingStrategy(Strategy_10_CSS_WriteCssToResourceFolder);
//----------------------------------------------------------------------------
// Run converter
//----------------------------------------------------------------------------
pdfDocument.Save(outHtmlFile, saveOptions);
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void Strategy_10_CSS_WriteCssToResourceFolder(HtmlSaveOptions.CssSavingInfo resourceInfo)
{
// -------------------------------------------------------
// This is only one of possible implementation of saving
// You can write and use Your own implementation if You will
// -------------------------------------------------------
// Get CSS file name from requested file.
// Some trick required cause we get in parameters of this method
// Not pure file name but full URL that
// Created with usage of our template returned in Strategy_9_CSS_ReturnResultPathInPredefinedTestFolder()
// So, knowing of that template we must extract from it CSS file name itself
string guid = System.Guid.NewGuid().ToString();
string fullPathWithGuid = Strategy_10_CSS_ReturnResultPathInPredefinedTestFolder(new HtmlSaveOptions.CssUrlRequestInfo());
fullPathWithGuid = string.Format(fullPathWithGuid, guid);
int prefixLength = fullPathWithGuid.IndexOf(guid);
int suffixLength = fullPathWithGuid.Length - (fullPathWithGuid.IndexOf(guid) + guid.Length);
string fullPath = resourceInfo.SupposedURL;
fullPath = fullPath.Substring(prefixLength);
string cssFileNameCore = fullPath.Substring(0, fullPath.Length - suffixLength);
// Get final file name for saving
string cssFileName = "style" + cssFileNameCore + ".css";
string path = _folderForReferencedResources_36435 + cssFileName;
// Saving itself
System.IO.BinaryReader reader = new BinaryReader(resourceInfo.ContentStream);
System.IO.File.WriteAllBytes(path, reader.ReadBytes((int)resourceInfo.ContentStream.Length));
}
private static string Strategy_10_CSS_ReturnResultPathInPredefinedTestFolder(HtmlSaveOptions.CssUrlRequestInfo requestInfo)
{
string template = "http:// Localhost:24661/document-viewer/GetResourceForHtmlHandler?documentPath=Deutschland201207Arbeit.pdf&resourcePath=style{0}.css&fileNameOnly=false";
return template;
}
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_DocumentConversion_PDFToHTMLFormat();
string licenseFile = ""; // E.g F:\_Sources\Aspose_5\trunk\testdata\License\Aspose.Total.lic
(new Aspose.Pdf.License()).SetLicense(licenseFile);
Document doc = new Document(dataDir + "input.pdf");
HtmlSaveOptions saveOptions = new HtmlSaveOptions();
// SaveOptions.CustomProgressHandler = new HtmlSaveOptions.ConversionProgessEventHandler(ShowProgressOnConsole);
saveOptions.SplitIntoPages = false;
doc.Save(dataDir + "ProgressDetails_out_.html", saveOptions);
Console.ReadLine();
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
public static void ShowProgressOnConsole(HtmlSaveOptions.ProgressEventHandlerInfo eventInfo)
{
switch (eventInfo.EventType)
{
case HtmlSaveOptions.ProgressEventType.TotalProgress:
Console.WriteLine(String.Format("{0} - Conversion progress : {1}% .", DateTime.Now.TimeOfDay, eventInfo.Value.ToString()));
break;
case HtmlSaveOptions.ProgressEventType.SourcePageAnalized:
Console.WriteLine(String.Format("{0} - Source page {1} of {2} analyzed.", DateTime.Now.TimeOfDay, eventInfo.Value.ToString(), eventInfo.MaxValue.ToString()));
break;
case HtmlSaveOptions.ProgressEventType.ResultPageCreated:
Console.WriteLine(String.Format("{0} - Result page's {1} of {2} layout created.", DateTime.Now.TimeOfDay, eventInfo.Value.ToString(), eventInfo.MaxValue.ToString()));
break;
case HtmlSaveOptions.ProgressEventType.ResultPageSaved:
Console.WriteLine(String.Format("{0} - Result page {1} of {2} exported.", DateTime.Now.TimeOfDay, eventInfo.Value.ToString(), eventInfo.MaxValue.ToString()));
break;
default:
break;
}
}
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_DocumentConversion_PDFToHTMLFormat();
// Output HTML file path information
string outFile = Path.GetFullPath(dataDir + "36192_out.html");
// Source PDF document
Document doc = new Document(dataDir + "input.pdf");
// Create HtmlSaveOption with tested feature
HtmlSaveOptions saveOptions = new HtmlSaveOptions();
saveOptions.FixedLayout = true;
saveOptions.SplitIntoPages = false;
// Save the fonts as TTF format
saveOptions.FontSavingMode = HtmlSaveOptions.FontSavingModes.AlwaysSaveAsTTF;
string htmlFile = Path.GetFullPath(outFile);
string linkedFilesFolder = Path.GetDirectoryName(htmlFile) + @"\36192_files";
if (Directory.Exists(linkedFilesFolder))
{
Directory.Delete(linkedFilesFolder, true);
}
// Save the output
doc.Save(outFile, saveOptions);