XPS Document get trimmed on WPF DocumentViewer - c#

I have an XPS document generated in code which is having a image visual. I am assigning the same XPS Document to a WPF documentViewer control as shown below in the code snippet:
.cs File
xp = new XpsDocument(pack, CompressionOption.Fast, URIAdress);
FixedDocumentSequence fixedDocumentSequence = xp.GetFixedDocumentSequence();
documentViewer.Document = fixedDocumentSequence as IDocumentPaginatorSource;
.XAML File
<DocumentViewer Name="uxDocumentViewerWithImage" Width="Auto" />
Problem: When I luanch my client to see the document viewer, it trimmed the image of XPS document from right hand side. One solution is to reduce the size of image, but due to requirements I can not modify the image size.
Please suggest how can I make the image visible fully on the document viewer?
Update:
I tried the following solution it worked but with assigning hardcode width value:
(fixedDocumentSequence as IDocumentPaginatorSource).DocumentPaginator.GetPage(pageIndex).Visual as FixedPage).Width = widthToAdjust; // Hard Code Width value
Still looking for more generic aproach here, like moving to LandScape mode ? but do not know how :(

Related

Convert Image file to PDF document letter size using PDFTron

I am trying to convert an image file to a PDF document with a defined page size (letter size).
Currenlty I am able to convert an image to a PDF document without defining any page dimensions (default dimensions of the PDF is the image size). I would like to define the page dimensions on the creation of the document, and place the image on that page (possibly with margins).
The following code snippet shows how I am currently converting an image file to a PDF document without setting any dimensions for the page:
async static Task<bool> ConvertImageToPDF(TestFile file)
{
pdftron.PDF.PDFDoc pdfdoc = new PDFDoc(); //Initialize a new PDF document
pdftron.PDF.Convert.ToPdf(pdfdoc, file.InputFile); //Use the Convert.ToPdf to generate the file from the image file
await pdfdoc.SaveAsync(file.OutputFile, SDFDocSaveOptions.e_linearized); //Save the PDF document generated from the conversion
pdfdoc.Destroy();
pdfdoc = null;
}
Any help or direction for assigning dimensions (letter size) to a PDF page and inserting the image file in that page would be more than welcome.
If ToPDF is given an image then PDFNet will query the DPI information of the image metadata and make page dimensions to match the DPI and resolution of the source.
If you like, you can always post-process the PDF generated by ToPDF.
Or, you can follow the AddImage sample code to do everything yourself.
https://www.pdftron.com/pdfnet/samplecode.html#AddImage

Create SVG using C# by overlaying one SVG icon onto another

How (if possible to do so) can I create an SVG image, using C#? I wish to take one SVG, overlay another SVG on it, and save it as a third SVG image.
In my specific case, I allow a module of my software to provide an icon for a folder. I want to overlay a warning or error icon on top of the folder icon when there is an error in the contained data.
I recommend using SVG Rendering Engine to do this. Create a third document, and put the two given svgs one after the other into it. (Nesting svg documents in each other is completely valid):
var icon = SvgDocument.Open(...);
var overlayIcon = SvgDocument.Open(...);
var overlayed = new SvgDocument();
overlayed.Children.Add(icon);
overlayed.Children.Add(overlayIcon);
overlayed.Write(...); // saving
Note: check the overlaying icon's size, viewbox, etc., you may have to change these through SvgDocument.Width, SvgDocument.Height, SvgDocument.ViewBox to get proper result.
SVG is a very simple text format, even simpler than HTML. Overlaying SVGs is a work for XML library. You can just open it and append every node from one file to another file.
Just try taking these two text files:
http://upload.wikimedia.org/wikipedia/commons/0/02/SVG_logo.svg
http://upload.wikimedia.org/wikipedia/commons/6/6b/Bitmap_VS_SVG.svg
Open the first one, copy contents of the the <svg>...</svg>, paste at the end of the other file (just before the ending </svg> tag). You can check in some editor or validator that the result is just a perfectly fine SVG file.
Use System.Xml or System.Xml.Linq for handling it from your C# code. Additionally you can add new nodes, edit attributes like width, height, color, etc... if you want:
<svg width="637" height="637">
<path style="fill:white" d="... "/>
...
</svg>
Have fun.

Windows API Code Pack - ShellFile not generating PDF bitmap

Using the code from previous stack overflow questions:
System.Drawing.Bitmap image;
ShellFile f = ShellFile.FromFilePath(fileLocation);
image = f.Thumbnail.ExtraLargeBitmap;
image.Save(tempfile, ImageFormat.Png);
I am trying to use window API to get the thumbnail of a PDF
I am led to believe that this generates an image file that resembles the first page of the PDF document.
The reality however is that it does NOT look like that, and merely looks like the PDF icon.
Is there anything that I'm missing that is required before this actually works as intended?
PDF files are correctly associated with adobe reader.
When browsing directories in windows explorer I DO see thumbnails associated with the documents.
I should note that the code DOES in fact correctly extract thumbnails when dealing with Excel and Word documents.
EDIT (references):
C# get thumbnail from file via windows api
Get thumbnail of any file, not only image files on Windows XP/Vista
Windows API Code Pack Thumbnail gives preview thumb of pdf but not Word or Excel
You need to specify that you want the thumbnail, not the icon (the default).
Change your code into this:
System.Drawing.Bitmap image;
ShellFile f = ShellFile.FromFilePath(fileLocation);
//force the actual thumbnail, not the icon
f.Thumbnail.FormatOption = ShellThumbnailFormatOption.ThumbnailOnly;
image = f.Thumbnail.ExtraLargeBitmap;
image.Save(tempfile, ImageFormat.Png);
The problem is because you have not selected the active frame that you will create the thumbnail from.
I cannot verify it on my current machine, because I don't have the Windows API on it, but it's giving you the standard PDF thumbnail because in your code you have't specified which page to use for the thumbnail.
Try doing something like this:
Image image = new Image();
//Load image here
int frameindex = 1; // Page number you want to use for thumbnail
Guid guide = image.FrameDimensionsList[0];
FrameDimension fDimension = new FrameDimension(guide);
image.SelectActiveFrame(fDimension, frameindex);
//Then go on to extract your thumbnail
I was not able to get ExtraLargeBitmap to work for PDF files but all other sizes (Large, Medium, and Small) worked fine.
Dim MyShellFile As ShellFile = ShellFile.FromFilePath(fi.FullName)
Dim MyThumbNail As Image
MyShellFile.Thumbnail.FormatOption = ShellThumbnailFormatOption.ThumbnailOnly
MyThumbNail = MyShellFile.Thumbnail.LargeBitmap
Me.PictureBox2.Image = MyThumbNail

image inline in RichTextBox in windows forms

I read rtf file from system. and load to richtextbox.
richTextBox1.LoadFile(#"C:\Users\cpsinghal\Desktop\newt.rtf", RichTextBoxStreamType.RichText);
In this rtf has image also. but richtextbox is not display image properly. image inline is bed.
I found image tag in richtextbox.rtf is {\pict\wmetafil.........}
and i add {\dn20{\pict\wmetafil.........}} and save as .rtf in system. then image is inline.
but richtextbox is not showing image inline.
is any other option or can i extend richtextbox.
please guide me.

Replace text in PDF

I'm trying to replace a section of a PDF with different text. From research on all major PDF libraries for .NET, it seems this is complicated and not a trivial task. I think it may be easier to convert the PDF to an image, replace the text (always in the same place), then convert it back to a PDF (or leave it as an image if converting back isn't possible). Is it possible to extract an image from a PDF page with .NET?
If your text is in a known location, you can simply cover it with a rectangle filled with the background color, and then draw your text over top.
Note that the text will still be there, it simply won't be visible. Someone selecting text will still pick up the old stuff. If that's acceptable, it's quite trivial.
If the PDF was created from image, you can import it into Photoshop to edit it as an graphic. Or you can use screenshot program like "Snagit" to capture pdf page as image and use snagit's editor to erase old text and replace new one.
But this method may bring you problem is that the new added text may not the same font as text around it. Personally, I use pdf editor to replace text in pdf since the added text will be automatically fit with the original font and size.

Categories